cvs: pear /Services_Trackback/Services/Trackback SpamCheck.php /Services_Trackback/Services/Trackback/SpamCheck DNSBL.php SURBL.php Wordlist.php

From: Date: Mon, 23 May 2005 11:48:37 +0000
Subject: cvs: pear /Services_Trackback/Services/Trackback SpamCheck.php /Services_Trackback/Services/Trackback/SpamCheck DNSBL.php SURBL.php Wordlist.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-32051@lists.php.net to get a copy of this message
toby Mon May 23 07:48:37 2005 EDT Modified files: /pear/Services_Trackback/Services/Trackback SpamCheck.php /pear/Services_Trackback/Services/Trackback/SpamCheck DNSBL.php SURBL.php Wordlist.php Log: * Fixed renaming issues SpamProtection -> SpamCheck. * Implemented global factory for SpamChecks (create()). * SpamCheck data is now reset before each check (to reuse 1 check for multiple trackbacks). * The specific SpamCheck modules are now created using their constructor. Creation generally works through Services_Trackback_SpamCheck::create(). * Made SpamCheck modules DNSBL/SURBL/Wordlist working.

http://cvs.php.net/diff.php/pear/Services_Trackback/Services/Trackback/SpamCheck.php?r1=1.3&r2=1.4&ty=u Index: pear/Services_Trackback/Services/Trackback/SpamCheck.php diff -u pear/Services_Trackback/Services/Trackback/SpamCheck.php:1.3 pear/Services_Trackback/Services/Trackback/SpamCheck.php:1.4 --- pear/Services_Trackback/Services/Trackback/SpamCheck.php:1.3 Mon Apr 25 07:19:57 2005 +++ pear/Services_Trackback/Services/Trackback/SpamCheck.php Mon May 23 07:48:35 2005 @@ -24,7 +24,7 @@ * @author Tobias Schlitt <toby@php.net> * @copyright 1997-2005 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 - * @version CVS: $Id: SpamCheck.php,v 1.3 2005/04/25 11:19:57 toby Exp $ + * @version CVS: $Id: SpamCheck.php,v 1.4 2005/05/23 11:48:35 toby Exp $ * @link http://pear.php.net/package/Services_Trackback * @since File available since Release 0.5.0 */ @@ -40,7 +40,7 @@ // }}} /** - * SpamProtection + * SpamCheck * Base class for Services_Trackback spam protection modules. * * @license http://www.php.net/license/3_0.txt PHP License 3.0 @@ -53,7 +53,7 @@ * @since 0.5.0 * @access public */ -class Services_Trackback_SpamProtection { +class Services_Trackback_SpamCheck { // {{{ _options /** @@ -96,9 +96,17 @@ * All further options depend on the specific module. * @return object(Services_Trackback_SpamCheck) The newly created SpamCheck object. */ - function create($options = null) + function &create($type, $options = null) { - $this->_options = $options; + $filename = 'Services/Trackback/SpamCheck/' . $type . '.php'; + $classname = 'Services_Trackback_SpamCheck_' . $type; + + include_once $filename; + if (!class_exists($classname)) { + return PEAR::raiseError('SpamCheck ' . $type . ' not found.'); + } + + return new $classname(@$options); } // }}} @@ -117,6 +125,7 @@ */ function check($trackback) { + $this->reset(); $spam = false; foreach ($this->_options['sources'] as $id => $source) { if ($spam && !$this->_options['continuose']) { @@ -124,43 +133,67 @@ $this->_results[$id] = false; } else { $this->_results[$id] = $this->_checkSource($this->_options['sources'][$id], $trackback); + $spam = ($spam || $this->_results[$id]); } } return $spam; } // }}} - // {{{ _checkSource() - + // {{{ getResults() + /** - * Check a specific source if a trackback has to be considered spam. - * Check a specific source if a trackback has to be considered spam. + * Get spam check results. + * Receive the results determined by the spam check. * * @since 0.5.0 - * @access protected - * @abstract - * @param mixed $source Element of the _sources array to check. - * @param object(Services_Trackback) The trackback to check. - * @return bool True if trackback is spam, false, if not, PEAR_Error on error. + * @access public + * @return array Array of specific spam check results. */ - function _checkSource($source, $trackback) + function getResults() { - PEAR::raiseError('Method not implemented.', -1); + return $this->_results; } // }}} - // {{{ check() + // {{{ reset() /** - * Get spam check results. - * Receive the results determined by the spam check. + * Reset results. + * Reset results to reuse SpamCheck. * * @since 0.5.0 + * @static * @access public - * @return array Array of specific spam check results. + * @return null */ - function getResults() + function reset() { - return $this->_results; + $this->_results = array(); + } + + // }}} + // {{{ _checkSource() + + /** + * Check a specific source if a trackback has to be considered spam. + * Check a specific source if a trackback has to be considered spam. + * + * @since 0.5.0 + * @access protected + * @abstract + * @param mixed $source Element of the _sources array to check. + * @param object(Services_Trackback) The trackback to check. + * @return bool True if trackback is spam, false, if not, PEAR_Error on error. + */ + function _checkSource($source, $trackback) + { + $this->_surbl->setBlacklists(array($source)); + foreach ($this->_urls as $url) { + if ($this->_surbl->isListed($url)) { + return true; + } + } + return false; } // }}} http://cvs.php.net/diff.php/pear/Services_Trackback/Services/Trackback/SpamCheck/DNSBL.php?r1=1.3&r2=1.4&ty=u Index: pear/Services_Trackback/Services/Trackback/SpamCheck/DNSBL.php diff -u pear/Services_Trackback/Services/Trackback/SpamCheck/DNSBL.php:1.3 pear/Services_Trackback/Services/Trackback/SpamCheck/DNSBL.php:1.4 --- pear/Services_Trackback/Services/Trackback/SpamCheck/DNSBL.php:1.3 Mon Apr 25 07:19:57 2005 +++ pear/Services_Trackback/Services/Trackback/SpamCheck/DNSBL.php Mon May 23 07:48:36 2005 @@ -21,7 +21,7 @@ * @author Tobias Schlitt <toby@php.net> * @copyright 1997-2005 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 - * @version CVS: $Id: DNSBL.php,v 1.3 2005/04/25 11:19:57 toby Exp $ + * @version CVS: $Id: DNSBL.php,v 1.4 2005/05/23 11:48:36 toby Exp $ * @link http://pear.php.net/package/Services_Trackback * @since File available since Release 0.5.0 */ @@ -37,7 +37,12 @@ * Load Net_DNSBL for spam cheching */ require_once 'Net/DNSBL.php'; - + +/** + * Load SpamCheck base class + */ +require_once 'Services/Trackback/SpamCheck.php'; + // }}} /** @@ -54,7 +59,7 @@ * @since 0.5.0 * @access public */ -class Services_Trackback_SpamProtection_DNSBL extends Services_Trackback_SpamProtection { +class Services_Trackback_SpamCheck_DNSBL extends Services_Trackback_SpamCheck { // {{{ _options @@ -85,23 +90,26 @@ var $_dnsbl; // }}} - // {{{ create() + // {{{ Services_Trackback_SpamCheck_DNSBL() /** - * Factory. + * Constructor. * Create a new instance of the DNSBL spam protection module. * * @since 0.5.0 - * @static * @access public * @param array $options An array of options for this spam protection module. General options are * 'continuose': Whether to continue checking more sources, if a match has been found. * 'sources': List of blacklist nameservers. Indexed. * @return object(Services_Trackback_SpamCheck_DNSBL) The newly created SpamCheck object. */ - function create($options = null) + function Services_Trackback_SpamCheck_DNSBL($options = null) { - $this->_options = $options; + if (is_array($options)) { + foreach ($options as $key => $val) { + $this->_options[$key] = $val; + } + } $this->_dnsbl = new Net_DNSBL(); } http://cvs.php.net/diff.php/pear/Services_Trackback/Services/Trackback/SpamCheck/SURBL.php?r1=1.2&r2=1.3&ty=u Index: pear/Services_Trackback/Services/Trackback/SpamCheck/SURBL.php diff -u pear/Services_Trackback/Services/Trackback/SpamCheck/SURBL.php:1.2 pear/Services_Trackback/Services/Trackback/SpamCheck/SURBL.php:1.3 --- pear/Services_Trackback/Services/Trackback/SpamCheck/SURBL.php:1.2 Mon Apr 25 07:19:57 2005 +++ pear/Services_Trackback/Services/Trackback/SpamCheck/SURBL.php Mon May 23 07:48:36 2005 @@ -21,22 +21,27 @@ * @author Tobias Schlitt <toby@php.net> * @copyright 1997-2005 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 - * @version CVS: $Id: SURBL.php,v 1.2 2005/04/25 11:19:57 toby Exp $ + * @version CVS: $Id: SURBL.php,v 1.3 2005/05/23 11:48:36 toby Exp $ * @link http://pear.php.net/package/Services_Trackback * @since File available since Release 0.5.0 */ - // {{{ require_once + // {{{ require_once /** * Load PEAR error handling */ require_once 'PEAR.php'; - + +/** + * Load SpamCheck base. + */ +require_once 'Services/Trackback/SpamCheck.php'; + /** * Load Net_SURBL for spam cheching */ -require_once 'Net/SURBL.php'; +require_once 'Net/DNSBL/SURBL.php'; // }}} @@ -54,12 +59,12 @@ * @since 0.5.0 * @access public */ -class Services_Trackback_SpamProtection_SURBL extends Services_Trackback_SpamProtection { +class Services_Trackback_SpamCheck_SURBL extends Services_Trackback_SpamCheck { // {{{ _options /** - * Options for the SpamProtection. + * Options for the SpamCheck. * * @var array * @since 0.5.0 @@ -71,6 +76,7 @@ 'multi.surbl.org' ), 'elements' => array( + 'url', 'title', 'excerpt', ), @@ -98,17 +104,16 @@ * @access private * @since 0.5.0 */ - var $_urls; + var $_urls = array(); // }}} - // {{{ create() + // {{{ Services_Trackback_SpamCheck_SURBL() /** - * Factory. + * Constructor. * Create a new instance of the SURBL spam protection module. * * @since 0.5.0 - * @static * @access public * @param array $options An array of options for this spam protection module. General options are * 'continuose': Whether to continue checking more sources, if a match has been found. @@ -117,10 +122,32 @@ * and 'excerpt'). * @return object(Services_Trackback_SpamCheck_SURBL) The newly created SpamCheck object. */ - function create($options = null) + function Services_Trackback_SpamCheck_SURBL($options = null) { - $this->_options = $options; - $this->_dnsbl = new Net_DNSBL_SURBL(); + if (is_array($options)) { + foreach ($options as $key => $val) { + $this->_options[$key] = $val; + } + } + $this->_surbl = new Net_DNSBL_SURBL(); + } + + // }}} + // {{{ reset() + + /** + * Reset results. + * Reset results to reuse SpamCheck. + * + * @since 0.5.0 + * @static + * @access public + * @return null + */ + function reset() + { + $this->_urls = array(); + $this->_results = array(); } // }}} @@ -128,13 +155,13 @@ function _checkSource(&$source, $trackback) { - if (!isset($this->_urls)) { + if (count($this->_urls) == 0) { $this->_extractURLs($trackback); } - $this->_dnsbl->setBlacklists(array($source)); + $this->_surbl->setBlacklists(array($source)); $spam = false; foreach ($this->_urls as $url) { - $spam = ($spam || $this->_dnsbl->isListed($url)); + $spam = ($spam || $this->_surbl->isListed($url)); if ($spam) { break; } @@ -143,5 +170,34 @@ } // }}} + // {{{ _extractURLs() + + function _extractURLs($trackback) + { + $urls = '(?:http|file|ftp)'; + $ltrs = 'a-z0-9'; + $gunk = '.-'; + $any = "$ltrs$gunk"; + $regex = "{ + $urls :// + [$any]+ + + + (?= + [$punc]* + [^$any] + | + ) + }x"; + foreach ($this->_options['elements'] as $element) { + if (0 !== preg_match_all($regex, $trackback->get($element), $matches)) { + foreach ($matches[0] as $match) { + $this->_urls[md5($match)] = $match; + } + } + } + } + + // }}} } http://cvs.php.net/diff.php/pear/Services_Trackback/Services/Trackback/SpamCheck/Wordlist.php?r1=1.2&r2=1.3&ty=u Index: pear/Services_Trackback/Services/Trackback/SpamCheck/Wordlist.php diff -u pear/Services_Trackback/Services/Trackback/SpamCheck/Wordlist.php:1.2 pear/Services_Trackback/Services/Trackback/SpamCheck/Wordlist.php:1.3 --- pear/Services_Trackback/Services/Trackback/SpamCheck/Wordlist.php:1.2 Mon Apr 25 07:19:57 2005 +++ pear/Services_Trackback/Services/Trackback/SpamCheck/Wordlist.php Mon May 23 07:48:36 2005 @@ -21,7 +21,7 @@ * @author Tobias Schlitt <toby@php.net> * @copyright 1997-2005 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 - * @version CVS: $Id: Wordlist.php,v 1.2 2005/04/25 11:19:57 toby Exp $ + * @version CVS: $Id: Wordlist.php,v 1.3 2005/05/23 11:48:36 toby Exp $ * @link http://pear.php.net/package/Services_Trackback * @since File available since Release 0.5.0 */ @@ -32,11 +32,12 @@ * Load PEAR error handling */ require_once 'PEAR.php'; - + /** - * Load Net_SURBL for spam cheching + * Load SpamCheck base class */ -require_once 'Net/SURBL.php'; + +require_once 'Services/Trackback/SpamCheck.php'; // }}} @@ -54,7 +55,7 @@ * @since 0.5.0 * @access public */ -class Services_Trackback_SpamProtection_Wordlist extends Services_Trackback_SpamProtection { +class Services_Trackback_SpamCheck_Wordlist extends Services_Trackback_SpamCheck { // {{{ _options @@ -92,31 +93,35 @@ 'elements' => array( 'title', 'excerpt', + 'blog_name', ), - 'comparefunc' => 'stripos', + 'comparefunc' => array('Services_Trackback_SpamCheck_Wordlist', '_stripos'), 'minmatches' => 1, ); // }}} - // {{{ create() + // {{{ Services_Trackback_SpamCheck_Wordlist() /** - * Factory. - * Create a new instance of the SURBL spam protection module. + * Constructor. + * Create a new instance of the Wordlist spam protection module. * * @since 0.5.0 - * @static * @access public * @param array $options An array of options for this spam protection module. General options are * 'continuose': Whether to continue checking more sources, if a match has been found. * 'sources': List of blacklist servers. Indexed. * 'comparefunc': A compare function callback with parameters $haystack, $needle (like 'stripos'). * 'minmatches': How many words have to be found to consider spam. - * @return object(Services_Trackback_SpamCheck_SURBL) The newly created SpamCheck object. + * @return object(Services_Trackback_SpamCheck_WordList) The newly created SpamCheck object. */ - function create($options = null) + function Services_Trackback_SpamCheck_Wordlist($options = null) { - $this->_options = $options; + if (is_array($options)) { + foreach ($options as $key => $val) { + $this->_options[$key] = $val; + } + } } // }}} @@ -126,11 +131,13 @@ { $spamCount = 0; foreach ($this->_options['sources'] as $id => $source) { - if ($spam > 0 && !$this->_options['continuose']) { + if ($spamCount >= $this->_options['minmatches'] && !$this->_options['continuose']) { // We already found spam and shall not continue $this->_results[$id] = false; } else { - $this->_results[$id] = $this->_checkSource($this->_options['sources'][$id], $trackback); + $res = $this->_checkSource($this->_options['sources'][$id], $trackback); + $spamCount += ($res === true) ? 1 : 0; + $this->_results[$id] = $res; } } return ($spamCount >= $this->_options['minmatches']); @@ -143,7 +150,7 @@ { $spam = false; foreach ($this->_options['elements'] as $element) { - if (false !== $this->_options['comparefunc']($source, $trackback->get($element)) { + if (false !== call_user_func($this->_options['comparefunc'], $source, $trackback->get($element))) { $spam = true; break; } @@ -152,5 +159,14 @@ } // }}} + // {{{ _stripos() + + function _stripos($source, $element) + { + // echo "Search in " . strtolower($element) . " for '" . strtolower($source) . "'\n"; + return (strpos(strtolower($element), strtolower($source))); + } + + // }}} }
« previous php.pear.cvs (#32051) next »