2 // Pivot-Blacklist version 0.4 (with Nucleus Support!)
4 // A simple (but effective) spam blocker based on the MT-Blacklist
5 // available at: http://www.jayallen.org/comment_spam/
7 // Includes realtime blacklist check functions by
8 // John Sinteur (http://weblog.sinteur.com/)
10 // This code (c) 2004 by Marco van Hylckama Vlieg
11 // adapted and extended by Appie Verschoor
12 // License is GPL, just like Pivot / Nucleus
14 // http://www.i-marco.nl/
20 define('__WEBLOG_ROOT', dirname(dirname(realpath(__FILE__))));
21 define('__EXT', '/blacklist');
23 define('NP_BLACKLIST_CACHE_DIR', dirname(__FILE__).'/cache');
24 define('NP_BLACKLIST_CACHE_LIFE', 86400);
25 define('NP_BLACKLIST_CACHE_GC_INTERVAL', NP_BLACKLIST_CACHE_LIFE/8);
26 define('NP_BLACKLIST_CACHE_GC_TIMESTAMP', 'gctime');
27 define('NP_BLACKLIST_CACHE_GC_TIMESTAMP_LIFE', NP_BLACKLIST_CACHE_LIFE*3);
28 //require_once(dirname(__FILE__).'/cache_file.php');
29 require_once(dirname(__FILE__).'/cache_eaccelerator.php');
31 function pbl_getconfig() {
33 $pbl_config = array();
34 $pbl_config['enabled'] = getPluginOption('enabled');
35 $pbl_config['redirect'] = getPluginOption('redirect');
36 //$pbl_config['update'] = getPluginOption('update');
37 // convert 'yes' into '1'
38 if ($pbl_config['enabled'] == 'yes') {$pbl_config['enabled'] = 1;}
42 function pbl_checkforspam($text, $ipblock = false, $ipthreshold = 10, $logrule = true) {
43 // check whether a string contains spam
44 // if it does, we return the rule that was matched first
45 //$text = strtolower($text);
48 // first line of defense; block notorious spammers
51 return "<b>IP Blocked</b>: ".serverVar('REMOTE_ADDR')." (".serverVar('REMOTE_HOST').")";
54 // second line of defense: Check whether our poster is using
56 //if(check_for_open_proxy()) {
57 // if ($ipblock == 'yes') {
58 // pbl_suspectIP ($ipthreshold);
60 // return "open spam proxy";
63 // third line of defense: Check whether our poster promotes
64 // known spamsite url's listed at www.surbl.org
65 //if(check_for_surbl($text)) {
66 // if ($ipblock == 'yes') {
67 // pbl_suspectIP ($ipthreshold);
69 // return("url(s) listed on www.surbl.org found");
72 // fourth line of defense: Run the MT-Blacklist check
73 if( $text && file_exists(__WEBLOG_ROOT.__EXT."/settings/blacklist.pbl") ){
74 $handle = fopen(__WEBLOG_ROOT.__EXT."/settings/blacklist.pbl", "r");
75 while (!feof($handle)) {
76 $buffer = fgets($handle, 4096);
77 $splitbuffer = explode("####", $buffer);
78 $expression = $splitbuffer[0];
79 $explodedSplitBuffer = explode("/", $expression);
80 $expression = $explodedSplitBuffer[0];
81 if (strlen($expression) > 0) {
82 if(preg_match("/".trim($expression)."/im", $text)) {
84 pbl_suspectIP ($ipthreshold);
87 pbl_logRule($expression);
96 // fifth line of defense: run the personal blacklist entries
97 if ($text &&file_exists(__WEBLOG_ROOT.__EXT.'/settings/personal_blacklist.pbl')) {
98 $handle = fopen(__WEBLOG_ROOT.__EXT."/settings/personal_blacklist.pbl", "r");
99 while (!feof($handle)) {
100 $buffer = fgets($handle, 4096);
101 $splitbuffer = explode("####", $buffer);
102 $expression = $splitbuffer[0];
103 if (strlen($expression) > 0) {
104 // if(is_domain($expression)) {
105 // $expression = str_replace(".","\.",$expression);
107 if(preg_match("/".trim($expression)."/im", $text)) {
109 pbl_suspectIP ($ipthreshold);
112 pbl_logRule($expression);
122 if( $ipblock && $listedrbl = check_for_iprbl() ) {
123 pbl_suspectIP ($ipthreshold);
124 $ref = serverVar('HTTP_REFERER');
125 return "ip listed on {$listedrbl[0]} found (Referer:{$ref})";
128 if( $text && ($listedrbl = check_for_domainrbl($text)) ) {
130 pbl_suspectIP ($ipthreshold);
132 return("url(s) listed on {$listedrbl[0]} ({$listedrbl[1]}) found");
135 // w00t! it's probably not spam!
139 function pbl_updateblacklist($url, $force=false) {
141 $listAge = time() - @filemtime(__WEBLOG_ROOT.__EXT.'/settings/blacklist.txt');
142 // 86400 is 24hours (24*60*60)
143 if ((($listAge > 86400 ) || (!file_exists(__WEBLOG_ROOT.__EXT.'/settings/blacklist.txt'))) || ($force)) {
144 $handle = @fopen($url, "r");
146 while (!feof($handle)) {
147 $buffer = fgets($handle, 4096);
148 $newBlackList .= $buffer;
153 // Check whether we really have the file
154 // if not we keep the old one because we don't want to break
155 // the engine with a bad or missing file
157 if(strstr($newBlackList, "MT-Blacklist Master Copy")) {
158 $newFile = fopen(__WEBLOG_ROOT.__EXT.'/settings/blacklist.txt', 'w');
159 fwrite($newFile, $newBlackList);
161 pbl_processblacklist();
169 function pbl_processblacklist() {
170 // reformat the list to match our own format
172 $handle = fopen(__WEBLOG_ROOT.__EXT."/settings/blacklist.txt", "r") or die ("could not open: ".__WEBLOG_ROOT.__EXT."/settings/blacklist.txt");
173 while (!feof($handle)) {
174 $buffer = fgets($handle, 4096);
175 $splitbuffer = explode("#", $buffer);
176 $expression = $splitbuffer[0];
177 $explodedSplitBuffer = explode("/", $expression);
178 $expression = $explodedSplitBuffer[0];
179 if (strlen($expression) > 0) {
180 $listString .= preg_replace("/([^\\\|^_]|^)\./",'$1\\.',trim($expression));
181 if(strlen($splitbuffer[1]) > 5) {
182 $listString .= " #### ".trim($splitbuffer[1]);
188 if(file_exists(__WEBLOG_ROOT.__EXT.'/settings/blacklist.pbl')) {
190 $newhandle = fopen(__WEBLOG_ROOT.__EXT."/settings/blacklist.pbl", "w");
191 fwrite($newhandle, $listString);
196 function is_domain($stheDomain) {
197 return ( (strpos($stheDomain,"\\")==0) && (strpos($stheDomain,"[")==0) && (strpos($stheDomain, "(")==0) );
201 function pbl_nucmenu() {
203 echo "<h2>Blacklist menu</h2>\n";
205 echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?action=blacklist"),ENT_QUOTES)."\"><img src=\"".dirname(serverVar('PHP_SELF'))."/icons/i_edit.gif\" /> Blacklist Editor</a></li>\n";
206 echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?action=log"),ENT_QUOTES)."\"><img src=\"".dirname(serverVar('PHP_SELF'))."/icons/i_log.gif\" /> Blacklist Log</a></li>\n";
207 echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(dirname(serverVar('PHP_SELF'))."/../../index.php?action=pluginoptions&plugid=".getPlugid()),ENT_QUOTES)."\"><img src=\"".dirname(serverVar('PHP_SELF'))."/icons/i_prefs.gif\" /> Blacklist options</a></li>\n";
208 echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?action=testpage"),ENT_QUOTES)."\"><img src=\"".dirname(serverVar('PHP_SELF'))."/icons/i_edit.gif\" /> Test Blacklist</a></li>\n";
209 echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?action=showipblock"),ENT_QUOTES)."\"><img src=\"".dirname(serverVar('PHP_SELF'))."/icons/i_log.gif\" /> Show blocked ip addresses</a></li>\n";
210 echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?action=htaccess"),ENT_QUOTES)."\"><img src=\"".dirname(serverVar('PHP_SELF'))."/icons/i_edit.gif\" /> Generate .htaccess snippets</a></li>\n";
211 echo "<li><a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?action=spamsubmission"),ENT_QUOTES)."\"><img src=\"".dirname(serverVar('PHP_SELF'))."/icons/i_edit.gif\" /> Spam submission (Bulkfeeds)</a></li>\n";
215 function pbl_blacklisteditor() {
217 global $pblmessage, $manager;
219 if(strlen($pblmessage) > 0) {
220 echo "<div class=\"pblmessage\">$pblmessage</div>\n";
224 echo "<div id=\"jayallen\">\n";
225 echo "<div class=\"pbldescription\">";
226 if(!file_exists(__WEBLOG_ROOT.__EXT."/settings/blacklist.pbl")) {
227 echo "You don't have a blacklist file yet!<br />";
228 echo "Click the button below to get the latest MT-Blacklist from Jay Allen's site.";
230 echo "<div class=\"pbform\">\n";
231 echo "<form action=\"index.php\" method=\"get\">\n";
232 echo "<input type=\"hidden\" name=\"page\" value=\"getblacklist\" />\n";
233 echo "<input type=\"submit\" value=\"Download and install\" />\n";
238 $updatetime = @filemtime(__WEBLOG_ROOT.__EXT."/settings/blacklist.txt");
239 echo "Your MT-Blacklist file was last updated at: ";
240 echo date("Y/m/d H:i:s", $updatetime)." <br />";
241 # echo date("F d Y H:i", $updatetime)." <br />";
242 echo "It's updated automatically every day but you can click below to update it immediately";
244 echo "<div class=\"pbform\" style=\"margin-left:10px;\">\n";
245 echo "<form action=\"".serverVar('PHP_SELF')."\" method=\"get\">\n";
246 echo "<input type=\"hidden\" name=\"page\" value=\"getblacklist\" />\n";
247 echo "<input type=\"submit\" value=\"Update now\" />\n";
253 echo "<div id=\"personal\">\n";
254 echo "<div class=\"pbldescription\">";
255 echo "You can add url's, regular expressions or words to your personal blacklist below.";
257 echo "<div class=\"pbform\">\n";
258 echo "<form action=\"".serverVar('PHP_SELF')."\" method=\"get\">\n";
259 $manager->addTicketHidden();
261 echo "<input type=\"hidden\" name=\"action\" value=\"addpersonal\" />\n";
262 echo "<table class=\"pblform\">\n";
264 echo "<td>expression</td>\n";
265 echo "<td><input class=\"pbltextinput\" type=\"text\" name=\"expression\" /></td>\n";
268 echo " <td>comment</td>\n";
269 echo " <td><input class=\"pbltextinput\" type=\"text\" name=\"comment\" /></td>\n";
272 echo " <td>enable regular expressions ?</td>\n";
273 echo " <td><input class=\"pbltextinput\" type=\"checkbox\" name=\"enable_regex\" value=\"1\" /></td>\n";
275 echo "<tr><td colspan=\"2\" style=\"border:none;\"><input type=\"submit\" value=\"Add\" /></td>\n";
280 echo "<div class=\"pbldescription\">Below is your personal blacklist</div>\n";
281 if (file_exists(__WEBLOG_ROOT.__EXT.'/settings/personal_blacklist.pbl')) {
282 $handle = fopen(__WEBLOG_ROOT.__EXT."/settings/personal_blacklist.pbl", "r");
285 echo "<th>expression</th>\n";
286 echo "<th>comment</th>\n";
287 echo "<th>deletion</th>\n";
290 while (!feof($handle)) {
291 $buffer = fgets($handle, 4096);
293 $configParam = explode("####", $buffer);
294 $key = $configParam[0];
295 $value = $configParam[1];
296 if(strlen($key) > 0) {
298 echo "<td>".htmlspecialchars($key,ENT_QUOTES)."</td>\n";
299 echo "<td>".htmlspecialchars($value,ENT_QUOTES)."</td>\n";
301 echo "<a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?action=deleteexpression&line=".$line),ENT_QUOTES)."\">delete</a>";
309 function pbl_deleteexpression() {
310 if(isset($_GET["line"])) {
311 if( ! is_writable(__WEBLOG_ROOT.__EXT."/settings/personal_blacklist.pbl") ){
312 echo "Error: personal_blacklist.pbl is not writable. ";
314 $handle = fopen(__WEBLOG_ROOT.__EXT."/settings/personal_blacklist.pbl", "r");
317 while (!feof($handle)) {
318 $buffer = fgets($handle, 4096);
320 if($line != getVar("line")) {
325 $handle = fopen(__WEBLOG_ROOT.__EXT."/settings/personal_blacklist.pbl", "w");
326 fwrite($handle, $newFile);
330 function pbl_addexpression($expression, $comment) {
331 if(strlen($expression) > 0) {
332 if( ! is_writable(__WEBLOG_ROOT.__EXT."/settings/personal_blacklist.pbl") ){
333 echo "Error: personal_blacklist.pbl is not writable. ";
335 $handle = fopen(__WEBLOG_ROOT.__EXT."/settings/personal_blacklist.pbl", "a");
336 if(strlen($comment) > 0) {
337 $expression = $expression." #### ".$comment;
339 fwrite($handle, $expression."\n");
346 function _hdl($errno, $errstr) {
351 function pbl_checkregexp($re) {
352 // Thanks to 'OneOfBorg' on Gathering Of Tweakers
353 // http://gathering.tweakers.net/forum/user_profile/109376
356 set_error_handler("_hdl");
357 preg_match("/".trim($re)."/im", "");
358 restore_error_handler();
362 function pbl_addpersonal() {
363 if(isset($_GET["expression"])) {
364 $expression = getVar("expression");
365 if( getVar('comment') ){
366 $comment = getVar('comment');
368 if($expression != "") {
369 $enable_regex = true;
370 if( ! getVar('enable_regex') ){
371 $enable_regex = false;
372 $expression = preg_quote($expression,'/');
375 if($enable_regex && (!pbl_checkregexp($expression))) {
376 echo "<div class=\"pblmessage\">Your expression contained errors and couldn't be added: <b>".htmlspecialchars($expression,ENT_QUOTES)."</b></div>\n";
379 $existTest = pbl_checkforspam($expression);
381 if (strlen($existTest) > 0) {
382 echo "<div class=\"pblmessage\">Expression <b>".htmlspecialchars($expression,ENT_QUOTES)."</b> already matched by the following rule in your system:<br/> <b>$existTest</b></div>\n";
385 pbl_addexpression($expression,$comment);
386 echo "<div class=\"pblmessage\">New entry added to your list: <b>".htmlspecialchars($expression,ENT_QUOTES)."</b></div>";
391 echo "<div class=\"pblmessage\">There's no use in adding empty expressions.<b>".htmlspecialchars($expression,ENT_QUOTES)."</b></div>";
396 function pbl_logspammer($spam) {
398 if( ! is_writable(__WEBLOG_ROOT.__EXT."/settings/blacklist.log") ){
399 echo "Error: blacklist.log is not writable. ";
401 $handle = fopen(__WEBLOG_ROOT.__EXT."/settings/blacklist.log", "a");
402 $lastVisit = cookieVar($CONF['CookiePrefix'] .'lastVisit');
404 //$lastVisit = $this->getCorrectTime($lastVisit);
405 $logline = date("Y/m/d H:i:s")." #### ".serverVar("REMOTE_ADDR")." #### ".$spam. ' [lastVisit ' .date("Y/m/d H:i:s", $lastVisit). "]\n";
407 $logline = date("Y/m/d H:i:s")." #### ".serverVar("REMOTE_ADDR")." #### ".$spam."\n";
409 fwrite($handle, $logline);
413 function pbl_log($text) {
415 if( ! is_writable(__WEBLOG_ROOT.__EXT."/settings/blacklist.log") ){
416 echo "Error: blacklist.log is not writable. ";
418 $handle = fopen(__WEBLOG_ROOT.__EXT."/settings/blacklist.log", "a");
419 $logline = date("Y/m/d H:i:s")." #### localhost #### ".$text."\n";
420 fwrite($handle, $logline);
425 function pbl_logtable() {
427 if (file_exists(__WEBLOG_ROOT.__EXT."/settings/blacklist.log")) {
428 $handle = fopen(__WEBLOG_ROOT.__EXT."/settings/blacklist.log", "r");
431 while (!feof($handle)) {
432 $buffer = fgets($handle, 4096);
433 $thisline = explode("####", $buffer);
434 if($thisline[0] != "") {
436 $logrows .= "<td class=\"log$numb\" >$thisline[0]</td>";
437 if( getPluginOption('SkipNameResolve') == 'no' )
438 $logrows .= "<td class=\"log$numb\" >$thisline[1]<br />(" . gethostbyaddr( trim($thisline[1]) ) . ")</td>";
440 $logrows .= "<td class=\"log$numb\" >$thisline[1]</td>";
441 $logrows .= "<td class=\"log$numb\" >$thisline[2]</td>";
442 $logrows .= "</tr>\n";
450 echo "<table class=\"pbllog\">\n";
451 echo "<tr><th>Date/Time</th><th>IP</th><th>Rule Matched</th></tr>\n";
455 if(strlen($logrows) < 10) {
456 echo "<div class=\"pbldescription\">Your log is empty.</div>\n";
458 echo "<div class=\"pbform\" style=\"margin-left:10px;\">\n";
459 echo "<form action=\"".serverVar('PHP_SELF')."\" method=\"get\">\n";
460 echo "<input type=\"hidden\" name=\"action\" value=\"resetlog\" />\n";
461 echo "<input type=\"submit\" value=\"Reset log\" />\n";
462 $manager->addTicketHidden();
468 function check_for_open_proxy() {
469 $spammer_ip = serverVar('REMOTE_ADDR');
470 list($a, $b, $c, $d) = split('.', $spammer_ip);
471 if( gethostbyname("$d.$c.$b.$a.list.dsbl.org") != "$d.$c.$b.$a.list.dsbl.org") {
477 function check_for_surbl ( $comment_text ) {
478 /* for a full explanation, see http://www.surbl.org
479 summary: blocks comment if it contains an url that's on a known spammers list.
481 //get site names found in body of comment.
482 $regex_url = "/(www\.)([^\/\"<\s]*)/i";
483 $mk_regex_array = array();
484 preg_match_all($regex_url, $comment_text, $mk_regex_array);
486 for( $cnt=0; $cnt < count($mk_regex_array[2]); $cnt++ ) {
487 $domain_to_test = rtrim($mk_regex_array[2][$cnt],"\\");
489 if (strlen($domain_to_test) > 3)
491 $domain_to_test = $domain_to_test . ".multi.surbl.org";
492 if( strstr(gethostbyname($domain_to_test),'127.0.0')) {
500 //add hsur +++++++++++++
502 function check_for_iprbl () {
503 if( pbl_ipcache_read() ) return false;
505 //$iprbl = array('sc.surbl.org', 'bsb.spamlookup.net', 'opm.blitzed.org', 'list.dsbl.org');
506 $iprbl = array('niku.2ch.net', 'list.dsbl.org', 'bsb.spamlookup.net');
508 $spammer_ip = serverVar('REMOTE_ADDR');
509 list($a, $b, $c, $d) = explode('.', $spammer_ip);
511 foreach($iprbl as $rbl ){
512 if( strstr( gethostbyname( "$d.$c.$b.$a.$rbl" ),'127.0.0') ) {
513 return array($rbl, $spammer_ip);
520 function check_for_domainrbl ( $comment_text ) {
521 $domainrbl = array('rbl.bulkfeeds.jp', 'url.rbl.jp', 'bsb.spamlookup.net');
522 //$regex_url = "/((http:\/\/)|(www\.))([^\/\"<\s]*)/i";
523 $regex_url = "{https?://(?:www\.)?([a-z0-9._-]{2,})(?::[0-9]+)?((?:/[_.!~*a-z0-9;@&=+$,%-]+){0,2})}m";
524 $comment_text = mb_strtolower($comment_text);
526 $mk_regex_array = array();
527 preg_match_all($regex_url, $comment_text, $mk_regex_array);
529 $mk_regex_array[1] = array_unique($mk_regex_array[1]);
531 for( $cnt=0; $cnt < count($mk_regex_array[1]); $cnt++ ) {
532 $domain_to_test = rtrim($mk_regex_array[1][$cnt],"\\");
533 foreach($domainrbl as $rbl ){
534 if (strlen($domain_to_test) > 3)
536 if( strstr(gethostbyname($domain_to_test.'.'.$rbl),'127.0.0')) {
537 return array($rbl, $domain_to_test);
545 //add hsur end ++++++++++++++
547 function pbl_blockIP() {
548 $remote_ip = trim(serverVar('REMOTE_ADDR'));
549 $filename = __WEBLOG_ROOT.__EXT."/settings/blockip.pbl";
551 // already in ipblock?
552 if (file_exists($filename)) {
553 $fp = fopen(__WEBLOG_ROOT.__EXT."/settings/blockip.pbl", "r");
554 while ($line = trim(fgets($fp,255))) {
555 if( strpos($remote_ip, $line) !== false){$block = true;}
559 $fp = fopen(__WEBLOG_ROOT.__EXT."/settings/blockip.pbl", "w");
566 function pbl_logRule($expression) {
567 $filename = __WEBLOG_ROOT.__EXT."/settings/matched.pbl";
569 $fp = fopen($filename,"r+");
571 while ($line = fgets($fp, 4096)) {
572 if (! (strpos($line, $expression) === false )) {
579 if ($count == 0 && !trim($expression) == "" ) {
580 $fp = fopen($filename,"a+");
581 fwrite($fp,$expression."\n");
585 // this function logs all ip-adresses in a 'suspected ip-list'
586 // if the ip of the currently catched spammer is above the ip-treshold (plugin option) then
587 // the spamming ipaddress is transfered to the blocked-ip list.
588 // this list is the first line of defense, so notorious spamming machine will be kicked of real fast
589 // improves blacklist performance
590 // possible danger: blacklisting real humans who post on-the-edge comments
591 function pbl_suspectIP($threshold, $remote_ip = '') {
592 if ($remote_ip == '' ) {$remote_ip = serverVar('REMOTE_ADDR');}
593 $filename = __WEBLOG_ROOT.__EXT."/settings/suspects.pbl";
594 $blockfile = __WEBLOG_ROOT.__EXT."/settings/blockip.pbl";
597 if (! file_exists($filename)) {
598 $fp = fopen($filename, "w");
603 $fp = fopen($filename, "r");
604 while ($line = fgets($fp,255)) {
605 if ( strpos($line, $remote_ip) !== false ) {
611 // not above threshold ? add ip to suspect ...
612 if ($count < $threshold) {
613 $fp = fopen($filename,'a+');
614 fwrite($fp,$remote_ip."\n");
617 // remove from suspect to ip-block
618 $fp = fopen($filename, "r");
620 while ($line = fgets($fp,255)) {
621 // keep all lines except the catched ip-address
622 if(strpos ($line, $remote_ip) !== false) {
627 $fp = fopen($filename, "w");
628 fwrite($fp, $rewrite);
630 // transfer to blocked-ip file
631 $fp = fopen($blockfile,'a+');
632 fwrite($fp,$remote_ip."\n");
637 function pbl_showipblock() {
638 global $pblmessage, $manager;
639 $filename = __WEBLOG_ROOT.__EXT."/settings/blockip.pbl";
641 $fp = fopen($filename,'r');
642 echo "<div class=\"pbform\">\n";
643 echo "<form action=\"".serverVar('PHP_SELF')."\" method=\"get\">\n";
644 echo "<input type=\"hidden\" name=\"action\" value=\"addip\" />\n";
645 $manager->addTicketHidden();
647 echo "Add IP to block: ";
648 echo "<input class=\"pbltextinput\" type=\"text\" name=\"ipaddress\" />\n";
649 echo "<input type=\"submit\" value=\"Add\" />\n";
654 echo "<th>IP Address</th>\n";
655 echo "<th>reversed lookup</th>\n";
656 echo "<th>deletion</th>\n";
658 while ($ip = fgets($fp,255)) {
660 if( getPluginOption('SkipNameResolve') == 'no' )
661 echo "<tr><td>".$ip."</td><td>[".gethostbyaddr(rtrim($ip))."]</td><td>";
663 echo "<tr><td>".$ip."</td><td>[<em>skipped</em>]</td><td>";
665 echo "<a href=\"".htmlspecialchars($manager->addTicketToUrl(serverVar('PHP_SELF')."?action=deleteipblock&line=".$line),ENT_QUOTES)."\">delete</a>";
670 function pbl_addipblock() {
671 if(isset($_GET["ipaddress"])) {
672 pbl_suspectIP(0,getVar("ipaddress"));
676 function pbl_deleteipblock() {
678 $filename = __WEBLOG_ROOT.__EXT."/settings/blockip.pbl";
679 if(isset($_GET["line"])) {
680 $handle = fopen($filename, "r");
683 while (!feof($handle)) {
684 $buffer = fgets($handle, 4096);
686 if($line != getVar("line")) {
691 $handle = fopen($filename, "w");
692 fwrite($handle, $newFile);
697 function pbl_htaccess($type) {
701 $filename = __WEBLOG_ROOT.__EXT."/settings/blockip.pbl";
702 $htaccess = "# This htaccess snippet blocks machine based on IP Address. \n"
703 . "# these lines are generated by NP_Blackist\n";
706 $filename = __WEBLOG_ROOT.__EXT."/settings/matched.pbl";
707 $htaccess = "# This htaccess snippet blocks machine based on referrers. \n"
708 . "# these lines are generated by NP_Blackist\n"
709 . "# You need to have the following line once in your .htaccess file\n"
710 . "# RewriteEngine On\n";
713 $htaccess = "Here you can generate two types of .htaccess snippets. The first part is based on blocked ip's. This is only relevant if you have IP blocking enabled in the options. \nThe other part is referrer based rewrite rules. Blacklist stores all rules matched in a different file. With this tool you convert these matched rules into .htaccess rewrite rules which you can incorporate into your existings .htaccess file (Apache only)\n After you've added the snippet to your .htaccess file it's safe and wise to reset the blocked ip list and/or matched rules file. That way you won't end up with double rules inside your .htaccess file\n";
717 $fp = fopen($filename, 'r');
719 while ($line = fgets($fp,4096)) {
721 $htaccess .= "deny from ".$line;
723 if (rtrim($line) != "" ) {
724 if ($count > 0) {$htaccess .= "[NC,OR]\n";}
725 // preg_replace does the magic of converting . into \. while keeping \. and _. intact
726 $htaccess .= "RewriteCond %{HTTP_REFERER} ". preg_replace("/([^\\\|^_]|^)\./",'$1\\.',rtrim($line)).".*$ ";
732 $htaccess .= "\nRewriteRule .* ?¿½ [F,L]\n";
737 function pbl_htaccesspage() {
738 global $pblmessage, $manager;
739 if(strlen($pblmessage) > 0) {
740 echo "<div class=\"pblmessage\">$pblmessage</div>\n";
743 if (isset($_POST["type"])) {
744 if (strstr(postVar("type"),"blocked")) {
750 echo "<div class=\"pbform\" style=\"margin-left:10px;\">\n";
751 echo "<form action=\"".serverVar('PHP_SELF')."\" method=\"post\">\n";
752 $manager->addTicketHidden();
754 echo "<input type=\"submit\" label=\"ip\" value=\"Generate blocked IP's\" name=\"type\" />\n";
755 echo "<input type=\"submit\" label=\"ip\" value=\"Generate rewrite rules\" name=\"type\" />\n";
758 echo "<input type=\"hidden\" name=\"action\" value=\"htaccess\" />\n";
759 echo "<textarea class=\"pbltextinput\" cols=\"60\" rows=\"15\" name=\"snippet\" >". pbl_htaccess($type)."</textarea><br />";
761 echo "<input title=\"this will clean your block IP addresses file\" type=\"submit\" label=\"ip\" value=\"Reset blocked IP's\" name=\"type\" />\n";
762 echo "<input title=\"This will clean your matched file\" type=\"submit\" label=\"ip\" value=\"Reset rewrite rules\" name=\"type\" />\n";
764 // if user asked for a reset, do it now
765 if (stristr(postVar("type"),"reset")) {
766 echo "restting file ...";
767 pbl_resetfile($type);
770 } // pbl_htaccesspage()
772 function pbl_resetfile($type){
776 $filename = __WEBLOG_ROOT.__EXT."/settings/blacklist.log";
779 $filename = __WEBLOG_ROOT.__EXT."/settings/blockip.pbl";
782 $filename = __WEBLOG_ROOT.__EXT."/settings/matched.pbl";
785 if(file_exists($filename)) {
786 $fp = fopen($filename, "w");
792 function pbl_test () {
793 // test's user input, no loggin.
795 if(isset($_GET["expression"])) {
796 if(getVar("expression") != "") {
797 $pblmessage = "Your expression: <br />".htmlspecialchars(getVar("expression"), ENT_QUOTES);
798 $return = pbl_checkforspam(getVar("expression"),false,0,false);
800 if (! $return == "" ) {
801 $pblmessage .= "<br />matched rule: <strong>".$return."</strong>";
803 $pblmessage .= "<br /> did not match any rule.";
809 function pbl_testpage () {
812 // shows user testpage ...
814 if(strlen($pblmessage) > 0) {
815 echo "<div class=\"pblmessage\">$pblmessage</div>\n";
817 echo "<div class=\"pbform\" style=\"margin-left:10px;\">\n";
818 echo "<form action=\"".serverVar('PHP_SELF')."\" method=\"get\">\n";
819 echo "<input type=\"hidden\" name=\"action\" value=\"test\" />\n";
820 $manager->addTicketHidden();
822 echo "<textarea class=\"pbltextinput\" cols=\"60\" rows=\"6\" name=\"expression\" ></textarea><br />";
823 echo "<input type=\"submit\" value=\"Test this\" />\n";
828 function pbl_spamsubmission_form() {
832 echo "<form action=\"".serverVar('PHP_SELF')."\" method=\"post\">\n";
833 echo "<input type=\"hidden\" name=\"action\" value=\"spamsubmission\" />\n";
834 echo "<input type=\"hidden\" name=\"type\" value=\"send\" />\n";
835 $manager->addTicketHidden();
840 echo "<th>Report Spam</th>\n";
844 echo "<td><textarea name=\"url\" rows=\"6\" cols=\"60\"></textarea></td>\n";
847 echo '<tr><td><div align="right"><input type="submit" name="submit" value="submit" /></div></td></tr>';