December 19, 2006
Amazon S3 PHP Class
A few days ago I stumbled over Amazon's Simple Storage System and started playing around with it.
I just could not find any good premade S3 PHP class that I liked so I wrote my own. The first beta version is now ready for release. I have no documentation yet so check the source, just a few basics are below.
I created 4 classes:
AmazonS3 - Base Amazon S3 class
S3Bucket - Represents an Amazon S3 Bucket
S3Object - Represents an Amazone S3 Object
S3ACL - Represents an Amazon S3 ACL Record
You can create each of those by yourself, or start with an S3 object and get a Bucket out of that, and an Object out of the Bucket... I deceided that this would be nicer for usage later on too.
Below is a sample which would create a bucket (if it does not exist yet) and add a test object to that bucket with public read access.
$s3 = new AmazonS3($awskeyid, $secretkey);
$bucket = $s3->Bucket('mytestbucket');
$object = $bucket->S3Object('test_object');
$object->SetData('This is a Test Object accessible via http://s3.amazonaws.com/mytestbucket/test_object');
$object->SetType('text/html');
$object->SetCannedACL(AS3_ACL_PUBLIC_READ);
$object->Put();
That is all that is needed. I'll be posting more updates about this hopefully soon when I find some more time.
The Classes require PHP 5, the PHP CURL extension, and the PECL HASH extension to work!
The class file is below.

This work is licensed under a Creative Commons Attribution-Share Alike 2.0 Germany License.
Posted by Nathan on December 19, 2006 at 02:15 PM in Coding | Permalink | Comments (6) | TrackBack
November 02, 2006
Postfix To PHP
I have been looking for a good way to setup special email addresses which will be handled by a php script so I can push them into a database.
I'm writing a service where certain members of a website can exchange messages and I want those members to be able to have an email address they can give people which sends messages right into their account on the website.
The problem also was that the domain I wanted the emails to use had other addresses on it also AND on top of all that they were virtual domains in Postfix. Postfix does not support pipes in targets to VIRTUAL aliases (it does so for normal ones of course) so I could not just setup an alias for each account and pipe it to some script...
But that was not even needed, I came up with a way nicer solution. Here is what I did:
I already had a mysql configured virtual email setup running with Postfix / dovecot so I was familiar with the mysql maps setup. So all I did was setup a 2nd config file for a 2nd mysql powered virtual alias map, lets call it mysql_virtual_alias_php.cf.
In that file, among with the login info of course, I setup this query:
SELECT concat(username, "@php.domain.tld") FROM accounts WHERE username='%u' AND type = 2 AND active = 1
I then added the mysql_virtual_alias_php.cf file to the end of my virtual_alias_maps directive in main.cf and thus this is checked at the end to see if there might still be any other aliases to be resolved.
%u expands to just the local part of the address, so emails going to foo@domain.tld would run this query with username='foo' unless of course they matched anything in a previous virtual alias map.
One thing to note of course is that this will the run for all mails going to any virtual domains you have setup and would not care about it, you could also add some additional checks in the where clause to limit that, for example foo@other.tld would also trigger that query, in case you do not want that the easiest way would be to add a simple AND '%d' = 'domain.tld' to the where clause which would make sure it would never return any valid info unless the domain is correct.
So, once it gets passed through that map and foo@domain.tld DOES result in a good lookup postfix will now send the mail on to foo@php.domain.tld.
The second thing I did was really easy, setup a transport_maps entry that points to a transport which causes postfix to use a new transport, for example viaphp. To do that, all you have to do is make a file with:
php.domain.tld viaphp:
In it, call it for example transport, run postmap transport and then point to it via transport_maps in main.cf. This will cause postfix to use a non-standard transport for any mail going to the php.domain.tld domain, and all you have to do now is tell postfix what that transport does. Simple 2 lines at the end of master.cf are enough:
viaphp unix - n n - - pipe
flags= user=nobody argv=/usr/bin/php /path/to/script.php ${sender} ${mailbox}
This needs to be on 2 lines and there needs to be whitespace at the beginning of the 2nd one!
Once thats all done, restart postfix to make it reread its config files and you are done.
Each mail addressed to an addy causing the sql query to return anything good will be sent to php.domain.tld and on to /path/to/script.php via a pipe. So in that script you will have $argv[1] as the sender, $argv[2] as the username of the recipient and of course simply use php://stdin to read the whole mail and get everything you need.
Posted by Nathan on November 2, 2006 at 11:39 PM in Coding | Permalink | Comments (0) | TrackBack
September 25, 2006
Smarty & GNU gettext System
I was looking for a good Smarty GNU gettext implementation a few weeks ago and found one here. Problem with that specific one though was that I did not like that it used a Smarty block (the obvious choice normally) to handle the gettext blocks, since that kills Smarty variable usage in them.
So what I did was I wrote my own handler based on the smarty-gettext I found using a Smarty Prefilter instead. This way I can use smarty syntax inside the gettext blocks. Of course using full ifs and such is not wise and will confuse translators, but using variable names will actually be easier than using %1 %2 %3 and so on as needed with smarty-gettext, since its more obvious for the translater what will actually be displayed in its place.
The source of the prefilter with a short explanation of its usage can is attached.
Download smarty-gettext-prefilter.php
And also a short script taken from smarty-gettext to scan templates for gettext blocks and build fake C code from it to use with the standard gettext tools can be downloaded here.
Enjoy!

This work is licensed under a Creative Commons Attribution 2.5 License.
Posted by Nathan on September 25, 2006 at 04:09 PM in Coding | Permalink | Comments (1) | TrackBack