OSDN Git Service

Added Usage for glob_regex()
[pukiwiki/pukiwiki_sandbox.git] / spam / spam.php
index 81a7943..1766489 100644 (file)
@@ -1,8 +1,10 @@
 <?php
-// $Id: spam.php,v 1.106 2007/01/11 15:51:12 henoheno Exp $
+// $Id: spam.php,v 1.122 2007/03/04 03:59:37 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
+//
 // (PHP 4 >= 4.3.0): preg_match_all(PREG_OFFSET_CAPTURE): $method['uri_XXX'] related feature
 
 if (! defined('SPAM_INI_FILE')) define('SPAM_INI_FILE', 'spam.ini.php');
@@ -51,7 +53,7 @@ function uri_pickup($string = '', $normalize = TRUE,
        preg_match_all(
                // scheme://userinfo@host:port/path/or/pathinfo/maybefile.and?query=string#fragment
                // Refer RFC3986 (Regex below is not strict)
-               '#(\b[a-z][a-z0-9.+-]{1,8})://' .       // 1: Scheme
+               '#(\b[a-z][a-z0-9.+-]{1,8}):/+' .       // 1: Scheme
                '(?:' .
                        '([^\s<>"\'\[\]/\#?@]*)' .              // 2: Userinfo (Username)
                '@)?' .
@@ -337,6 +339,7 @@ function _preg_replace_callback_domain_exposure($matches = array())
 // NOTE: It's maybe danger to var_dump(result). [e.g. 'javascript:']
 // [OK] http://victim.example.org/go?http%3A%2F%2Fnasty.example.org
 // [OK] http://victim.example.org/http://nasty.example.org
+// TODO: link.toolbot.com, urlx.org
 function spam_uri_pickup_preprocess($string = '')
 {
        if (! is_string($string)) return '';
@@ -346,9 +349,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+bar hoge
+                       '#(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',
@@ -614,6 +625,10 @@ function query_normalize($string = '', $equal = FALSE, $equal_cutempty = TRUE)
 // ---------------------
 // Part One : Checker
 
+// Rough implementation of globbing
+//
+// USAGE: $regex = '/^' . generate_glob_regex('*.txt', '/') . '$/i';
+//
 function generate_glob_regex($string = '', $divider = '/')
 {
        static $from = array(
@@ -655,43 +670,35 @@ 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';
-                       }
-               }
+       static $regexs;
 
-               // Load
+       if (! isset($regexs)) {
+               $regexs = array();
                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';
+                       include(SPAM_INI_FILE);
+                       //      $blocklist['badhost'] = array(
+                       //              '*.blogspot.com',       // Blog services's subdomains (only)
+                       //              'IANA-examples' => '#^(?:.*\.)?example\.(?:com|net|org)$#',
+                       //      );
+                       foreach(array('goodhost', 'badhost') as $_list) {
+                               if (! isset($blocklist[$list])) continue;
+                               foreach ($blocklist[$_list] as $key => $value) {
+                                       if (is_string($key)) {
+                                               $regexs[$_list][$key] = $value;
+                                       } else {
+                                               $regexs[$_list][$value] =
+                                                       '/^(?:www\.)?' . generate_glob_regex($value, '/') . '$/i';
+                                       }
                                }
                        }
                }
        }
 
        if ($list == '') {
-               return $regex;
-       } else if (isset($regex[$list])) {
-               return $regex[$list];
+               return $regexs; // ALL
+       } else if (isset($regexs[$list])) {
+               return $regexs[$list];
        } else {        
                return array();
        }
@@ -706,18 +713,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;
                }
        }
@@ -791,7 +798,7 @@ function check_uri_spam($target = '', $method = array())
        $remains = & $progress['remains'];
        $asap    = isset($method['asap']);
 
-       // Return if ...
+       // Recurse
        if (is_array($target)) {
                foreach($target as $str) {
                        // Recurse
@@ -818,7 +825,11 @@ function check_uri_spam($target = '', $method = array())
                        }
                        foreach ($_remains as $key=>$value) {
                                foreach ($value as $_key=>$_value) {
-                                       $remains[$key][$_key] = $_value;
+                                       if (is_int($_key)) {
+                                               $remains[$key][]      = $_value;
+                                       } else {
+                                               $remains[$key][$_key] = $_value;
+                                       }
                                }
                        }
                        if ($asap && $is_spam) break;
@@ -853,14 +864,14 @@ function check_uri_spam($target = '', $method = array())
        }
 
        // Return if ...
-       if ($asap && $is_spam) {
-               return $progress;
-       }
-       // URI Init
+       if ($asap && $is_spam) return $progress;
+
+       // URI: Pickup
        $pickups = spam_uri_pickup($target, $method);
-       if (empty($pickups)) {
-               return $progress;
-       }
+       //$remains['uri_pickup'] = & $pickups;
+
+       // Return if ...
+       if (empty($pickups)) return $progress;
 
        // URI: Check quantity
        $sum['quantity'] += count($pickups);
@@ -928,14 +939,13 @@ function check_uri_spam($target = '', $method = array())
        }
 
        // Return if ...
-       if ($asap && $is_spam) {
-               return $progress;
-       }
+       if ($asap && $is_spam) return $progress;
 
        // Host: Uniqueness (uniq / non-uniq)
        $hosts = array();
        foreach ($pickups as $pickup) $hosts[] = & $pickup['host'];
        $hosts = array_unique($hosts);
+       //$remains['uniqhost'] = & $hosts;
        $sum['uniqhost'] += count($hosts);
        if ((! $asap || ! $is_spam) && isset($method['non_uniqhost'])) {
                $sum['non_uniqhost'] = $sum['quantity'] - $sum['uniqhost'];
@@ -945,9 +955,7 @@ function check_uri_spam($target = '', $method = array())
        }
 
        // Return if ...
-       if ($asap && $is_spam) {
-               return $progress;
-       }
+       if ($asap && $is_spam) return $progress;
 
        // URI: Bad host
        if ((! $asap || ! $is_spam) && isset($method['badhost'])) {
@@ -957,9 +965,9 @@ function check_uri_spam($target = '', $method = array())
                } else {
                        $badhost = is_badhost($hosts, $asap, $__remains);
                        if ($__remains) {
-                               $progress['remains']['badhost'] = array();
+                               $remains['badhost'] = array();
                                foreach ($__remains as $value) {
-                                       $progress['remains']['badhost'][$value] = TRUE;
+                                       $remains['badhost'][$value] = TRUE;
                                }
                        }
                }
@@ -1092,7 +1100,7 @@ function pkwk_spamnotify($action, $page, $target = array('title' => ''), $progre
        $summary['URI']     = get_script_uri() . '?' . rawurlencode($page);
        $summary['USER_AGENT']  = TRUE;
        $summary['REMOTE_ADDR'] = TRUE;
-       pkwk_mail_notify($notify_subject,  var_export($target, TRUE), $summary);
+       pkwk_mail_notify($notify_subject,  var_export($target, TRUE), $summary, TRUE);
 }
 
 ?>