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