#!/usr/local/php5/bin/php
<?php
/*  @package Sendmail Wrapper
    @version 0.1
    @author MiRacLe <miracle@rpz.name>
    @since 13.03.2006
    @description For php, it means changing sendmail_path to point at this file. 
*/
    // unique message id to use for this message
    $messageid = uniqid('php');
    // log file to write to (should use $messageid)
    $logfile = '/var/log/sendmail/'.$messageid;
    // path to sendmail 
    $sendmail = '/usr/sbin/sendmail.real -t -i';
    // additional headers
    $add_headers = array();
    $add_headers['X-MsgID'] = $messageid;
    if (preg_match('~/home/([a-zA-Z0-9\._-]*)(/.*)?~', $_SERVER['PWD'], $matches)) {
        $add_headers['X-Generating-Domain'] = $matches[1];
        $logfile = '/var/log/sendmail/'.$matches[1].'_'.$messageid;
    }
/*****************************************************************************
    Workhourse here ;o)
 *****************************************************************************/
    // read STDIN (mail message)
    while (!feof(STDIN)) {
        $data .= fread(STDIN, 1024);
    }
    // split out headers
    list ($headers, $message) = explode("\n\n", $data, 2); 
    // add additional headers
    if ($add_headers) {
        foreach ($add_headers as $field=>$contents) {
            $headers .= "\n".$field.": ".$contents;
        }
    }
    // reassemble the message
    $data = $headers."\n\n".$message;
    $processUser = posix_getpwuid(posix_geteuid());
    // write to our log file
    $_message = 'Date:'."\t".date('r')."\n";
    $_message .= 'PWD:'."\t".$_SERVER['PWD']."\n";
    $_message .= 'UID:'."\t".$processUser['name']."\n";
    $_message .= 'Msg ID'."\t".$messageid."\n\nMessage:\n----------\n";
    $_message .= stripslashes($data);
    file_put_contents($logfile,$_message);
    $isSpam = false;
/*    
    check spam signatures here
*/    
    // write to the real sendmail
    if (! $isSpam) {
        $h = popen($sendmail, "w");
        fwrite($h, $data);
        pclose($h);
    }
?>