Archive

Archive for January, 2009

I’m Attending MIT, Stanford & Harvard

January 29th, 2009

Well, sort of.

Thanks to Academic Earth, a friggin’ gift, you can follow (video) lectures given at universities like Stanford, MIT, Harvard & Yale on some of the most popular subjects. There once was a time when you couldn’t wait to get out of school, I suppose it makes sense to have a time where you’d do anything to learn new things.

Especially things you choose. And only those subjects you really like (did anyone say obligated French?).

The videos include all sort of subjects, such as Computer Science (/love), Mathematics, Engineering, … all explained by well-respected professors.

I’m currently following the Computer Science III: Programming Paradigms course, which features a lot of interesting topics such as:

There are many other (complete!) courses to follow, such as Understanding Computers and the Internet (Harvard), Computer System Engineering (MIT), Computer Science I: Programming Methodology (Stanford) and many more!

I can only assume this will eat yet another chunk of my already severely shortened sleeping period. Oh, did I just hear the faint sound of coffee beans crushing?

Matti Tech , , , , , , ,

PEAR:: Classes Worthy Of Attention

January 26th, 2009

PEAR offers an excellent framework for re-usable PHP components. A wide range of PEAR classes is available, from XML Parsers to Captcha’s and Mail components.

They’re all easy to install, and have extensive documentation available so you can get started right away. Here’s a list of some of the better, and probably lesser known ones.

  • PEAR::Mail
    PEAR’s Mail package defines an interface for implementing mailers under the PEAR hierarchy. It also provides supporting functions useful to multiple mailer backends. Currently supported backends include: PHP’s native mail() function, sendmail, and SMTP. This package also provides a RFC822 email address list validation utility class.
  • PEAR::XML_Parser
    This is an XML parser based on PHPs built-in xml extension. It supports two basic modes of operation: “func” and “event”. In “func” mode, it will look for a function named after each element (xmltag_ELEMENT for start tags and xmltag_ELEMENT_ for end tags), and in “event” mode it uses a set of generic callbacks.
  • PEAR::MIME_Type
    Provide functionality for dealing with MIME types, such as auto-detecting MIME type on files.
  • PEAR::Net_Socket
    Net_Socket is a class interface to TCP sockets. It provides blocking and non-blocking operation, with different reading and writing modes (byte-wise, block-wise, line-wise and special formats like network byte-order ip addresses). It provides the base class for other popular PEAR-classes such as Net_SMTP and HTTP_Request.
  • PEAR::Archive_Tar
    This class provides handling of tar files in PHP.
    It supports creating, listing, extracting and adding to tar files. Gzip support is available if PHP has the zlib extension built-in or loaded. Bz2 compression is also supported with the bz2 extension loaded.

The list of available packages goes on for miles, and includes a Facebook package, a Number-to-Words convertor, Command line Argument Parser, CAPTCHA generator, Barcode Generator, … Too much to mention!

For installation instructions, I’ll refer you to the following two posts:

I’m interested to know what PEAR packages you’re using, and why. Where do you draw the line between writing your own classes, and switching to PEAR’s?

Matti PHP ,

Install New PEAR Packages From The Linux CLI

January 24th, 2009

I previously wrote about how to Enable Pear Packages when using Plesk, here’s how to do it when you run your own LAMP stack.

First up, see if the PEAR management tool is already installed (the  command pear should be available).

mattias-laptop ~ # pear
The program ‘pear’ is currently not installed.  You can install it by typing:
apt-get install php-pear

If it’s not, simply install the package by issueing the command suggested.

mattias-laptop ~ # apt-get install php-pear
Reading package lists… Done
Building dependency tree
Reading state information… Done
….
Setting up php-pear (5.2.6-2ubuntu4) …

After installation of the management tool is completed, you can quickly install new PEAR packages like this.

pear install PackageName

A useful hint is that the Pear Packages which are missing, can be easily installed by typing in the full packagename. Here’s an example.

mattias-laptop # php -q test.php
Warning: require_once(Console/Getargs.php): failed to open stream: No such file or directory in /var/www/test/test.php on line 36

Fatal error: require_once(): Failed opening required ‘Console/Getargs.php’ (include_path=’.:/usr/share/php:/usr/share/pear’) in /var/www/test/test.php on line 36

The missing PEAR package “Console/Getargs.php” can be installed like this.

pear install Console_Getargs

Just replace each folder within the PEAR class by an underscore, leave out the “.php” extension, and you can install any PEAR package you want. That’s good news, because there are a lot of very useful PEAR packages around!

Matti PHP , ,

Method Chaining In PHP – Fluent Interface

