I updated the code for the function to allow the use of a rule set as well as contacting the surbl list to check domain names which have been.
I created three more global variables to hold the names of local and an imported spam rules files and a log file. I have a master file assembled. I am just working on a way to update it based on some of the blacklist projects which are around. I will host the rules as part of another antispam project I have undertaken. I need to work out an updating method. Instead of updating the full file, people would just update the file they have in hand.
People need to maintain their own local rules. I need to extend the program on which this is based to better integrate into pligg so they can make these changes through a web interface.
At any rate. Following is the latest version of the antispam module functions:
Code:
function check_spam($text )
{
global $MAIN_SPAM_RULESET;
global $USER_SPAM_RULESET;
$regex_url = "/(http:\/\/|https:\/\/|ftp:\/\/|www\.)([^\/\"<\s]*)/im";
$mk_regex_array = array();
preg_match_all($regex_url, $text, $mk_regex_array);
for( $cnt=0; $cnt < count($mk_regex_array[2]); $cnt++ )
{
$test_domain = rtrim($mk_regex_array[2][$cnt],"\\");
if (strlen($domain_to_test) > 3)
{
$domain_to_test = $test_domain . ".multi.surbl.org";
if( strstr(gethostbyname($domain_to_test),'127.0.0'))
{ logSpam( "surbl rejected $test_domain"); return true; }
}
}
$retVal = check_spam_rules( $MAIN_SPAM_RULESET, $text);
if(!$retVal) { $retVal = check_spam_rules( $USER_SPAM_RULESET, $text); }
return $retVal;
}
#####################################
# check a file of local rules
# . . the rules are written in a regex format for php
# . . or one entry per line eg: bigtimespammer.com on one line
####################
function check_spam_rules( $ruleFile, $text)
{
if(!file_exists( $ruleFile)) { echo $ruleFile . " does not exist\n"; return false; }
$handle = fopen( $ruleFile, "r");
while (!feof($handle))
{
$buffer = fgets($handle, 4096);
$splitbuffer = explode("####", $buffer);
$expression = $splitbuffer[0];
$explodedSplitBuffer = explode("/", $expression);
$expression = $explodedSplitBuffer[0];
if (strlen($expression) > 0)
{
if(preg_match("/".trim($expression)."/", $text))
{ logSpam( "$ruleFile violation: $expression"); return true; }
}
}
fclose($handle);
return false;
}
##############
## log date, time, IP address and rule which triggered the spam
##############
function logSpam($message)
{
global $SPAM_LOG_BOOK;
$ip = "127.0.0.0";
if(!empty($_SERVER["REMOTE_ADDR"])) { $ip = $_SERVER["REMOTE_ADDR"]; }
$date = date('M-d-Y');
$timestamp = time();
$message = $date . "\t" . $timestamp . "\t" . $ip . "\t" . $message . "\n";
$file = fopen( $SPAM_LOG_BOOK, "a");
fwrite( $file, $message );
fclose($file);
} The three new globval variables are:
Code:
$MAIN_SPAM_RULESET = "antispam.txt";
$USER_SPAM_RULESET = "local-antispam.txt";
$SPAM_LOG_BOOK = "spamlog.log";