root/miscfunctions.lib.php

Revision 209, 7.0 kB (checked in by dez, 2 years ago)

Alters version number calculator to take into account symlinks

Line 
1 <?php
2 // does the opposite of PHP's nl2br()
3 function br2nl($string) {
4     $string = preg_replace("/(\r\n|\n|\r)/", "", $string);
5     $string = preg_replace("/<br *\/?>/i", "\n", $string);
6     return $string;
7 }
8
9 // generate a pseudo-word random password
10 function makePassword($length=8)
11 {
12     $password = "";
13     $vowels = "aeiouy";
14     $consonants = "bcdfghjklmnprst";
15     $cn = strlen($consonants)-1;
16     $vn = strlen($vowels)-1;
17     // Start on cons or vowel
18     $alt = mt_rand(0, 1);
19     // How many numbers
20     $len = mt_rand($length-3,$length);
21     //add the letters
22     for ($i = 0; $i < $len; $i++)
23     {
24         if ($alt == 1)    {
25             $password .= $consonants[ mt_rand(0,$cn) ];
26             $alt = 0;
27         }
28         else    {
29             $password .= $vowels[ mt_rand(0,$vn) ];
30             $alt = 1;
31         }
32     }
33     //add the numbers
34     for ($i = 0; $i < $length-$len; $i++)
35     {
36         $password .= mt_rand(0,9);
37     }
38     return $password;
39 }
40
41 $revision = "0";
42 function startElement($parser, $name, $attrs)
43 {
44     global $revision;
45     if ($attrs['REVISION']>$revision) $revision = $attrs['REVISION'];
46 }
47
48 function endElement($parser, $name){}
49
50 function getSVNRevision()
51 {
52     global $revision;
53     $xml_parser = xml_parser_create();
54     xml_set_element_handler($xml_parser, "startElement", "endElement");
55     $entriesfile = ".svn/entries";
56     if (is_link($_SERVER['SCRIPT_FILENAME'])) {
57         $entriesfile = dirname(readlink($_SERVER['SCRIPT_FILENAME']))."/".$entriesfile;
58     }
59     if (!($fp = fopen($entriesfile, "r"))) {
60         return "unknown - couldn't open SVN XML file.";
61     }
62     while(($data = fread($fp, 1024))) {
63         if (!xml_parse($xml_parser, $data, feof($fp))) {
64             return "unknown - couldn't parse SVN XML file";
65         }
66     }
67     xml_parser_free($xml_parser);
68     if ($revision=="0") return "unknown";
69     else return $revision;
70 }
71
72 /*
73  *
74  * Spam Checks
75  *
76  */
77 //Check the Spam URI Realtime Blocklist
78 function checkSpamURLs($text) {
79     $spam = false;
80     //find urls, ugly but works
81     while (ereg("http://[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}",$text,$match)) {
82         $matches[] = $match[0];
83         $text = ereg_replace($match[0],"",$text);
84     }
85     //pull in list of two level tlds, make an array from them. from http://spamcheck.freeapp.net/two-level-tlds
86     $twoLevelTLD = file("/var/www/blog/two-level-tlds");
87     foreach($twoLevelTLD as $TLD) {
88         $two_level_tlds[trim($TLD)] = true;
89     }
90     if (!$matches)
91         return;
92     //for each url
93     foreach ($matches as $url) {
94         //break it down
95         $urlBits = explode(".",substr($url, 7));
96         //reverse the oder
97         $bitsURL = array_reverse($urlBits);
98         //if its a two level tld, we want the first 3 bits of the url.. if not just the first 2
99         if ($two_level_tlds[($bitsURL[1].".".$bitsURL[0])]) {
100             $URLstoTest[] = ($bitsURL[2].".".$bitsURL[1].".".$bitsURL[0]);
101         } else {
102             $URLstoTest[] = ($bitsURL[1].".".$bitsURL[0]);
103         }
104     }
105     if (!$URLstoTest)
106         return;
107     //actualy test each of he domains against the surbl
108     foreach($URLstoTest as $url) {
109         $result = gethostbyname($url.'.multi.surbl.org');
110         if ($result != $url.'.multi.surbl.org') {
111             $spam = true;
112         }
113         elseif ($url == "blogspot.com") {
114             $spam = true;
115         }
116     }
117     return $spam;
118 }
119 //feeds a message body though LinkSleeve (http://www.linksleeve.org/) which at the time of testing seems quite good.
120 function checkSpamLinkSleeve ($text) {
121     // Include the Pear XML-RPC Client Package
122     require_once 'XML/RPC.php';
123     // Build the XML-RPC message
124     $params = array(new XML_RPC_Value($text, 'string'));
125     $msg = new XML_RPC_Message('slv', $params);
126     //Send the XML-RPC message
127     $cli = new XML_RPC_Client('/slv.php', 'http://www.linksleeve.org');
128     $resp = $cli->send($msg);
129     //Check for a responce
130     if (!$resp) {
131         echo 'Communication error: ' . $cli->errstr;
132         return false;
133     }
134     //spam?
135     if (!$resp->faultCode()) {
136         $val = $resp->value();
137         if($val->scalarval()=='1') {
138             $spam = false;
139         }
140         else {
141             $spam = true;
142         }
143     }
144     //Handle Errors
145     else {
146          echo 'Fault Code: ' . $resp->faultCode() . "\n";
147          echo 'Fault Reason: ' . $resp->faultString() . "\n";
148     }
149     return $spam;
150 }
151
152 //checks an ip in several blacklists returns true if its present
153 function checkSpamIP($ip) {
154     $spam = false;
155     //reverse the ip
156     $ip = implode('.',array_reverse(explode('.',$ip)));
157     //look up in various rbls
158     $rbl = gethostbyname($ip.'.rbl-plus.mail-abuse.ja.net');
159     $scbl = gethostbynamel($ip.'.bl.spamcop.net');
160     $sorbs = gethostbynamel($ip.'.dnsbl.sorbs.net');
161     $sbl = gethostbynamel($ip.'.sbl.spamhaus.org');
162     $njabl = gethostbynamel($ip.'.dnsbl.njabl.org');
163     $opm = gethostbyname($ip.'.opm.blitzed.org');
164     $cbl = gethostbynamel($ip.'.cbl.abuseat.org');
165
166     //CBL
167     if ($cbl) {
168         $spam = true;
169     }
170     
171     //OPM
172     if ($opm != $ip.".opm.blitzed.org") {
173         //this bl uses a decimal to represent one catagory of spam source
174         $code = decbin(ip2long($opm));
175         //check for WinGate
176         if ($code[30])
177             $spam = true;
178         //check for SOCKS
179         if ($code[29])
180             $spam = true;
181         //check for HTTP CONNECT
182         if ($code[28])
183             $spam = true;
184         //check for Router
185         if ($code[27])
186             $spam = true;
187         //check for HTTP POST
188         if ($code[26])
189             $spam = true;
190     }
191
192     //RBL+
193     if ($rbl != $ip.".rbl-plus.mail-abuse.ja.net")     {
194         $code = decbin(ip2long($rbl));
195         //check for rbl
196         if ($code[30])
197             $spam = true;
198         //check for dul
199         if ($code[29])
200             //we dont care about dul
201         //check for rss
202         if ($code[28])
203             $spam = true;
204         //check for ops
205         if ($code[27])
206             $spam = true;
207     }
208     
209     //SpamCop
210     if ($scbl) {
211         $spam = true;
212     }
213     
214     //SORBS
215     if ($sorbs) {
216         foreach($sorbs as $result) {
217             $result = explode('.',$result);
218             //check for http
219             if ($result[3] == 2)
220                 $spam = true;
221             //check for socks
222             if ($result[3] == 3)
223                 $spam = true;
224             //check for misc
225             if ($result[3] == 4)
226                 $spam = true;
227             //check for smtp
228             if ($result[3] == 5)
229                 $spam = true;
230             //check for spam
231             if ($result[3] == 6)
232                 $spam = true;
233             //check for web
234             if ($result[3] == 7)
235                 $spam = true;
236             //check for block
237             if ($result[3] == 8)
238                 $spam = true;
239             //check for zombie
240             if ($result[3] == 9)
241                 $spam = true;
242             //check for dul
243             if ($result[3] == 10)
244                 //dont care about dul
245             //check for badconf
246             if ($result[3] == 11)
247                 $spam = true;
248             //check for nomail
249             if ($result[3] == 12)
250                 $spam = true;
251         }
252     }
253
254     //NJABL
255     if ($njabl) {
256         foreach($njabl as $result) {
257             $result = explode('.',$result);
258             //check for relay
259             if ($result[3] == 2)
260                 $spam = true;
261             //check for dul
262             if ($result[3] == 3) {
263                 //dont care about dul
264             }
265             //check for spam
266             if ($result[3] == 4)
267                 $spam = true;
268             //check for relay
269             if ($result[3] == 5)
270                 $spam = true;
271             //check for web
272             if ($result[3] == 8)
273                 $spam = true;
274             //check for proxy
275             if ($result[3] == 9)
276                 $spam = true;
277         }
278     }
279
280     //SBL
281     if($sbl) {
282         $spam = true;
283     }
284     return $spam;
285 }
286
287 # General spam function combining all checks
288 function checkSpam($ip, $text) {
289     //Check LinkSleeve first, its a collaborative statistical thing, and will benefit from seeing all messages, spam or not
290     if (checkSpamLinkSleeve($text)) {
291         $spam = true;
292     //Check any URL's the Spam URL Black List
293     } elseif (checkSpamURLs($text)) {
294         $spam = true;
295     //If all else fails lookup the posting IP in all the normal IP Black Lists
296     } elseif (checkSpamIP($ip)) {
297         $spam = true;
298     //Decide its probably not spam
299     } else {
300         $spam = false;
301     }
302     return $spam;
303 }
304
Note: See TracBrowser for help on using the browser.