This file is part of phpCF. phpCF is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. phpCF is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with phpCF; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *****************************************************************************/ require_once dirname(__FILE__) . '/phpCF.config.php'; // Define a couple of constants. define("PHPCF_VERSION", "0.5b"); define("DEBUG", true); @ini_set('user_agent', "phpCF/".PHPCF_VERSION." (http://lillesvin.net/phpcf/)"); class phpCF { var $rules = array(); var $error = array(); var $log = array(); var $score; // Map types to methods: var $type_map = array( "regex" => "applyRegexRule", "regex_count" => "applyRegexCountRule", "php" => "applyPhpRule" ); // For debugging purposes only! var $debug = ""; // Main methods. function phpCF($body, $email = false, $ip = false) { $this->debug("phpCF: New instance created."); $rfs = $this->getRulesFiles(); if ($rfs !== false) { foreach ($rfs as $rf) { $this->parseRules($rf); } $this->score = $this->applyRules($body, $email, $ip); } else { $this->error[] = "Error finding/opening rules files."; } } function isSpam() { global $SPAM_THRESHOLD; if ($this->score > $SPAM_THRESHOLD) { return true; } else { return false; } } function applyRules($body, $email = false, $ip = false) { $this->debug("applyRules: Called with: 'body' => '$body'; 'email' => '$email'; 'ip' => '$ip'"); $final_score = 0.0; foreach ($this->rules as $r) { $label = $r[0]; $rule_type = $r[1]; $applies_to = $r[2]; $score = $r[3]; $rule = $r[4]; $type = $this->type_map[$rule_type]; if ($applies_to == "body") { $this->debug("appleRules: Running '$label' on 'body'"); $s = $this->$type($label, $score, $rule, $body); if ($s !== false) { $final_score += $s; } } else if ($applies_to == "email" && strlen($email) > 0) { $this->debug("applyRules: Running '$label' on 'email'"); $s = $this->$type($label, $score, $rule, $email); if ($s !== false) { $final_score += $s; } } else if ($applies_to == "ip" && strlen($ip) > 6) { $this->debug("applyRules: Running '$label' on 'ip'"); $s = $this->$type($label, $score, $rule, $ip); if ($s !== false) { $final_score += $s; } } } return $final_score; } function applyRegexRule($label, $score, $rule, $target) { if (preg_match($rule, $target)) { $this->log[] = $label . ": " . $score; return $score; } else { return false; } } function applyRegexCountRule($label, $score, $rule, $target) { if(preg_match_all($rule, $target, $matches) !== false) { if (count($matches[0]) > 0) { $count = count($matches[0]); $this->log[] = $label . ": (" . $count . "): " . ($count * $score); return ($count * $score); } else { return false; } } else { $this->debug("applyRegexCountRule: Problem with pattern: $rule"); return false; } } function applyPhpRule($label, $score, $rule, $target) { // Load support files: $rulesSupport = $this->getRulesSupport(); foreach ($rulesSupport as $supportFile) { $this->debug("applyPhpRule: Loading support structures from: \"" . $supportFile . "\""); include_once $supportFile; } // Support loaded. if (!function_exists($rule)) { return false; } $scored = $rule($score, $target, &$this); if ($scored > 0) { $this->log[] = $label . ": " . $scored; return $scored; } else { return false; } } function getRulesSupport() { $rules_dir = $this->getRulesDir(); // Run through the right dir and include everything: return glob($rules_dir . "/support/*.php"); } // Parse rules // Layout of the rules file should be: //