]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/spam_babycart.php
rcs_id no longer makes sense with Subversion global version number
[SourceForge/phpwiki.git] / lib / spam_babycart.php
1 <?php
2 // rcs_id('$Id$');
3 /*
4 * $Id$
5 * Author: Bob Apthorpe <apthorpe+babycart@cynistar.net>
6 * Proof-of-concept PHP fragment to flag blog/wiki spam
7 *
8 * URL: <http://www.cynistar.net/~apthorpe/code/babycart/babycart.html>
9 * INSTALL: 
10 *   cpan Blog::SpamAssassin
11 *   copy contrib/babycart to /usr/local/bin/
12 */
13
14 function check_babycart(&$text, $ip, $user_id='') {
15     // $X_babycart = '/usr/bin/perl /home/apthorpe/pjx/babycart/babycart';
16     // cygwin:
17     if (!defined('BABYCART_PATH'))
18         define('BABYCART_PATH', '/usr/local/bin/babycart');
19     // cygwin:
20     //$X_babycart = 'n:/bin/perl /usr/local/bin/babycart';
21
22     $comment = "IP: $ip\n";
23     $subject = $GLOBALS['request']->getArg('pagename');
24     $comment .= "SUBJECT: $subject\n";
25     $comment .= "END_COMMENT_METADATA\n";
26     $comment .= $text;
27
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]
35
36         // Send comment out for analysis
37         fwrite($pipes[0], $comment);
38         fclose($pipes[0]);
39
40         // Get response from stdout (should be one line)
41         $response = '';
42         while (!feof($pipes[1])) {
43             $response .= fgets($pipes[1], 1024);
44         }
45         fclose($pipes[1]);
46
47         // Get error from stderr (should be empty)
48         $error = '';
49         while (!feof($pipes[2])) {
50             $error .= fgets($pipes[2], 1024);
51         }
52         fclose($pipes[2]);
53
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);
57
58         // Interpret results and yield judgment
59         
60         // print "Response: $response\n";
61         // split into status, note, score, rules...
62         if ($response) {
63             if (substr($response,0,2) == 'OK')
64                 return false;
65             /*
66              response fields are:
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) 
71             */
72             return explode(',', $response, 4);
73         }
74     }
75     trigger_error("Couldn't process ".BABYCART_PATH.".\n".$error, E_USER_WARNING);
76     return -1; // process error
77 }
78 ?>