root/antispam/trunk/plugin/antispam.php

Revision 298, 2.7 kB (checked in by haypo, 7 months ago)
  • exit(1) on init error
  • antispam.php: exit(1) is not a spam but an error
Line 
1 <?php
2
3 # Default configuration
4 $DEFAULT_ANTISPAM_CONFIG = Array(
5     'program' => "python antispam.py %s", # --verbose --config=YOURCONFIG.conf
6     'mail_to' => null, # => "your_mail@example.com"
7     'email_min_score' => 0.0,
8     'email_max_score' => 10.0,
9     'mail_headers' => "Content-type: text/plain; charset=utf-8\r\n",
10     'filename_prefix' => "spam-",
11 );
12
13 function antispam($message)
14 {
15     global $DEFAULT_ANTISPAM_CONFIG, $ANTISPAM_CONFIG;
16     $config = array_merge($DEFAULT_ANTISPAM_CONFIG, $ANTISPAM_CONFIG);
17
18     // Create filename
19     do {
20         $filename = uniqid(null, true);
21         $filename = $config['filename_prefix'].$filename.'.txt';
22     } while (file_exists($filename));
23
24     // Write message to temporary file
25     $file = fopen($filename, "w");
26     if (!$file) {
27         die("ANTISPAM FATAL ERROR: Unable to create file $filename!");
28     }
29     fwrite($file, $message);
30     fclose($file);
31
32     // Run antispam program
33     $command = sprintf($config['program'], escapeshellarg($filename));
34     ob_start();
35     passthru($command, $exitcode);
36     $output = ob_get_contents();
37     ob_end_clean();
38
39     // Remove temporary file
40     unlink($filename);
41
42     // Read output to check if it's a spam
43     if (ereg("score: ([+-][0-9]+\.[0-9]{2})", $output, $regs)) {
44         $score = $regs[1];
45         $score = (float)$score;
46         $is_spam = (0 < $score);
47         $send_email = (($config['email_min_score'] <= $score) and ($score <= $config['email_max_score']));
48     } else {
49         $score = null;
50         $is_spam = ($exitcode == 2) or (strpos($output, "***SPAM***") !== false);
51         $send_email = true;
52     }
53
54     if (!$config['mail_to']) {
55         # Email address not configured!
56         $send_email = false;
57     }
58
59     if ($send_email) {
60         // Email subject
61         if ($is_spam) {
62             $subject = "Antispam result: SPAM!";
63         } else {
64             $subject = "Antispam result: ok";
65         }
66         if (!is_null($score)) {
67             $subject .= sprintf(" (score: %+.2f)", $score);
68         } else {
69             $subject .= " (error: unable to parse score!)";
70         }
71
72         // Email content
73         $email  = "$command\n";
74         $email .= "Timestamp: ".date('l dS F Y h:i:s A')."\n";
75         $email .= "Exit code: ".(int)$exitcode."\n";
76         $email .= sprintf("Score: min=%.1f <= %.1f <= max=%.1f\n",
77                 $config['email_min_score'],
78                 $score,
79                 $config['email_max_score']);
80         $email .= "\n";
81         $email .= "$output";
82         $email .= str_repeat("-", 70)."\n";
83         $email .= $message;
84
85         // Send email
86         mail($config['mail_to'], $subject, $email, $config['mail_headers']);
87     }
88
89     // Return result
90     return $is_spam;
91 }
92
93 ?>
94
Note: See TracBrowser for help on using the browser.