3 /* Copyright (C) 2005 Reini Urban
5 * This file is part of PhpWiki.
7 * PhpWiki is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * PhpWiki is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * See http://www.surbl.org/
26 * Perform a name lookup (A) for any link tld against multi.surbl.org and bl.spamcop.net,
27 * like: domainundertest.com.multi.surbl.org
28 * or 40.30.20.10.multi.surbl.org (for http://10.20.30.40/)
29 * This is the same, but a bit lighter than PEAR::Net_DNSBL_SURBL
33 * Strip domain prefixes so that either the last two name parts are returned,
34 * or if it's a known tld (like "co.uk") the last three.
39 function stripDomainPrefixes($host)
41 static $twoleveltlds = array();
42 $host_elements = explode('.', $host);
43 while (count($host_elements) > 3) {
44 array_shift($host_elements);
46 $host_3_elements = implode('.', $host_elements);
47 if (count($host_elements) > 2) {
48 array_shift($host_elements);
50 $host_2_elements = implode('.', $host_elements);
51 if (empty($twoleveltlds)) {
52 $data = @file(dirname(__FILE__) . "/../config/two-level-tlds");
53 $twoleveltlds = $data ? array_flip($data) : array();
55 if (array_key_exists($host_2_elements, $twoleveltlds))
56 //IS_IN_2LEVEL: we want the last three names
57 $host = $host_3_elements;
59 // IS_NOT_2LEVEL: we want the last two names
60 $host = $host_2_elements;
69 function IsBlackListed($uri)
71 static $blacklists = array("multi.surbl.org", "bl.spamcop.net");
72 /* "sbl-xbl.spamhaus.net" */
73 static $whitelist = array();
74 if (empty($whitelist)) { // list of domains
75 $data = @file(dirname(__FILE__) . "/../config/whitelist");
76 $whitelist = $data ? array_flip($data) : array();
79 $parsed_uri = parse_url($uri);
80 if (!empty($parsed_uri['host']))
81 $host = $parsed_uri['host'];
83 $host = $parsed_uri['path'];
84 if (array_key_exists($host, $whitelist))
86 if (preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $host)) {
87 $host = implode('.', array_reverse(explode('.', $host)));
92 foreach ($blacklists as $bl) {
93 if (!$revip and $bl == "multi.surbl.org") {
94 $host = stripDomainPrefixes($host); // strip domain prefixes
95 if (array_key_exists($host, $whitelist))
98 // convert to IP addr and revert it.
99 $host = implode('.', array_reverse(explode('.', gethostbyname($host))));
101 //echo "($host.$bl)";
102 $res = gethostbyname($host . "." . $bl);
103 if (preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $res))
104 return array($bl, $res, $host);
113 // c-hanging-comment-ender-p: nil
114 // indent-tabs-mode: nil