Update of /cvsroot/php-blog/serendipity/bundled-libs/Services
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5805/bundled-libs/Services
Added Files:
Tag: branch-smarty
SURBL.php
Log Message:
Added SURBL support to serendipity_event_spamblock
--- NEW FILE: SURBL.php ---
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PEAR::Services_SURBL |
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 Sebastian Nohn <seb...@no...> |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at the following url: |
// | http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | li...@ph... so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Sebastian Nohn <seb...@no...> |
// +----------------------------------------------------------------------+
//
// $Id: SURBL.php,v 1.1.2.1 2004/11/11 13:31:43 nohn Exp $
/**
* PEAR::Services_SURBL
*
* This class acts as interface to the SURBL - Spam URI Realtime Blocklists.
*
* Services_SURBL looks up an supplied URI if it's listed in a
* Spam URI Realtime Blocklists.
*
* @author Sebastian Nohn <seb...@no...>
* @package Services_SURBL
* @license http://www.php.net/license/3_0.txt
* @version 0.3
*/
require_once "Net/CheckIP.php";
require_once "Cache.php";
class Services_SURBL {
/**
* @var string
* @access protected
*/
var $blacklist = "multi.surbl.org";
/**
* @var string
* @access protected
*/
var $doubleCcTldFile = "http://spamcheck.freeapp.net/two-level-tlds";
/**
* @var array
* @access private
*/
var $twoLevelCcTld = array();
/**
* Check if the last two parts of the FQDN are whitelisted
*
* @param $fqdn
* @access protected
*/
function isDoubleCcTld($fqdn) {
// 30 Day should be way enough
$options = array('lifeTime' => 2);
$id = md5($this->doubleCcTldFile);
$cache = new Cache("file", $options);
if ($data = $cache->get($id)) {
// Cache hit
} else {
// Cache miss
$fp = fopen ($this->doubleCcTldFile, "r");
while (!feof($fp)) {
$buffer = fgets($fp, 4096);
$data[] = trim($buffer);
} // while
$data = array_flip($data);
fclose ($fp);
$cache->save($id, $data);
} // if
if (array_key_exists($fqdn, $data)) {
return true;
} else {
return false;
} // if
} // function
/**
* Get Hostname to ask for
*
* Performs the following steps:
*
* (1) Extract the hostname from the given URI
* (2) Check if the "hostname" is an ip
* (3a) IS_IP Reverse the IP (1.2.3.4 -> 4.3.2.1)
* (3b) IS_FQDN Check if is in "CC-2-level-TLD"
* (3b1) IS_IN_2LEVEL: we want the last three names
* (3b2) IS_NOT_2LEVEL: we want the last two names
* (4) return the FQDN to query
*
* @param string $uri
* @access protected
*/
function getNameForCheck($uri) {
$host = "";
// (1) Extract the hostname from the given URI
$parsed_uri = parse_url($uri);
$host = $parsed_uri["host"];
// (2) Check if the "hostname" is an ip
if (Net_CheckIP::check_ip($host)) {
// (3a) IS_IP Reverse the IP (1.2.3.4 -> 4.3.2.1)
$host = implode(".", array_reverse(explode(".", $host)));
} else {
$host_elements = explode(".", $host);
while (count($host_elements) > 3) {
array_shift($host_elements);
} // while
$host_3_elements = implode(".", $host_elements);
$host_elements = explode(".", $host);
while (count($host_elements) > 2) {
array_shift($host_elements);
} // while
$host_2_elements = implode(".", $host_elements);
// (3b) IS_FQDN Check if is in "CC-2-level-TLD"
if ($this->isDoubleCcTld($host_2_elements)) {
// (3b1) IS_IN_2LEVEL: we want the last three names
$host = $host_3_elements;
} else {
// (3b2) IS_NOT_2LEVEL: we want the last two names
$host = $host_2_elements;
} // if
} // if
// (4) return the FQDN to query
$host .= ".".$this->blacklist;
return $host;
} // function
/**
* Set the blacklist to a desired blacklist.
*
* Default is multi.surbl.org.
*
* It is suggested to use one of these:
*
* sc.surbl.org - SpamCop message-body URI domains
* ws.surbl.org - sa-blacklist domains as a SURBL
* ob.surbl.org - OutBlaze spamvertised sites
* ab.surbl.org - AbuseButler spamvertised sites
* multi.surbl.org - Combined SURBL list
*
* @param string $blacklist
* @access public
*/
function setBlacklist($blacklist = "multi.surbl.org") {
$this->blacklist = $blacklist;
}
/**
* Get the blacklist.
*
* @access public
*/
function getBlacklist() {
return $this->blacklist;
}
/**
* Checks if the supplied URL is listen in an SURBL
*
* @param string $host
* @access public
*/
function isSpam($host) {
$result = gethostbyname($this->getNameForCheck($host));
if ($result != $this->getNameForCheck($host)) {
return true;
} else {
return false;
}
} // function
} // class
?>
|