From 78de4c894baaaf2112bf82bf3542c4bc90653c16 Mon Sep 17 00:00:00 2001
From: onli <onli@paskuda.biz>
Date: Tue, 10 May 2016 03:01:16 +0000
Subject: [PATCH] Update Net_DNSBL to 1.3.7 (#399)

---
 bundled-libs/Net/DNSBL.php       | 281 +++++++++++++++++++++++++------
 bundled-libs/Net/DNSBL/SURBL.php | 121 +++++++------
 2 files changed, 297 insertions(+), 105 deletions(-)

diff --git a/bundled-libs/Net/DNSBL.php b/bundled-libs/Net/DNSBL.php
index 3091dab3..244a5bb5 100644
--- a/bundled-libs/Net/DNSBL.php
+++ b/bundled-libs/Net/DNSBL.php
@@ -1,22 +1,6 @@
 <?php
+
 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
-// +----------------------------------------------------------------------+
-// | PEAR::Net_DNSBL                                                      |
-// +----------------------------------------------------------------------+
-// | Copyright (c) 2004 Sebastian Nohn <sebastian@nohn.net>               |
-// +----------------------------------------------------------------------+
-// | 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          |
-// | license@php.net so we can mail you a copy immediately.               |
-// +----------------------------------------------------------------------+
-// | Authors: Sebastian Nohn <sebastian@nohn.net>                         |
-// +----------------------------------------------------------------------+
-//
-// $Id: DNSBL.php,v 1.4 2004/12/02 14:23:51 nohn Exp $
 
 /**
  * PEAR::Net_DNSBL
@@ -24,17 +8,49 @@
  * This class acts as interface to generic Realtime Blocking Lists
  * (RBL)
  *
- * Net_RBL looks up an supplied host if it's listed in 1-n supplied
+ * PHP versions 5
+ *
+ * LICENSE: This source file is subject to version 3.01 of the PHP license
+ * that is available through the world-wide-web at the following URI:
+ * http://www.php.net/license/3_01.txt.  If you did not receive a copy of
+ * the PHP License and are unable to obtain it through the web, please
+ * send a note to license@php.net so we can mail you a copy immediately.
+ *
+ * Net_DNSBL looks up an supplied host if it's listed in 1-n supplied
  * Blacklists
  *
- * @author  Sebastian Nohn <sebastian@nohn.net>
- * @package Net_DNSBL
- * @license http://www.php.net/license/3_0.txt
- * @version 0.5.3
+ * @category  Net
+ * @package   Net_DNSBL
+ * @author    Sebastian Nohn <sebastian@nohn.net>
+ * @author    Ammar Ibrahim <fixxme@fixme.com>
+ * @copyright 2004-2012 Sebastian Nohn <sebastian@nohn.net>
+ * @license   http://www.php.net/license/3_01.txt  PHP License 3.01
+ * @version   CVS: $Id: DNSBL.php 325344 2012-04-20 04:31:30Z nohn $
+ * @link      http://pear.php.net/package/Net_DNSBL
+ * @see       Net_DNS
+ * @since     File available since Release 1.0.0
  */
-require_once dirname(__FILE__) . '/CheckIP.php';
 
-class Net_DNSBL {
+require_once 'Net/DNS.php';
+
+/**
+ * PEAR::Net_DNSBL
+ *
+ * This class acts as interface to DNSBLs
+ *
+ * Net_DNSBL looks up an supplied IP if it's listed in a
+ * DNS Blacklist.
+ *
+ * @category Net
+ * @package  Net_DNSBL
+ * @author   Sebastian Nohn <sebastian@nohn.net>
+ * @license  http://www.php.net/license/3_01.txt  PHP License 3.01
+ * @version  Release: 1.3.7
+ * @link     http://pear.php.net/package/net_dnsbl Package Home
+ */
+
+class Net_DNSBL
+{
 
     /**     
      * Array of blacklists.
@@ -44,17 +60,26 @@ class Net_DNSBL {
      * @var    array
      * @access protected
      */
-    var $blacklists = array('sbl-xbl.spamhaus.net',
-                            'bl.spamcop.net');
+    protected $blacklists = array('sbl-xbl.spamhaus.org',
+                                  'bl.spamcop.net');
+
+    /**     
+     * Array of Results
+     *
+     * @var    array
+     * @access protected
+     */
+    protected $results = array();
 
     /**
      * Set the blacklist to a desired blacklist.
      *
-     * @param  array Array of blacklists to use. May contain only one element.
+     * @param array $blacklists Array of blacklists to use.
+     *
      * @access public
      * @return bool true if the operation was successful
      */
-    function setBlacklists($blacklists)
+    public function setBlacklists($blacklists)
     {
         if (is_array($blacklists)) {
             $this->blacklists = $blacklists;
@@ -70,32 +95,176 @@ class Net_DNSBL {
      * @access public
      * @return array Currently set blacklists.
      */
-    function getBlacklists()
+    public function getBlacklists()
     {
         return $this->blacklists;
     }
 
+    /** 
+     * Returns Blacklist and Reply from the Blacklist, a host is listed in.
+     *
+     * @param string $host Host to check
+     *
+     * @access public
+     * @return array result. $result['dnsbl'] contains DNSBL,
+     *               $result['record'] contains returned DNS record.
+     */
+    public function getDetails($host)
+    {
+        if (isset($this->results[$host])) {
+            return $this->results[$host];
+        } else {
+            return false;
+        }
+    } // function
+
+    /**
+     * Returns Blacklist, host is listed in.
+     *
+     * @param string $host Host to check
+     *
+     * @access public
+     * @return bl, a host is listed in or false
+     */
+    public function getListingBl($host)
+    {
+        if (isset($this->results[$host]['dnsbl'])) {
+            return $this->results[$host]['dnsbl'];
+        }
+
+        if (isset($this->results[$host]) && is_array($this->results[$host])) {
+            $result = array_keys($this->results[$host]);
+            if ($result == null) {
+                return false;
+            }
+
+            return 'multiple ('.implode(', ', $result).')';          
+        }
+        return false;
+    } // function
+
+    /**
+     * Returns Blacklists, host is listed in. isListed() must have
+     * been called with checkall = true
+     *
+     * @param string $host Host to check
+     *
+     * @access public
+     * @return array blacklists, a host is listed in or false
+     */
+    public function getListingBls($host)
+    {
+        if (isset($this->results[$host]) && is_array($this->results[$host])) {
+            $result = array_keys($this->results[$host]);
+            if ($result == null) {
+                return false;
+            }
+
+            return $result;           
+        }
+
+        return false;
+        
+    } // function
+
+    /**
+     * Returns result, when a host is listed.
+     *
+     * @param string $host Host to check
+     *
+     * @access public
+     * @return bl, a host is listed in or false
+     */
+    public function getListingRecord($host)
+    {
+        if (isset($this->results[$host]['record'])) {
+            return $this->results[$host]['record'];
+        } else {
+            return false;
+        }
+    } // function
+
+    /**
+     * Returns TXT-Records, when a host is listed.
+     *
+     * @param string $host Host to check
+     *
+     * @access public
+     * @return array TXT-Records for this host
+     */
+    public function getTxt($host)
+    {
+        if (isset($this->results[$host]['txt'])) {
+            return $this->results[$host]['txt'];
+        } else {
+            return false;
+        }
+    } // function
+
     /** 
      * Checks if the supplied Host is listed in one or more of the
      * RBLs.
      *
-     * @param  string Host to check for being listed.
+     * @param string  $host     Host to check for being listed.
+     * @param boolean $checkall Iterate through all blacklists and
+     *                          return all A records or stop after 
+     *                          the first hit?
+     *
      * @access public
      * @return boolean true if the checked host is listed in a blacklist.
      */
-    function isListed($host)
+    public function isListed($host, $checkall = false)
     {
-        
         $isListed = false;
-        
+        $resolver = new Net_DNS_Resolver;
+
+        if (!is_string($host)) {
+            return false;
+        }
+
         foreach ($this->blacklists as $blacklist) {
-            $result = gethostbyname($this->getHostForLookup($host, $blacklist));
-            if ($result != $this->getHostForLookup($host, $blacklist)) { 
+            $response = $resolver->query($this->getHostForLookup($host, $blacklist));
+            if ($response) {
                 $isListed = true;
-                
-                //if the Host was listed we don't need to check other RBLs,
-                break;
-                
+                if ($checkall) {
+                    $this->results[$host][$blacklist] = array();
+                    foreach ($response->answer as $answer) {
+                        $this->results[$host][$blacklist]['record'][] 
+                            = $answer->address;
+                    }
+                    $response_txt 
+                        = $resolver->query(
+                            $this->getHostForLookup(
+                                $host, 
+                                $blacklist
+                            ), 
+                            'TXT'
+                        );
+                    if (isset($response_txt->answer)) {
+                        foreach ($response_txt->answer as $txt) {
+                            $this->results[$host][$blacklist]['txt'][]
+                                = $txt->text[0];
+                        }
+                    }
+                } else {
+                    $this->results[$host]['dnsbl']  = $blacklist;
+                    $this->results[$host]['record'] = $response->answer[0]->address;
+                    $response_txt
+                        = $resolver->query(
+                            $this->getHostForLookup(
+                                $host, 
+                                $blacklist
+                            ), 
+                            'TXT'
+                        );
+                    if ((isset($response_txt)) && ($response_txt != false)) {
+                        foreach ($response_txt->answer as $txt) {
+                            $this->results[$host]['txt'][] = $txt->text[0];
+                        }
+                    }
+                    // if the Host was listed we don't need to check other RBLs,
+                    break;
+                }
             } // if
         } // foreach
         
@@ -106,18 +275,25 @@ class Net_DNSBL {
      * Get host to lookup. Lookup a host if neccessary and get the
      * complete FQDN to lookup.
      *
-     * @param  string Host OR IP to use for building the lookup.
-     * @param  string Blacklist to use for building the lookup.
+     * @param string $host      Host OR IP to use for building the lookup.
+     * @param string $blacklist Blacklist to use for building the lookup.
+     *
      * @access protected
      * @return string Ready to use host to lookup
      */    
-    function getHostForLookup($host, $blacklist) 
+    protected function getHostForLookup($host, $blacklist) 
     {
         // Currently only works for v4 addresses.
-        if (!Net_CheckIP::check_ip($host)) {
-            $ip = gethostbyname($host);
+        if (filter_var($host, FILTER_VALIDATE_IP)) {
+                                        $ip = $host;
         } else {
-            $ip = $host;
+            $resolver = new Net_DNS_Resolver;
+            $response = $resolver->query($host);
+            $ip       = isset($response->answer[0]->address) ? 
+                        $response->answer[0]->address : null;
+        }
+        if (!$ip || !filter_var($ip, FILTER_VALIDATE_IP)) {
+            return;
         }
 
         return $this->buildLookUpHost($ip, $blacklist);
@@ -126,12 +302,13 @@ class Net_DNSBL {
     /**
      * Build the host to lookup from an IP.
      *
-     * @param  string IP to use for building the lookup.
-     * @param  string Blacklist to use for building the lookup.
+     * @param string $ip        IP to use for building the lookup.
+     * @param string $blacklist Blacklist to use for building the lookup.
+     *
      * @access protected
      * @return string Ready to use host to lookup
      */    
-    function buildLookUpHost($ip, $blacklist)
+    protected function buildLookUpHost($ip, $blacklist)
     {
         return $this->reverseIp($ip).'.'.$blacklist;        
     } // function
@@ -140,14 +317,14 @@ class Net_DNSBL {
      * Reverse the order of an IP. 127.0.0.1 -> 1.0.0.127. Currently
      * only works for v4-adresses
      *
-     * @param  string IP to reverse.
+     * @param string $ip IP address to reverse.
+     *
      * @access protected
      * @return string Reversed IP
      */    
-    function reverseIp($ip) 
+    protected function reverseIp($ip) 
     {        
         return implode('.', array_reverse(explode('.', $ip)));        
     } // function
-
 } // class
-?>
\ No newline at end of file
+?>
diff --git a/bundled-libs/Net/DNSBL/SURBL.php b/bundled-libs/Net/DNSBL/SURBL.php
index 9fe4b448..4b874741 100644
--- a/bundled-libs/Net/DNSBL/SURBL.php
+++ b/bundled-libs/Net/DNSBL/SURBL.php
@@ -1,22 +1,39 @@
 <?php
 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
-// +----------------------------------------------------------------------+
-// | PEAR::Net_DNSBL_SURBL                                                |
-// +----------------------------------------------------------------------+
-// | Copyright (c) 2004 Sebastian Nohn <sebastian@nohn.net>               |
-// +----------------------------------------------------------------------+
-// | 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          |
-// | license@php.net so we can mail you a copy immediately.               |
-// +----------------------------------------------------------------------+
-// | Authors: Sebastian Nohn <sebastian@nohn.net>                         |
-// +----------------------------------------------------------------------+
-//
-// $Id: SURBL.php,v 1.4 2004/12/02 14:23:51 nohn Exp $
+
+/**
+ * PEAR::Net_DNSBL
+ *
+ * This class acts as interface to generic Realtime Blocking Lists
+ * (RBL)
+ *
+ * PHP versions 5
+ *
+ * LICENSE: This source file is subject to version 3.01 of the PHP license
+ * that is available through the world-wide-web at the following URI:
+ * http://www.php.net/license/3_01.txt.  If you did not receive a copy of
+ * the PHP License and are unable to obtain it through the web, please
+ * send a note to license@php.net so we can mail you a copy immediately.
+ *
+ * Net_DNSBL looks up an supplied host if it's listed in 1-n supplied
+ * Blacklists
+ *
+ * @category  Net
+ * @package   Net_DNSBL
+ * @author    Sebastian Nohn <sebastian@nohn.net>
+ * @author    Ammar Ibrahim <fixxme@fixme.com>
+ * @copyright 2004-2012 Sebastian Nohn <sebastian@nohn.net>
+ * @license   http://www.php.net/license/3_01.txt  PHP License 3.01
+ * @version   CVS: $Id: SURBL.php 325344 2012-04-20 04:31:30Z nohn $
+ * @link      http://pear.php.net/package/Net_DNSBL
+ * @see       Net_DNS2
+ * @since     File available since Release 1.0.0
+ */
+
+require_once 'Cache/Lite.php';
+require_once 'HTTP/Request2.php';
+require_once 'Net/DNSBL.php';
+require_once 'PEAR.php';
 
 /**
  * PEAR::Net_DNSBL_SURBL
@@ -26,17 +43,16 @@
  * Services_SURBL looks up an supplied URI if it's listed in a
  * Spam URI Realtime Blocklists.
  *
- * @author  Sebastian Nohn <sebastian@nohn.net>
- * @package Net_DNSBL
- * @license http://www.php.net/license/3_0.txt
- * @version 0.5.4
+ * @category Net
+ * @package  Net_DNSBL
+ * @author   Sebastian Nohn <sebastian@nohn.net>
+ * @license  http://www.php.net/license/3_01.txt PHP License 3.01
+ * @version  Release: 1.3.7
+ * @link     http://pear.php.net/package/net_dnsbl Package Home
  */
-require_once dirname(__FILE__) . '/../../Cache/Lite.php';
-require_once dirname(__FILE__) . '/../../HTTP/Request.php';
-require_once dirname(__FILE__) . '/../CheckIP.php';
-require_once dirname(__FILE__) . '/../DNSBL.php';
 
-class Net_DNSBL_SURBL extends Net_DNSBL {
+class Net_DNSBL_SURBL extends Net_DNSBL
+{
 
     /**     
      * Array of blacklists.
@@ -46,7 +62,7 @@ class Net_DNSBL_SURBL extends Net_DNSBL {
      * @var    string[]
      * @access protected
      */
-    var $blacklists = array('multi.surbl.org');
+     protected $blacklists = array('multi.surbl.org');
 
     /**
      * File containing whitelisted hosts.
@@ -59,41 +75,33 @@ class Net_DNSBL_SURBL extends Net_DNSBL {
      * @see    $twoLevelCcTld
      * @access protected
      */
-    var $doubleCcTldFile = 'http://spamcheck.freeapp.net/two-level-tlds';
-
-    /**
-     * Array of whitelisted hosts.
-     *
-     * @var    array
-     * @see    $twoLevelCcTldFile
-     * @access private
-     */
-    var $twoLevelCcTld = array();
+    protected $doubleCcTldFile = 'http://george.surbl.org/two-level-tlds';
 
     /**
      * Check if the last two parts of the FQDN are whitelisted.
      *
-     * @param  string Host to check if it is whitelisted
+     * @param string $fqdn Host to check if it is whitelisted.
+     *
      * @access protected
      * @return boolean True if the host is whitelisted
      */
-    function isDoubleCcTld($fqdn)
+    protected function isDoubleCcTld($fqdn)
     {
         // 30 Days should be way enough
         $options = array(
                          'lifeTime' => '2592000',
                          'automaticSerialization' => true
                          );
-        $id = md5($this->doubleCcTldFile);
+        $id      = md5($this->doubleCcTldFile);
 
         $cache = new Cache_Lite($options);
         if ($data = $cache->get($id)) {
             // Cache hit
         } else {
             // Cache miss
-            $http = new HTTP_Request($this->doubleCcTldFile);
-            if (!PEAR::isError($http->sendRequest())) {
-                $data = $http->getResponseBody();
+            $http = new HTTP_Request2($this->doubleCcTldFile);
+            if (!PEAR::isError($http->send())) {
+                $data = $http->getBody();
             }
             $data = explode("\n", $data);
             $data = array_flip($data);
@@ -119,18 +127,25 @@ class Net_DNSBL_SURBL extends Net_DNSBL {
      * (3b2) IS_NOT_2LEVEL: we want the last two names
      * (4) return the FQDN to query.
      *
-     * @param  string URL to check. 
+     * @param string $uri       URL to check. 
+     * @param string $blacklist Blacklist to check against. 
+     *
      * @access protected
      * @return string Host to lookup
      */
-    function getHostForLookup($uri, $blacklist) 
+    protected function getHostForLookup($uri, $blacklist) 
     {
-        $host       = '';
         // (1) Extract the hostname from the given URI
+        $host       = '';
         $parsed_uri = parse_url($uri);
-        $host       = $parsed_uri['host'];
+
+        if (empty($parsed_uri['host'])) {
+            return false;
+        }
+
+        $host       = urldecode($parsed_uri['host']);
         // (2) Check if the "hostname" is an ip
-        if (Net_CheckIP::check_ip($host)) {
+        if (filter_var($host, FILTER_VALIDATE_IP)) {
             // (3a) IS_IP Reverse the IP (1.2.3.4 -> 4.3.2.1)
             $host = $this->reverseIp($host);
         } else {
@@ -139,13 +154,13 @@ class Net_DNSBL_SURBL extends Net_DNSBL {
                 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
@@ -156,9 +171,9 @@ class Net_DNSBL_SURBL extends Net_DNSBL {
             } // if
         } // if
         // (4) return the FQDN to query
-        $host      .= '.'.$blacklist;
+        $host .= '.'.$blacklist;
         return $host;
     } // function
-
+    
 } // class
-?>
\ No newline at end of file
+?>