2 // $Id: spam.php,v 1.177 2007/06/15 14:46:15 henoheno Exp $
3 // Copyright (C) 2006-2007 PukiWiki Developers Team
4 // License: GPL v2 or (at your option) any later version
6 // Functions for Concept-work of spam-uri metrics
8 // (PHP 4 >= 4.3.0): preg_match_all(PREG_OFFSET_CAPTURE): $method['uri_XXX'] related feature
10 if (! defined('SPAM_INI_FILE')) define('SPAM_INI_FILE', 'spam.ini.php');
12 // ---------------------
15 // (PHP 4 >= 4.2.0): var_export(): mail-reporting and dump related
16 if (! function_exists('var_export')) {
17 function var_export() {
18 return 'var_export() is not found on this server' . "\n";
22 // (PHP 4 >= 4.2.0): preg_grep() enables invert option
23 function preg_grep_invert($pattern = '//', $input = array())
26 if (! isset($invert)) $invert = defined('PREG_GREP_INVERT');
29 return preg_grep($pattern, $input, PREG_GREP_INVERT);
31 $result = preg_grep($pattern, $input);
33 return array_diff($input, preg_grep($pattern, $input));
42 // Very roughly, shrink the lines of var_export()
43 // NOTE: If the same data exists, it must be corrupted.
44 function var_export_shrink($expression, $return = FALSE, $ignore_numeric_keys = FALSE)
46 $result = var_export($expression, TRUE);
48 $result = preg_replace(
49 // Remove a newline and spaces
50 '# => \n *array \(#', ' => array (',
54 if ($ignore_numeric_keys) {
55 $result =preg_replace(
56 // Remove numeric keys
57 '#^( *)[0-9]+ => #m', '$1',
70 // Remove redundant values from array()
71 function array_unique_recursive($array = array())
73 if (! is_array($array)) return $array;
76 foreach($array as $key => $value){
77 if (is_array($value)) {
78 $array[$key] = array_unique_recursive($value);
80 if (isset($tmp[$value])) {
91 // Renumber all numeric keys from 0
92 function array_renumber_numeric_keys(& $array)
94 if (! is_array($array)) return $array;
98 foreach($array as $key => $value){
99 if (is_array($value)) array_renumber_numeric_keys($array[$key]); // Recurse
100 if (is_numeric($key)) $tmp[$key] = ++$count;
102 array_rename_keys($array, $tmp);
107 // Roughly strings(1) using PCRE
108 // This function is useful to:
109 // * Reduce the size of data, from removing unprintable binary data
110 // * Detect _bare_strings_ from binary data
112 // http://www.freebsd.org/cgi/man.cgi?query=strings (Man-page of GNU strings)
113 // http://www.pcre.org/pcre.txt
114 function strings($binary = '', $min_len = 4, $ignore_space = FALSE)
117 $binary = preg_replace(
119 '/(?:[^[:graph:] \t\n]|[\r])+/s',
132 // Remove "\0" etc. Preserve readable spaces if possible.
133 $binary = preg_replace('/(?:[^[:graph:][:space:]]|[\r])+/s', "\n", $binary);
137 $min_len = min(1024, intval($min_len));
138 $regex = '/^.{' . $min_len . ',}/S';
139 if (is_array($binary)) {
140 foreach(array_keys($binary) as $key) {
141 $binary[$key] = implode("\n", preg_grep($regex, explode("\n", $binary[$key])));
144 $binary = implode("\n", preg_grep($regex, explode("\n", $binary)));
151 // Reverse $string with specified delimiter
152 function delimiter_reverse($string = 'foo.bar.example.com', $from_delim = '.', $to_delim = '.')
154 if (! is_string($string) || ! is_string($from_delim) || ! is_string($to_delim))
157 // com.example.bar.foo
158 return implode($to_delim, array_reverse(explode($from_delim, $string)));
162 // ---------------------
165 // Return an array of URIs in the $string
166 // [OK] http://nasty.example.org#nasty_string
167 // [OK] http://nasty.example.org:80/foo/xxx#nasty_string/bar
168 // [OK] ftp://nasty.example.org:80/dfsdfs
169 // [OK] ftp://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htm (from RFC3986)
170 function uri_pickup($string = '')
172 if (! is_string($string)) return array();
174 // Not available for: IDN(ignored)
177 // scheme://userinfo@host:port/path/or/pathinfo/maybefile.and?query=string#fragment
178 // Refer RFC3986 (Regex below is not strict)
179 '#(\b[a-z][a-z0-9.+-]{1,8}):[/\\\]+' . // 1: Scheme
181 '([^\s<>"\'\[\]/\#?@]*)' . // 2: Userinfo (Username)
185 '\[[0-9a-f:.]+\]' . '|' . // IPv6([colon-hex and dot]): RFC2732
186 '(?:[0-9]{1,3}\.){3}[0-9]{1,3}' . '|' . // IPv4(dot-decimal): 001.22.3.44
187 '[a-z0-9][a-z0-9.-]+[a-z0-9]' . // hostname(FQDN) : foo.example.org
189 '(?::([0-9]*))?' . // 4: Port
190 '((?:/+[^\s<>"\'\[\]/\#]+)*/+)?' . // 5: Directory path or path-info
191 '([^\s<>"\'\[\]\#?]+)?' . // 6: File?
192 '(?:\?([^\s<>"\'\[\]\#]+))?' . // 7: Query string
193 '(?:\#([a-z0-9._~%!$&\'()*+,;=:@-]*))?' . // 8: Fragment
195 $string, $array, PREG_SET_ORDER | PREG_OFFSET_CAPTURE
199 static $parts = array(
200 1 => 'scheme', 2 => 'userinfo', 3 => 'host', 4 => 'port',
201 5 => 'path', 6 => 'file', 7 => 'query', 8 => 'fragment'
203 $default = array('');
204 foreach(array_keys($array) as $uri) {
205 $_uri = & $array[$uri];
206 array_rename_keys($_uri, $parts, TRUE, $default);
207 $offset = $_uri['scheme'][1]; // Scheme's offset = URI's offset
208 foreach(array_keys($_uri) as $part) {
209 $_uri[$part] = & $_uri[$part][0]; // Remove offsets
213 foreach(array_keys($array) as $uri) {
214 $_uri = & $array[$uri];
215 if ($_uri['scheme'] === '') {
216 unset($array[$uri]); // Considererd harmless
219 unset($_uri[0]); // Matched string itself
220 $_uri['area']['offset'] = $offset; // Area offset for area_measure()
226 // Normalize an array of URI arrays
227 // NOTE: Give me the uri_pickup() results
228 function uri_pickup_normalize(& $pickups, $destructive = TRUE)
230 if (! is_array($pickups)) return $pickups;
233 foreach (array_keys($pickups) as $key) {
234 $_key = & $pickups[$key];
235 $_key['scheme'] = isset($_key['scheme']) ? scheme_normalize($_key['scheme']) : '';
236 $_key['host'] = isset($_key['host']) ? host_normalize($_key['host']) : '';
237 $_key['port'] = isset($_key['port']) ? port_normalize($_key['port'], $_key['scheme'], FALSE) : '';
238 $_key['path'] = isset($_key['path']) ? strtolower(path_normalize($_key['path'])) : '';
239 $_key['file'] = isset($_key['file']) ? file_normalize($_key['file']) : '';
240 $_key['query'] = isset($_key['query']) ? query_normalize($_key['query']) : '';
241 $_key['fragment'] = isset($_key['fragment']) ? strtolower($_key['fragment']) : '';
244 foreach (array_keys($pickups) as $key) {
245 $_key = & $pickups[$key];
246 $_key['scheme'] = isset($_key['scheme']) ? scheme_normalize($_key['scheme']) : '';
247 $_key['host'] = isset($_key['host']) ? strtolower($_key['host']) : '';
248 $_key['port'] = isset($_key['port']) ? port_normalize($_key['port'], $_key['scheme'], FALSE) : '';
249 $_key['path'] = isset($_key['path']) ? path_normalize($_key['path']) : '';
256 // An URI array => An URI (See uri_pickup())
258 // $pickups = uri_pickup('a string include some URIs');
260 // foreach (array_keys($pickups) as $key) {
261 // $uris[$key] = uri_pickup_implode($pickups[$key]);
263 function uri_pickup_implode($uri = array())
265 if (empty($uri) || ! is_array($uri)) return NULL;
268 if (isset($uri['scheme']) && $uri['scheme'] !== '') {
269 $tmp[] = & $uri['scheme'];
272 if (isset($uri['userinfo']) && $uri['userinfo'] !== '') {
273 $tmp[] = & $uri['userinfo'];
276 if (isset($uri['host']) && $uri['host'] !== '') {
277 $tmp[] = & $uri['host'];
279 if (isset($uri['port']) && $uri['port'] !== '') {
281 $tmp[] = & $uri['port'];
283 if (isset($uri['path']) && $uri['path'] !== '') {
284 $tmp[] = & $uri['path'];
286 if (isset($uri['file']) && $uri['file'] !== '') {
287 $tmp[] = & $uri['file'];
289 if (isset($uri['query']) && $uri['query'] !== '') {
291 $tmp[] = & $uri['query'];
293 if (isset($uri['fragment']) && $uri['fragment'] !== '') {
295 $tmp[] = & $uri['fragment'];
298 return implode('', $tmp);
301 // $array['something'] => $array['wanted']
302 function array_rename_keys(& $array, $keys = array('from' => 'to'), $force = FALSE, $default = '')
304 if (! is_array($array) || ! is_array($keys)) return FALSE;
306 // Nondestructive test
308 foreach(array_keys($keys) as $from)
309 if (! isset($array[$from]))
312 foreach($keys as $from => $to) {
313 if ($from === $to) continue;
314 if (! $force || isset($array[$from])) {
315 $array[$to] = & $array[$from];
316 unset($array[$from]);
318 $array[$to] = $default;
325 // ---------------------
328 // Pickup all of markup areas
329 function area_pickup($string = '', $method = array())
332 if (empty($method)) return $area;
334 // Anchor tag pair by preg_match and preg_match_all()
336 // [OK] <a href= >Good site!</a>
337 // [OK] <a href= "#" >test</a>
338 // [OK] <a href="http://nasty.example.com">visit http://nasty.example.com/</a>
339 // [OK] <a href=\'http://nasty.example.com/\' >discount foobar</a>
340 // [NG] <a href="http://ng.example.com">visit http://ng.example.com _not_ended_
341 $regex = '#<a\b[^>]*\bhref\b[^>]*>.*?</a\b[^>]*(>)#is';
342 if (isset($method['area_anchor'])) {
344 $count = isset($method['asap']) ?
345 preg_match($regex, $string) :
346 preg_match_all($regex, $string, $areas);
347 if (! empty($count)) $area['area_anchor'] = $count;
349 if (isset($method['uri_anchor'])) {
351 preg_match_all($regex, $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
352 foreach(array_keys($areas) as $_area) {
353 $areas[$_area] = array(
354 $areas[$_area][0][1], // Area start (<a href>)
355 $areas[$_area][1][1], // Area end (</a>)
358 if (! empty($areas)) $area['uri_anchor'] = $areas;
361 // phpBB's "BBCode" pair by preg_match and preg_match_all()
363 // [OK] [url]http://nasty.example.com/[/url]
364 // [OK] [link]http://nasty.example.com/[/link]
365 // [OK] [url=http://nasty.example.com]visit http://nasty.example.com/[/url]
366 // [OK] [link http://nasty.example.com/]buy something[/link]
367 $regex = '#\[(url|link)\b[^\]]*\].*?\[/\1\b[^\]]*(\])#is';
368 if (isset($method['area_bbcode'])) {
370 $count = isset($method['asap']) ?
371 preg_match($regex, $string) :
372 preg_match_all($regex, $string, $areas, PREG_SET_ORDER);
373 if (! empty($count)) $area['area_bbcode'] = $count;
375 if (isset($method['uri_bbcode'])) {
377 preg_match_all($regex, $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
378 foreach(array_keys($areas) as $_area) {
379 $areas[$_area] = array(
380 $areas[$_area][0][1], // Area start ([url])
381 $areas[$_area][2][1], // Area end ([/url])
384 if (! empty($areas)) $area['uri_bbcode'] = $areas;
387 // Various Wiki syntax
388 // [text_or_uri>text_or_uri]
389 // [text_or_uri:text_or_uri]
390 // [text_or_uri|text_or_uri]
391 // [text_or_uri->text_or_uri]
392 // [text_or_uri text_or_uri] // MediaWiki
393 // MediaWiki: [http://nasty.example.com/ visit http://nasty.example.com/]
398 // If in doubt, it's a little doubtful
399 // if (Area => inside <= Area) $brief += -1
400 function area_measure($areas, & $array, $belief = -1, $a_key = 'area', $o_key = 'offset')
402 if (! is_array($areas) || ! is_array($array)) return;
404 $areas_keys = array_keys($areas);
405 foreach(array_keys($array) as $u_index) {
406 $offset = isset($array[$u_index][$o_key]) ?
407 intval($array[$u_index][$o_key]) : 0;
408 foreach($areas_keys as $a_index) {
409 if (isset($array[$u_index][$a_key])) {
410 $offset_s = intval($areas[$a_index][0]);
411 $offset_e = intval($areas[$a_index][1]);
412 // [Area => inside <= Area]
413 if ($offset_s < $offset && $offset < $offset_e) {
414 $array[$u_index][$a_key] += $belief;
421 // ---------------------
424 // Domain exposure callback (See spam_uri_pickup_preprocess())
425 // http://victim.example.org/?foo+site:nasty.example.com+bar
426 // => http://nasty.example.com/?refer=victim.example.org
427 // NOTE: 'refer=' is not so good for (at this time).
428 // Consider about using IP address of the victim, try to avoid that.
429 function _preg_replace_callback_domain_exposure($matches = array())
433 // Preserve the victim URI as a complicity or ...
434 if (isset($matches[5])) {
436 $matches[1] . '://' . // scheme
437 $matches[2] . '/' . // victim.example.org
438 $matches[3]; // The rest of all (before victim)
442 if (isset($matches[4])) {
444 $matches[1] . '://' . // scheme
445 $matches[4] . // nasty.example.com
446 '/?refer=' . strtolower($matches[2]) . // victim.example.org
453 // Preprocess: rawurldecode() and adding space(s) and something
454 // to detect/count some URIs _if possible_
455 // NOTE: It's maybe danger to var_dump(result). [e.g. 'javascript:']
456 // [OK] http://victim.example.org/?site:nasty.example.org
457 // [OK] http://victim.example.org/nasty.example.org
458 // [OK] http://victim.example.org/go?http%3A%2F%2Fnasty.example.org
459 // [OK] http://victim.example.org/http://nasty.example.org
460 function spam_uri_pickup_preprocess($string = '')
462 if (! is_string($string)) return '';
464 $string = rawurldecode($string);
466 // Domain exposure (simple)
467 // http://victim.example.org/nasty.example.org/path#frag
468 // => http://nasty.example.org/?refer=victim.example.org and original
469 $string = preg_replace(
472 'ime\.nu' . '|' . // 2ch.net
473 'ime\.st' . '|' . // 2ch.net
474 'link\.toolbot\.com' . '|' .
477 '/([a-z0-9.%_-]+\.[a-z0-9.%_-]+)#i', // nasty.example.org
478 'http://$2/?refer=$1 $0', // Preserve $0 or remove?
482 // Domain exposure (gate-big5)
483 // http://victim.example.org/gate/big5/nasty.example.org/path
484 // => http://nasty.example.org/?refer=victim.example.org and original
485 $string = preg_replace(
488 'big5.51job.com' . '|' .
489 'big5.china.com' . '|' .
490 'big5.xinhuanet.com' . '|' .
493 '/([a-z0-9.%_-]+\.[a-z0-9.%_-]+)' .
494 '#i', // nasty.example.org
495 'http://$2/?refer=$1 $0', // Preserve $0 or remove?
499 // Domain exposure (See _preg_replace_callback_domain_exposure())
500 $string = preg_replace_callback(
504 // Something Google: http://www.google.com/supported_domains
505 '(?:[a-z0-9.]+\.)?google\.[a-z]{2,3}(?:\.[a-z]{2})?' .
508 '(?:[a-z0-9.]+\.)?altavista.com' .
512 '([a-z0-9?=&.%_/\'\\\+-]+)' . // path/?query=foo+bar+
513 '\bsite:([a-z0-9.%_-]+\.[a-z0-9.%_-]+)' . // site:nasty.example.com
514 //'()' . // Preserve or remove?
517 '_preg_replace_callback_domain_exposure',
521 // URI exposure (uriuri => uri uri)
522 $string = preg_replace(
524 '#(?<! )(?:https?|ftp):/#i',
525 // '#[a-z][a-z0-9.+-]{1,8}://#i',
526 // '#[a-z][a-z0-9.+-]{1,8}://#i'
535 // Main function of spam-uri pickup,
536 // A wrapper function of uri_pickup()
537 function spam_uri_pickup($string = '', $method = array())
539 if (! is_array($method) || empty($method)) {
540 $method = check_uri_spam_method();
543 $string = spam_uri_pickup_preprocess($string);
545 $array = uri_pickup($string);
547 // Area elevation of URIs, for '(especially external)link' intension
548 if (! empty($array)) {
550 if (isset($method['uri_anchor'])) $_method['uri_anchor'] = & $method['uri_anchor'];
551 if (isset($method['uri_bbcode'])) $_method['uri_bbcode'] = & $method['uri_bbcode'];
552 $areas = area_pickup($string, $_method, TRUE);
553 if (! empty($areas)) {
554 $area_shadow = array();
555 foreach (array_keys($array) as $key) {
556 $area_shadow[$key] = & $array[$key]['area'];
557 foreach (array_keys($_method) as $_key) {
558 $area_shadow[$key][$_key] = 0;
561 foreach (array_keys($_method) as $_key) {
562 if (isset($areas[$_key])) {
563 area_measure($areas[$_key], $area_shadow, 1, $_key);
569 // Remove 'offset's for area_measure()
570 foreach(array_keys($array) as $key)
571 unset($array[$key]['area']['offset']);
577 // ---------------------
580 // Scheme normalization: Renaming the schemes
581 // snntp://example.org => nntps://example.org
582 // NOTE: Keep the static lists simple. See also port_normalize().
583 function scheme_normalize($scheme = '', $abbrevs_harmfull = TRUE)
585 // Abbreviations they have no intention of link
586 static $abbrevs = array(
591 // Aliases => normalized ones
592 static $aliases = array(
602 if (! is_string($scheme)) return '';
604 $scheme = strtolower($scheme);
605 if (isset($abbrevs[$scheme])) {
606 $scheme = $abbrevs_harmfull ? $abbrevs[$scheme] : '';
608 if (isset($aliases[$scheme])) {
609 $scheme = $aliases[$scheme];
615 // Hostname normlization (Destructive)
616 // www.foo => www.foo ('foo' seems TLD)
617 // www.foo.bar => foo.bar
618 // www.10.20 => www.10.20 (Invalid hostname)
620 // 'www' is mostly used as traditional hostname of WWW server.
621 // 'www.foo.bar' may be identical with 'foo.bar'.
622 function host_normalize($host = '')
624 if (! is_string($host)) return '';
626 $host = strtolower($host);
628 if (preg_match('/^www\.(.+\.[a-z]+)$/', $host, $matches)) {
635 // Port normalization: Suppress the (redundant) default port
636 // HTTP://example.org:80/ => http://example.org/
637 // HTTP://example.org:8080/ => http://example.org:8080/
638 // HTTPS://example.org:443/ => https://example.org/
639 function port_normalize($port, $scheme, $scheme_normalize = FALSE)
641 // Schemes that users _maybe_ want to add protocol-handlers
642 // to their web browsers. (and attackers _maybe_ want to use ...)
643 // Reference: http://www.iana.org/assignments/port-numbers
644 static $array = array(
645 // scheme => default port
671 // intval() converts '0-1' to '0', so preg_match() rejects these invalid ones
672 if (! is_numeric($port) || $port < 0 || preg_match('/[^0-9]/i', $port))
675 $port = intval($port);
676 if ($scheme_normalize) $scheme = scheme_normalize($scheme);
677 if (isset($array[$scheme]) && $port == $array[$scheme])
678 $port = ''; // Ignore the defaults
683 // Path normalization
684 // http://example.org => http://example.org/
685 // http://example.org#hoge => http://example.org/#hoge
686 // http://example.org/path/a/b/./c////./d => http://example.org/path/a/b/c/d
687 // http://example.org/path/../../a/../back => http://example.org/back
688 function path_normalize($path = '', $divider = '/', $add_root = TRUE)
690 if (! is_string($divider)) return is_string($path) ? $path : '';
693 $first_div = & $divider;
697 if (! is_string($path) || $path == '') return $first_div;
699 if (strpos($path, $divider, strlen($path) - strlen($divider)) === FALSE) {
702 $last_div = & $divider;
705 $array = explode($divider, $path);
707 // Remove paddings ('//' and '/./')
708 foreach(array_keys($array) as $key) {
709 if ($array[$key] == '' || $array[$key] == '.') {
714 // Remove back-tracks ('/../')
716 foreach($array as $value) {
717 if ($value == '..') {
720 array_push($tmp, $value);
728 return $first_div . implode($divider, $array) . $last_div;
732 // DirectoryIndex normalize (Destructive and rough)
733 // TODO: sample.en.ja.html.gz => sample.html
734 function file_normalize($file = 'index.html.en')
736 static $simple_defaults = array(
737 'default.htm' => TRUE,
738 'default.html' => TRUE,
739 'default.asp' => TRUE,
740 'default.aspx' => TRUE,
741 'index' => TRUE, // Some system can omit the suffix
744 static $content_suffix = array(
745 // index.xxx, sample.xxx
760 static $language_suffix = array(
761 // Reference: Apache 2.0.59 'AddLanguage' default
790 // Reference: Apache 2.0.59 default 'index.html' variants
796 static $charset_suffix = array(
797 // Reference: Apache 2.0.59 'AddCharset' default
798 'iso8859-1' => TRUE, // ISO-8859-1
799 'latin1' => TRUE, // ISO-8859-1
800 'iso8859-2' => TRUE, // ISO-8859-2
801 'latin2' => TRUE, // ISO-8859-2
802 'cen' => TRUE, // ISO-8859-2
803 'iso8859-3' => TRUE, // ISO-8859-3
804 'latin3' => TRUE, // ISO-8859-3
805 'iso8859-4' => TRUE, // ISO-8859-4
806 'latin4' => TRUE, // ISO-8859-4
807 'iso8859-5' => TRUE, // ISO-8859-5
808 'latin5' => TRUE, // ISO-8859-5
809 'cyr' => TRUE, // ISO-8859-5
810 'iso-ru' => TRUE, // ISO-8859-5
811 'iso8859-6' => TRUE, // ISO-8859-6
812 'latin6' => TRUE, // ISO-8859-6
813 'arb' => TRUE, // ISO-8859-6
814 'iso8859-7' => TRUE, // ISO-8859-7
815 'latin7' => TRUE, // ISO-8859-7
816 'grk' => TRUE, // ISO-8859-7
817 'iso8859-8' => TRUE, // ISO-8859-8
818 'latin8' => TRUE, // ISO-8859-8
819 'heb' => TRUE, // ISO-8859-8
820 'iso8859-9' => TRUE, // ISO-8859-9
821 'latin9' => TRUE, // ISO-8859-9
822 'trk' => TRUE, // ISO-8859-9
823 'iso2022-jp'=> TRUE, // ISO-2022-JP
824 'jis' => TRUE, // ISO-2022-JP
825 'iso2022-kr'=> TRUE, // ISO-2022-KR
826 'kis' => TRUE, // ISO-2022-KR
827 'iso2022-cn'=> TRUE, // ISO-2022-CN
828 'cis' => TRUE, // ISO-2022-CN
830 'cp-1251' => TRUE, // ru, WINDOWS-1251
831 'win-1251' => TRUE, // ru, WINDOWS-1251
832 'cp866' => TRUE, // ru
833 'koi8-r' => TRUE, // ru, KOI8-r
834 'koi8-ru' => TRUE, // ru, KOI8-r
835 'koi8-uk' => TRUE, // ru, KOI8-ru
836 'ua' => TRUE, // ru, KOI8-ru
837 'ucs2' => TRUE, // ru, ISO-10646-UCS-2
838 'ucs4' => TRUE, // ru, ISO-10646-UCS-4
841 // Reference: Apache 2.0.59 default 'index.html' variants
846 // May uncompress by web browsers on the fly
847 // Must be at the last of the filename
848 // Reference: Apache 2.0.59 'AddEncoding'
849 static $encoding_suffix = array(
854 if (! is_string($file)) return '';
855 $_file = strtolower($file);
856 if (isset($simple_defaults[$_file])) return '';
859 // Roughly removing language/character-set/encoding suffixes
861 // * Apache 2 document about 'Content-negotiaton', 'mod_mime' and 'mod_negotiation'
862 // http://httpd.apache.org/docs/2.0/content-negotiation.html
863 // http://httpd.apache.org/docs/2.0/mod/mod_mime.html
864 // http://httpd.apache.org/docs/2.0/mod/mod_negotiation.html
865 // * http://www.iana.org/assignments/character-sets
866 // * RFC3066: Tags for the Identification of Languages
867 // http://www.ietf.org/rfc/rfc3066.txt
868 // * ISO 639: codes of 'language names'
869 $suffixes = explode('.', $_file);
870 $body = array_shift($suffixes);
872 // Remove the last .gz/.z
873 $last_key = end(array_keys($suffixes));
874 if (isset($encoding_suffix[$suffixes[$last_key]])) {
875 unset($suffixes[$last_key]);
878 // Cut language and charset suffixes
879 foreach($suffixes as $key => $value){
880 if (isset($language_suffix[$value]) || isset($charset_suffix[$value])) {
881 unset($suffixes[$key]);
884 if (empty($suffixes)) return $body;
887 $count = count($suffixes);
889 $current = current($suffixes);
890 if ($body == 'index' && $count == 1 && isset($content_suffix[$current])) return '';
895 // Sort query-strings if possible (Destructive and rough)
896 // [OK] &&&&f=d&b&d&c&a=0dd => a=0dd&b&c&d&f=d
897 // [OK] nothing==&eg=dummy&eg=padding&eg=foobar => eg=foobar
898 function query_normalize($string = '', $equal = TRUE, $equal_cutempty = TRUE, $stortolower = TRUE)
900 if (! is_string($string)) return '';
901 if ($stortolower) $string = strtolower($string);
903 $array = explode('&', $string);
905 // Remove '&' paddings
906 foreach(array_keys($array) as $key) {
907 if ($array[$key] == '') {
912 // Consider '='-sepalated input and paddings
914 $equals = $not_equals = array();
915 foreach ($array as $part) {
916 if (strpos($part, '=') === FALSE) {
917 $not_equals[] = $part;
919 list($key, $value) = explode('=', $part, 2);
920 $value = ltrim($value, '=');
921 if (! $equal_cutempty || $value != '') {
922 $equals[$key] = $value;
927 $array = & $not_equals;
928 foreach ($equals as $key => $value) {
929 $array[] = $key . '=' . $value;
935 return implode('&', $array);
938 // ---------------------
939 // Part One : Checker
941 // Rough implementation of globbing
943 // USAGE: $regex = '/^' . generate_glob_regex('*.txt', '/') . '$/i';
945 function generate_glob_regex($string = '', $divider = '/')
947 static $from = array(
950 // 22 => '[', // Maybe cause regex compilation error (e.g. '[]')
966 if (! is_string($string)) return '';
968 $string = str_replace($from, $mid, $string); // Hide
969 $string = preg_quote($string, $divider);
970 $string = str_replace($mid, $to, $string); // Unhide
975 // Rough hostname checker
977 // TODO: Strict digit, 0x, CIDR, IPv6
978 function is_ip($string = '')
980 if (preg_match('/^' .
981 '(?:[0-9]{1,3}\.){3}[0-9]{1,3}' . '|' .
982 '(?:[0-9]{1,3}\.){1,3}' . '$/',
984 return 4; // Seems IPv4(dot-decimal)
986 return 0; // Seems not IP
990 // Generate host (FQDN, IPv4, ...) regex
991 // 'localhost' : Matches with 'localhost' only
992 // 'example.org' : Matches with 'example.org' only (See host_normalize() about 'www')
993 // '.example.org' : Matches with ALL FQDN ended with '.example.org'
994 // '*.example.org' : Almost the same of '.example.org' except 'www.example.org'
995 // '10.20.30.40' : Matches with IPv4 address '10.20.30.40' only
996 // [TODO] '192.' : Matches with all IPv4 hosts started with '192.'
997 // TODO: IPv4, CIDR?, IPv6
998 function generate_host_regex($string = '', $divider = '/')
1000 if (! is_string($string)) return '';
1002 if (mb_strpos($string, '.') === FALSE)
1003 return generate_glob_regex($string, $divider);
1006 if (is_ip($string)) {
1008 return generate_glob_regex($string, $divider);
1010 // FQDN or something
1011 $part = explode('.', $string, 2);
1012 if ($part[0] == '') {
1013 $part[0] = '(?:.*\.)?'; // And all related FQDN
1014 } else if ($part[0] == '*') {
1015 $part[0] = '.*\.'; // All subdomains/hosts only
1017 return generate_glob_regex($string, $divider);
1019 $part[1] = generate_glob_regex($part[1], $divider);
1020 return implode('', $part);
1024 function get_blocklist($list = '')
1028 if ($list === NULL) {
1029 $regexes = NULL; // Unset
1033 if (! isset($regexes)) {
1035 if (file_exists(SPAM_INI_FILE)) {
1036 $blocklist = array();
1037 include(SPAM_INI_FILE);
1038 // $blocklist['badhost'] = array(
1039 // '*.blogspot.com', // Blog services's subdomains (only)
1040 // 'IANA-examples' => '#^(?:.*\.)?example\.(?:com|net|org)$#',
1042 if (isset($blocklist['list'])) {
1043 $regexes['list'] = & $blocklist['list'];
1046 $blocklist['list'] = array(
1047 'goodhost' => FALSE,
1051 foreach(array_keys($blocklist['list']) as $_list) {
1052 if (! isset($blocklist[$_list])) continue;
1053 foreach ($blocklist[$_list] as $key => $value) {
1054 if (is_array($value)) {
1055 $regexes[$_list][$key] = array();
1056 foreach($value as $_key => $_value) {
1057 get_blocklist_add($regexes[$_list][$key], $_key, $_value);
1060 get_blocklist_add($regexes[$_list], $key, $value);
1063 unset($blocklist[$_list]);
1069 return $regexes; // ALL
1070 } else if (isset($regexes[$list])) {
1071 return $regexes[$list];
1077 // Subroutine of get_blocklist()
1078 function get_blocklist_add(& $array, $key = 0, $value = '*.example.org')
1080 if (is_string($key)) {
1081 $array[$key] = & $value; // Treat $value as a regex
1083 $array[$value] = '/^' . generate_host_regex($value, '/') . '$/i';
1087 // Blocklist metrics: Separate $host, to $blocked and not blocked
1088 function blocklist_distiller(& $hosts, $keys = array('goodhost', 'badhost'), $asap = FALSE)
1090 if (! is_array($hosts)) $hosts = array($hosts);
1091 if (! is_array($keys)) $keys = array($keys);
1093 $list = get_blocklist('list');
1096 foreach($keys as $key){
1097 foreach (get_blocklist($key) as $label => $regex) {
1098 if (is_array($regex)) {
1099 foreach($regex as $_label => $_regex) {
1100 $group = preg_grep($_regex, $hosts);
1102 $hosts = array_diff($hosts, $group);
1103 $blocked[$key][$label][$_label] = $group;
1104 if ($asap && $list[$key]) break;
1108 $group = preg_grep($regex, $hosts);
1110 $hosts = array_diff($hosts, $group);
1111 $blocked[$key][$label] = $group;
1112 if ($asap && $list[$key]) break;
1121 // Default (enabled) methods and thresholds (for content insertion)
1122 function check_uri_spam_method($times = 1, $t_area = 0, $rule = TRUE)
1124 $times = intval($times);
1125 $t_area = intval($t_area);
1129 'quantity' => 8 * $times, // Allow N URIs
1130 'non_uniqhost' => 3 * $times, // Allow N duped (and normalized) Hosts
1131 //'non_uniquri'=> 3 * $times, // Allow N duped (and normalized) URIs
1134 'area_anchor' => $t_area, // Using <a href> HTML tag
1135 'area_bbcode' => $t_area, // Using [url] or [link] BBCode
1136 //'uri_anchor' => $t_area, // URI inside <a href> HTML tag
1137 //'uri_bbcode' => $t_area, // URI inside [url] or [link] BBCode
1142 //'asap' => TRUE, // Quit or return As Soon As Possible
1143 'uniqhost' => TRUE, // Show uniq host (at block notification mail)
1144 'badhost' => TRUE, // Check badhost
1150 // Remove non-$positive values
1151 foreach (array_keys($positive) as $key) {
1152 if ($positive[$key] < 0) unset($positive[$key]);
1155 return $positive + $bool;
1158 // Simple/fast spam check
1159 function check_uri_spam($target = '', $method = array())
1164 // Theme to do => Dummy, optional value, or optional array()
1166 //'uniqhost' => TRUE,
1167 //'non_uniqhost'=> 3,
1168 //'non_uniquri' => 3,
1169 //'badhost' => TRUE,
1170 //'area_anchor' => 0,
1171 //'area_bbcode' => 0,
1172 //'uri_anchor' => 0,
1173 //'uri_bbcode' => 0,
1176 // Theme => Volume found (int)
1179 // Flag. If someting defined here,
1180 // one or more spam will be included
1185 //'category' => array(
1190 // Hosts not blocked
1195 $sum = & $progress['sum'];
1196 $is_spam = & $progress['is_spam'];
1197 $progress['method'] = & $method; // Argument
1198 $blocked = & $progress['blocked'];
1199 $hosts = & $progress['hosts'];
1200 $asap = isset($method['asap']);
1203 if (! is_array($method) || empty($method)) {
1204 $method = check_uri_spam_method();
1206 foreach(array_keys($method) as $key) {
1207 if (! isset($sum[$key])) $sum[$key] = 0;
1210 if (is_array($target)) {
1211 foreach($target as $str) {
1212 if (! is_string($str)) continue;
1214 $_progress = check_uri_spam($str, $method); // Recurse
1217 $_sum = & $_progress['sum'];
1218 foreach (array_keys($_sum) as $key) {
1219 if (! isset($sum[$key])) {
1220 $sum[$key] = & $_sum[$key];
1222 $sum[$key] += $_sum[$key];
1227 $_is_spam = & $_progress['is_spam'];
1228 foreach (array_keys($_is_spam) as $key) {
1229 $is_spam[$key] = TRUE;
1232 if ($asap && $is_spam) break;
1235 $blocked = array_merge_recursive($blocked, $_progress['blocked']);
1236 $hosts = array_merge_recursive($hosts, $_progress['hosts']);
1240 $blocked = array_unique_recursive($blocked);
1241 $hosts = array_unique_recursive($hosts);
1243 // Recount $sum['badhost']
1244 $sum['badhost'] = array_count_leaves($blocked);
1249 // Area: There's HTML anchor tag
1250 if ((! $asap || ! $is_spam) && isset($method['area_anchor'])) {
1251 $key = 'area_anchor';
1252 $_asap = isset($method['asap']) ? array('asap' => TRUE) : array();
1253 $result = area_pickup($target, array($key => TRUE) + $_asap);
1255 $sum[$key] = $result[$key];
1256 if (isset($method[$key]) && $sum[$key] > $method[$key]) {
1257 $is_spam[$key] = TRUE;
1262 // Area: There's 'BBCode' linking tag
1263 if ((! $asap || ! $is_spam) && isset($method['area_bbcode'])) {
1264 $key = 'area_bbcode';
1265 $_asap = isset($method['asap']) ? array('asap' => TRUE) : array();
1266 $result = area_pickup($target, array($key => TRUE) + $_asap);
1268 $sum[$key] = $result[$key];
1269 if (isset($method[$key]) && $sum[$key] > $method[$key]) {
1270 $is_spam[$key] = TRUE;
1276 if ($asap && $is_spam) return $progress;
1279 $pickups = uri_pickup_normalize(spam_uri_pickup($target, $method));
1282 if (empty($pickups)) return $progress;
1284 // URI: Check quantity
1285 $sum['quantity'] += count($pickups);
1287 if ((! $asap || ! $is_spam) && isset($method['quantity']) &&
1288 $sum['quantity'] > $method['quantity']) {
1289 $is_spam['quantity'] = TRUE;
1292 // URI: used inside HTML anchor tag pair
1293 if ((! $asap || ! $is_spam) && isset($method['uri_anchor'])) {
1294 $key = 'uri_anchor';
1295 foreach($pickups as $pickup) {
1296 if (isset($pickup['area'][$key])) {
1297 $sum[$key] += $pickup['area'][$key];
1298 if(isset($method[$key]) &&
1299 $sum[$key] > $method[$key]) {
1300 $is_spam[$key] = TRUE;
1301 if ($asap && $is_spam) break;
1303 if ($asap && $is_spam) break;
1308 // URI: used inside 'BBCode' pair
1309 if ((! $asap || ! $is_spam) && isset($method['uri_bbcode'])) {
1310 $key = 'uri_bbcode';
1311 foreach($pickups as $pickup) {
1312 if (isset($pickup['area'][$key])) {
1313 $sum[$key] += $pickup['area'][$key];
1314 if(isset($method[$key]) &&
1315 $sum[$key] > $method[$key]) {
1316 $is_spam[$key] = TRUE;
1317 if ($asap && $is_spam) break;
1319 if ($asap && $is_spam) break;
1324 // URI: Uniqueness (and removing non-uniques)
1325 if ((! $asap || ! $is_spam) && isset($method['non_uniquri'])) {
1328 foreach (array_keys($pickups) as $key) {
1329 $uris[$key] = uri_pickup_implode($pickups[$key]);
1331 $count = count($uris);
1332 $uris = array_unique($uris);
1333 $sum['non_uniquri'] += $count - count($uris);
1334 if ($sum['non_uniquri'] > $method['non_uniquri']) {
1335 $is_spam['non_uniquri'] = TRUE;
1337 if (! $asap || ! $is_spam) {
1338 foreach (array_diff(array_keys($pickups),
1339 array_keys($uris)) as $remove) {
1340 unset($pickups[$remove]);
1347 if ($asap && $is_spam) return $progress;
1349 // Host: Uniqueness (uniq / non-uniq)
1350 foreach ($pickups as $pickup) $hosts[] = & $pickup['host'];
1351 $hosts = array_unique($hosts);
1352 $sum['uniqhost'] += count($hosts);
1353 if ((! $asap || ! $is_spam) && isset($method['non_uniqhost'])) {
1354 $sum['non_uniqhost'] = $sum['quantity'] - $sum['uniqhost'];
1355 if ($sum['non_uniqhost'] > $method['non_uniqhost']) {
1356 $is_spam['non_uniqhost'] = TRUE;
1361 if ($asap && $is_spam) return $progress;
1363 // URI: Bad host (Separate good/bad hosts from $hosts)
1364 if ((! $asap || ! $is_spam) && isset($method['badhost'])) {
1367 $list = get_blocklist('list');
1368 $blocked = blocklist_distiller($hosts, array_keys($list), $asap);
1369 foreach($list as $key=>$type){
1370 if (! $type) unset($blocked[$key]); // Ignore goodhost etc
1374 if (! empty($blocked)) $is_spam['badhost'] = TRUE;
1380 // Count leaves (A leaf = value that is not an array, or an empty array)
1381 function array_count_leaves($array = array(), $count_empty = FALSE)
1383 if (! is_array($array) || (empty($array) && $count_empty)) return 1;
1387 foreach ($array as $part) {
1388 $count += array_count_leaves($part, $count_empty);
1393 // An array-leaves to a flat array
1394 function array_flat_leaves($array, $unique = TRUE)
1396 if (! is_array($array)) return $array;
1399 foreach(array_keys($array) as $key) {
1400 if (is_array($array[$key])) {
1402 foreach(array_flat_leaves($array[$key]) as $_value) {
1406 $tmp[] = & $array[$key];
1410 return $unique ? array_values(array_unique($tmp)) : $tmp;
1413 // An array() to an array leaf
1414 function array_leaf($array = array('A', 'B', 'C.D'), $stem = FALSE, $edge = TRUE)
1416 if (! is_array($array)) return $array;
1420 foreach($array as $arg) {
1421 if (! is_string($arg) && ! is_int($arg)) continue;
1422 $tmp[$arg] = array();
1424 $tmp = & $tmp[$arg];
1427 $parent[key($parent)] = & $edge;
1429 $parent = key($parent);
1432 return $leaf; // array('A' => array('B' => 'C.D'))
1436 // ---------------------
1439 // Summarize $progress (blocked only)
1440 function summarize_spam_progress($progress = array(), $blockedonly = FALSE)
1443 $tmp = array_keys($progress['is_spam']);
1446 $method = & $progress['method'];
1447 if (isset($progress['sum'])) {
1448 foreach ($progress['sum'] as $key => $value) {
1449 if (isset($method[$key]) && $value) {
1450 $tmp[] = $key . '(' . $value . ')';
1456 return implode(', ', $tmp);
1459 function summarize_detail_badhost($progress = array())
1461 if (! isset($progress['blocked']) || empty($progress['blocked'])) return '';
1465 foreach($progress['blocked'] as $list => $lvalue) {
1466 foreach($lvalue as $group => $gvalue) {
1467 $flat = implode(', ', array_flat_leaves($gvalue));
1468 if ($flat === $group) {
1469 $blocked[$list][] = $flat;
1471 $blocked[$list][$group] = $flat;
1477 // From: 'A-1' => array('ie.to')
1478 // To: 'A-1' => 'ie.to'
1479 foreach($blocked as $list => $lvalue) {
1480 if (is_array($lvalue) &&
1481 count($lvalue) == 1 &&
1482 is_numeric(key($lvalue))) {
1483 $blocked[$list] = current($lvalue);
1487 return var_export_shrink($blocked, TRUE, TRUE);
1490 function summarize_detail_newtral($progress = array())
1492 if (! isset($progress['hosts']) ||
1493 ! is_array($progress['hosts']) ||
1494 empty($progress['hosts'])) return '';
1500 foreach($progress['hosts'] as $value) {
1502 // Try to shorten (pre) -- array('example.com', 'bar', 'foo')
1503 $resp = whois_responsibility($value); // 'example.com'
1504 $rest = rtrim(substr($value, 0, - strlen($resp)), '.'); // 'foo.bar'
1506 $parts = explode('.', delimiter_reverse('.' . $rest));
1507 array_unshift($parts, $resp);
1509 $parts = array($resp, $rest);
1512 $trie = array_merge_recursive(
1514 array_leaf($parts, TRUE, $value)
1518 // Try to shorten (post, non-recursive) -- 'foo.bar.example.com'
1519 array_joinbranch_leaf($trie, '.', 0, TRUE);
1521 // Sort and flatten -- 'A.foo.bar.example.com, B.foo.bar.example.com'
1522 foreach(array_keys($trie) as $key) {
1523 if (is_array($trie[$key])) {
1524 ksort_by_domain($trie[$key]);
1525 $trie[$key] = implode(', ', array_flat_leaves($trie[$key]));
1529 // TODO: ltrim('.') from $trie
1531 ksort_by_domain($trie);
1533 // TODO: from array('foobar' => 'foobar') to 'foobar'
1535 return var_export_shrink($trie, TRUE, TRUE);
1538 // ksort() by domain
1539 function ksort_by_domain(& $array)
1542 foreach(array_keys($array) as $key) {
1543 $sort[delimiter_reverse($key)] = $key;
1545 ksort($sort, SORT_STRING);
1547 foreach($sort as $key) {
1548 $result[$key] = & $array[$key];
1553 // array('F' => array('B' => array('C' => array('d' => array('' => 'foobar')))))
1555 // array('F.B.C.d.' => 'foobar')
1556 function array_joinbranch_leaf(& $array, $delim = '.', $limit = 0, $reverse = FALSE)
1559 if (! is_array($array)) return $result; // Nothing to do
1561 $limit = max(0, intval($limit));
1564 foreach(array_keys($array) as $key) {
1568 $single = array($key => & $array[$key]); // Keep it single
1569 $cursor = & $single;
1570 while(is_array($cursor) && count($cursor) == 1) { // Once
1572 $kstack[] = key($cursor);
1573 $cursor = & $cursor[$kstack[$k]];
1574 if ($limit != 0 && $k == $limit) break;
1579 if ($reverse) $kstack = array_reverse($kstack);
1580 $joinkey = implode($delim, $kstack);
1582 unset($array[$key]);
1583 $array[$joinkey] = & $cursor;
1584 $result[$joinkey] = $k + 1; // Key seems not an single array => joined length
1592 // Check responsibility-root of the FQDN
1593 // 'foo.bar.example.com' => 'example.com' (.com has the last whois for it)
1594 // 'foo.bar.example.au' => 'example.au' (.au has the last whois for it)
1595 // 'foo.bar.example.edu.au' => 'example.edu.au' (.edu.au has the last whois for it)
1596 // 'foo.bar.example.act.edu.au' => 'example.act.edu.au' (.act.edu.au has the last whois for it)
1597 function whois_responsibility($fqdn = 'foo.bar.example.com', $parent = FALSE, $implicit = TRUE)
1599 // Domains who have 2nd and/or 3rd level domains
1600 static $domain = array(
1603 // http://www.auda.org.au/
1604 // NIC : http://www.aunic.net/
1605 // Whois: http://www.ausregistry.com.au/
1607 // .au Second Level Domains
1608 // http://www.auda.org.au/domains/
1613 'edu' => array( // http://www.domainname.edu.au/
1626 'act' => TRUE, // Australian Capital Territory
1627 'nt' => TRUE, // Northern Territory
1628 'nsw' => TRUE, // New South Wales
1629 'qld' => TRUE, // Queensland
1630 'sa' => TRUE, // South Australia
1631 'tas' => TRUE, // Tasmania
1632 'vic' => TRUE, // Victoria
1633 'wa' => TRUE, // Western Australia
1642 // NIC : http://www.cnnic.net.cn/en/index/
1643 // Whois: http://ewhois.cnnic.cn/
1645 // Provisional Administrative Rules for Registration of Domain Names in China
1646 // http://www.cnnic.net.cn/html/Dir/2003/11/27/1520.htm
1693 // ccTLD: South Korea
1694 // NIC : http://www.nic.or.kr/english/
1695 // Whois: http://whois.nida.or.kr/english/
1697 // .kr domain policy [appendix 1] : Qualifications for Second Level Domains
1698 // http://domain.nida.or.kr/eng/policy.jsp
1729 'gyeongbuk' => TRUE,
1730 'gyeongnam' => TRUE,
1735 // NIC : http://jprs.co.jp/en/
1736 // Whois: http://whois.jprs.jp/en/
1738 // Guide to JP Domain Name
1739 // http://jprs.co.jp/en/jpdomain.html
1753 // Examples for 3rd level domains
1754 //'kumamoto' => array(
1755 // // http://www.pref.kumamoto.jp/link/list.asp#4
1756 // 'amakusa' => TRUE,
1757 // 'hitoyoshi' => TRUE,
1759 // 'kumamoto' => TRUE,
1769 'fukushima' => TRUE,
1772 'hiroshima' => TRUE,
1779 'kagoshima' => TRUE,
1782 'kitakyushu'=> TRUE,
1807 'tokushima' => TRUE,
1813 'yamaguchi' => TRUE,
1814 'yamanashi' => TRUE,
1819 // NIC : http://www.nic.net.ua/
1820 // Whois: http://whois.com.ua/
1822 // policy for alternative 2nd level domain names (a2ld)
1823 // http://www.nic.net.ua/doc/a2ld
1824 // http://whois.com.ua/
1825 'cherkassy' => TRUE,
1826 'chernigov' => TRUE,
1827 'chernovtsy' => TRUE,
1834 'dnepropetrovsk' => TRUE,
1840 'ivano-frankivsk' => TRUE,
1845 'kirovograd' => TRUE,
1863 'sebastopol' => TRUE,
1871 'zaporizhzhe' => TRUE,
1877 // ccTLD: United Kingdom
1878 // NIC : http://www.nic.uk/
1880 // Second Level Domains
1881 // http://www.nic.uk/registrants/aboutdomainnames/sld/
1891 // Delegated Second Level Domains
1892 // http://www.nic.uk/registrants/aboutdomainnames/sld/delegated/
1901 // ccTLD: United States of America
1902 // NIC : http://nic.us/
1903 // Whois: http://whois.us/
1915 // United States Postal Service: State abbreviations (for postal codes)
1916 // http://www.usps.com/ncsc/lookups/abbreviations.html
1917 'ak' => TRUE, // Alaska
1918 'al' => TRUE, // Alabama
1919 'ar' => TRUE, // Arkansas
1920 'as' => TRUE, // American samoa
1921 'az' => TRUE, // Arizona
1922 'ca' => TRUE, // California
1923 'co' => TRUE, // Colorado
1924 'ct' => TRUE, // Connecticut
1925 'dc' => TRUE, // District of Columbia
1926 'de' => TRUE, // Delaware
1927 'fl' => TRUE, // Florida
1928 'fm' => TRUE, // Federated states of Micronesia
1929 'ga' => TRUE, // Georgia
1930 'gu' => TRUE, // Guam
1931 'hi' => TRUE, // Hawaii
1932 'ia' => TRUE, // Iowa
1933 'id' => TRUE, // Idaho
1934 'il' => TRUE, // Illinois
1935 'in' => TRUE, // Indiana
1936 'ks' => TRUE, // Kansas
1937 'ky' => TRUE, // Kentucky
1938 'la' => TRUE, // Louisiana
1939 'ma' => TRUE, // Massachusetts
1940 'md' => TRUE, // Maryland
1941 'me' => TRUE, // Maine
1942 'mh' => TRUE, // Marshall Islands
1943 'mi' => TRUE, // Michigan
1944 'mn' => TRUE, // Minnesota
1945 'mo' => TRUE, // Missouri
1946 'mp' => TRUE, // Northern mariana islands
1947 'ms' => TRUE, // Mississippi
1948 'mt' => TRUE, // Montana
1949 'nc' => TRUE, // North Carolina
1950 'nd' => TRUE, // North Dakota
1951 'ne' => TRUE, // Nebraska
1952 'nh' => TRUE, // New Hampshire
1953 'nj' => TRUE, // New Jersey
1954 'nm' => TRUE, // New Mexico
1955 'nv' => TRUE, // Nevada
1956 'ny' => TRUE, // New York
1957 'oh' => TRUE, // Ohio
1958 'ok' => TRUE, // Oklahoma
1959 'or' => TRUE, // Oregon
1960 'pa' => TRUE, // Pennsylvania
1961 'pr' => TRUE, // Puerto Rico
1962 'pw' => TRUE, // Palau
1963 'ri' => TRUE, // Rhode Island
1964 'sc' => TRUE, // South Carolina
1965 'sd' => TRUE, // South Dakota
1966 'tn' => TRUE, // Tennessee
1967 'tx' => TRUE, // Texas
1968 'ut' => TRUE, // Utah
1969 'va' => TRUE, // Virginia
1970 'vi' => TRUE, // Virgin Islands
1971 'vt' => TRUE, // Vermont
1972 'wa' => TRUE, // Washington
1973 'wi' => TRUE, // Wisconsin
1974 'wv' => TRUE, // West Virginia
1975 'wy' => TRUE, // Wyoming
1979 if (! is_string($fqdn)) return '';
1982 $dcursor = & $domain;
1983 $array = array_reverse(explode('.', $fqdn));
1986 $acursor = $array[$i];
1987 if (is_array($dcursor) && isset($dcursor[$acursor])) {
1988 $result[] = & $array[$i];
1989 $dcursor = & $dcursor[$acursor];
1991 if (! $parent && isset($acursor)) {
1992 $result[] = & $array[$i]; // Whois servers must know this subdomain
1999 // Implicit responsibility: Top-Level-Domains must not be yours
2000 // 'bar.foo.something' => 'foo.something'
2001 if ($implicit && count($result) == 1 && count($array) > 1) {
2002 $result[] = & $array[1];
2005 return $result ? implode('.', array_reverse($result)) : '';
2009 // ---------------------
2013 function spam_dispose()
2015 get_blocklist(NULL);
2018 // Common bahavior for blocking
2019 // NOTE: Call this function from various blocking feature, to disgueise the reason 'why blocked'
2020 function spam_exit($mode = '', $data = array())
2029 echo('<pre>' . "\n");
2030 echo htmlspecialchars(var_export($data, TRUE));
2031 echo('</pre>' . "\n");
2035 if ($exit) exit; // Force exit
2039 // ---------------------
2042 // TODO: Record them
2043 // Simple/fast spam filter ($target: 'a string' or an array())
2044 function pkwk_spamfilter($action, $page, $target = array('title' => ''), $method = array(), $exitmode = '')
2046 $progress = check_uri_spam($target, $method);
2048 if (empty($progress['is_spam'])) {
2051 $target = string($target, 0); // Removing "\0" etc
2052 pkwk_spamnotify($action, $page, $target, $progress, $method);
2053 spam_exit($exitmode, $progress);
2057 // ---------------------
2058 // PukiWiki original
2060 // Mail to administrator(s)
2061 function pkwk_spamnotify($action, $page, $target = array('title' => ''), $progress = array(), $method = array())
2063 global $notify, $notify_subject;
2065 if (! $notify) return;
2067 $asap = isset($method['asap']);
2069 $summary['ACTION'] = 'Blocked by: ' . summarize_spam_progress($progress, TRUE);
2071 $summary['METRICS'] = summarize_spam_progress($progress);
2074 $tmp = summarize_detail_badhost($progress);
2075 if ($tmp != '') $summary['DETAIL_BADHOST'] = $tmp;
2077 $tmp = summarize_detail_newtral($progress);
2078 if (! $asap && $tmp != '') $summary['DETAIL_NEUTRAL_HOST'] = $tmp;
2080 $summary['COMMENT'] = $action;
2081 $summary['PAGE'] = '[blocked] ' . (is_pagename($page) ? $page : '');
2082 $summary['URI'] = get_script_uri() . '?' . rawurlencode($page);
2083 $summary['USER_AGENT'] = TRUE;
2084 $summary['REMOTE_ADDR'] = TRUE;
2085 pkwk_mail_notify($notify_subject, var_export($target, TRUE), $summary, TRUE);