3 * Author: Bob Apthorpe <apthorpe+babycart@cynistar.net>
4 * Proof-of-concept PHP fragment to flag blog/wiki spam
6 * URL: <http://www.cynistar.net/~apthorpe/code/babycart/babycart.html>
8 * cpan Blog::SpamAssassin
9 * copy contrib/babycart to /usr/local/bin/
12 function check_babycart(&$text, $ip, $user_id = '')
14 // $X_babycart = '/usr/bin/perl /home/apthorpe/pjx/babycart/babycart';
16 if (!defined('BABYCART_PATH'))
17 define('BABYCART_PATH', '/usr/local/bin/babycart');
19 //$X_babycart = 'n:/bin/perl /usr/local/bin/babycart';
21 $comment = "IP: $ip\n";
22 $subject = $GLOBALS['request']->getArg('pagename');
23 $comment .= "SUBJECT: $subject\n";
24 $comment .= "END_COMMENT_METADATA\n";
27 $descriptorspec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w"));
28 $process = proc_open(BABYCART_PATH, $descriptorspec, $pipes);
29 if (is_resource($process)) {
30 // $pipes now looks like this:
31 // 0 => writeable handle connected to child stdin
32 // 1 => readable handle connected to child stdout
33 // Any error output will be appended to $pipes[2]
35 // Send comment out for analysis
36 fwrite($pipes[0], $comment);
39 // Get response from stdout (should be one line)
41 while (!feof($pipes[1])) {
42 $response .= fgets($pipes[1], 1024);
46 // Get error from stderr (should be empty)
48 while (!feof($pipes[2])) {
49 $error .= fgets($pipes[2], 1024);
53 // It is important that you close any pipes before calling
54 // proc_close in order to avoid a deadlock
55 $return_value = proc_close($process);
57 // Interpret results and yield judgment
59 // print "Response: $response\n";
60 // split into status, note, score, rules...
62 if (substr($response, 0, 2) == 'OK')
66 0 - verdict (OK or SUSPICIOUS)
67 1 - note (additional info on verdict, whether tests ran, etc.)
68 2 - numeric score; anything greater than 5-7 is suspect
69 3 - comma-delimited list of rules hit (may be empty)
71 return explode(',', $response, 4);
74 trigger_error("Couldn't process " . BABYCART_PATH . ".\n" . $error, E_USER_WARNING);
75 return -1; // process error