]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/SpamBlocklist.php
fix SpamBlocklist config file reading
[SourceForge/phpwiki.git] / lib / SpamBlocklist.php
1 <?php
2 rcs_id('$Id: SpamBlocklist.php,v 1.2 2007-06-07 16:52:18 rurban Exp $');
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
18  * along with PhpWiki; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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     static $twoleveltlds = array();
38     $host_elements = explode('.', $host);
39     while (count($host_elements) > 3) {
40         array_shift($host_elements);
41     }
42     $host_3_elements = implode('.', $host_elements);
43     if (count($host_elements) > 2) {
44         array_shift($host_elements);
45     }
46     $host_2_elements = implode('.', $host_elements);
47     if (empty($twoleveltlds)) {
48         $data = @file(dirname(__FILE__)."/../config/two-level-tlds");
49         $twoleveltlds = $data ? array_flip($data) : array();
50     }
51     if (array_key_exists($host_2_elements, $twoleveltlds))
52         //IS_IN_2LEVEL: we want the last three names
53         $host = $host_3_elements;
54     else
55         // IS_NOT_2LEVEL: we want the last two names
56         $host = $host_2_elements;
57     return $host;
58 }
59
60 function IsBlackListed($uri) {
61     static $blacklists = array("multi.surbl.org", "bl.spamcop.net");
62                               /* "sbl-xbl.spamhaus.net" */
63     static $whitelist = array();
64     if (empty($whitelist)) { // list of domains
65         $data = @file(dirname(__FILE__)."/../config/whitelist");
66         $whitelist = $data ? array_flip($data) : array();
67     }
68
69     $parsed_uri = parse_url($uri);
70     if (!empty($parsed_uri['host']))
71         $host = $parsed_uri['host'];
72     else
73         $host = $parsed_uri['path'];
74     if (array_key_exists($host, $whitelist))
75         return 0;
76     if (preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $host)) {
77         $host = implode('.', array_reverse(explode('.', $host))); 
78         $revip = 1;
79     } else {
80         $revip = 0;
81     }
82     foreach ($blacklists as $bl) {
83         if (!$revip and $bl == "multi.surbl.org") {
84             $host = stripDomainPrefixes($host); // strip domain prefixes
85             if (array_key_exists($host, $whitelist))
86                 return 0;
87         } elseif (!$revip) {
88             // convert to IP addr and revert it.
89             $host = implode('.', array_reverse(explode('.', gethostbyname($host)))); 
90         }
91         //echo "($host.$bl)";
92         $res = gethostbyname($host . "." . $bl);
93         if (preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $res))
94             return array($bl, $res, $host);
95     }
96     return 0;
97 }
98
99
100 /*
101 if (defined('SPAMBLOCKLIST_TEST') and SPAMBLOCKLIST_TEST) {
102     BlackListDebug("10.20.30.40");
103     BlackListDebug("spammer.org");
104     BlackListDebug("www.ricoruca.com");
105     BlackListDebug("ricoruca.com");
106     BlackListDebug("soft-sky.org");
107     BlackListDebug("lovers.lv");
108     BlackListDebug("wumort.net");
109     BlackListDebug("bibleinayear.org");
110     BlackListDebug("p50927464.dip.t-dialin.net");
111 }
112 function BlackListDebug($host) {
113     $res = IsBlackListed($host);
114     echo sprintf("%12s", $host), "\tis";
115     if ($res)
116         echo " listed at $res[0] - $res[1]\n";
117     else
118         echo " not blacklisted.\n";
119 }
120 */
121
122 // $Log: not supported by cvs2svn $
123 // Revision 1.1  2005/10/29 08:21:58  rurban
124 // ENABLE_SPAMBLOCKLIST:
125 //   Check for links to blocked external tld domains in new edits, against
126 //   multi.surbl.org and bl.spamcop.net.
127 //
128
129 // Local Variables:
130 // mode: php
131 // tab-width: 8
132 // c-basic-offset: 4
133 // c-hanging-comment-ender-p: nil
134 // indent-tabs-mode: nil
135 // End:   
136 ?>