OSDN Git Service

Stop $asap, or do them all?
[pukiwiki/pukiwiki_sandbox.git] / spam.php
index 7994926..b3e7128 100644 (file)
--- a/spam.php
+++ b/spam.php
 <?php
-// $Id: spam.php,v 1.12 2006/11/03 16:15:00 henoheno Exp $
+// $Id: spam.php,v 1.36 2006/11/25 13:55:34 henoheno Exp $
 // Copyright (C) 2006 PukiWiki Developers Team
 // License: GPL v2 or (at your option) any later version
 
 // Functions for Concept-work of spam-uri metrics
 
-// Return an array of normalized/parsed URIs in the $string
+// Return an array of URIs in the $string
 // [OK] http://nasty.example.org#nasty_string
-// [OK] http://nasty.example.org/foo/xxx#nasty_string/bar
-// [OK] ftp://dfshodfs:80/dfsdfs
-// [OK] http://victim.example.org/go?http%3A%2F%2Fnasty.example.org
-// [OK] http://victim.example.org/gphttp://nasty.example.org
-function spam_pickup($string = '')
+// [OK] http://nasty.example.org:80/foo/xxx#nasty_string/bar
+// [OK] ftp://nasty.example.org:80/dfsdfs
+// [OK] ftp://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htm (from RFC3986)
+function uri_pickup($string = '', $normalize = TRUE,
+       $preserve_rawuri = FALSE, $preserve_chunk = TRUE)
 {
-       // Preprocess: urldecode() and adding space(s)
-       $string = preg_replace(
-               array(
-                       '#(?:https?|ftp):/#',
-                       '#\b[a-z][a-z0-9.+-]{1,8}://#i',
-                       '#[a-z][a-z0-9.+-]{1,8}://#i'
-               ), ' $0', urldecode($string));
-
-       // URI pickup: Not available for user@password, IDN, Fragment(=ignored)
+       // Not available for: IDN(ignored)
        $array = array();
        preg_match_all(
-               // Refer RFC3986
+               // 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
+               '(?:' .
+                       '([^\s<>"\'\[\]/\#?@]*)' .              // 2: Userinfo (Username)
+               '@)?' .
                '(' .
-                       // 2: Host
+                       // 3: Host
                        '\[[0-9a-f:.]+\]' . '|' .                               // IPv6([colon-hex and dot]): RFC2732
                        '(?:[0-9]{1-3}\.){3}[0-9]{1-3}' . '|' . // IPv4(dot-decimal): 001.22.3.44
                        '[^\s<>"\'\[\]:/\#?]+' .                                // FQDN: foo.example.org
                ')' .
-               '(?::([a-z0-9]{2,}))?' .                        // 3: Port
-               '((?:/+[^\s<>"\'\[\]/\#]+)*/+)?' .      // 4: Directory path or path-info
-               '([^\s<>"\'\[\]\#]+)?' .                        // 5: File and query string
-                                                                                       // #: Fragment(ignored)
+               '(?::([0-9]*))?' .                                      // 4: Port
+               '((?:/+[^\s<>"\'\[\]/\#]+)*/+)?' .      // 5: Directory path or path-info
+               '([^\s<>"\'\[\]\#]+)?' .                        // 6: File and query string
+               '(?:\#([a-z0-9._~%!$&\'()*+,;=:@-]*))?' .       // 7: Fragment
                '#i',
-                $string, $array, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
+                $string, $array, PREG_SET_ORDER | PREG_OFFSET_CAPTURE
+       );
        //var_dump(recursive_map('htmlspecialchars', $array));
+
        // Shrink $array
-       $parts = array(1 => 'scheme', 2 => 'host', 3 => 'port',
-               4 => 'path', 5 => 'file');
+       static $parts = array(
+               1 => 'scheme', 2 => 'userinfo', 3 => 'host', 4 => 'port',
+               5 => 'path', 6 => 'file', 7 => 'fragment'
+       );
        $default = array('');
        foreach(array_keys($array) as $uri) {
-               unset($array[$uri][0]); // Matched string itself
                array_rename_keys($array[$uri], $parts, TRUE, $default);
                $offset = $array[$uri]['scheme'][1]; // Scheme's offset
 
-               // Remove offsets (with normalization)
                foreach(array_keys($array[$uri]) as $part) {
-                       $array[$uri][$part] =
-                                       strtolower($array[$uri][$part][0]);
+                       // Remove offsets for each part
+                       $array[$uri][$part] = & $array[$uri][$part][0];
+               }
+
+               if ($normalize) {
+                       $array[$uri]['scheme'] = scheme_normalize($array[$uri]['scheme']);
+                       //if ($array[$uri]['scheme'] === '') {
+                       //      // Ignore
+                       //      unset ($array[$uri]);
+                       //      continue;
+                       //}
+                       
+                       $array[$uri]['host']   = strtolower($array[$uri]['host']);
+                       $array[$uri]['port']   = port_normalize($array[$uri]['port'], $array[$uri]['scheme'], FALSE);
+                       $array[$uri]['path']   = path_normalize($array[$uri]['path']);
+                       //$array[$uri]['uri']    = uri_array_implode($array[$uri]);
+                       if ($preserve_rawuri) $array[$uri]['rawuri'] = & $array[$uri][0];
+               } else {
+                       $array[$uri]['uri'] = & $array[$uri][0]; // Raw
+               }
+               unset($array[$uri][0]); // Matched string itself
+               if (! $preserve_chunk) {
+                       unset(
+                               $array[$uri]['scheme'],
+                               $array[$uri]['userinfo'],
+                               $array[$uri]['host'],
+                               $array[$uri]['port'],
+                               $array[$uri]['path'],
+                               $array[$uri]['file'],
+                               $array[$uri]['fragment']
+                       );
                }
-               $array[$uri]['path']   = path_normalize($array[$uri]['path']);
+
                $array[$uri]['offset'] = $offset;
                $array[$uri]['area']   = 0;
        }
 
+       return $array;
+}
+
+// Domain exposure callback (See spam_uri_pickup_preprocess())
+// http://victim.example.org/?foo+site:nasty.example.com+bar
+// => http://nasty.example.com/?refer=victim.example.org
+// NOTE: 'refer=' is not so good for (at this time).
+// Consider about using IP address of the victim, try to avoid that.
+function _preg_replace_callback_domain_exposure($matches = array())
+{
+       $result = '';
+
+       // Preserve the victim URI as a complicity or ...
+       if (isset($matches[5])) {
+               $result =
+                       $matches[1] . '://' .   // scheme
+                       $matches[2] . '/' .             // victim.example.org
+                       $matches[3];                    // The rest of all (before victim)
+       }
+
+       // Flipped URI
+       $result = 
+               $matches[1] . '://' .   // scheme
+               $matches[4] .                   // nasty.example.com
+               '/?refer=' . strtolower($matches[2]) .  // victim.example.org
+               ' ' . $result;
+
+       return $result;
+}
+
+// Preprocess: rawurldecode() and adding space(s) to detect/count some URIs _if possible_
+// 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
+function spam_uri_pickup_preprocess($string = '')
+{
+       if (! is_string($string)) return '';
+
+       $string = rawurldecode($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+
+                       '\bsite:([a-z0-9.%_-]+)' .      // site:nasty.example.com
+                       '()' .  // Preserve?
+                       '#i',
+               ),
+               '_preg_replace_callback_domain_exposure',
+               $string
+       );
+
+       // URI exposure (uriuri => uri uri)
+       $string = preg_replace(
+               array(
+                       '#(?<! )(?:https?|ftp):/#',
+               //      '#[a-z][a-z0-9.+-]{1,8}://#i',
+               //      '#[a-z][a-z0-9.+-]{1,8}://#i'
+               ),
+               ' $0',
+               $string
+       );
+
+       return $string;
+}
+
+// TODO: Area selection (Check BBCode only, check anchor only, check ...)
+// Main function of spam-uri pickup
+function spam_uri_pickup($string = '')
+{
+       $string = spam_uri_pickup_preprocess($string);
+
+       $array  = uri_pickup($string);
+
        // Area elevation for '(especially external)link' intension
        if (! empty($array)) {
                // Anchor tags by preg_match_all()
                // [OK] <a href="http://nasty.example.com">visit http://nasty.example.com/</a>
+               // [OK] <a href=\'http://nasty.example.com/\' >discount foobar</a> 
                // [NG] <a href="http://ng.example.com">visit http://ng.example.com _not_ended_
                // [NG] <a href=  >Good site!</a> <a href= "#" >test</a>
                $areas = array();
@@ -70,20 +174,12 @@ function spam_pickup($string = '')
                //var_dump(recursive_map('htmlspecialchars', $areas));
                foreach(array_keys($areas) as $area) {
                        $areas[$area] =  array(
-                               $areas[$area][0][1], // [0][1] = Area start (<a href>)
-                               $areas[$area][1][1], // [1][1] = Area end   (</a>)
+                               $areas[$area][0][1], // Area start (<a href>)
+                               $areas[$area][1][1], // Area end   (</a>)
                        );
                }
                area_measure($areas, $array);
 
-               // Various Wiki syntax
-               // [text_or_uri>text_or_uri]
-               // [text_or_uri:text_or_uri]
-               // [text_or_uri|text_or_uri]
-               // [text_or_uri->text_or_uri]
-               // [text_or_uri text_or_uri] // MediaWiki
-               // MediaWiki: [http://nasty.example.com/ visit http://nasty.example.com/]
-
                // phpBB's "BBCode" by preg_match_all()
                // [url]http://nasty.example.com/[/url]
                // [link]http://nasty.example.com/[/link]
@@ -96,52 +192,165 @@ function spam_pickup($string = '')
                //var_dump(recursive_map('htmlspecialchars', $areas));
                foreach(array_keys($areas) as $area) {
                        $areas[$area] = array(
-                               $areas[$area][0][1], // [0][1] = Area start ([url])
-                               $areas[$area][2][1], // [4][1] = Area end   ([/url])
+                               $areas[$area][0][1], // Area start ([url])
+                               $areas[$area][2][1], // Area end   ([/url])
                        );
                }
                area_measure($areas, $array);
 
+               // Various Wiki syntax
+               // [text_or_uri>text_or_uri]
+               // [text_or_uri:text_or_uri]
+               // [text_or_uri|text_or_uri]
+               // [text_or_uri->text_or_uri]
+               // [text_or_uri text_or_uri] // MediaWiki
+               // MediaWiki: [http://nasty.example.com/ visit http://nasty.example.com/]
+
                // Remove 'offset's for area_measure()
-               foreach(array_keys($array) as $key)
-                       unset($array[$key]['offset']);
+               //foreach(array_keys($array) as $key)
+               //      unset($array[$key]['offset']);
        }
 
        return $array;
 }
 
-// $array[0] => $array['name']
-function array_rename_keys(& $array, $rename = array(), $force = FALSE, $default = '')
+// $array['something'] => $array['wanted']
+function array_rename_keys(& $array, $keys = array('from' => 'to'), $force = FALSE, $default = '')
 {
-    if ($force) {
-               foreach($rename as $from => $to) {
-                       if (isset($array[$from])) {
-                               $array[$to] = & $array[$from];
-                               unset($array[$from]);
-                       } else  {
-                               $array[$to] = $default;
-                       }
-               }
-       } else {
-               foreach(array_keys($rename) as $from) {
-                       if (! isset($array[$from])) {
+       if (! is_array($array) || ! is_array($keys))
+               return FALSE;
+
+       // Nondestructive test
+       if (! $force)
+               foreach(array_keys($keys) as $from)
+                       if (! isset($array[$from]))
                                return FALSE;
-                       }
-               }
-               foreach($rename as $from => $to) {
+
+       foreach($keys as $from => $to) {
+               if ($from === $to) continue;
+               if (! $force || isset($array[$from])) {
                        $array[$to] = & $array[$from];
                        unset($array[$from]);
+               } else  {
+                       $array[$to] = $default;
                }
        }
+
        return TRUE;
 }
 
+// If in doubt, it's a little doubtful
+function area_measure($areas, & $array, $belief = -1, $a_key = 'area', $o_key = 'offset')
+{
+       if (! is_array($areas) || ! is_array($array)) return;
+
+       $areas_keys = array_keys($areas);
+       foreach(array_keys($array) as $u_index) {
+               $offset = isset($array[$u_index][$o_key]) ?
+                       intval($array[$u_index][$o_key]) : 0;
+               foreach($areas_keys as $a_index) {
+                       if (isset($array[$u_index][$a_key])) {
+                               $offset_s = intval($areas[$a_index][0]);
+                               $offset_e = intval($areas[$a_index][1]);
+                               // [Area => inside <= Area]
+                               if ($offset_s < $offset && $offset < $offset_e) {
+                                       $array[$u_index][$a_key] += $belief;
+                               }
+                       }
+               }
+       }
+}
+
+
+// ---------------------
+// Part Two
+
+// Scheme normalization: Renaming the schemes
+// snntp://example.org =>  nntps://example.org
+// NOTE: Keep the static lists simple. See also port_normalize().
+function scheme_normalize($scheme = '', $considerd_harmfull = TRUE)
+{
+       // Abbreviations considerable they don't have link intension
+       static $abbrevs = array(
+               'ttp'   => 'http',
+               'ttps'  => 'https',
+       );
+
+       // Alias => normalized
+       static $aliases = array(
+               'pop'   => 'pop3',
+               'news'  => 'nntp',
+               'imap4' => 'imap',
+               'snntp' => 'nntps',
+               'snews' => 'nntps',
+               'spop3' => 'pop3s',
+               'pops'  => 'pop3s',
+       );
+
+       $scheme = strtolower(trim($scheme));
+       if (isset($abbrevs[$scheme])) {
+               if ($considerd_harmfull) {
+                       $scheme = $abbrevs[$scheme];
+               } else {
+                       $scheme = '';
+               }
+       }
+       if (isset($aliases[$scheme])) $scheme = $aliases[$scheme];
+
+       return $scheme;
+}
+
+// Port normalization: Suppress the (redundant) default port
+// HTTP://example.org:80/ => http://example.org/
+// HTTP://example.org:8080/ => http://example.org:8080/
+// HTTPS://example.org:443/ => https://example.org/
+function port_normalize($port, $scheme, $scheme_normalize = TRUE)
+{
+       // Schemes that users _maybe_ want to add protocol-handlers
+       // to their web browsers. (and attackers _maybe_ want to use ...)
+       // Reference: http://www.iana.org/assignments/port-numbers
+       static $array = array(
+               // scheme => default port
+               'ftp'     =>    21,
+               'ssh'     =>    22,
+               'telnet'  =>    23,
+               'smtp'    =>    25,
+               'tftp'    =>    69,
+               'gopher'  =>    70,
+               'finger'  =>    79,
+               'http'    =>    80,
+               'pop3'    =>   110,
+               'sftp'    =>   115,
+               'nntp'    =>   119,
+               'imap'    =>   143,
+               'irc'     =>   194,
+               'wais'    =>   210,
+               'https'   =>   443,
+               'nntps'   =>   563,
+               'rsync'   =>   873,
+               'ftps'    =>   990,
+               'telnets' =>   992,
+               'imaps'   =>   993,
+               'ircs'    =>   994,
+               'pop3s'   =>   995,
+               'mysql'   =>  3306,
+       );
+
+       $port = trim($port);
+       if ($port === '') return $port;
+
+       if ($scheme_normalize) $scheme = scheme_normalize($scheme);
+       if (isset($array[$scheme]) && $port == $array[$scheme])
+               $port = ''; // Ignore the defaults
+
+       return $port;
+}
 
 // Path normalization
-// example.org => example.org/
-// example.org#hoge -> example.org/#hoge
-// example.org/path/a/b/./c////./d -> example.org/path/a/b/c/d
-// example.org/path/../../a/../back
+// http://example.org => http://example.org/
+// http://example.org#hoge => http://example.org/#hoge
+// http://example.org/path/a/b/./c////./d => http://example.org/path/a/b/c/d
+// http://example.org/path/../../a/../back => http://example.org/back
 function path_normalize($path = '', $divider = '/', $addroot = TRUE)
 {
        if (! is_string($path) || $path == '') {
@@ -174,74 +383,254 @@ function path_normalize($path = '', $divider = '/', $addroot = TRUE)
        return $path;
 }
 
-// If in doubt, it's a little doubtful
-function area_measure($areas, &$array, $belief = -1, $a_key = 'area', $o_key = 'offset')
+// An URI array => An URI (See uri_pickup())
+function uri_array_implode($uri = array())
 {
-       if (! is_array($areas) || ! is_array($array)) return;
+       if (empty($uri) || ! is_array($uri)) return NULL;
+       
+       $tmp = array();
+       if (isset($uri['scheme']) && $uri['scheme'] !== '') {
+               $tmp[] = & $uri['scheme'];
+               $tmp[] = '://';
+       }
+       if (isset($uri['userinfo']) && $uri['userinfo'] !== '') {
+               $tmp[] = & $uri['userinfo'];
+               $tmp[] = '@';
+       }
+       if (isset($uri['host']) && $uri['host'] !== '') {
+               $tmp[] = & $uri['host'];
+       }
+       if (isset($uri['port']) && $uri['port'] !== '') {
+               $tmp[] = ':';
+               $tmp[] = & $uri['port'];
+       }
+       if (isset($uri['path']) && $uri['path'] !== '') {
+               $tmp[] = & $uri['path'];
+       }
+       if (isset($uri['file']) && $uri['file'] !== '') {
+               $tmp[] = & $uri['file'];
+       }
+       if (isset($uri['fragment']) && $uri['fragment'] !== '') {
+               $tmp[] = '#';
+               $tmp[] = & $uri['fragment'];
+       }
 
-       $areas_keys = array_keys($areas);
-       foreach(array_keys($array) as $u_index) {
-               $offset = isset($array[$u_index][$o_key]) ?
-                       intval($array[$u_index][$o_key]) : 0;
-               foreach($areas_keys as $a_index) {
-                       if (isset($array[$u_index][$a_key])) {
-                               $offset_s = intval($areas[$a_index][0]);
-                               $offset_e = intval($areas[$a_index][1]);
-                               // [Area => inside <= Area]
-                               if ($offset_s < $offset && $offset < $offset_e) {
-                                       $array[$u_index][$a_key] += $belief;
+       return implode('', $tmp);
+}
+
+// ---------------------
+// Part One : Checker
+
+function generate_glob_regex($string = '', $divider = '/')
+{
+       static $from = array(
+                       0 => '*',
+                       1 => '?',
+                       2 => '\[',
+                       3 => '\]',
+                       4 => '[',
+                       5 => ']',
+               );
+       static $mid = array(
+                       0 => '_AST_',
+                       1 => '_QUE_',
+                       2 => '_eRBR_',
+                       3 => '_eLBR_',
+                       4 => '_RBR_',
+                       5 => '_LBR_',
+               );
+       static $to = array(
+                       0 => '.*',
+                       1 => '.',
+                       2 => '\[',
+                       3 => '\]',
+                       4 => '[',
+                       5 => ']',
+               );
+
+       $string = str_replace($from, $mid, $string); // Hide
+       $string = preg_quote($string, $divider);
+       $string = str_replace($mid, $to, $string);   // Unhide
+
+       return $string;
+}
+
+// TODO: Ignore list
+// TODO: require_or_include_once(another file)
+function is_badhost($hosts = '', $asap = TRUE)
+{
+       static $blocklist_regex;
+
+       if (! isset($blocklist_regex)) {
+               $blocklist_regex = array();
+               $blocklist = array(
+                       // Deny all uri
+                       //'*',
+
+                       // IP address or ...
+                       //'10.20.*.*',  // 10.20.example.com also matches
+                       //'\[1\]',
+                       
+                       // Too much malicious sub-domains
+                       '*.blogspot.com',
+
+                       // 2006-11 dev
+                       'wwwtahoo.com',
+
+                       // 2006-11 dev
+                       '*.infogami.com',
+
+                       // 2006/11/19 17:50 dev
+                       '*.google0site.org',
+                       '*.bigpricesearch.org',
+                       '*.osfind.org',
+                       '*.bablomira.biz',
+               );
+               foreach ($blocklist as $part) {
+                       $blocklist_regex[] = '#^' . generate_glob_regex($part, '#') . '$#i';
+               }
+       }
+
+       $result = 0;
+       if (! is_array($hosts)) $hosts = array($hosts);
+       foreach($hosts as $host) {
+               if (! is_string($host)) $host = '';
+               foreach ($blocklist_regex as $regex) {
+                       if (preg_match($regex, $host)) {
+                               ++$result;
+                               if ($asap) {
+                                       return $result;
+                               } else {
+                                       break; // Check next host
                                }
                        }
                }
        }
+
+       return $result;
 }
 
-function is_uri_spam($target = '')
+// TODO return TRUE or FALSE!
+// Simple/fast spam check
+function check_uri_spam($target = '', $method = array(), $asap = TRUE)
 {
-       $is_spam = FALSE;
-       $urinum = 0;
+       $is_spam  = FALSE;
+       $progress = array(
+               'quantity' => 0,
+               'area'     => 0,
+               'non_uniq' => 0,
+               'badhost'  => 0,
+               );
+
+       if (! is_array($method) || empty($method)) {
+               // Default
+               $method = array(
+                       'quantity' => 8,                // Allow N URIs
+                       'area'     => TRUE,
+                       'non_uniq' => 3,                // Allow N times dupe
+                       'badhost'  => TRUE,
+               );
+       }
 
        if (is_array($target)) {
                foreach($target as $str) {
-                       list($is_spam, $_urinum) = is_uri_spam($str);
-                       $urinum += $_urinum;
-                       if ($is_spam) break;
+                       // Recurse
+                       list($is_spam, $_progress) = check_uri_spam($str, $method);
+                       $progress['quantity'] += $_progress['quantity'];
+                       $progress['non_uniq'] += $_progress['non_uniq'];
+                       if ($asap || $is_spam) break;
                }
        } else {
-               $pickups = spam_pickup($target);
-               $urinum += count($pickups);
+               $pickups = spam_uri_pickup($target);
+               $progress['quantity'] += count($pickups);
+
                if (! empty($pickups)) {
-                       // Some users want to post some URLs, but ...
-                       if ($urinum > 8) {
-                               $is_spam = TRUE;        // Too many!
-                       } else {
+
+                       // URI quantity
+                       if ((! $is_spam || ! $asap) && isset($method['quantity']) &&
+                               $progress['quantity'] > $method['quantity']) {
+                               $is_spam = TRUE;
+                       }
+                       //var_dump($method['quantity'], $is_spam);
+
+                       // Using invalid area
+                       if ((! $is_spam || ! $asap) && isset($method['area'])) {
                                foreach($pickups as $pickup) {
                                        if ($pickup['area'] < 0) {
+                                               ++$progress['area'];
                                                $is_spam = TRUE;
-                                               break;
+                                               if ($asap) break;
+                                       }
+                               }
+                       }
+                       //var_dump($method['area'], $is_spam);
+
+                       // URI uniqueness (and removing non-uniques)
+                       if ((! $is_spam || ! $asap) && isset($method['non_uniq'])) {
+                               $uris = array();
+                               foreach ($pickups as $key => $pickup) {
+                                       $uris[$key] = uri_array_implode($pickup);
+                               }
+                               $count = count($uris);
+                               $uris = array_unique($uris);
+                               $progress['non_uniq'] += $count - count($uris);
+                               if ($progress['non_uniq'] > $method['non_uniq']) {
+                                       $is_spam = TRUE;
+                               }
+                               if (! $asap || ! $is_spam) {
+                                       foreach (array_diff(array_keys($pickups),
+                                               array_keys($uris)) as $remove) {
+                                               unset($pickups[$remove]);
                                        }
                                }
+                               unset($uris);
+                               //var_dump($uris, $pickups);
                        }
+                       //var_dump($method['non_uniq'], $is_spam);
+
+                       // Bad host
+                       if ((! $is_spam || ! $asap) && isset($method['badhost'])) {
+                               $hosts = array();
+                               foreach ($pickups as $pickup) {
+                                       $hosts[] = & $pickup['host'];
+                               }
+                               $count = is_badhost(array_unique($hosts), $asap);
+                               $progress['badhost'] += $count;
+                               if ($count !== 0) $is_spam = TRUE;
+                       }
+                       //var_dump($method['badhost'], $is_spam);
                }
        }
 
-       return array($is_spam, $urinum);
+       return array($is_spam, $progress);
 }
 
+// ---------------------
 
-// TODO: tracker\82ª\95Ï\93®\82·\82é\8e\96\82à\82 \82è\81A\82¢\82Á\82»\82Ì\82±\82Æ$post\82â $vars\91S\82Ä\82ð\91Î\8fÛ\82É\82µ\82½\95û\82ª\82¢\82¢\81B
-//   \82»\82¤\82·\82ê\82Î\98R\82ê\82à\96³\82¢\81B
-//   \82Å\81A\83\81\81[\83\8b\82Í\82Ð\82Á\82©\82¯\82½\83t\83B\81[\83\8b\83h\82¾\82¯\82É\82·\82é\82Æ\82©\81B
-//   edit\91Î\8dô\82Æ\82µ\82Ä\82Í\96³\8e\8b\82·\82é\83t\83B\81[\83\8b\83h\82ð\97p\88Ó\82·\82é\82Æ\82©\81B
+// Check User-Agent (not testing yet)
+function is_invalid_useragent($ua_name = '' /*, $ua_vars = ''*/ )
+{
+       return $ua_name === '';
+}
 
-// Mail to administrator with more measurement data?
-// Simple/fast spam filter (for one text field)
-function pkwk_spamfilter($action, $page, $target = array('title' => ''))
+// ---------------------
+
+// TODO: Separate check-part(s) and mail part
+// TODO: Mail to administrator with more measurement data?
+// Simple/fast spam filter ($target: 'a string' or an array())
+function pkwk_spamfilter($action, $page, $target = array('title' => ''), $method = array())
 {
        $is_spam = FALSE;
-       list($is_spam) = is_uri_spam($target);
 
+       //$is_spam =  is_invalid_useragent('NOTYET');
        if ($is_spam) {
+               $action .= ' (Invalid User-Agent)';
+       } else {
+               list($is_spam) = check_uri_spam($target, $method);
+       }
+
+       if ($is_spam) {
+               // Mail to administrator(s)
                global $notify, $notify_subject;
                if ($notify) {
                        $footer['ACTION'] = $action;
@@ -257,6 +646,10 @@ function pkwk_spamfilter($action, $page, $target = array('title' => ''))
        if ($is_spam) spam_exit();
 }
 
+// ---------------------
+
+// Common bahavior for blocking
+// NOTE: Call this function from various blocking feature, to disgueise the reason 'why blocked'
 function spam_exit()
 {
        die("\n");