]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/spam_babycart.php
Simplify backlinks
[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)
13 {
14     /**
15       * @var WikiRequest $request
16       */
17     global $request;
18
19     // $X_babycart = '/usr/bin/perl /home/apthorpe/pjx/babycart/babycart';
20     // cygwin:
21     if (!defined('BABYCART_PATH'))
22         define('BABYCART_PATH', '/usr/local/bin/babycart');
23     // cygwin:
24     //$X_babycart = 'n:/bin/perl /usr/local/bin/babycart';
25
26     $comment = "IP: $ip\n";
27     $subject = $request->getArg('pagename');
28     $comment .= "SUBJECT: $subject\n";
29     $comment .= "END_COMMENT_METADATA\n";
30     $comment .= $text;
31
32     $descriptorspec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w"));
33     $process = proc_open(BABYCART_PATH, $descriptorspec, $pipes);
34     $error = '';
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]
40
41         // Send comment out for analysis
42         fwrite($pipes[0], $comment);
43         fclose($pipes[0]);
44
45         // Get response from stdout (should be one line)
46         $response = '';
47         while (!feof($pipes[1])) {
48             $response .= fgets($pipes[1], 1024);
49         }
50         fclose($pipes[1]);
51
52         // Get error from stderr (should be empty)
53         $error = '';
54         while (!feof($pipes[2])) {
55             $error .= fgets($pipes[2], 1024);
56         }
57         fclose($pipes[2]);
58
59         // It is important that you close any pipes before calling
60         // proc_close in order to avoid a deadlock
61         proc_close($process);
62
63         // Interpret results and yield judgment
64
65         // print "Response: $response\n";
66         // split into status, note, score, rules...
67         if ($response) {
68             if (substr($response, 0, 2) == 'OK')
69                 return false;
70             /*
71              response fields are:
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)
76             */
77             return explode(',', $response, 4);
78         }
79     }
80     trigger_error("Couldn't process " . BABYCART_PATH . ".\n" . $error, E_USER_WARNING);
81     return -1; // process error
82 }