OSDN Git Service

Simplify. Disable multiple items for glob, using array()
[pukiwiki/pukiwiki_sandbox.git] / spam.php
index 7391b4b..d05463a 100644 (file)
--- a/spam.php
+++ b/spam.php
@@ -1,5 +1,5 @@
 <?php
-// $Id: spam.php,v 1.103 2007/01/06 02:42:29 henoheno Exp $
+// $Id: spam.php,v 1.114 2007/02/01 14:46:55 henoheno Exp $
 // Copyright (C) 2006-2007 PukiWiki Developers Team
 // License: GPL v2 or (at your option) any later version
 // Functions for Concept-work of spam-uri metrics
@@ -13,7 +13,7 @@ if (! defined('SPAM_INI_FILE')) define('SPAM_INI_FILE', 'spam.ini.php');
 // (PHP 4 >= 4.2.0): var_export(): mail-reporting and dump related
 if (! function_exists('var_export')) {
        function var_export() {
-               return 'var_export() is not found' . "\n";
+               return 'var_export() is not found on this server' . "\n";
        }
 }
 
@@ -346,9 +346,17 @@ function spam_uri_pickup_preprocess($string = '')
        // Domain exposure (See _preg_replace_callback_domain_exposure())
        $string = preg_replace_callback(
                array(
-                       // Something Google: http://www.google.com/supported_domains
-                       '#(http)://([a-z0-9.]+\.google\.[a-z]{2,3}(?:\.[a-z]{2})?)/' .
-                       '([a-z0-9?=&.%_+-]+)' .         // ?query=foo+
+                       '#(http)://' .
+                       '(' .
+                               // Something Google: http://www.google.com/supported_domains
+                               '(?:[a-z0-9.]+\.)?google\.[a-z]{2,3}(?:\.[a-z]{2})?' .
+                               '|' .
+                               // AltaVista
+                               '(?:[a-z0-9.]+\.)?altavista.com' .
+                               
+                       ')' .
+                       '/' .
+                       '([a-z0-9?=&.%_/\'\\\+-]+)' .                           // path/?query=foo+bar+
                        '\bsite:([a-z0-9.%_-]+\.[a-z0-9.%_-]+)' .       // site:nasty.example.com
                        //'()' .        // Preserve or remove?
                        '#i',
@@ -356,6 +364,8 @@ function spam_uri_pickup_preprocess($string = '')
                '_preg_replace_callback_domain_exposure',
                $string
        );
+       
+
 
        // URI exposure (uriuri => uri uri)
        $string = preg_replace(
@@ -655,43 +665,39 @@ function generate_glob_regex($string = '', $divider = '/')
 
 function get_blocklist($list = '')
 {
-       static $regex;
-
-       if (! isset($regex)) {
-               $regex = array();
-
-               // Sample
-               if (FALSE) {
-                       $blocklist['badhost'] = array(
-                               //'*',                  // Deny all uri
-                               //'10.20.*.*',  // 10.20.example.com also matches
-                               //'*.blogspot.com',     // Blog services subdomains
-                               //array('blogspot.com', '*.blogspot.com')
-                       );
-                       foreach ($blocklist['badhost'] as $part) {
-                               $_part = is_array($part) ? implode('/', $part) : $part;
-                               $regex['badhost'][$_part] = '/^' . generate_glob_regex($part) . '$/i';
-                       }
-               }
-
-               // Load
+       static $regexs;
+
+       if (! isset($regexs)) {
+               $regexs = array();
+               //      $blocklist['badhost'] = array(
+               //              '*',                    // Deny all uri
+               //              '10.20.*.*',    // 10.20.example.com also matches
+               //              '*.blogspot.com',       // Blog services's subdomains (only)
+               //              'IANA-examples' => '#^(?:.*\.)?example\.(?:com|net|org)$#',
+               //              'net-10.20'     => '#^10\.20\.[0-9]+\.[0-9]+$#',        // 10.20.12345.12 also matches
+               //      );
                if (file_exists(SPAM_INI_FILE)) {
                        $blocklist = array();
                        require(SPAM_INI_FILE);
-                       foreach(array('goodhost', 'badhost') as $key) {
-                               if (! isset($blocklist[$key])) continue;
-                               foreach ($blocklist[$key] as $part) {
-                                       $_part = is_array($part) ? implode('/', $part) : $part;
-                                       $regex[$key][$_part] = '/^' . generate_glob_regex($part) . '$/i';
+                       foreach(array('goodhost', 'badhost') as $_list) {
+                               if (! isset($blocklist[$list])) continue;
+                               foreach ($blocklist[$_list] as $key=>$value) {
+                                       if (is_string($key)) {
+                                               // Pre-defined regex
+                                               $regexs[$_list][$key] = $value;
+                                       } else {
+                                               // Glob to regex
+                                               $regexs[$_list][$value] = '/^' . generate_glob_regex($value) . '$/i';
+                                       }
                                }
                        }
                }
        }
 
        if ($list == '') {
-               return $regex;
-       } else if (isset($regex[$list])) {
-               return $regex[$list];
+               return $regexs;
+       } else if (isset($regexs[$list])) {
+               return $regexs[$list];
        } else {        
                return array();
        }
@@ -706,18 +712,18 @@ function is_badhost($hosts = array(), $asap = TRUE, & $remains)
        }
        if (empty($hosts)) return $result;
 
-       foreach (get_blocklist('goodhost') as $_regex) {
-               $hosts = preg_grep_invert($_regex, $hosts);
+       foreach (get_blocklist('goodhost') as $regex) {
+               $hosts = preg_grep_invert($regex, $hosts);
        }
        if (empty($hosts)) return $result;
 
        $tmp = array();
-       foreach (get_blocklist('badhost') as $part => $_regex) {
-               $result[$part] = preg_grep($_regex, $hosts);
-               if (empty($result[$part])) {
-                       unset($result[$part]);
+       foreach (get_blocklist('badhost') as $label => $regex) {
+               $result[$label] = preg_grep($regex, $hosts);
+               if (empty($result[$label])) {
+                       unset($result[$label]);
                } else {
-                       $hosts = array_diff($hosts, $result[$part]);
+                       $hosts = array_diff($hosts, $result[$label]);
                        if ($asap) break;
                }
        }
@@ -951,19 +957,19 @@ function check_uri_spam($target = '', $method = array())
 
        // URI: Bad host
        if ((! $asap || ! $is_spam) && isset($method['badhost'])) {
+               $__remains = array();
                if ($asap) {
-                       $badhost = is_badhost($hosts, $asap);
+                       $badhost = is_badhost($hosts, $asap, $__remains);
                } else {
-                       $__remains = array();
                        $badhost = is_badhost($hosts, $asap, $__remains);
                        if ($__remains) {
                                $progress['remains']['badhost'] = array();
                                foreach ($__remains as $value) {
                                        $progress['remains']['badhost'][$value] = TRUE;
                                }
-                               unset($__remains);
                        }
                }
+               unset($__remains);
                if (! empty($badhost)) {
                        $sum['badhost'] += array_count_leaves($badhost);
                        foreach(array_keys($badhost) as $keys) {