]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/spam_babycart.php
Allow bold, italics or underlined for numbers
[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 {
14     // $X_babycart = '/usr/bin/perl /home/apthorpe/pjx/babycart/babycart';
15     // cygwin:
16     if (!defined('BABYCART_PATH'))
17         define('BABYCART_PATH', '/usr/local/bin/babycart');
18     // cygwin:
19     //$X_babycart = 'n:/bin/perl /usr/local/bin/babycart';
20
21     $comment = "IP: $ip\n";
22     $subject = $GLOBALS['request']->getArg('pagename');
23     $comment .= "SUBJECT: $subject\n";
24     $comment .= "END_COMMENT_METADATA\n";
25     $comment .= $text;
26
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]
34
35         // Send comment out for analysis
36         fwrite($pipes[0], $comment);
37         fclose($pipes[0]);
38
39         // Get response from stdout (should be one line)
40         $response = '';
41         while (!feof($pipes[1])) {
42             $response .= fgets($pipes[1], 1024);
43         }
44         fclose($pipes[1]);
45
46         // Get error from stderr (should be empty)
47         $error = '';
48         while (!feof($pipes[2])) {
49             $error .= fgets($pipes[2], 1024);
50         }
51         fclose($pipes[2]);
52
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);
56
57         // Interpret results and yield judgment
58
59         // print "Response: $response\n";
60         // split into status, note, score, rules...
61         if ($response) {
62             if (substr($response, 0, 2) == 'OK')
63                 return false;
64             /*
65              response fields are:
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)
70             */
71             return explode(',', $response, 4);
72         }
73     }
74     trigger_error("Couldn't process " . BABYCART_PATH . ".\n" . $error, E_USER_WARNING);
75     return -1; // process error
76 }