2 // $Id: spam.php,v 1.174 2007/06/10 14:24:16 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 =preg_replace(
47 // Remove a newline and spaces
48 '# => \n *array \(#', ' => array (',
49 var_export($expression, TRUE)
52 if ($ignore_numeric_keys) {
53 $result =preg_replace(
54 // Remove numeric keys
55 '#^( *)[0-9]+ => #m', '$1',
68 // Remove redundant values from array()
69 function array_unique_recursive($array = array())
71 if (! is_array($array)) return $array;
74 foreach($array as $key => $value){
75 if (is_array($value)) {
76 $array[$key] = array_unique_recursive($value);
78 if (isset($tmp[$value])) {
89 // Renumber all numeric keys from 0
90 function array_renumber_numeric_keys(& $array)
92 if (! is_array($array)) return $array;
96 foreach($array as $key => $value){
97 if (is_array($value)) array_renumber_numeric_keys($array[$key]); // Recurse
98 if (is_numeric($key)) $tmp[$key] = ++$count;
100 array_rename_keys($array, $tmp);
105 // Roughly strings(1) using PCRE
106 // This function is useful to:
107 // * Reduce the size of data, from removing unprintable binary data
108 // * Detect _bare_strings_ from binary data
110 // http://www.freebsd.org/cgi/man.cgi?query=strings (Man-page of GNU strings)
111 // http://www.pcre.org/pcre.txt
112 function strings($binary = '', $min_len = 4, $ignore_space = FALSE)
115 $binary = preg_replace(
117 '/(?:[^[:graph:] \t\n]|[\r])+/s',
130 // Remove "\0" etc. Preserve readable spaces if possible.
131 $binary = preg_replace('/(?:[^[:graph:][:space:]]|[\r])+/s', "\n", $binary);
135 $min_len = min(1024, intval($min_len));
136 $regex = '/^.{' . $min_len . ',}/S';
137 if (is_array($binary)) {
138 foreach(array_keys($binary) as $key) {
139 $binary[$key] = implode("\n", preg_grep($regex, explode("\n", $binary[$key])));
142 $binary = implode("\n", preg_grep($regex, explode("\n", $binary)));
149 // Reverse $string with specified delimiter
150 function delimiter_reverse($string = 'foo.bar.example.com', $from_delim = '.', $to_delim = '.')
152 if (! is_string($string) || ! is_string($from_delim) || ! is_string($to_delim))
155 // com.example.bar.foo
156 return implode($to_delim, array_reverse(explode($from_delim, $string)));
160 // ---------------------
163 // Return an array of URIs in the $string
164 // [OK] http://nasty.example.org#nasty_string
165 // [OK] http://nasty.example.org:80/foo/xxx#nasty_string/bar
166 // [OK] ftp://nasty.example.org:80/dfsdfs
167 // [OK] ftp://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htm (from RFC3986)
168 function uri_pickup($string = '')
170 if (! is_string($string)) return array();
172 // Not available for: IDN(ignored)
175 // scheme://userinfo@host:port/path/or/pathinfo/maybefile.and?query=string#fragment
176 // Refer RFC3986 (Regex below is not strict)
177 '#(\b[a-z][a-z0-9.+-]{1,8}):[/\\\]+' . // 1: Scheme
179 '([^\s<>"\'\[\]/\#?@]*)' . // 2: Userinfo (Username)
183 '\[[0-9a-f:.]+\]' . '|' . // IPv6([colon-hex and dot]): RFC2732
184 '(?:[0-9]{1,3}\.){3}[0-9]{1,3}' . '|' . // IPv4(dot-decimal): 001.22.3.44
185 '[a-z0-9][a-z0-9.-]+[a-z0-9]' . // hostname(FQDN) : foo.example.org
187 '(?::([0-9]*))?' . // 4: Port
188 '((?:/+[^\s<>"\'\[\]/\#]+)*/+)?' . // 5: Directory path or path-info
189 '([^\s<>"\'\[\]\#?]+)?' . // 6: File?
190 '(?:\?([^\s<>"\'\[\]\#]+))?' . // 7: Query string
191 '(?:\#([a-z0-9._~%!$&\'()*+,;=:@-]*))?' . // 8: Fragment
193 $string, $array, PREG_SET_ORDER | PREG_OFFSET_CAPTURE
197 static $parts = array(
198 1 => 'scheme', 2 => 'userinfo', 3 => 'host', 4 => 'port',
199 5 => 'path', 6 => 'file', 7 => 'query', 8 => 'fragment'
201 $default = array('');
202 foreach(array_keys($array) as $uri) {
203 $_uri = & $array[$uri];
204 array_rename_keys($_uri, $parts, TRUE, $default);
205 $offset = $_uri['scheme'][1]; // Scheme's offset = URI's offset
206 foreach(array_keys($_uri) as $part) {
207 $_uri[$part] = & $_uri[$part][0]; // Remove offsets
211 foreach(array_keys($array) as $uri) {
212 $_uri = & $array[$uri];
213 if ($_uri['scheme'] === '') {
214 unset($array[$uri]); // Considererd harmless
217 unset($_uri[0]); // Matched string itself
218 $_uri['area']['offset'] = $offset; // Area offset for area_measure()
224 // Normalize an array of URI arrays
225 // NOTE: Give me the uri_pickup() results
226 function uri_pickup_normalize(& $pickups, $destructive = TRUE)
228 if (! is_array($pickups)) return $pickups;
231 foreach (array_keys($pickups) as $key) {
232 $_key = & $pickups[$key];
233 $_key['scheme'] = isset($_key['scheme']) ? scheme_normalize($_key['scheme']) : '';
234 $_key['host'] = isset($_key['host']) ? host_normalize($_key['host']) : '';
235 $_key['port'] = isset($_key['port']) ? port_normalize($_key['port'], $_key['scheme'], FALSE) : '';
236 $_key['path'] = isset($_key['path']) ? strtolower(path_normalize($_key['path'])) : '';
237 $_key['file'] = isset($_key['file']) ? file_normalize($_key['file']) : '';
238 $_key['query'] = isset($_key['query']) ? query_normalize($_key['query']) : '';
239 $_key['fragment'] = isset($_key['fragment']) ? strtolower($_key['fragment']) : '';
242 foreach (array_keys($pickups) as $key) {
243 $_key = & $pickups[$key];
244 $_key['scheme'] = isset($_key['scheme']) ? scheme_normalize($_key['scheme']) : '';
245 $_key['host'] = isset($_key['host']) ? strtolower($_key['host']) : '';
246 $_key['port'] = isset($_key['port']) ? port_normalize($_key['port'], $_key['scheme'], FALSE) : '';
247 $_key['path'] = isset($_key['path']) ? path_normalize($_key['path']) : '';
254 // An URI array => An URI (See uri_pickup())
256 // $pickups = uri_pickup('a string include some URIs');
258 // foreach (array_keys($pickups) as $key) {
259 // $uris[$key] = uri_pickup_implode($pickups[$key]);
261 function uri_pickup_implode($uri = array())
263 if (empty($uri) || ! is_array($uri)) return NULL;
266 if (isset($uri['scheme']) && $uri['scheme'] !== '') {
267 $tmp[] = & $uri['scheme'];
270 if (isset($uri['userinfo']) && $uri['userinfo'] !== '') {
271 $tmp[] = & $uri['userinfo'];
274 if (isset($uri['host']) && $uri['host'] !== '') {
275 $tmp[] = & $uri['host'];
277 if (isset($uri['port']) && $uri['port'] !== '') {
279 $tmp[] = & $uri['port'];
281 if (isset($uri['path']) && $uri['path'] !== '') {
282 $tmp[] = & $uri['path'];
284 if (isset($uri['file']) && $uri['file'] !== '') {
285 $tmp[] = & $uri['file'];
287 if (isset($uri['query']) && $uri['query'] !== '') {
289 $tmp[] = & $uri['query'];
291 if (isset($uri['fragment']) && $uri['fragment'] !== '') {
293 $tmp[] = & $uri['fragment'];
296 return implode('', $tmp);
299 // $array['something'] => $array['wanted']
300 function array_rename_keys(& $array, $keys = array('from' => 'to'), $force = FALSE, $default = '')
302 if (! is_array($array) || ! is_array($keys)) return FALSE;
304 // Nondestructive test
306 foreach(array_keys($keys) as $from)
307 if (! isset($array[$from]))
310 foreach($keys as $from => $to) {
311 if ($from === $to) continue;
312 if (! $force || isset($array[$from])) {
313 $array[$to] = & $array[$from];
314 unset($array[$from]);
316 $array[$to] = $default;
323 // ---------------------
326 // Pickup all of markup areas
327 function area_pickup($string = '', $method = array())
330 if (empty($method)) return $area;
332 // Anchor tag pair by preg_match and preg_match_all()
334 // [OK] <a href= >Good site!</a>
335 // [OK] <a href= "#" >test</a>
336 // [OK] <a href="http://nasty.example.com">visit http://nasty.example.com/</a>
337 // [OK] <a href=\'http://nasty.example.com/\' >discount foobar</a>
338 // [NG] <a href="http://ng.example.com">visit http://ng.example.com _not_ended_
339 $regex = '#<a\b[^>]*\bhref\b[^>]*>.*?</a\b[^>]*(>)#is';
340 if (isset($method['area_anchor'])) {
342 $count = isset($method['asap']) ?
343 preg_match($regex, $string) :
344 preg_match_all($regex, $string, $areas);
345 if (! empty($count)) $area['area_anchor'] = $count;
347 if (isset($method['uri_anchor'])) {
349 preg_match_all($regex, $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
350 foreach(array_keys($areas) as $_area) {
351 $areas[$_area] = array(
352 $areas[$_area][0][1], // Area start (<a href>)
353 $areas[$_area][1][1], // Area end (</a>)
356 if (! empty($areas)) $area['uri_anchor'] = $areas;
359 // phpBB's "BBCode" pair by preg_match and preg_match_all()
361 // [OK] [url]http://nasty.example.com/[/url]
362 // [OK] [link]http://nasty.example.com/[/link]
363 // [OK] [url=http://nasty.example.com]visit http://nasty.example.com/[/url]
364 // [OK] [link http://nasty.example.com/]buy something[/link]
365 $regex = '#\[(url|link)\b[^\]]*\].*?\[/\1\b[^\]]*(\])#is';
366 if (isset($method['area_bbcode'])) {
368 $count = isset($method['asap']) ?
369 preg_match($regex, $string) :
370 preg_match_all($regex, $string, $areas, PREG_SET_ORDER);
371 if (! empty($count)) $area['area_bbcode'] = $count;
373 if (isset($method['uri_bbcode'])) {
375 preg_match_all($regex, $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
376 foreach(array_keys($areas) as $_area) {
377 $areas[$_area] = array(
378 $areas[$_area][0][1], // Area start ([url])
379 $areas[$_area][2][1], // Area end ([/url])
382 if (! empty($areas)) $area['uri_bbcode'] = $areas;
385 // Various Wiki syntax
386 // [text_or_uri>text_or_uri]
387 // [text_or_uri:text_or_uri]
388 // [text_or_uri|text_or_uri]
389 // [text_or_uri->text_or_uri]
390 // [text_or_uri text_or_uri] // MediaWiki
391 // MediaWiki: [http://nasty.example.com/ visit http://nasty.example.com/]
396 // If in doubt, it's a little doubtful
397 // if (Area => inside <= Area) $brief += -1
398 function area_measure($areas, & $array, $belief = -1, $a_key = 'area', $o_key = 'offset')
400 if (! is_array($areas) || ! is_array($array)) return;
402 $areas_keys = array_keys($areas);
403 foreach(array_keys($array) as $u_index) {
404 $offset = isset($array[$u_index][$o_key]) ?
405 intval($array[$u_index][$o_key]) : 0;
406 foreach($areas_keys as $a_index) {
407 if (isset($array[$u_index][$a_key])) {
408 $offset_s = intval($areas[$a_index][0]);
409 $offset_e = intval($areas[$a_index][1]);
410 // [Area => inside <= Area]
411 if ($offset_s < $offset && $offset < $offset_e) {
412 $array[$u_index][$a_key] += $belief;
419 // ---------------------
422 // Domain exposure callback (See spam_uri_pickup_preprocess())
423 // http://victim.example.org/?foo+site:nasty.example.com+bar
424 // => http://nasty.example.com/?refer=victim.example.org
425 // NOTE: 'refer=' is not so good for (at this time).
426 // Consider about using IP address of the victim, try to avoid that.
427 function _preg_replace_callback_domain_exposure($matches = array())
431 // Preserve the victim URI as a complicity or ...
432 if (isset($matches[5])) {
434 $matches[1] . '://' . // scheme
435 $matches[2] . '/' . // victim.example.org
436 $matches[3]; // The rest of all (before victim)
440 if (isset($matches[4])) {
442 $matches[1] . '://' . // scheme
443 $matches[4] . // nasty.example.com
444 '/?refer=' . strtolower($matches[2]) . // victim.example.org
451 // Preprocess: rawurldecode() and adding space(s) and something
452 // to detect/count some URIs _if possible_
453 // NOTE: It's maybe danger to var_dump(result). [e.g. 'javascript:']
454 // [OK] http://victim.example.org/?site:nasty.example.org
455 // [OK] http://victim.example.org/nasty.example.org
456 // [OK] http://victim.example.org/go?http%3A%2F%2Fnasty.example.org
457 // [OK] http://victim.example.org/http://nasty.example.org
458 function spam_uri_pickup_preprocess($string = '')
460 if (! is_string($string)) return '';
462 $string = rawurldecode($string);
464 // Domain exposure (simple)
465 // http://victim.example.org/nasty.example.org/path#frag
466 // => http://nasty.example.org/?refer=victim.example.org and original
467 $string = preg_replace(
470 'ime\.nu' . '|' . // 2ch.net
471 'ime\.st' . '|' . // 2ch.net
472 'link\.toolbot\.com' . '|' .
475 '/([a-z0-9.%_-]+\.[a-z0-9.%_-]+)#i', // nasty.example.org
476 'http://$2/?refer=$1 $0', // Preserve $0 or remove?
480 // Domain exposure (gate-big5)
481 // http://victim.example.org/gate/big5/nasty.example.org/path
482 // => http://nasty.example.org/?refer=victim.example.org and original
483 $string = preg_replace(
486 'big5.51job.com' . '|' .
487 'big5.china.com' . '|' .
488 'big5.xinhuanet.com' . '|' .
491 '/([a-z0-9.%_-]+\.[a-z0-9.%_-]+)' .
492 '#i', // nasty.example.org
493 'http://$2/?refer=$1 $0', // Preserve $0 or remove?
497 // Domain exposure (See _preg_replace_callback_domain_exposure())
498 $string = preg_replace_callback(
502 // Something Google: http://www.google.com/supported_domains
503 '(?:[a-z0-9.]+\.)?google\.[a-z]{2,3}(?:\.[a-z]{2})?' .
506 '(?:[a-z0-9.]+\.)?altavista.com' .
510 '([a-z0-9?=&.%_/\'\\\+-]+)' . // path/?query=foo+bar+
511 '\bsite:([a-z0-9.%_-]+\.[a-z0-9.%_-]+)' . // site:nasty.example.com
512 //'()' . // Preserve or remove?
515 '_preg_replace_callback_domain_exposure',
519 // URI exposure (uriuri => uri uri)
520 $string = preg_replace(
522 '#(?<! )(?:https?|ftp):/#i',
523 // '#[a-z][a-z0-9.+-]{1,8}://#i',
524 // '#[a-z][a-z0-9.+-]{1,8}://#i'
533 // Main function of spam-uri pickup,
534 // A wrapper function of uri_pickup()
535 function spam_uri_pickup($string = '', $method = array())
537 if (! is_array($method) || empty($method)) {
538 $method = check_uri_spam_method();
541 $string = spam_uri_pickup_preprocess($string);
543 $array = uri_pickup($string);
545 // Area elevation of URIs, for '(especially external)link' intension
546 if (! empty($array)) {
548 if (isset($method['uri_anchor'])) $_method['uri_anchor'] = & $method['uri_anchor'];
549 if (isset($method['uri_bbcode'])) $_method['uri_bbcode'] = & $method['uri_bbcode'];
550 $areas = area_pickup($string, $_method, TRUE);
551 if (! empty($areas)) {
552 $area_shadow = array();
553 foreach (array_keys($array) as $key) {
554 $area_shadow[$key] = & $array[$key]['area'];
555 foreach (array_keys($_method) as $_key) {
556 $area_shadow[$key][$_key] = 0;
559 foreach (array_keys($_method) as $_key) {
560 if (isset($areas[$_key])) {
561 area_measure($areas[$_key], $area_shadow, 1, $_key);
567 // Remove 'offset's for area_measure()
568 foreach(array_keys($array) as $key)
569 unset($array[$key]['area']['offset']);
575 // ---------------------
578 // Scheme normalization: Renaming the schemes
579 // snntp://example.org => nntps://example.org
580 // NOTE: Keep the static lists simple. See also port_normalize().
581 function scheme_normalize($scheme = '', $abbrevs_harmfull = TRUE)
583 // Abbreviations they have no intention of link
584 static $abbrevs = array(
589 // Aliases => normalized ones
590 static $aliases = array(
600 if (! is_string($scheme)) return '';
602 $scheme = strtolower($scheme);
603 if (isset($abbrevs[$scheme])) {
604 $scheme = $abbrevs_harmfull ? $abbrevs[$scheme] : '';
606 if (isset($aliases[$scheme])) {
607 $scheme = $aliases[$scheme];
613 // Hostname normlization (Destructive)
614 // www.foo => www.foo ('foo' seems TLD)
615 // www.foo.bar => foo.bar
616 // www.10.20 => www.10.20 (Invalid hostname)
618 // 'www' is mostly used as traditional hostname of WWW server.
619 // 'www.foo.bar' may be identical with 'foo.bar'.
620 function host_normalize($host = '')
622 if (! is_string($host)) return '';
624 $host = strtolower($host);
626 if (preg_match('/^www\.(.+\.[a-z]+)$/', $host, $matches)) {
633 // Port normalization: Suppress the (redundant) default port
634 // HTTP://example.org:80/ => http://example.org/
635 // HTTP://example.org:8080/ => http://example.org:8080/
636 // HTTPS://example.org:443/ => https://example.org/
637 function port_normalize($port, $scheme, $scheme_normalize = FALSE)
639 // Schemes that users _maybe_ want to add protocol-handlers
640 // to their web browsers. (and attackers _maybe_ want to use ...)
641 // Reference: http://www.iana.org/assignments/port-numbers
642 static $array = array(
643 // scheme => default port
669 // intval() converts '0-1' to '0', so preg_match() rejects these invalid ones
670 if (! is_numeric($port) || $port < 0 || preg_match('/[^0-9]/i', $port))
673 $port = intval($port);
674 if ($scheme_normalize) $scheme = scheme_normalize($scheme);
675 if (isset($array[$scheme]) && $port == $array[$scheme])
676 $port = ''; // Ignore the defaults
681 // Path normalization
682 // http://example.org => http://example.org/
683 // http://example.org#hoge => http://example.org/#hoge
684 // http://example.org/path/a/b/./c////./d => http://example.org/path/a/b/c/d
685 // http://example.org/path/../../a/../back => http://example.org/back
686 function path_normalize($path = '', $divider = '/', $add_root = TRUE)
688 if (! is_string($divider)) return is_string($path) ? $path : '';
691 $first_div = & $divider;
695 if (! is_string($path) || $path == '') return $first_div;
697 if (strpos($path, $divider, strlen($path) - strlen($divider)) === FALSE) {
700 $last_div = & $divider;
703 $array = explode($divider, $path);
705 // Remove paddings ('//' and '/./')
706 foreach(array_keys($array) as $key) {
707 if ($array[$key] == '' || $array[$key] == '.') {
712 // Remove back-tracks ('/../')
714 foreach($array as $value) {
715 if ($value == '..') {
718 array_push($tmp, $value);
726 return $first_div . implode($divider, $array) . $last_div;
730 // DirectoryIndex normalize (Destructive and rough)
731 // TODO: sample.en.ja.html.gz => sample.html
732 function file_normalize($file = 'index.html.en')
734 static $simple_defaults = array(
735 'default.htm' => TRUE,
736 'default.html' => TRUE,
737 'default.asp' => TRUE,
738 'default.aspx' => TRUE,
739 'index' => TRUE, // Some system can omit the suffix
742 static $content_suffix = array(
743 // index.xxx, sample.xxx
758 static $language_suffix = array(
759 // Reference: Apache 2.0.59 'AddLanguage' default
788 // Reference: Apache 2.0.59 default 'index.html' variants
794 static $charset_suffix = array(
795 // Reference: Apache 2.0.59 'AddCharset' default
796 'iso8859-1' => TRUE, // ISO-8859-1
797 'latin1' => TRUE, // ISO-8859-1
798 'iso8859-2' => TRUE, // ISO-8859-2
799 'latin2' => TRUE, // ISO-8859-2
800 'cen' => TRUE, // ISO-8859-2
801 'iso8859-3' => TRUE, // ISO-8859-3
802 'latin3' => TRUE, // ISO-8859-3
803 'iso8859-4' => TRUE, // ISO-8859-4
804 'latin4' => TRUE, // ISO-8859-4
805 'iso8859-5' => TRUE, // ISO-8859-5
806 'latin5' => TRUE, // ISO-8859-5
807 'cyr' => TRUE, // ISO-8859-5
808 'iso-ru' => TRUE, // ISO-8859-5
809 'iso8859-6' => TRUE, // ISO-8859-6
810 'latin6' => TRUE, // ISO-8859-6
811 'arb' => TRUE, // ISO-8859-6
812 'iso8859-7' => TRUE, // ISO-8859-7
813 'latin7' => TRUE, // ISO-8859-7
814 'grk' => TRUE, // ISO-8859-7
815 'iso8859-8' => TRUE, // ISO-8859-8
816 'latin8' => TRUE, // ISO-8859-8
817 'heb' => TRUE, // ISO-8859-8
818 'iso8859-9' => TRUE, // ISO-8859-9
819 'latin9' => TRUE, // ISO-8859-9
820 'trk' => TRUE, // ISO-8859-9
821 'iso2022-jp'=> TRUE, // ISO-2022-JP
822 'jis' => TRUE, // ISO-2022-JP
823 'iso2022-kr'=> TRUE, // ISO-2022-KR
824 'kis' => TRUE, // ISO-2022-KR
825 'iso2022-cn'=> TRUE, // ISO-2022-CN
826 'cis' => TRUE, // ISO-2022-CN
828 'cp-1251' => TRUE, // ru, WINDOWS-1251
829 'win-1251' => TRUE, // ru, WINDOWS-1251
830 'cp866' => TRUE, // ru
831 'koi8-r' => TRUE, // ru, KOI8-r
832 'koi8-ru' => TRUE, // ru, KOI8-r
833 'koi8-uk' => TRUE, // ru, KOI8-ru
834 'ua' => TRUE, // ru, KOI8-ru
835 'ucs2' => TRUE, // ru, ISO-10646-UCS-2
836 'ucs4' => TRUE, // ru, ISO-10646-UCS-4
839 // Reference: Apache 2.0.59 default 'index.html' variants
844 // May uncompress by web browsers on the fly
845 // Must be at the last of the filename
846 // Reference: Apache 2.0.59 'AddEncoding'
847 static $encoding_suffix = array(
852 if (! is_string($file)) return '';
853 $_file = strtolower($file);
854 if (isset($simple_defaults[$_file])) return '';
857 // Roughly removing language/character-set/encoding suffixes
859 // * Apache 2 document about 'Content-negotiaton', 'mod_mime' and 'mod_negotiation'
860 // http://httpd.apache.org/docs/2.0/content-negotiation.html
861 // http://httpd.apache.org/docs/2.0/mod/mod_mime.html
862 // http://httpd.apache.org/docs/2.0/mod/mod_negotiation.html
863 // * http://www.iana.org/assignments/character-sets
864 // * RFC3066: Tags for the Identification of Languages
865 // http://www.ietf.org/rfc/rfc3066.txt
866 // * ISO 639: codes of 'language names'
867 $suffixes = explode('.', $_file);
868 $body = array_shift($suffixes);
870 // Remove the last .gz/.z
871 $last_key = end(array_keys($suffixes));
872 if (isset($encoding_suffix[$suffixes[$last_key]])) {
873 unset($suffixes[$last_key]);
876 // Cut language and charset suffixes
877 foreach($suffixes as $key => $value){
878 if (isset($language_suffix[$value]) || isset($charset_suffix[$value])) {
879 unset($suffixes[$key]);
882 if (empty($suffixes)) return $body;
885 $count = count($suffixes);
887 $current = current($suffixes);
888 if ($body == 'index' && $count == 1 && isset($content_suffix[$current])) return '';
893 // Sort query-strings if possible (Destructive and rough)
894 // [OK] &&&&f=d&b&d&c&a=0dd => a=0dd&b&c&d&f=d
895 // [OK] nothing==&eg=dummy&eg=padding&eg=foobar => eg=foobar
896 function query_normalize($string = '', $equal = TRUE, $equal_cutempty = TRUE, $stortolower = TRUE)
898 if (! is_string($string)) return '';
899 if ($stortolower) $string = strtolower($string);
901 $array = explode('&', $string);
903 // Remove '&' paddings
904 foreach(array_keys($array) as $key) {
905 if ($array[$key] == '') {
910 // Consider '='-sepalated input and paddings
912 $equals = $not_equals = array();
913 foreach ($array as $part) {
914 if (strpos($part, '=') === FALSE) {
915 $not_equals[] = $part;
917 list($key, $value) = explode('=', $part, 2);
918 $value = ltrim($value, '=');
919 if (! $equal_cutempty || $value != '') {
920 $equals[$key] = $value;
925 $array = & $not_equals;
926 foreach ($equals as $key => $value) {
927 $array[] = $key . '=' . $value;
933 return implode('&', $array);
936 // ---------------------
937 // Part One : Checker
939 // Rough implementation of globbing
941 // USAGE: $regex = '/^' . generate_glob_regex('*.txt', '/') . '$/i';
943 function generate_glob_regex($string = '', $divider = '/')
945 static $from = array(
948 // 22 => '[', // Maybe cause regex compilation error (e.g. '[]')
964 if (! is_string($string)) return '';
966 $string = str_replace($from, $mid, $string); // Hide
967 $string = preg_quote($string, $divider);
968 $string = str_replace($mid, $to, $string); // Unhide
973 // Rough hostname checker
975 // TODO: Strict digit, 0x, CIDR, IPv6
976 function is_ip($string = '')
978 if (preg_match('/^' .
979 '(?:[0-9]{1,3}\.){3}[0-9]{1,3}' . '|' .
980 '(?:[0-9]{1,3}\.){1,3}' . '$/',
982 return 4; // Seems IPv4(dot-decimal)
984 return 0; // Seems not IP
988 // Generate host (FQDN, IPv4, ...) regex
989 // 'localhost' : Matches with 'localhost' only
990 // 'example.org' : Matches with 'example.org' only (See host_normalize() about 'www')
991 // '.example.org' : Matches with ALL FQDN ended with '.example.org'
992 // '*.example.org' : Almost the same of '.example.org' except 'www.example.org'
993 // '10.20.30.40' : Matches with IPv4 address '10.20.30.40' only
994 // [TODO] '192.' : Matches with all IPv4 hosts started with '192.'
995 // TODO: IPv4, CIDR?, IPv6
996 function generate_host_regex($string = '', $divider = '/')
998 if (! is_string($string)) return '';
1000 if (mb_strpos($string, '.') === FALSE)
1001 return generate_glob_regex($string, $divider);
1004 if (is_ip($string)) {
1006 return generate_glob_regex($string, $divider);
1008 // FQDN or something
1009 $part = explode('.', $string, 2);
1010 if ($part[0] == '') {
1011 $part[0] = '(?:.*\.)?'; // And all related FQDN
1012 } else if ($part[0] == '*') {
1013 $part[0] = '.*\.'; // All subdomains/hosts only
1015 return generate_glob_regex($string, $divider);
1017 $part[1] = generate_glob_regex($part[1], $divider);
1018 return implode('', $part);
1022 function get_blocklist($list = '')
1026 if ($list === NULL) {
1027 $regexes = NULL; // Unset
1031 if (! isset($regexes)) {
1033 if (file_exists(SPAM_INI_FILE)) {
1034 $blocklist = array();
1035 include(SPAM_INI_FILE);
1036 // $blocklist['badhost'] = array(
1037 // '*.blogspot.com', // Blog services's subdomains (only)
1038 // 'IANA-examples' => '#^(?:.*\.)?example\.(?:com|net|org)$#',
1040 if (isset($blocklist['list'])) {
1041 $regexes['list'] = & $blocklist['list'];
1044 $blocklist['list'] = array(
1045 'goodhost' => FALSE,
1049 foreach(array_keys($blocklist['list']) as $_list) {
1050 if (! isset($blocklist[$_list])) continue;
1051 foreach ($blocklist[$_list] as $key => $value) {
1052 if (is_array($value)) {
1053 $regexes[$_list][$key] = array();
1054 foreach($value as $_key => $_value) {
1055 get_blocklist_add($regexes[$_list][$key], $_key, $_value);
1058 get_blocklist_add($regexes[$_list], $key, $value);
1061 unset($blocklist[$_list]);
1067 return $regexes; // ALL
1068 } else if (isset($regexes[$list])) {
1069 return $regexes[$list];
1075 // Subroutine of get_blocklist()
1076 function get_blocklist_add(& $array, $key = 0, $value = '*.example.org')
1078 if (is_string($key)) {
1079 $array[$key] = & $value; // Treat $value as a regex
1081 $array[$value] = '/^' . generate_host_regex($value, '/') . '$/i';
1085 // Blocklist metrics: Separate $host, to $blocked and not blocked
1086 function blocklist_distiller(& $hosts, $keys = array('goodhost', 'badhost'), $asap = FALSE)
1088 if (! is_array($hosts)) $hosts = array($hosts);
1089 if (! is_array($keys)) $keys = array($keys);
1091 $list = get_blocklist('list');
1094 foreach($keys as $key){
1095 foreach (get_blocklist($key) as $label => $regex) {
1096 if (is_array($regex)) {
1097 foreach($regex as $_label => $_regex) {
1098 $group = preg_grep($_regex, $hosts);
1100 $hosts = array_diff($hosts, $group);
1101 $blocked[$key][$label][$_label] = $group;
1102 if ($asap && $list[$key]) break;
1106 $group = preg_grep($regex, $hosts);
1108 $hosts = array_diff($hosts, $group);
1109 $blocked[$key][$label] = $group;
1110 if ($asap && $list[$key]) break;
1119 // Default (enabled) methods and thresholds (for content insertion)
1120 function check_uri_spam_method($times = 1, $t_area = 0, $rule = TRUE)
1122 $times = intval($times);
1123 $t_area = intval($t_area);
1127 'quantity' => 8 * $times, // Allow N URIs
1128 'non_uniqhost' => 3 * $times, // Allow N duped (and normalized) Hosts
1129 //'non_uniquri'=> 3 * $times, // Allow N duped (and normalized) URIs
1132 'area_anchor' => $t_area, // Using <a href> HTML tag
1133 'area_bbcode' => $t_area, // Using [url] or [link] BBCode
1134 //'uri_anchor' => $t_area, // URI inside <a href> HTML tag
1135 //'uri_bbcode' => $t_area, // URI inside [url] or [link] BBCode
1140 //'asap' => TRUE, // Quit or return As Soon As Possible
1141 'uniqhost' => TRUE, // Show uniq host (at block notification mail)
1142 'badhost' => TRUE, // Check badhost
1148 // Remove non-$positive values
1149 foreach (array_keys($positive) as $key) {
1150 if ($positive[$key] < 0) unset($positive[$key]);
1153 return $positive + $bool;
1156 // Simple/fast spam check
1157 function check_uri_spam($target = '', $method = array())
1162 // Theme to do => Dummy, optional value, or optional array()
1164 //'uniqhost' => TRUE,
1165 //'non_uniqhost'=> 3,
1166 //'non_uniquri' => 3,
1167 //'badhost' => TRUE,
1168 //'area_anchor' => 0,
1169 //'area_bbcode' => 0,
1170 //'uri_anchor' => 0,
1171 //'uri_bbcode' => 0,
1174 // Theme => Volume found (int)
1177 // Flag. If someting defined here,
1178 // one or more spam will be included
1183 //'category' => array(
1188 // Hosts not blocked
1193 $sum = & $progress['sum'];
1194 $is_spam = & $progress['is_spam'];
1195 $progress['method'] = & $method; // Argument
1196 $blocked = & $progress['blocked'];
1197 $hosts = & $progress['hosts'];
1198 $asap = isset($method['asap']);
1201 if (! is_array($method) || empty($method)) {
1202 $method = check_uri_spam_method();
1204 foreach(array_keys($method) as $key) {
1205 if (! isset($sum[$key])) $sum[$key] = 0;
1208 if (is_array($target)) {
1209 foreach($target as $str) {
1210 if (! is_string($str)) continue;
1212 $_progress = check_uri_spam($str, $method); // Recurse
1215 $_sum = & $_progress['sum'];
1216 foreach (array_keys($_sum) as $key) {
1217 if (! isset($sum[$key])) {
1218 $sum[$key] = & $_sum[$key];
1220 $sum[$key] += $_sum[$key];
1225 $_is_spam = & $_progress['is_spam'];
1226 foreach (array_keys($_is_spam) as $key) {
1227 $is_spam[$key] = TRUE;
1230 if ($asap && $is_spam) break;
1233 $blocked = array_merge_recursive($blocked, $_progress['blocked']);
1234 $hosts = array_merge_recursive($hosts, $_progress['hosts']);
1238 $blocked = array_unique_recursive($blocked);
1239 $hosts = array_unique_recursive($hosts);
1241 // Recount $sum['badhost']
1242 $sum['badhost'] = array_count_leaves($blocked);
1247 // Area: There's HTML anchor tag
1248 if ((! $asap || ! $is_spam) && isset($method['area_anchor'])) {
1249 $key = 'area_anchor';
1250 $_asap = isset($method['asap']) ? array('asap' => TRUE) : array();
1251 $result = area_pickup($target, array($key => TRUE) + $_asap);
1253 $sum[$key] = $result[$key];
1254 if (isset($method[$key]) && $sum[$key] > $method[$key]) {
1255 $is_spam[$key] = TRUE;
1260 // Area: There's 'BBCode' linking tag
1261 if ((! $asap || ! $is_spam) && isset($method['area_bbcode'])) {
1262 $key = 'area_bbcode';
1263 $_asap = isset($method['asap']) ? array('asap' => TRUE) : array();
1264 $result = area_pickup($target, array($key => TRUE) + $_asap);
1266 $sum[$key] = $result[$key];
1267 if (isset($method[$key]) && $sum[$key] > $method[$key]) {
1268 $is_spam[$key] = TRUE;
1274 if ($asap && $is_spam) return $progress;
1277 $pickups = uri_pickup_normalize(spam_uri_pickup($target, $method));
1280 if (empty($pickups)) return $progress;
1282 // URI: Check quantity
1283 $sum['quantity'] += count($pickups);
1285 if ((! $asap || ! $is_spam) && isset($method['quantity']) &&
1286 $sum['quantity'] > $method['quantity']) {
1287 $is_spam['quantity'] = TRUE;
1290 // URI: used inside HTML anchor tag pair
1291 if ((! $asap || ! $is_spam) && isset($method['uri_anchor'])) {
1292 $key = 'uri_anchor';
1293 foreach($pickups as $pickup) {
1294 if (isset($pickup['area'][$key])) {
1295 $sum[$key] += $pickup['area'][$key];
1296 if(isset($method[$key]) &&
1297 $sum[$key] > $method[$key]) {
1298 $is_spam[$key] = TRUE;
1299 if ($asap && $is_spam) break;
1301 if ($asap && $is_spam) break;
1306 // URI: used inside 'BBCode' pair
1307 if ((! $asap || ! $is_spam) && isset($method['uri_bbcode'])) {
1308 $key = 'uri_bbcode';
1309 foreach($pickups as $pickup) {
1310 if (isset($pickup['area'][$key])) {
1311 $sum[$key] += $pickup['area'][$key];
1312 if(isset($method[$key]) &&
1313 $sum[$key] > $method[$key]) {
1314 $is_spam[$key] = TRUE;
1315 if ($asap && $is_spam) break;
1317 if ($asap && $is_spam) break;
1322 // URI: Uniqueness (and removing non-uniques)
1323 if ((! $asap || ! $is_spam) && isset($method['non_uniquri'])) {
1326 foreach (array_keys($pickups) as $key) {
1327 $uris[$key] = uri_pickup_implode($pickups[$key]);
1329 $count = count($uris);
1330 $uris = array_unique($uris);
1331 $sum['non_uniquri'] += $count - count($uris);
1332 if ($sum['non_uniquri'] > $method['non_uniquri']) {
1333 $is_spam['non_uniquri'] = TRUE;
1335 if (! $asap || ! $is_spam) {
1336 foreach (array_diff(array_keys($pickups),
1337 array_keys($uris)) as $remove) {
1338 unset($pickups[$remove]);
1345 if ($asap && $is_spam) return $progress;
1347 // Host: Uniqueness (uniq / non-uniq)
1348 foreach ($pickups as $pickup) $hosts[] = & $pickup['host'];
1349 $hosts = array_unique($hosts);
1350 $sum['uniqhost'] += count($hosts);
1351 if ((! $asap || ! $is_spam) && isset($method['non_uniqhost'])) {
1352 $sum['non_uniqhost'] = $sum['quantity'] - $sum['uniqhost'];
1353 if ($sum['non_uniqhost'] > $method['non_uniqhost']) {
1354 $is_spam['non_uniqhost'] = TRUE;
1359 if ($asap && $is_spam) return $progress;
1361 // URI: Bad host (Separate good/bad hosts from $hosts)
1362 if ((! $asap || ! $is_spam) && isset($method['badhost'])) {
1365 $list = get_blocklist('list');
1366 $blocked = blocklist_distiller($hosts, array_keys($list), $asap);
1367 foreach($list as $key=>$type){
1368 if (! $type) unset($blocked[$key]); // Ignore goodhost etc
1372 if (! empty($blocked)) $is_spam['badhost'] = TRUE;
1378 // Count leaves (A leaf = value that is not an array, or an empty array)
1379 function array_count_leaves($array = array(), $count_empty = FALSE)
1381 if (! is_array($array) || (empty($array) && $count_empty)) return 1;
1385 foreach ($array as $part) {
1386 $count += array_count_leaves($part, $count_empty);
1391 // An array-leaves to a flat array
1392 function array_flat_leaves($array, $unique = TRUE)
1394 if (! is_array($array)) return $array;
1397 foreach(array_keys($array) as $key) {
1398 if (is_array($array[$key])) {
1400 foreach(array_flat_leaves($array[$key]) as $_value) {
1404 $tmp[] = & $array[$key];
1408 return $unique ? array_values(array_unique($tmp)) : $tmp;
1411 // An array() to an array leaf
1412 function array_leaf($array = array('A', 'B', 'C.D'), $stem = FALSE, $edge = TRUE)
1414 if (! is_array($array)) return $array;
1418 foreach($array as $arg) {
1419 if (! is_string($arg) && ! is_int($arg)) continue;
1420 $tmp[$arg] = array();
1422 $tmp = & $tmp[$arg];
1425 $parent[key($parent)] = & $edge;
1427 $parent = key($parent);
1430 return $leaf; // array('A' => array('B' => 'C.D'))
1434 // ---------------------
1437 // Summarize $progress (blocked only)
1438 function summarize_spam_progress($progress = array(), $blockedonly = FALSE)
1441 $tmp = array_keys($progress['is_spam']);
1444 $method = & $progress['method'];
1445 if (isset($progress['sum'])) {
1446 foreach ($progress['sum'] as $key => $value) {
1447 if (isset($method[$key]) && $value) {
1448 $tmp[] = $key . '(' . $value . ')';
1454 return implode(', ', $tmp);
1457 function summarize_detail_badhost($progress = array())
1459 if (! isset($progress['blocked']) || empty($progress['blocked'])) return '';
1463 foreach($progress['blocked'] as $list => $lvalue) {
1464 foreach($lvalue as $group => $gvalue) {
1465 $flat = implode(', ', array_flat_leaves($gvalue));
1466 if ($flat === $group) {
1467 $blocked[$list][] = $flat;
1469 $blocked[$list][$group] = $flat;
1475 // From: 'A-1' => array('ie.to')
1476 // To: 'A-1' => 'ie.to'
1477 foreach($blocked as $list => $lvalue) {
1478 if (is_array($lvalue) &&
1479 count($lvalue) == 1 &&
1480 is_numeric(key($lvalue))) {
1481 $blocked[$list] = current($lvalue);
1485 return var_export_shrink($blocked, TRUE, TRUE);
1488 function summarize_detail_newtral($progress = array())
1490 if (! isset($progress['hosts']) ||
1491 ! is_array($progress['hosts']) ||
1492 empty($progress['hosts'])) return '';
1498 foreach($progress['hosts'] as $value) {
1499 $tmp[delimiter_reverse($value)] = $value;
1501 ksort($tmp, SORT_STRING);
1502 $result = count($tmp) . ' (' .implode(', ', $tmp) . ')';
1505 foreach($progress['hosts'] as $value) {
1506 $tmp = array_merge_recursive(
1508 array_leaf(explode('.', delimiter_reverse($value)), TRUE, $value)
1513 // TODO: IP address 1.2.3.4 => "0"-3-2-1 by array_shrinkbranch_leaves()
1515 array_shrinkbranch_leaves($tmp, '.', TRUE); // "domain.tld"
1516 array_joinbranch_leaf($tmp, '.', 0, TRUE);
1517 foreach($tmp as $key => $value) {
1518 if (is_array($value)) {
1519 ksort($tmp[$key], SORT_STRING);
1520 $tmp[$key] = implode(', ', array_flat_leaves($value));
1523 ksort($tmp, SORT_STRING);
1525 $result = var_export_shrink($tmp, TRUE, TRUE);
1531 // array('F' => array('B' => array('C' => array('d' => array('' => 'foobar')))))
1533 // array('F.B.C.d.' => 'foobar')
1534 function array_joinbranch_leaf(& $array, $delim = '.', $limit = 0, $reverse = FALSE)
1537 if (! is_array($array)) return $result; // Nothing to do
1539 $limit = max(0, intval($limit));
1542 foreach(array_keys($array) as $key) {
1546 $single = array($key => & $array[$key]); // Keep it single
1547 $cursor = & $single;
1548 while(is_array($cursor) && count($cursor) == 1) { // Once
1550 $kstack[] = key($cursor);
1551 $cursor = & $cursor[$kstack[$k]];
1552 if ($limit != 0 && $k == $limit) break;
1557 if ($reverse) $kstack = array_reverse($kstack);
1558 $joinkey = implode($delim, $kstack);
1560 unset($array[$key]);
1561 $array[$joinkey] = & $cursor;
1562 $result[$joinkey] = $k + 1; // Key seems not an single array => joined length
1570 // array('A' => array('B' => 'C')) to
1571 // array('A.B' => 'C')
1587 // 'A.B.C.D' => '1',
1589 function array_shrinkbranch_leaves(& $array, $delim = '.', $reverse = FALSE, $recurse = FALSE)
1592 if (! is_array($array) || empty($array)) return $result;
1594 foreach(array_keys($array) as $key) {
1595 $branch = & $array[$key];
1596 if (! is_array($branch) || empty($branch)) continue;
1598 foreach(array_keys($branch) as $bkey) {
1599 $joinkey = $reverse ?
1600 $bkey . $delim . $key :
1601 $key . $delim . $bkey;
1602 $array[$joinkey] = & $branch[$bkey];
1603 unset($array[$key]);
1609 if ($recurse && $result) {
1610 $result = array_shrinkbranch_leaves($array, $delim, $reverse, $recurse);
1613 return $result; // Tell me how many
1617 // 'berkeley' => array (
1618 // 'polisci' => array (
1619 // '' => 'polisci.berkeley.edu',
1622 // 'cmich' => array (
1624 // '' => 'rso.cmich.edu',
1629 //array_shrinkbranch_leaves($a, '.', TRUE);
1641 //array_shrinkbranch_leaves($a, '.', TRUE);
1646 // Check responsibility-root of the FQDN
1647 // 'foobar.example.co.jp' => 'example.co.jp' (.co.jp seems public)
1648 // 'foobar.example.act.edu.au' => 'example.act.edu.au' (.act.edu.au seems public)
1649 // 'foobar.example.com' => 'example.com' (.com seems public)
1650 function domain_responsibility($fqdn = 'fqdn.foo.bar.example.com', $implicit = TRUE)
1652 // Domains who have 2nd and/or 3rd level domains
1653 static $domain = array(
1655 // ccTLD Australia http://www.auda.org.au/ http://www.aunic.net/ http://www.ausregistry.com.au/
1657 // ".au Second Level Domains" http://www.auda.org.au/domains/
1662 'edu' => array( // http://www.domainname.edu.au/
1673 'act' => TRUE, // Australian Capital Territory
1674 'nt' => TRUE, // Northern Territory
1675 'nsw' => TRUE, // New South Wales
1676 'qld' => TRUE, // Queensland
1677 'sa' => TRUE, // South Australia
1678 'tas' => TRUE, // Tasmania
1679 'vic' => TRUE, // Victoria
1680 'wa' => TRUE, // Western Australia
1688 // ccTLD Japan http://jprs.co.jp/en/ http://whois.jprs.jp/en/
1690 // http://jprs.co.jp/en/jpdomain.html
1704 // Examples for 3rd level domains
1705 //'kumamoto' => array(
1706 // // http://www.pref.kumamoto.jp/link/list.asp#4
1707 // 'amakusa' => TRUE,
1708 // 'hitoyoshi' => TRUE,
1710 // 'kumamoto' => TRUE,
1720 'fukushima' => TRUE,
1723 'hiroshima' => TRUE,
1730 'kagoshima' => TRUE,
1733 'kitakyushu'=> TRUE,
1758 'tokushima' => TRUE,
1764 'yamaguchi' => TRUE,
1765 'yamanashi' => TRUE,
1769 // ccTLD Ukraine http://www.nic.net.ua/ http://whois.com.ua/
1771 'cherkassy' => TRUE, // www.cherkassy.ua
1772 'chernigov' => TRUE,
1773 'chernovtsy' => TRUE,
1780 'dnepropetrovsk' => TRUE,
1786 'ivano-frankivsk' => TRUE,
1791 'kirovograd' => TRUE,
1809 'sebastopol' => TRUE,
1817 'zaporizhzhe' => TRUE,
1823 // ccTLD United Kingdom http://www.nic.uk/
1825 // http://www.nominet.org.uk/registrants/faq/#available
1835 // "Delegated Second Level Domains" http://www.nominet.org.uk/registrants/aboutdomainnames/sld/delegated/
1846 if (! is_string($fqdn)) return '';
1849 $dcursor = & $domain;
1850 $array = array_reverse(explode('.', $fqdn));
1853 $acursor = $array[$i];
1854 if (is_array($dcursor) && isset($dcursor[$acursor])) {
1855 $result[] = & $array[$i];
1856 $dcursor = & $dcursor[$acursor];
1858 if (isset($acursor)) {
1859 $result[] = & $array[$i]; // Whois servers must know this subdomain
1866 // Implicit responsibility: Top-Level-Domains must not be yours
1867 // 'bar.foo.something' => 'foo.something'
1868 if ($implicit && count($result) == 1 && count($array) > 1) {
1869 $result[] = & $array[1];
1872 return $result ? implode('.', array_reverse($result)) : '';
1876 // ---------------------
1880 function spam_dispose()
1882 get_blocklist(NULL);
1885 // Common bahavior for blocking
1886 // NOTE: Call this function from various blocking feature, to disgueise the reason 'why blocked'
1887 function spam_exit($mode = '', $data = array())
1896 echo('<pre>' . "\n");
1897 echo htmlspecialchars(var_export($data, TRUE));
1898 echo('</pre>' . "\n");
1902 if ($exit) exit; // Force exit
1906 // ---------------------
1909 // TODO: Record them
1910 // Simple/fast spam filter ($target: 'a string' or an array())
1911 function pkwk_spamfilter($action, $page, $target = array('title' => ''), $method = array(), $exitmode = '')
1913 $progress = check_uri_spam($target, $method);
1915 if (empty($progress['is_spam'])) {
1918 $target = string($target, 0); // Removing "\0" etc
1919 pkwk_spamnotify($action, $page, $target, $progress, $method);
1920 spam_exit($exitmode, $progress);
1924 // ---------------------
1925 // PukiWiki original
1927 // Mail to administrator(s)
1928 function pkwk_spamnotify($action, $page, $target = array('title' => ''), $progress = array(), $method = array())
1930 global $notify, $notify_subject;
1932 if (! $notify) return;
1934 $asap = isset($method['asap']);
1936 $summary['ACTION'] = 'Blocked by: ' . summarize_spam_progress($progress, TRUE);
1938 $summary['METRICS'] = summarize_spam_progress($progress);
1941 $tmp = summarize_detail_badhost($progress);
1942 if ($tmp != '') $summary['DETAIL_BADHOST'] = $tmp;
1944 $tmp = summarize_detail_newtral($progress);
1945 if (! $asap && $tmp != '') $summary['DETAIL_NEUTRAL_HOST'] = $tmp;
1947 $summary['COMMENT'] = $action;
1948 $summary['PAGE'] = '[blocked] ' . (is_pagename($page) ? $page : '');
1949 $summary['URI'] = get_script_uri() . '?' . rawurlencode($page);
1950 $summary['USER_AGENT'] = TRUE;
1951 $summary['REMOTE_ADDR'] = TRUE;
1952 pkwk_mail_notify($notify_subject, var_export($target, TRUE), $summary, TRUE);