| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
$DEFAULT_ANTISPAM_CONFIG = Array( |
|---|
| 5 |
'program' => "python antispam.py %s", |
|---|
| 6 |
'mail_to' => null, |
|---|
| 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 |
|
|---|
| 19 |
do { |
|---|
| 20 |
$filename = uniqid(null, true); |
|---|
| 21 |
$filename = $config['filename_prefix'].$filename.'.txt'; |
|---|
| 22 |
} while (file_exists($filename)); |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 40 |
unlink($filename); |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 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 |
|
|---|
| 56 |
$send_email = false; |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
if ($send_email) { |
|---|
| 60 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 86 |
mail($config['mail_to'], $subject, $email, $config['mail_headers']); |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
return $is_spam; |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
?> |
|---|
| 94 |
|
|---|