OSDN Git Service

check_uri_spam(): Simplify: 'area_anchor' + 'area_bbcode' + somthing else = area_pickup()
[pukiwiki/pukiwiki_sandbox.git] / spam / spam.php
index 2f4a809..7216a5f 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// $Id: spam.php,v 1.204 2008/12/27 11:25:30 henoheno Exp $
+// $Id: spam.php,v 1.210 2008/12/28 15:37:07 henoheno Exp $
 // Copyright (C) 2006-2007 PukiWiki Developers Team
 // License: GPL v2 or (at your option) any later version
 //
@@ -355,8 +355,10 @@ function generate_host_regex($string = '', $divider = '/')
 {
        if (! is_string($string)) return '';
 
-       if (mb_strpos($string, '.') === FALSE)
+       if (mb_strpos($string, '.') === FALSE) {
+               // localhost
                return generate_glob_regex($string, $divider);
+       }
 
        if (is_ip($string)) {
                // IPv4
@@ -365,10 +367,13 @@ function generate_host_regex($string = '', $divider = '/')
                // FQDN or something
                $part = explode('.', $string, 2);
                if ($part[0] == '') {
-                       $part[0] = '(?:.*\.)?'; // And all related FQDN
+                       // .example.org
+                       $part[0] = '(?:.*\.)?';
                } else if ($part[0] == '*') {
-                       $part[0] = '.*\.';      // All subdomains/hosts only
+                       // *.example.org
+                       $part[0] = '.*\.';
                } else {
+                       // example.org, etc
                        return generate_glob_regex($string, $divider);
                }
                $part[1] = generate_glob_regex($part[1], $divider);
@@ -377,20 +382,26 @@ function generate_host_regex($string = '', $divider = '/')
 }
 
 // Rough hostname checker
-// [OK] 192.168.
-// TODO: Strict digit, 0x, CIDR, IPv6
+// TODO: Strict digit, 0x, CIDR, '999.999.999.999', ':', '::G'
 function is_ip($string = '')
 {
+       if (! is_string($string)) return FALSE;
+
+       if (strpos($string, ':') !== FALSE) {
+               return 6;       // Seems IPv6
+       }
+
        if (preg_match('/^' .
                '(?:[0-9]{1,3}\.){3}[0-9]{1,3}' . '|' .
-               '(?:[0-9]{1,3}\.){1,3}' . '$/',
+               '(?:[0-9]{1,3}\.){1,3}'         . '$/',
                $string)) {
                return 4;       // Seems IPv4(dot-decimal)
-       } else {
-               return 0;       // Seems not IP
        }
+
+       return FALSE;   // Seems not IP
 }
 
+// Load SPAM_INI_FILE and return parsed one
 function get_blocklist($list = '')
 {
        static $regexes;
@@ -404,6 +415,7 @@ function get_blocklist($list = '')
                $regexes = array();
                if (file_exists(SPAM_INI_FILE)) {
                        $blocklist = array();
+
                        include(SPAM_INI_FILE);
                        //      $blocklist['list'] = array(
                        //      //'goodhost' => FALSE;
@@ -413,11 +425,19 @@ function get_blocklist($list = '')
                        //              '*.blogspot.com',       // Blog services's subdomains (only)
                        //              'IANA-examples' => '#^(?:.*\.)?example\.(?:com|net|org)$#',
                        //      );
-                       foreach(array('pre', 'list') as $special) {
+
+                       foreach(array(
+                                       'pre',
+                                       'list',
+                               ) as $special) {
+
                                if (! isset($blocklist[$special])) continue;
+
                                $regexes[$special] = $blocklist[$special];
+
                                foreach(array_keys($blocklist[$special]) as $_list) {
                                        if (! isset($blocklist[$_list])) continue;
+
                                        foreach ($blocklist[$_list] as $key => $value) {
                                                if (is_array($value)) {
                                                        $regexes[$_list][$key] = array();
@@ -428,6 +448,7 @@ function get_blocklist($list = '')
                                                        get_blocklist_add($regexes[$_list], $key, $value);
                                                }
                                        }
+
                                        unset($blocklist[$_list]);
                                }
                        }
@@ -435,21 +456,21 @@ function get_blocklist($list = '')
        }
 
        if ($list === '') {
-               return $regexes;        // ALL
+               return $regexes;                // ALL of
        } else if (isset($regexes[$list])) {
-               return $regexes[$list];
+               return $regexes[$list]; // A part of
        } else {
-               return array();
+               return array();                 // Found nothing
        }
 }
 
-// Subroutine of get_blocklist()
-function get_blocklist_add(& $array, $key = 0, $value = '*.example.org')
+// Subroutine of get_blocklist(): Add new regex to the $array
+function get_blocklist_add(& $array, $key = 0, $value = '*.example.org/path/to/file.html')
 {
        if (is_string($key)) {
-               $array[$key] = & $value; // Treat $value as a regex
+               $array[$key]   = & $value; // Treat $value as a regex for FQDN(host)s
        } else {
-               $array[$value] = '/^' . generate_host_regex($value, '/') . '$/i';
+               $array[$value] = '#^' . generate_host_regex($value, '#') . '$#i';
        }
 }
 
@@ -630,29 +651,32 @@ function check_uri_spam($target = '', $method = array())
        // ----------------------------------------
        // Area measure
 
-       // Area: There's HTML anchor tag
-       if ((! $asap || ! $is_spam) && isset($method['area_anchor'])) {
-               $key = 'area_anchor';
-               $_asap = isset($method['asap']) ? array('asap' => TRUE) : array();
-               $result = area_pickup($target, array($key => TRUE) + $_asap);
-               if ($result) {
-                       $sum[$key] = $result[$key];
-                       if (isset($method[$key]) && $sum[$key] > $method[$key]) {
-                               $is_spam[$key] = TRUE;
-                       }
+       if (! $asap || ! $is_spam) {
+       
+               // Method pickup
+               $_method = array();
+               foreach(array(
+                               'area_anchor',  // There's HTML anchor tag
+                               'area_bbcode',  // There's 'BBCode' linking tag
+                       ) as $key) {
+                       if (isset($method[$key])) $_method[$key] = TRUE;
                }
-       }
 
-       // Area: There's 'BBCode' linking tag
-       if ((! $asap || ! $is_spam) && isset($method['area_bbcode'])) {
-               $key = 'area_bbcode';
-               $_asap = isset($method['asap']) ? array('asap' => TRUE) : array();
-               $result = area_pickup($target, array($key => TRUE) + $_asap);
-               if ($result) {
-                       $sum[$key] = $result[$key];
-                       if (isset($method[$key]) && $sum[$key] > $method[$key]) {
-                               $is_spam[$key] = TRUE;
+               if ($_method) {
+                       $_asap = isset($method['asap']) ? array('asap' => TRUE) : array();
+                       $_result = area_pickup($target, $_method + $_asap);
+               } else {
+                       $_result = FALSE;
+               }
+
+               if ($_result) {
+                       foreach(array_keys($_method) as $key) {
+                               $sum[$key] = $_result[$key];
+                               if (isset($method[$key]) && $sum[$key] > $method[$key]) {
+                                       $is_spam[$key] = TRUE;
+                               }
                        }
+                       $_result = NULL;
                }
        }
 
@@ -897,9 +921,9 @@ function summarize_detail_newtral($progress = array())
                        $subs = array();
                        foreach(array_keys($trie[$key]) as $sub) {
                                if ($sub == '') {
-                                       $subs[] = $key;
+                                       $subs[] = $key;                 // 'example.com'
                                } else {
-                                       $subs[] = $sub . '.' . $key;
+                                       $subs[] = $sub . '. ';  // 'A.foo.bar. '
                                }
                        }
                        $result[] = '  \'' . $key . '\' => \'' . implode(', ', $subs) . '\',';