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)
15 * @var WikiRequest $request
19 // $X_babycart = '/usr/bin/perl /home/apthorpe/pjx/babycart/babycart';
21 if (!defined('BABYCART_PATH'))
22 define('BABYCART_PATH', '/usr/local/bin/babycart');
24 //$X_babycart = 'n:/bin/perl /usr/local/bin/babycart';
26 $comment = "IP: $ip\n";
27 $subject = $request->getArg('pagename');
28 $comment .= "SUBJECT: $subject\n";
29 $comment .= "END_COMMENT_METADATA\n";
32 $descriptorspec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w"));
33 $process = proc_open(BABYCART_PATH, $descriptorspec, $pipes);
35 if (is_resource($process)) {
36 // $pipes now looks like this:
37 // 0 => writeable handle connected to child stdin
38 // 1 => readable handle connected to child stdout
39 // Any error output will be appended to $pipes[2]
41 // Send comment out for analysis
42 fwrite($pipes[0], $comment);
45 // Get response from stdout (should be one line)
47 while (!feof($pipes[1])) {
48 $response .= fgets($pipes[1], 1024);
52 // Get error from stderr (should be empty)
54 while (!feof($pipes[2])) {
55 $error .= fgets($pipes[2], 1024);
59 // It is important that you close any pipes before calling
60 // proc_close in order to avoid a deadlock
63 // Interpret results and yield judgment
65 // print "Response: $response\n";
66 // split into status, note, score, rules...
68 if (substr($response, 0, 2) == 'OK')
72 0 - verdict (OK or SUSPICIOUS)
73 1 - note (additional info on verdict, whether tests ran, etc.)
74 2 - numeric score; anything greater than 5-7 is suspect
75 3 - comma-delimited list of rules hit (may be empty)
77 return explode(',', $response, 4);
80 trigger_error("Couldn't process " . BABYCART_PATH . ".\n" . $error, E_USER_WARNING);
81 return -1; // process error