<?
/*
 * Class: Dnsbl
 * Compatibility: PHP5 only
 * Version: 1.0
 * Description: DNS Blacklist (http://en.wikipedia.org/wiki/DNSBL) Lookup
 * Author: Valery Dachev <contact@vdachev.net>
 * License: Creative Commons Attribution-Noncommercial-Share Alike 3.0 License
 * License URL: (http://creativecommons.org/licenses/by-nc-sa/3.0/)
 *
 * Notes:
 *
 * - To change the blacklists addresses are checked against, modify the $blacklists class variable.
 *
 * - You can modify the $blacklists varibla on-the-fly:
 *    Dnsbl::$blacklists[] = 'list.dsbl.org';
 *    $dnsbl = new Dnsbl($_SERVER['REMOTE_ADDR']);
 *
 * - To check if your visitor's IP is blacklisted:
 *    $dnsbl = new Dnsbl($_SERVER['REMOTE_ADDR']);
 *    if($dnsbl->isListed)
 *    {
 *        echo "Your IP address has been blacklisted in " . $dnsbl->blacklist . " because of the following reason: " . $dnsbl->reason . ".";
 *        exit;
 *    }
 *    else
 *    {
 *        echo "Your IP address is not blacklisted."
 *    }
 * 
 */

class Dnsbl
{
    public 
$ip null;
    public 
$isListed false;
    public 
$blacklist null;
    public 
$reason null;

    public static 
$blacklists = array(
        
'dnsbl.dronebl.org',
        
'ircbl.ahbl.org',
        
'tor.dnsbl.sectoor.de',
        
'rbl.efnet.org',
        
'dnsbl.njabl.org',
    );

    public function 
__construct($ip)
    {
        if(! 
preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/'$ip))
            throw new 
Exception('Invalid IP address.');

        
$this->ip $ip;

        
$chunks explode('.'$this->ip);
        
$reverseIp sprintf('%d.%d.%d.%d'$chunks[3], $chunks[2], $chunks[1], $chunks[0]);

        foreach(
self::$blacklists as $blacklist)
        {
            
$lookupHost $reverseIp '.' $blacklist;
            if(
function_exists('dns_get_record'))
            {
                
$result dns_get_record($lookupHostDNS_A);
                if(
count($result) == 0)
                    continue;

                
$this->isListed true;
                
$this->blacklist $blacklist;

                
$result dns_get_record($lookupHostDNS_TXT);
                if(
count($result) == 0)
                    break;

                
$this->reason $result[0]['txt'];

                break;
            }
            else
            {
                
$result checkdnsrr($lookupHost'A');
                if(
$result === false)
                    continue;

                
$this->isListed true;
                
$this->blacklist $blacklist;

                break;
            }
        }
    }
}
?>