January 23rd, 2009

The technical definition of “Fluent Interface”, as defined by Wikipedia, is: “An object oriented construct that defines a behavior capable of relaying the instruction context of a subsequent call.“. It’s just as sexy as it sounds.

In short: any Object Oriented language allows you to call upon a new method, directly followed by another one and still updating the same parent object.

Code explains this easier. The following examples creates a one-armed, one-legged and one-headed person.

class Person {
	public function addLeg() {
		// Adds a leg
	}
 
	public function addArm() {
		// Adds an arm
	}
 
	public function addHead() {
		// Adds a head
	}
 
	// .... and the list goes on
}
 
$person = new Person();
$person->addLeg();
$person->addArm();
$person->addHead();

After creating an instance of the object Person, we call upon every method once to add a certain body part.

If we slightly rewrite the class, by returning the object itself upon every method call, we can also write those method calls like this.

class Person {
	public function addLeg() {
		// Adds a leg
 
		return $this;
	}
 
	public function addArm() {
		// Adds an arm
 
		return $this;
	}
 
	public function addHead() {
		// Adds a head
 
		return $this;
	}
 
	// .... and the list goes on
}
 
$person = new Person();
$person->addLeg()->addArm()->addHead();

While this may, or may not, help in readability – the option to use Fluent Interfaces exist in PHP5 (and JavaScript as well, in the same way). It’ll depend on the situation whether or not this is useful. I can see this creating very chaotic code, and it’s a serious PITA to handle errors or exceptions this way.

Matti PHP , ,

Exception Handling In PHP5 – Good Cop, Bad Cop

January 17th, 2009

Exception handling. The “I’m so good, I don’t produce errors“-mentality can’t even save you from them. It’s bound to happen – either your code fails, or external libraries you’ve grown to accept produce faults outside of your control.

You need’m handled, it’s that simple.

And PHP offers you a couple of methods to “handle” errors, or warnings, generated by your PHP code. From basic Error Suppresion, to proper Exception Handling. Read more…

Matti PHP , , , ,

Using Plesk’s SMTP Server: DNS Blacklist Prevents Sending

January 14th, 2009

If you’re trying to use your Plesk’s SMTP service to allow sending e-mail, you might run into the following error: rblsmtpd: <IP ADDRESS>: 451 http://www.spamhaus.org/query/bl?ip=<IP ADDRESS>.

The cause of this error, is the enabled option to check for DNS blacklists. It’ll find that the user’s (often dynamic) IP address isn’t allowed to send e-mail to the Plesk’s SMTP service. A common cause for this, is the use of the “zen.spamhaus.org“, which is a combination of SBL, XBL and PBL blacklists.

And the PBL blacklist, has the following warning-message in the FAQ.

Caution: Because the PBL lists normal customer IP space, do not use PBL on smarthosts or SMTP AUTH outbound servers for your own customers (or you risk blocking your own customers if their dynamic IPs are in the PBL). Do not use PBL in filters that do any ‘deep parsing’ of Received headers, or for other than checking IP addresses that hand off to your mailservers.

Disabling the zen.spamhaus.org will cure this issue. You can enable the xbl.spamhaus.org;sbl.spamhaus.org DNS blackhole list to get a combination of XBL and SBL blacklists, without PBL.

This does, however, have another drawback. Enabling the remaining DNS blackhole lists, will prevent (authenticated) e-mail communication over port 25. Meaning you still can’t send e-mail.

Bummer. But fixeable.

Enable the Message Submission option, on the Plesk page Server > Mail.

This will make the SMTP service also available on port 587, configured to allow Authenticated SMTP connections with disregard of the DNS Blackhole list. The only drawback is it can’t be used in combination with the zen.spamhaus.org (which is why we changed it in the first place).

Outlook should then be configured to send e-mail over port 587, instead of the default port 25.

Go to Tools > Account Options and doubleclick the account you’re using. Go to More Settings and proceed to the tab “Advanced“. There, you can change the Outgoing mailserver (SMTP) port to 587.

Seems like a lot of work, but it appears to be the only way to use the SMTP service in Plesk, in combination with a DNS Blackhole list and dynamic IP ranges.

Matti Plesk , , , ,

Web 2.0 Doesn’t Exist, You Schmuck

January 11th, 2009

The Web 2.0 is wrongfully used all too often. Besides the obvious (the fact that “The Web” doesn’t exist, and a 2.0 version *can’t* exist either), it’s implying something it shouldn’t. The “Web” didn’t evolve, the developers did.

Hooray for (Web)developer 2.0. Read more…

Matti Webdevelopment ,