development

PHP API to SpamAssassin spamd Protocol

Posted in development on May 4th, 2010 by Pedro Padron – 1 Comment

On the past few days I’ve been working on a PHP spamd client, and I think it is somewhat usable for now since it already implements all of the protocol commands. This is release 0.1.0, so you might expect some API changes for the next versions. Feedback is always welcome.

I tried to make this the most straight-forward as possible, abstracting some nasty things that happens under the hood, so you can do things like:

require_once 'SpamAssassin/Client.php';
 
$params = array(
    "hostname" => "localhost",
    "port" => "783",
    "user" => "ppadron",
);
 
$sa = new SpamAssassin_Client($params);
 
$message = file_get_contents('/path/to/message/file');
 
var_dump($sa->isSpam($message));
bool(true)

Methods available

ping()
Pings the server to test the connection
getSpamReport($message)
Returns a detailed report if the message is spam or null if it’s ham
headers($message)
Processes the message and returns it’s headers (like X-Spam-Flag and X-Spam-Status)
check($message)
Checks if a message is spam with the CHECK protocol command
isSpam($message)
Shortcut to check() method that returns a boolean
process($message)
Processes the message, checks it for spam and returning it’s modified version
symbols($message)
Returns all rules matched by the message
learn($message, $learnType)
Uses SpamAssassin learning feature with TELL. Must be enabled on the server.
report($message)
Report message as spam, both local and remote
revoke($message)
Revokes a message previously reported as spam

I’m still working on providing some good usage examples, but I’ve put (temporarily) API docs available here. In the meantime, you can browse the tests directory and learn some basic stuff.

If you want to use this library to implement a spam filter for your mail server, maybe you should consider using the spamc command line utility or the Perl library that already comes with SpamAssassin (which was my main source of inspiration).

This library is most useful when you already have stuff written in PHP and you need to do something with SpamAssassin. If you have a webmail written in PHP and would like to interact with a remote SpamAssassin server to report spam, for example.

The project is hosted @ Github, so you can:

I owe you a PEAR repo to serve this package.

(Português) Vídeo: Augeas no Locaweb 5o. TechDay

Posted in development on February 1st, 2010 by Pedro Padron – Be the first to comment

Sorry, this entry is only available in Português.

PHP Augeas extension released

Posted in development on December 1st, 2009 by Pedro Padron – Be the first to comment

A few weeks ago I did the first release of augeas, my first PECL extension. Augeas is a configuration editing tool that provides an unified API to different configuration file formats. Quoting the official website:

Augeas is a configuration editing tool. It parses configuration files in their native formats and transforms them into a tree. Configuration changes are made by manipulating this tree and saving it back into native config files.

After the second release, I notified David Lutterkort, who was kind enough to add the PHP extension to the list of language bindings in the Augeas website.

Well, today I just released version 0.4.0 (thanks to Pierre Joye for fixing the upload page), which includes a new method (Augeas::mv()) and finally a PHPUnit test suite (AllTests.php).

The next items in my TODO list regarding this project are (in no particular order):

Here’s a sample of what you can do with Augeas:

$aug = new Augeas();
 
// let's disable register_globals!
$aug->set("/files/etc/php.ini/PHP/register_globals", "Off");
 
if (!$aug->save()) {
    die("You do not have enough privileges to edit php.ini file.");
}

How easy was that? No regex, no fopen, no hassle.

Resources on how to write your own PHP extension

Posted in development on November 18th, 2009 by Pedro Padron – Be the first to comment

A few days ago I started to write my first PHP extension, and I have to admit it was a pain in the beginning. Actually it still is, but I’m the one to blame for lacking some C skills. Anyway, my advice is: if you want to write your own extension you’d better have a good reason to do so. Here are some of the most common ones:

  • increase performance
  • change PHP behavior (take a look at these PECL extensions)
  • wrap a C library
  • you are very curious and want to know how to do it anyway

If you still want to give it a try, here are some of the things that helped me along the way:

Maybe I’ll write some posts about this. Maybe.

(Português) Obtendo informações de um pacote RPM usando a extensão rpmreader do PHP

Posted in development on September 15th, 2009 by Pedro Padron – 2 Comments

Sorry, this entry is only available in Português.