File: spam_babycart.php

package info (click to toggle)
phpwiki 1.3.14-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 15,716 kB
  • ctags: 23,548
  • sloc: php: 88,295; sql: 1,476; sh: 1,378; perl: 765; makefile: 602; awk: 28
file content (78 lines) | stat: -rwxr-xr-x 2,729 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
rcs_id('$Id: spam_babycart.php,v 1.2 2004/12/06 19:49:58 rurban Exp $');
/*
* $Id: spam_babycart.php,v 1.2 2004/12/06 19:49:58 rurban Exp $
* Author: Bob Apthorpe <apthorpe+babycart@cynistar.net>
* Proof-of-concept PHP fragment to flag blog/wiki spam
*
* URL: <http://www.cynistar.net/~apthorpe/code/babycart/babycart.html>
* INSTALL: 
*   cpan Blog::SpamAssassin
*   copy contrib/babycart to /usr/local/bin/
*/

function check_babycart(&$text, $ip, $user_id='') {
    // $X_babycart = '/usr/bin/perl /home/apthorpe/pjx/babycart/babycart';
    // cygwin:
    if (!defined('BABYCART_PATH'))
        define('BABYCART_PATH', '/usr/local/bin/babycart');
    // cygwin:
    //$X_babycart = 'n:/bin/perl /usr/local/bin/babycart';

    $comment = "IP: $ip\n";
    $subject = $GLOBALS['request']->getArg('pagename');
    $comment .= "SUBJECT: $subject\n";
    $comment .= "END_COMMENT_METADATA\n";
    $comment .= $text;

    $descriptorspec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w"));
    $process = proc_open(BABYCART_PATH, $descriptorspec, $pipes);
    if (is_resource($process)) {
	// $pipes now looks like this:
	// 0 => writeable handle connected to child stdin
	// 1 => readable handle connected to child stdout
	// Any error output will be appended to $pipes[2]

        // Send comment out for analysis
        fwrite($pipes[0], $comment);
        fclose($pipes[0]);

        // Get response from stdout (should be one line)
        $response = '';
        while (!feof($pipes[1])) {
            $response .= fgets($pipes[1], 1024);
        }
        fclose($pipes[1]);

        // Get error from stderr (should be empty)
        $error = '';
        while (!feof($pipes[2])) {
            $error .= fgets($pipes[2], 1024);
        }
        fclose($pipes[2]);

        // It is important that you close any pipes before calling
        // proc_close in order to avoid a deadlock
        $return_value = proc_close($process);

        // Interpret results and yield judgment
        
        // print "Response: $response\n";
        // split into status, note, score, rules...
        if ($response) {
            if (substr($response,0,2) == 'OK')
                return false;
            /*
             response fields are:
             0 - verdict (OK or SUSPICIOUS)
             1 - note (additional info on verdict, whether tests ran, etc.)
             2 - numeric score; anything greater than 5-7 is suspect
             3 - comma-delimited list of rules hit (may be empty) 
            */
            return explode(',', $response, 4);
        }
    }
    trigger_error("Couldn't process ".BABYCART_PATH.".\n".$error, E_USER_WARNING);
    return -1; // process error
}
?>