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