]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/SpamBlocklist.php
Allow bold, italics or underlined for numbers
[SourceForge/phpwiki.git] / lib / SpamBlocklist.php
1 <?php
2
3 /* Copyright (C) 2005 Reini Urban
4  *
5  * This file is part of PhpWiki.
6  *
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.
11  *
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.
16  *
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.
20  *
21  */
22
23 /**
24  * See http://www.surbl.org/
25  *
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
30  */
31
32 /*
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.
35  */
36 function stripDomainPrefixes($host)
37 {
38     static $twoleveltlds = array();
39     $host_elements = explode('.', $host);
40     while (count($host_elements) > 3) {
41         array_shift($host_elements);
42     }
43     $host_3_elements = implode('.', $host_elements);
44     if (count($host_elements) > 2) {
45         array_shift($host_elements);
46     }
47     $host_2_elements = implode('.', $host_elements);
48     if (empty($twoleveltlds)) {
49         $data = @file(dirname(__FILE__) . "/../config/two-level-tlds");
50         $twoleveltlds = $data ? array_flip($data) : array();
51     }
52     if (array_key_exists($host_2_elements, $twoleveltlds))
53         //IS_IN_2LEVEL: we want the last three names
54         $host = $host_3_elements;
55     else
56         // IS_NOT_2LEVEL: we want the last two names
57         $host = $host_2_elements;
58     return $host;
59 }
60
61 function IsBlackListed($uri)
62 {
63     static $blacklists = array("multi.surbl.org", "bl.spamcop.net");
64     /* "sbl-xbl.spamhaus.net" */
65     static $whitelist = array();
66     if (empty($whitelist)) { // list of domains
67         $data = @file(dirname(__FILE__) . "/../config/whitelist");
68         $whitelist = $data ? array_flip($data) : array();
69     }
70
71     $parsed_uri = parse_url($uri);
72     if (!empty($parsed_uri['host']))
73         $host = $parsed_uri['host'];
74     else
75         $host = $parsed_uri['path'];
76     if (array_key_exists($host, $whitelist))
77         return 0;
78     if (preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $host)) {
79         $host = implode('.', array_reverse(explode('.', $host)));
80         $revip = 1;
81     } else {
82         $revip = 0;
83     }
84     foreach ($blacklists as $bl) {
85         if (!$revip and $bl == "multi.surbl.org") {
86             $host = stripDomainPrefixes($host); // strip domain prefixes
87             if (array_key_exists($host, $whitelist))
88                 return 0;
89         } elseif (!$revip) {
90             // convert to IP addr and revert it.
91             $host = implode('.', array_reverse(explode('.', gethostbyname($host))));
92         }
93         //echo "($host.$bl)";
94         $res = gethostbyname($host . "." . $bl);
95         if (preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $res))
96             return array($bl, $res, $host);
97     }
98     return 0;
99 }
100
101 /*
102 if (defined('SPAMBLOCKLIST_TEST') and SPAMBLOCKLIST_TEST) {
103     BlackListDebug("10.20.30.40");
104     BlackListDebug("spammer.org");
105     BlackListDebug("www.ricoruca.com");
106     BlackListDebug("ricoruca.com");
107     BlackListDebug("soft-sky.org");
108     BlackListDebug("lovers.lv");
109     BlackListDebug("wumort.net");
110     BlackListDebug("bibleinayear.org");
111     BlackListDebug("p50927464.dip.t-dialin.net");
112 }
113 function BlackListDebug($host) {
114     $res = IsBlackListed($host);
115     echo sprintf("%12s", $host), "\tis";
116     if ($res)
117     echo " listed at $res[0] - $res[1]\n";
118     else
119     echo " not blacklisted.\n";
120 }
121 */
122
123 // Local Variables:
124 // mode: php
125 // tab-width: 8
126 // c-basic-offset: 4
127 // c-hanging-comment-ender-p: nil
128 // indent-tabs-mode: nil
129 // End: