OSDN Git Service

.ru, .tz, .za
[pukiwiki/pukiwiki_sandbox.git] / spam / spam.php
index d771157..128fbfa 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// $Id: spam.php,v 1.175 2007/06/11 14:28:07 henoheno Exp $
+// $Id: spam.php,v 1.191 2007/06/24 03:13:09 henoheno Exp $
 // Copyright (C) 2006-2007 PukiWiki Developers Team
 // License: GPL v2 or (at your option) any later version
 //
@@ -43,10 +43,12 @@ function preg_grep_invert($pattern = '//', $input = array())
 // NOTE: If the same data exists, it must be corrupted.
 function var_export_shrink($expression, $return = FALSE, $ignore_numeric_keys = FALSE)
 {
-       $result =preg_replace(
+       $result = var_export($expression, TRUE);
+
+       $result = preg_replace(
                // Remove a newline and spaces
                '# => \n *array \(#', ' => array (',
-               var_export($expression, TRUE)
+               $result
        );
 
        if ($ignore_numeric_keys) {
@@ -109,38 +111,43 @@ function array_renumber_numeric_keys(& $array)
 // References:
 //   http://www.freebsd.org/cgi/man.cgi?query=strings (Man-page of GNU strings)
 //   http://www.pcre.org/pcre.txt
-function strings($binary = '', $min_len = 4, $ignore_space = FALSE)
+// Note: mb_ereg_replace() is one of mbstring extension's functions
+//   and need to init its encoding.
+function strings($binary = '', $min_len = 4, $ignore_space = FALSE, $multibyte = FALSE)
 {
+       // String only
+       $binary = (is_array($binary) || $binary === TRUE) ? '' : strval($binary);
+
+       $regex = $ignore_space ?
+               '[^[:graph:] \t\n]+' :          // Remove "\0" etc, and readable spaces
+               '[^[:graph:][:space:]]+';       // Preserve readable spaces if possible
+
+       $binary = $multibyte ?
+               mb_ereg_replace($regex,           "\n",  $binary) :
+               preg_replace('/' . $regex . '/s', "\n",  $binary);
+
        if ($ignore_space) {
                $binary = preg_replace(
                        array(
-                               '/(?:[^[:graph:] \t\n]|[\r])+/s',
                                '/[ \t]{2,}/',
                                '/^[ \t]/m',
                                '/[ \t]$/m',
                        ),
                        array(
-                               "\n",
                                ' ',
                                '',
                                ''
                        ),
                         $binary);
-       } else {
-               // Remove "\0" etc. Preserve readable spaces if possible.
-               $binary = preg_replace('/(?:[^[:graph:][:space:]]|[\r])+/s', "\n", $binary);
        }
 
        if ($min_len > 1) {
+               // The last character seems "\n" or not
+               $br = (! empty($binary) && $binary[strlen($binary) - 1] == "\n") ? "\n" : '';
+
                $min_len = min(1024, intval($min_len));
                $regex = '/^.{' . $min_len . ',}/S';
-               if (is_array($binary)) {
-                       foreach(array_keys($binary) as $key) {
-                               $binary[$key] = implode("\n", preg_grep($regex, explode("\n", $binary[$key])));
-                       }
-               } else {
-                       $binary = implode("\n", preg_grep($regex, explode("\n", $binary)));
-               }
+               $binary = implode("\n", preg_grep($regex, explode("\n", $binary))) . $br;
        }
 
        return $binary;
@@ -182,7 +189,7 @@ function uri_pickup($string = '')
                        // 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
-                       '[a-z0-9][a-z0-9.-]+[a-z0-9]' .                 // hostname(FQDN) : foo.example.org
+                       '[a-z0-9_-][a-z0-9_.-]+[a-z0-9_-]' .            // hostname(FQDN) : foo.example.org
                ')' .
                '(?::([0-9]*))?' .                                      // 4: Port
                '((?:/+[^\s<>"\'\[\]/\#]+)*/+)?' .      // 5: Directory path or path-info
@@ -448,6 +455,27 @@ function _preg_replace_callback_domain_exposure($matches = array())
        return $result;
 }
 
+// Preprocess: Removing uninterest part for URI detection
+function spam_uri_removing_hocus_pocus($binary = '', $method = array())
+{
+       $length = 4 ; // 'http'(1) and '://'(2) and 'fqdn'(1)
+       if (is_array($method)) {
+               // '<a'(2) or 'href='(5) or '>'(1) or '</a>'(4)
+               // '[uri'(4) or ']'(1) or '[/uri]'(6) 
+               if (isset($method['area_anchor']) || isset($method['uri_anchor']) ||
+                   isset($method['area_bbcode']) || isset($method['uri_bbcode']))
+                               $length = 1;    // Seems not effective
+       }
+
+       // Removing sequential spaces and too short lines
+       $binary = strings($binary, $length, TRUE, FALSE); // Multibyte NOT needed
+
+       // Remove words (has no '<>[]:') between spaces
+       $binary = preg_replace('/[ \t][\w.,()\ \t]+[ \t]/', ' ', $binary);
+
+       return $binary;
+}
+
 // Preprocess: rawurldecode() and adding space(s) and something
 // to detect/count some URIs _if possible_
 // NOTE: It's maybe danger to var_dump(result). [e.g. 'javascript:']
@@ -455,11 +483,12 @@ function _preg_replace_callback_domain_exposure($matches = array())
 // [OK] http://victim.example.org/nasty.example.org
 // [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 = '')
+function spam_uri_pickup_preprocess($string = '', $method = array())
 {
        if (! is_string($string)) return '';
 
-       $string = rawurldecode($string);
+       $string = spam_uri_removing_hocus_pocus(rawurldecode($string), $method);
+       //var_dump(htmlspecialchars($string));
 
        // Domain exposure (simple)
        // http://victim.example.org/nasty.example.org/path#frag
@@ -538,7 +567,7 @@ function spam_uri_pickup($string = '', $method = array())
                $method = check_uri_spam_method();
        }
 
-       $string = spam_uri_pickup_preprocess($string);
+       $string = spam_uri_pickup_preprocess($string, $method);
 
        $array  = uri_pickup($string);
 
@@ -1204,6 +1233,7 @@ function check_uri_spam($target = '', $method = array())
        foreach(array_keys($method) as $key) {
                if (! isset($sum[$key])) $sum[$key] = 0;
        }
+       if (! isset($sum['quantity'])) $sum['quantity'] = 0;
 
        if (is_array($target)) {
                foreach($target as $str) {
@@ -1491,175 +1521,87 @@ function summarize_detail_newtral($progress = array())
            ! is_array($progress['hosts']) ||
            empty($progress['hosts'])) return '';
 
-       $result = '';
-       if (FALSE) {
-               // Sort by domain
-               $tmp = array();
-               foreach($progress['hosts'] as $value) {
-                       $tmp[delimiter_reverse($value)] = $value;
-               }
-               ksort($tmp, SORT_STRING);
-               $result = count($tmp) . ' (' .implode(', ', $tmp) . ')';
-       } else {
-               $tmp = array();
-               foreach($progress['hosts'] as $value) {
-                       $tmp = array_merge_recursive(
-                               $tmp,
-                               array_leaf(explode('.', delimiter_reverse($value)), TRUE, $value)
-                       );
-               }
-
-//var_dump($tmp);
-// TODO: IP address 1.2.3.4 => "0"-3-2-1 by array_shrinkbranch_leaves()
-
-               array_shrinkbranch_leaves($tmp, '.', TRUE); // "domain.tld"
-               array_joinbranch_leaf($tmp, '.', 0, TRUE);
-               foreach($tmp as $key => $value) {
-                       if (is_array($value)) {
-                               ksort($tmp[$key], SORT_STRING);
-                               $tmp[$key] = implode(', ', array_flat_leaves($value));
-                       }
+       // Generate a responsible $trie
+       $trie = array();
+       foreach($progress['hosts'] as $value) {
+               // 'A.foo.bar.example.com'
+               $resp = whois_responsibility($value);   // 'example.com'
+               if (empty($resp)) {
+                       // One or more test, or do nothing here
+                       $resp = strval($value);
+                       $rest = '';
+               } else {
+                       $rest = rtrim(substr($value, 0, - strlen($resp)), '.'); // 'A.foo.bar'
                }
-               ksort($tmp, SORT_STRING);
-
-               $result = var_export_shrink($tmp, TRUE, TRUE);
+               $trie = array_merge_recursive($trie, array($resp => array($rest => NULL)));
        }
 
-       return $result;
-}
-
-// array('F' => array('B' => array('C' => array('d' => array('' => 'foobar')))))
-// to
-// array('F.B.C.d.' => 'foobar')
-function array_joinbranch_leaf(& $array, $delim = '.', $limit = 0, $reverse = FALSE)
-{
+       // Format: var_export_shrink() -like output
        $result = array();
-       if (! is_array($array)) return $result; // Nothing to do
-
-       $limit  = max(0, intval($limit));
-       $cstack = array();
-
-       foreach(array_keys($array) as $key) {
-               $kstack = array();
-               $k      = -1;
-
-               $single = array($key => & $array[$key]);        // Keep it single
-               $cursor = & $single;
-               while(is_array($cursor) && count($cursor) == 1) {       // Once
-                       ++$k;
-                       $kstack[] = key($cursor);
-                       $cursor   = & $cursor[$kstack[$k]];
-                       if ($limit != 0 && $k == $limit) break;
-               }
-
-               // Relink
-               if ($k != 0) {
-                       if ($reverse) $kstack = array_reverse($kstack);
-                       $joinkey = implode($delim, $kstack);
-
-                       unset($array[$key]);
-                       $array[$joinkey]  = & $cursor;
-                       $result[$joinkey] = $k + 1;     // Key seems not an single array => joined length
+       ksort_by_domain($trie);
+       foreach(array_keys($trie) as $key) {
+               ksort_by_domain($trie[$key]);
+               if (count($trie[$key]) == 1 && key($trie[$key]) == '') {
+                       // Just one 'responsibility.example.com'
+                       $result[] = '  \'' . $key . '\',';
+               } else {
+                       // One subdomain-or-host, or several ones
+                       $subs = array();
+                       foreach(array_keys($trie[$key]) as $sub) {
+                               if ($sub == '') {
+                                       $subs[] = $key;
+                               } else {
+                                       $subs[] = $sub . '.' . $key;
+                               }
+                       }
+                       $result[] = '  \'' . $key . '\' => \'' . implode(', ', $subs) . '\',';
                }
+               unset($trie[$key]);
        }
-
-       return $result;
+       return
+               'array (' . "\n" .
+                       implode("\n", $result) . "\n" .
+               ')';
 }
 
-
-// array('A' => array('B' => 'C')) to
-// array('A.B' => 'C')
-// array(
-//     'A' => array(
-//             'B' => array(
-//                     'C' => array(
-//                             'D' => '1'
-//                     ),
-//             ),
-//     ),
-//     'G' => array(
-//             'H' => '2'
-//     ),
-// )
-// to
-// array (
-//     'G.H'     => '2',
-//     'A.B.C.D' => '1',
-// )
-function array_shrinkbranch_leaves(& $array, $delim = '.', $reverse = FALSE, $recurse = FALSE)
+// ksort() by domain
+function ksort_by_domain(& $array)
 {
-       $result = 0;
-       if (! is_array($array) || empty($array)) return $result;
-
+       $sort = array();
        foreach(array_keys($array) as $key) {
-               $branch = & $array[$key];
-               if (! is_array($branch) || empty($branch)) continue;
-
-               foreach(array_keys($branch) as $bkey) {
-                       $joinkey = $reverse ?
-                               $bkey . $delim . $key :
-                               $key  . $delim . $bkey;
-                       $array[$joinkey] = & $branch[$bkey];
-                       unset($array[$key]);
-                       ++$result;
-               }
+               $sort[delimiter_reverse($key)] = $key;
        }
-
-       // Rescan (Recurse)
-       if ($recurse && $result) {
-               $result = array_shrinkbranch_leaves($array, $delim, $reverse, $recurse);
+       ksort($sort, SORT_STRING);
+       $result = array();
+       foreach($sort as $key) {
+               $result[$key] = & $array[$key];
        }
-
-       return $result; // Tell me how many
+       $array = $result;
 }
-//$a = array (
-//     'edu' => array (
-//             'berkeley' => array (
-//                     'polisci' => array (
-//                             '' => 'polisci.berkeley.edu',
-//                     ),
-//             ),
-//             'cmich' => array (
-//                     'rso' => array (
-//                             '' => 'rso.cmich.edu',
-//                     ),
-//             ),
-//     ),
-//);
-//array_shrinkbranch_leaves($a, '.', TRUE);
-//var_export($a);
-
-//$a = array (
-//     '4' => array (
-//             '5' => array (
-//                     '6' => array (
-//                             '' => '7.8.9',
-//                     ),
-//             ),
-//     ),
-//);
-//array_shrinkbranch_leaves($a, '.', TRUE);
-//var_export($a);
-
-
 
 // Check responsibility-root of the FQDN
-// 'foobar.example.co.jp'      => 'example.co.jp'      (.co.jp      seems public)
-// 'foobar.example.act.edu.au' => 'example.act.edu.au' (.act.edu.au seems public)
-// 'foobar.example.com'        => 'example.com'        (.com        seems public)
-function domain_responsibility($fqdn = 'fqdn.foo.bar.example.com', $implicit = TRUE)
+// 'foo.bar.example.com'        => 'example.com'        (.com        has the last whois for it)
+// 'foo.bar.example.au'         => 'example.au'         (.au         has the last whois for it)
+// 'foo.bar.example.edu.au'     => 'example.edu.au'     (.edu.au     has the last whois for it)
+// 'foo.bar.example.act.edu.au' => 'example.act.edu.au' (.act.edu.au has the last whois for it)
+function whois_responsibility($fqdn = 'foo.bar.example.com', $parent = FALSE, $implicit = TRUE)
 {
        // Domains who have 2nd and/or 3rd level domains
        static $domain = array(
 
-               // ccTLD: Australia http://www.auda.org.au/ http://www.aunic.net/ http://www.ausregistry.com.au/
+               // ccTLD: Australia
+               // http://www.auda.org.au/
+               // NIC  : http://www.aunic.net/
+               // Whois: http://www.ausregistry.com.au/
                'au' => array(
-                       // ".au Second Level Domains" http://www.auda.org.au/domains/
+                       // .au Second Level Domains
+                       // http://www.auda.org.au/domains/
                        'asn'   => TRUE,
                        'com'   => TRUE,
                        'conf'  => TRUE,
                        'csiro' => TRUE,
                        'edu'   => array(       // http://www.domainname.edu.au/
+                               // Geographic
                                'act' => TRUE,
                                'nt'  => TRUE,
                                'nsw' => TRUE,
@@ -1670,6 +1612,7 @@ function domain_responsibility($fqdn = 'fqdn.foo.bar.example.com', $implicit = T
                                'wa'  => TRUE,
                        ),
                        'gov'   => array(
+                               // Geographic
                                'act' => TRUE,  // Australian Capital Territory
                                'nt'  => TRUE,  // Northern Territory
                                'nsw' => TRUE,  // New South Wales
@@ -1685,14 +1628,141 @@ function domain_responsibility($fqdn = 'fqdn.foo.bar.example.com', $implicit = T
                        'info'  => TRUE,
                ),
 
-               // ccTLD: Japan http://jprs.co.jp/en/ http://whois.jprs.jp/en/
+               // ccTLD: Bahrain
+               // NIC  : http://www.inet.com.bh/ (.bh policies not found)
+               // Whois: (Not available) http://www.inet.com.bh/
+               'bh' => array(
+                       // Observed
+                       'com' => TRUE,
+                       'edu' => TRUE,
+                       'gov' => TRUE,
+                       'org' => TRUE,
+               ),
+
+               // ccTLD: China
+               // NIC  : http://www.cnnic.net.cn/en/index/
+               // Whois: http://ewhois.cnnic.cn/
+               'cn' => array(
+                       // Provisional Administrative Rules for Registration of Domain Names in China
+                       // http://www.cnnic.net.cn/html/Dir/2003/11/27/1520.htm
+
+                       // Organizational
+                       'ac'  => TRUE,
+                       'com' => TRUE,
+                       'edu' => TRUE,
+                       'gov' => TRUE,
+                       'net' => TRUE,
+                       'org' => TRUE,
+
+                       // Geographic
+                       'ah' => TRUE,
+                       'bj' => TRUE,
+                       'cq' => TRUE,
+                       'fj' => TRUE,
+                       'gd' => TRUE,
+                       'gs' => TRUE,
+                       'gx' => TRUE,
+                       'gz' => TRUE,
+                       'ha' => TRUE,
+                       'hb' => TRUE,
+                       'he' => TRUE,
+                       'hi' => TRUE,
+                       'hk' => TRUE,
+                       'hl' => TRUE,
+                       'hn' => TRUE,
+                       'jl' => TRUE,
+                       'js' => TRUE,
+                       'jx' => TRUE,
+                       'ln' => TRUE,
+                       'mo' => TRUE,
+                       'nm' => TRUE,
+                       'nx' => TRUE,
+                       'qh' => TRUE,
+                       'sc' => TRUE,
+                       'sd' => TRUE,
+                       'sh' => TRUE,
+                       'sn' => TRUE,
+                       'sx' => TRUE,
+                       'tj' => TRUE,
+                       'tw' => TRUE,
+                       'xj' => TRUE,
+                       'xz' => TRUE,
+                       'yn' => TRUE,
+                       'zj' => TRUE,
+               ),
+
+               // ccTLD: India
+               // NIC  : http://www.inregistry.in/
+               // Whois: http://www.inregistry.in/whois_search/
+               'in' => array(
+                       // Policies http://www.inregistry.in/policies/
+                       'ac'   => TRUE,
+                       'co'   => TRUE,
+                       'firm' => TRUE,
+                       'gen'  => TRUE,
+                       'gov'  => TRUE,
+                       'ind'  => TRUE,
+                       'mil'  => TRUE,
+                       'net'  => TRUE,
+                       'org'  => TRUE,
+                       'res'  => TRUE,
+                       // Reserved Names by the government (for the 2nd level)
+                       // http://www.inregistry.in/policies/reserved_names
+               ),
+
+               // ccTLD: South Korea
+               // NIC  : http://www.nic.or.kr/english/
+               // Whois: http://whois.nida.or.kr/english/
+               'kr' => array(
+                       // .kr domain policy [appendix 1] : Qualifications for Second Level Domains
+                       // http://domain.nida.or.kr/eng/policy.jsp
+
+                       // Organizational
+                       'co'  => TRUE,
+                       'ne ' => TRUE,
+                       'or ' => TRUE,
+                       're ' => TRUE,
+                       'pe'  => TRUE,
+                       'go ' => TRUE,
+                       'mil' => TRUE,
+                       'ac'  => TRUE,
+                       'hs'  => TRUE,
+                       'ms'  => TRUE,
+                       'es'  => TRUE,
+                       'sc'  => TRUE,
+                       'kg'  => TRUE,
+
+                       // Geographic
+                       'seoul'     => TRUE,
+                       'busan'     => TRUE,
+                       'daegu'     => TRUE,
+                       'incheon'   => TRUE,
+                       'gwangju'   => TRUE,
+                       'daejeon'   => TRUE,
+                       'ulsan'     => TRUE,
+                       'gyeonggi'  => TRUE,
+                       'gangwon'   => TRUE,
+                       'chungbuk'  => TRUE,
+                       'chungnam'  => TRUE,
+                       'jeonbuk'   => TRUE,
+                       'jeonnam'   => TRUE,
+                       'gyeongbuk' => TRUE,
+                       'gyeongnam' => TRUE,
+                       'jeju'      => TRUE,
+               ),
+
+               // ccTLD: Japan
+               // NIC  : http://jprs.co.jp/en/
+               // Whois: http://whois.jprs.jp/en/
                'jp' => array(
+                       // Guide to JP Domain Name
                        // http://jprs.co.jp/en/jpdomain.html
 
                        // Organizational
                        'ac' => TRUE,
                        'ad' => TRUE,
                        'co' => TRUE,
+                       'ed' => TRUE,
                        'go' => TRUE,
                        'gr' => TRUE,
                        'lg' => TRUE,
@@ -1766,10 +1836,220 @@ function domain_responsibility($fqdn = 'fqdn.foo.bar.example.com', $implicit = T
                        'yokohama'  => TRUE,
                ),
 
-               // ccTLD: Ukraine http://www.nic.net.ua/ http://whois.com.ua/
+               // ccTLD: Mexico
+               // NIC  : http://www.nic.mx/
+               // Whois: http://www.nic.mx/es/Busqueda.Who_Is
+               'mx' => array(
+                       // Politicas Generales de Nombres de Dominio
+                       // http://www.nic.mx/es/Politicas?CATEGORY=INDICE
+                       'com'  => TRUE,
+                       'edu'  => TRUE,
+                       'gob'  => TRUE,
+                       'net'  => TRUE,
+                       'org'  => TRUE,
+               ),
+
+               // ccTLD: Russia
+               // NIC  : http://www.cctld.ru/en/
+               // Whois: http://www.ripn.net:8080/nic/whois/en/
+               'ru' => array(
+                       // List of Reserved second-level Domain Names
+                       // http://www.cctld.ru/en/doc/detail.php?id21=20&i21=2
+
+                       // Organizational
+                       'ac'   => TRUE,
+                       'com'  => TRUE,
+                       'edu'  => TRUE,
+                       'gov'  => TRUE,
+                       'int'  => TRUE,
+                       'mil'  => TRUE,
+                       'net'  => TRUE,
+                       'org'  => TRUE,
+                       'pp'   => TRUE,
+                       //'test' => TRUE,
+
+                       // Geographic
+                       'adygeya'     => TRUE,
+                       'altai'       => TRUE,
+                       'amur'        => TRUE,
+                       'amursk'      => TRUE,
+                       'arkhangelsk' => TRUE,
+                       'astrakhan'   => TRUE,
+                       'baikal'      => TRUE,
+                       'bashkiria'   => TRUE,
+                       'belgorod'    => TRUE,
+                       'bir'         => TRUE,
+                       'bryansk'     => TRUE,
+                       'buryatia'    => TRUE,
+                       'cbg'         => TRUE,
+                       'chel'        => TRUE,
+                       'chelyabinsk' => TRUE,
+                       'chita'       => TRUE,
+                       'chukotka'    => TRUE,
+                       'chuvashia'   => TRUE,
+                       'cmw'         => TRUE,
+                       'dagestan'    => TRUE,
+                       'dudinka'     => TRUE,
+                       'e-burg'      => TRUE,
+                       'fareast'     => TRUE,
+                       'grozny'      => TRUE,
+                       'irkutsk'     => TRUE,
+                       'ivanovo'     => TRUE,
+                       'izhevsk'     => TRUE,
+                       'jamal'       => TRUE,
+                       'jar'         => TRUE,
+                       'joshkar-ola' => TRUE,
+                       'k-uralsk'    => TRUE,
+                       'kalmykia'    => TRUE,
+                       'kaluga'      => TRUE,
+                       'kamchatka'   => TRUE,
+                       'karelia'     => TRUE,
+                       'kazan'       => TRUE,
+                       'kchr'        => TRUE,
+                       'kemerovo'    => TRUE,
+                       'khabarovsk'  => TRUE,
+                       'khakassia'   => TRUE,
+                       'khv'         => TRUE,
+                       'kirov'       => TRUE,
+                       'kms'         => TRUE,
+                       'koenig'      => TRUE,
+                       'komi'        => TRUE,
+                       'kostroma'    => TRUE,
+                       'krasnoyarsk' => TRUE,
+                       'kuban'       => TRUE,
+                       'kurgan'      => TRUE,
+                       'kursk'       => TRUE,
+                       'kustanai'    => TRUE,
+                       'kuzbass'     => TRUE,
+                       'lipetsk'     => TRUE,
+                       'magadan'     => TRUE,
+                       'magnitka'    => TRUE,
+                       'mari-el'     => TRUE,
+                       'mari'        => TRUE,
+                       'marine'      => TRUE,
+                       'mordovia'    => TRUE,
+                       'mosreg'      => TRUE,
+                       'msk'         => TRUE,
+                       'murmansk'    => TRUE,
+                       'mytis'       => TRUE,
+                       'nakhodka'    => TRUE,
+                       'nalchik'     => TRUE,
+                       'nkz'         => TRUE,
+                       'nnov'        => TRUE,
+                       'norilsk'     => TRUE,
+                       'nov'         => TRUE,
+                       'novosibirsk' => TRUE,
+                       'nsk'         => TRUE,
+                       'omsk'        => TRUE,
+                       'orenburg'    => TRUE,
+                       'oryol'       => TRUE,
+                       'oskol'       => TRUE,
+                       'palana'      => TRUE,
+                       'penza'       => TRUE,
+                       'perm'        => TRUE,
+                       'pskov'       => TRUE,
+                       'ptz'         => TRUE,
+                       'pyatigorsk'  => TRUE,
+                       'rnd'         => TRUE,
+                       'rubtsovsk'   => TRUE,
+                       'ryazan'      => TRUE,
+                       'sakhalin'    => TRUE,
+                       'samara'      => TRUE,
+                       'saratov'     => TRUE,
+                       'simbirsk'    => TRUE,
+                       'smolensk'    => TRUE,
+                       'snz'         => TRUE,
+                       'spb'         => TRUE,
+                       'stavropol'   => TRUE,
+                       'stv'         => TRUE,
+                       'surgut'      => TRUE,
+                       'syzran'      => TRUE,
+                       'tambov'      => TRUE,
+                       'tatarstan'   => TRUE,
+                       'tom'         => TRUE,
+                       'tomsk'       => TRUE,
+                       'tsaritsyn'   => TRUE,
+                       'tsk'         => TRUE,
+                       'tula'        => TRUE,
+                       'tuva'        => TRUE,
+                       'tver'        => TRUE,
+                       'tyumen'      => TRUE,
+                       'udm'         => TRUE,
+                       'udmurtia'    => TRUE,
+                       'ulan-ude'    => TRUE,
+                       'vdonsk'      => TRUE,
+                       'vladikavkaz' => TRUE,
+                       'vladimir'    => TRUE,
+                       'vladivostok' => TRUE,
+                       'volgograd'   => TRUE,
+                       'vologda'     => TRUE,
+                       'voronezh'    => TRUE,
+                       'vrn'         => TRUE,
+                       'vyatka'      => TRUE,
+                       'yakutia'     => TRUE,
+                       'yamal'       => TRUE,
+                       'yaroslavl'   => TRUE,
+                       'yekaterinburg'     => TRUE,
+                       'yuzhno-sakhalinsk' => TRUE,
+                       'zgrad'       => TRUE,
+               ),
+
+               // ccTLD: Seychelles
+               // NIC  : http://www.nic.sc/
+               // Whois: (Not available)
+               'sc' => array(
+                       // http://www.nic.sc/policies.html
+                       'com' => TRUE,
+                       'edu' => TRUE,
+                       'gov' => TRUE,
+                       'net' => TRUE,
+                       'org' => TRUE,
+               ),
+
+               // ccTLD: Taiwan
+               // NIC  : http://www.twnic.net.tw/
+               // Whois: http://www.twnic.net.tw/
+               'tw' => array(
+                       // Guidelines for Administration of Domain Name Registration
+                       // http://www.twnic.net.tw/english/dn/dn_02.htm
+                       // II. Types of TWNIC Domain Names and Application Requirements
+                       // http://www.twnic.net.tw/english/dn/dn_02_b.htm
+                       'club' => TRUE,
+                       'com'  => TRUE,
+                       'ebiz' => TRUE,
+                       'edu'  => TRUE,
+                       'game' => TRUE,
+                       'gov'  => TRUE,
+                       'idv'  => TRUE,
+                       'mil'  => TRUE,
+                       'net'  => TRUE,
+                       'org'  => TRUE,
+                       // Reserved words for the 2nd level
+                       // http://mydn.twnic.net.tw/en/dn02/INDEX.htm
+               ),
+
+               // ccTLD: Tanzania
+               // NIC  : http://www.psg.com/dns/tz/
+               // Whois: (Not available)
+               'tz' => array(
+                       //  TZ DOMAIN NAMING STRUCTURE
+                       // http://www.psg.com/dns/tz/tz.txt
+                       'ac' => TRUE,
+                       'co' => TRUE,
+                       'go' => TRUE,
+                       'ne' => TRUE,
+                       'or' => TRUE,
+               ),
+
+               // ccTLD: Ukraine
+               // NIC  : http://www.nic.net.ua/
+               // Whois: http://whois.com.ua/
                'ua' => array(
-                       'cherkassy'  => TRUE,   // www.cherkassy.ua
-                       'chernigov'  => TRUE,   
+                       // policy for alternative 2nd level domain names (a2ld)
+                       // http://www.nic.net.ua/doc/a2ld
+                       // http://whois.com.ua/
+                       'cherkassy'  => TRUE,
+                       'chernigov'  => TRUE,
                        'chernovtsy' => TRUE,
                        'ck'         => TRUE,
                        'cn'         => TRUE,
@@ -1820,9 +2100,11 @@ function domain_responsibility($fqdn = 'fqdn.foo.bar.example.com', $implicit = T
                        'zt'         => TRUE,
                ),
 
-               // ccTLD: United Kingdom http://www.nic.uk/
+               // ccTLD: United Kingdom
+               // NIC  : http://www.nic.uk/
                'uk' => array(
-                       // http://www.nominet.org.uk/registrants/faq/#available
+                       // Second Level Domains
+                       // http://www.nic.uk/registrants/aboutdomainnames/sld/
                        'co'     => TRUE,
                        'ltd'    => TRUE,
                        'me'     => TRUE,
@@ -1832,7 +2114,8 @@ function domain_responsibility($fqdn = 'fqdn.foo.bar.example.com', $implicit = T
                        'plc'    => TRUE,
                        'sch'    => TRUE,
                        
-                       // "Delegated Second Level Domains" http://www.nominet.org.uk/registrants/aboutdomainnames/sld/delegated/
+                       // Delegated Second Level Domains
+                       // http://www.nic.uk/registrants/aboutdomainnames/sld/delegated/
                        'ac'     => TRUE,
                        'gov'    => TRUE,
                        'mil'    => TRUE,
@@ -1841,10 +2124,22 @@ function domain_responsibility($fqdn = 'fqdn.foo.bar.example.com', $implicit = T
                        'police' => TRUE,
                ),
 
-               // ccTLD: United States of America http://nic.us/ http://whois.us/
-               'us' => array( // RFC1480
+               // ccTLD: United States of America
+               // NIC  : http://nic.us/
+               // Whois: http://whois.us/
+               'us' => array(
+                       // See RFC1480
+
+                       // Organizational
+                       'dni',
+                       'fed',
+                       'isa',
+                       'kids',
+                       'nsn',
 
-                       // State abbreviations for postal codes http://www.usps.com/ncsc/lookups/abbreviations.html
+                       // Geographical
+                       // United States Postal Service: State abbreviations (for postal codes)
+                       // http://www.usps.com/ncsc/lookups/abbreviations.html
                        'ak' => TRUE, // Alaska
                        'al' => TRUE, // Alabama
                        'ar' => TRUE, // Arkansas
@@ -1904,29 +2199,58 @@ function domain_responsibility($fqdn = 'fqdn.foo.bar.example.com', $implicit = T
                        'wi' => TRUE, // Wisconsin
                        'wv' => TRUE, // West Virginia
                        'wy' => TRUE, // Wyoming
+               ),
 
-                       // Others
-                       'dni',
-                       'fed',
-                       'isa',
-                       'kids',
-                       'nsn',
+               // ccTLD: South Africa
+               // NIC  : http://www.zadna.org.za/
+               // Whois: 
+               //   ac.za  http://www.tenet.ac.za/cgi/cgi_domainquery.exe
+               //   co.za  http://co.za/whois.shtml
+               //   gov.za http://dnsadmin.gov.za/
+               //   org.za http://www.org.za/
+               'za' => array(
+                       // Second-level subdomains of .ZA
+                       // http://www.zadna.org.za/slds.html
+                       'ac'   => TRUE,
+                       'city' => TRUE,
+                       'co'   => TRUE,
+                       'edu'  => TRUE,
+                       'gov'  => TRUE,
+                       'law'  => TRUE,
+                       'mil'  => TRUE,
+                       'nom'  => TRUE,
+                       'org'  => TRUE,
+                       'school' => array(
+                               // Provincial Domains
+                               // http://www.esn.org.za/dns/
+                               'ecape' => TRUE,
+                               'fs.'   => TRUE,
+                               'gp'    => TRUE,
+                               'kzn'   => TRUE,
+                               'lp'    => TRUE,
+                               'mpm'   => TRUE,
+                               'ncape' => TRUE,
+                               'nw'    => TRUE,
+                               'wcape' => TRUE,
+                       ),
                ),
        );
 
        if (! is_string($fqdn)) return '';
+       if (is_ip($fqdn))       return $fqdn;
 
        $result  = array();
        $dcursor = & $domain;
        $array   = array_reverse(explode('.', $fqdn));
        $i = 0;
        while(TRUE) {
+               if (! isset($array[$i])) break;
                $acursor = $array[$i];
                if (is_array($dcursor) && isset($dcursor[$acursor])) {
                        $result[] = & $array[$i];
                        $dcursor  = & $dcursor[$acursor];
                } else {
-                       if (isset($acursor)) {
+                       if (! $parent && isset($acursor)) {
                                $result[] = & $array[$i];       // Whois servers must know this subdomain
                        }
                        break;
@@ -1957,8 +2281,8 @@ function spam_dispose()
 // NOTE: Call this function from various blocking feature, to disgueise the reason 'why blocked'
 function spam_exit($mode = '', $data = array())
 {
-
        $exit = TRUE;
+
        switch ($mode) {
                case '':
                        echo("\n");
@@ -1986,7 +2310,14 @@ function pkwk_spamfilter($action, $page, $target = array('title' => ''), $method
        if (empty($progress['is_spam'])) {
                spam_dispose();
        } else {
-               $target = string($target, 0);   // Removing "\0" etc
+
+// TODO: detect encoding from $target for mbstring functions
+//             $tmp = array();
+//             foreach(array_keys($target) as $key) {
+//                     $tmp[strings($key, 0, FALSE, TRUE)] = strings($target[$key], 0, FALSE, TRUE);   // Removing "\0" etc
+//             }
+//             $target = & $tmp;
+
                pkwk_spamnotify($action, $page, $target, $progress, $method);
                spam_exit($exitmode, $progress);
        }