2 // $Id: spam.php,v 1.90 2007/01/03 07:05:59 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
7 // (PHP 4 >= 4.3.0): preg_match_all(PREG_OFFSET_CAPTURE): $method['uri_XXX'] related feature
9 if (! defined('SPAM_INI_FILE')) define('SPAM_INI_FILE', 'spam.ini.php');
11 // ---------------------
14 // (PHP 4 >= 4.2.0): var_export(): mail-reporting and dump related
15 if (! function_exists('var_export')) {
16 function var_export() {
17 return 'var_export() is not found' . "\n";
21 // ---------------------
24 // Return an array of URIs in the $string
25 // [OK] http://nasty.example.org#nasty_string
26 // [OK] http://nasty.example.org:80/foo/xxx#nasty_string/bar
27 // [OK] ftp://nasty.example.org:80/dfsdfs
28 // [OK] ftp://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htm (from RFC3986)
29 function uri_pickup($string = '', $normalize = TRUE,
30 $preserve_rawuri = FALSE, $preserve_chunk = TRUE)
32 // Not available for: IDN(ignored)
35 // scheme://userinfo@host:port/path/or/pathinfo/maybefile.and?query=string#fragment
36 // Refer RFC3986 (Regex below is not strict)
37 '#(\b[a-z][a-z0-9.+-]{1,8})://' . // 1: Scheme
39 '([^\s<>"\'\[\]/\#?@]*)' . // 2: Userinfo (Username)
43 '\[[0-9a-f:.]+\]' . '|' . // IPv6([colon-hex and dot]): RFC2732
44 '(?:[0-9]{1-3}\.){3}[0-9]{1-3}' . '|' . // IPv4(dot-decimal): 001.22.3.44
45 '[^\s<>"\'\[\]:/\#?&\\\]+' . // FQDN: foo.example.org
47 '(?::([0-9]*))?' . // 4: Port
48 '((?:/+[^\s<>"\'\[\]/\#]+)*/+)?' . // 5: Directory path or path-info
49 '([^\s<>"\'\[\]\#?]+)?' . // 6: File?
50 '(?:\?([^\s<>"\'\[\]\#]+))?' . // 7: Query string
51 '(?:\#([a-z0-9._~%!$&\'()*+,;=:@-]*))?' . // 8: Fragment
53 $string, $array, PREG_SET_ORDER | PREG_OFFSET_CAPTURE
57 static $parts = array(
58 1 => 'scheme', 2 => 'userinfo', 3 => 'host', 4 => 'port',
59 5 => 'path', 6 => 'file', 7 => 'query', 8 => 'fragment'
62 foreach(array_keys($array) as $uri) {
63 $_uri = & $array[$uri];
64 array_rename_keys($_uri, $parts, TRUE, $default);
66 $offset = $_uri['scheme'][1]; // Scheme's offset
67 foreach(array_keys($_uri) as $part) {
68 // Remove offsets for each part
69 $_uri[$part] = & $_uri[$part][0];
73 $_uri['scheme'] = scheme_normalize($_uri['scheme']);
74 if ($_uri['scheme'] === '') {
78 $_uri['host'] = strtolower($_uri['host']);
79 $_uri['port'] = port_normalize($_uri['port'], $_uri['scheme'], FALSE);
80 $_uri['path'] = path_normalize($_uri['path']);
81 if ($preserve_rawuri) $_uri['rawuri'] = & $_uri[0];
84 //$_uri['uri'] = uri_array_implode($_uri);
86 $_uri['uri'] = & $_uri[0]; // Raw
88 unset($_uri[0]); // Matched string itself
89 if (! $preserve_chunk) {
102 // Area offset for area_measure()
103 $_uri['area']['offset'] = $offset;
109 // Destructive normalize of URI array
110 // NOTE: Give me the uri_pickup() result with chunks
111 function uri_array_normalize(& $pickups, $preserve = TRUE)
113 if (! is_array($pickups)) return $pickups;
115 foreach (array_keys($pickups) as $key) {
116 $_key = & $pickups[$key];
117 $_key['path'] = isset($_key['path']) ? strtolower($_key['path']) : '';
118 $_key['file'] = isset($_key['file']) ? file_normalize($_key['file']) : '';
119 $_key['query'] = isset($_key['query']) ? query_normalize(strtolower($_key['query']), TRUE) : '';
120 $_key['fragment'] = (isset($_key['fragment']) && $preserve) ?
121 strtolower($_key['fragment']) : ''; // Just ignore
127 // An URI array => An URI (See uri_pickup())
128 function uri_array_implode($uri = array())
130 if (empty($uri) || ! is_array($uri)) return NULL;
133 if (isset($uri['scheme']) && $uri['scheme'] !== '') {
134 $tmp[] = & $uri['scheme'];
137 if (isset($uri['userinfo']) && $uri['userinfo'] !== '') {
138 $tmp[] = & $uri['userinfo'];
141 if (isset($uri['host']) && $uri['host'] !== '') {
142 $tmp[] = & $uri['host'];
144 if (isset($uri['port']) && $uri['port'] !== '') {
146 $tmp[] = & $uri['port'];
148 if (isset($uri['path']) && $uri['path'] !== '') {
149 $tmp[] = & $uri['path'];
151 if (isset($uri['file']) && $uri['file'] !== '') {
152 $tmp[] = & $uri['file'];
154 if (isset($uri['query']) && $uri['query'] !== '') {
156 $tmp[] = & $uri['query'];
158 if (isset($uri['fragment']) && $uri['fragment'] !== '') {
160 $tmp[] = & $uri['fragment'];
163 return implode('', $tmp);
166 // $array['something'] => $array['wanted']
167 function array_rename_keys(& $array, $keys = array('from' => 'to'), $force = FALSE, $default = '')
169 if (! is_array($array) || ! is_array($keys)) return FALSE;
171 // Nondestructive test
173 foreach(array_keys($keys) as $from)
174 if (! isset($array[$from]))
177 foreach($keys as $from => $to) {
178 if ($from === $to) continue;
179 if (! $force || isset($array[$from])) {
180 $array[$to] = & $array[$from];
181 unset($array[$from]);
183 $array[$to] = $default;
190 // ---------------------
193 // Pickup all of markup areas
194 function area_pickup($string = '', $method = array())
197 if (empty($method)) return $area;
199 // Anchor tag pair by preg_match and preg_match_all()
201 // [OK] <a href= >Good site!</a>
202 // [OK] <a href= "#" >test</a>
203 // [OK] <a href="http://nasty.example.com">visit http://nasty.example.com/</a>
204 // [OK] <a href=\'http://nasty.example.com/\' >discount foobar</a>
205 // [NG] <a href="http://ng.example.com">visit http://ng.example.com _not_ended_
206 $regex = '#<a\b[^>]*\bhref\b[^>]*>.*?</a\b[^>]*(>)#i';
207 if (isset($method['area_anchor'])) {
209 $count = isset($method['asap']) ?
210 preg_match($regex, $string) :
211 preg_match_all($regex, $string, $areas);
212 if (! empty($count)) $area['area_anchor'] = $count;
214 if (isset($method['uri_anchor'])) {
216 preg_match_all($regex, $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
217 foreach(array_keys($areas) as $_area) {
218 $areas[$_area] = array(
219 $areas[$_area][0][1], // Area start (<a href>)
220 $areas[$_area][1][1], // Area end (</a>)
223 if (! empty($areas)) $area['uri_anchor'] = $areas;
226 // phpBB's "BBCode" pair by preg_match and preg_match_all()
228 // [OK] [url]http://nasty.example.com/[/url]
229 // [OK] [link]http://nasty.example.com/[/link]
230 // [OK] [url=http://nasty.example.com]visit http://nasty.example.com/[/url]
231 // [OK] [link http://nasty.example.com/]buy something[/link]
232 $regex = '#\[(url|link)\b[^\]]*\].*?\[/\1\b[^\]]*(\])#i';
233 if (isset($method['area_bbcode'])) {
235 $count = isset($method['asap']) ?
236 preg_match($regex, $string) :
237 preg_match_all($regex, $string, $areas, PREG_SET_ORDER);
238 if (! empty($count)) $area['area_bbcode'] = $count;
240 if (isset($method['uri_bbcode'])) {
242 preg_match_all($regex, $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
243 foreach(array_keys($areas) as $_area) {
244 $areas[$_area] = array(
245 $areas[$_area][0][1], // Area start ([url])
246 $areas[$_area][2][1], // Area end ([/url])
249 if (! empty($areas)) $area['uri_bbcode'] = $areas;
252 // Various Wiki syntax
253 // [text_or_uri>text_or_uri]
254 // [text_or_uri:text_or_uri]
255 // [text_or_uri|text_or_uri]
256 // [text_or_uri->text_or_uri]
257 // [text_or_uri text_or_uri] // MediaWiki
258 // MediaWiki: [http://nasty.example.com/ visit http://nasty.example.com/]
263 // If in doubt, it's a little doubtful
264 // if (Area => inside <= Area) $brief += -1
265 function area_measure($areas, & $array, $belief = -1, $a_key = 'area', $o_key = 'offset')
267 if (! is_array($areas) || ! is_array($array)) return;
269 $areas_keys = array_keys($areas);
270 foreach(array_keys($array) as $u_index) {
271 $offset = isset($array[$u_index][$o_key]) ?
272 intval($array[$u_index][$o_key]) : 0;
273 foreach($areas_keys as $a_index) {
274 if (isset($array[$u_index][$a_key])) {
275 $offset_s = intval($areas[$a_index][0]);
276 $offset_e = intval($areas[$a_index][1]);
277 // [Area => inside <= Area]
278 if ($offset_s < $offset && $offset < $offset_e) {
279 $array[$u_index][$a_key] += $belief;
286 // ---------------------
289 // Domain exposure callback (See spam_uri_pickup_preprocess())
290 // http://victim.example.org/?foo+site:nasty.example.com+bar
291 // => http://nasty.example.com/?refer=victim.example.org
292 // NOTE: 'refer=' is not so good for (at this time).
293 // Consider about using IP address of the victim, try to avoid that.
294 function _preg_replace_callback_domain_exposure($matches = array())
298 // Preserve the victim URI as a complicity or ...
299 if (isset($matches[5])) {
301 $matches[1] . '://' . // scheme
302 $matches[2] . '/' . // victim.example.org
303 $matches[3]; // The rest of all (before victim)
307 if (isset($matches[4])) {
309 $matches[1] . '://' . // scheme
310 $matches[4] . // nasty.example.com
311 '/?refer=' . strtolower($matches[2]) . // victim.example.org
318 // Preprocess: rawurldecode() and adding space(s) and something
319 // to detect/count some URIs _if possible_
320 // NOTE: It's maybe danger to var_dump(result). [e.g. 'javascript:']
321 // [OK] http://victim.example.org/go?http%3A%2F%2Fnasty.example.org
322 // [OK] http://victim.example.org/http://nasty.example.org
323 function spam_uri_pickup_preprocess($string = '')
325 if (! is_string($string)) return '';
327 $string = rawurldecode($string);
329 // Domain exposure (See _preg_replace_callback_domain_exposure())
330 $string = preg_replace_callback(
332 // Something Google: http://www.google.com/supported_domains
333 '#(http)://([a-z0-9.]+\.google\.[a-z]{2,3}(?:\.[a-z]{2})?)/' .
334 '([a-z0-9?=&.%_+-]+)' . // ?query=foo+
335 '\bsite:([a-z0-9.%_-]+)' . // site:nasty.example.com
336 //'()' . // Preserve or remove?
339 '_preg_replace_callback_domain_exposure',
343 // URI exposure (uriuri => uri uri)
344 $string = preg_replace(
346 '#(?<! )(?:https?|ftp):/#i',
347 // '#[a-z][a-z0-9.+-]{1,8}://#i',
348 // '#[a-z][a-z0-9.+-]{1,8}://#i'
357 // Main function of spam-uri pickup
358 function spam_uri_pickup($string = '', $method = array())
360 if (! is_array($method) || empty($method)) {
361 $method = check_uri_spam_method();
364 $string = spam_uri_pickup_preprocess($string);
366 $array = uri_pickup($string);
368 // Area elevation of URIs, for '(especially external)link' intension
369 if (! empty($array)) {
371 if (isset($method['uri_anchor'])) $_method['uri_anchor'] = & $method['uri_anchor'];
372 if (isset($method['uri_bbcode'])) $_method['uri_bbcode'] = & $method['uri_bbcode'];
373 $areas = area_pickup($string, $_method, TRUE);
374 if (! empty($areas)) {
375 $area_shadow = array();
376 foreach (array_keys($array) as $key) {
377 $area_shadow[$key] = & $array[$key]['area'];
378 foreach (array_keys($_method) as $_key) {
379 $area_shadow[$key][$_key] = 0;
382 foreach (array_keys($_method) as $_key) {
383 if (isset($areas[$_key])) {
384 area_measure($areas[$_key], $area_shadow, 1, $_key);
390 // Remove 'offset's for area_measure()
391 foreach(array_keys($array) as $key)
392 unset($array[$key]['area']['offset']);
398 // ---------------------
401 // Scheme normalization: Renaming the schemes
402 // snntp://example.org => nntps://example.org
403 // NOTE: Keep the static lists simple. See also port_normalize().
404 function scheme_normalize($scheme = '', $considerd_harmfull = TRUE)
406 // Abbreviations considerable they don't have link intension
407 static $abbrevs = array(
412 // Alias => normalized
413 static $aliases = array(
423 $scheme = strtolower(trim($scheme));
424 if (isset($abbrevs[$scheme])) {
425 if ($considerd_harmfull) {
426 $scheme = $abbrevs[$scheme];
431 if (isset($aliases[$scheme])) $scheme = $aliases[$scheme];
436 // Port normalization: Suppress the (redundant) default port
437 // HTTP://example.org:80/ => http://example.org/
438 // HTTP://example.org:8080/ => http://example.org:8080/
439 // HTTPS://example.org:443/ => https://example.org/
440 function port_normalize($port, $scheme, $scheme_normalize = TRUE)
442 // Schemes that users _maybe_ want to add protocol-handlers
443 // to their web browsers. (and attackers _maybe_ want to use ...)
444 // Reference: http://www.iana.org/assignments/port-numbers
445 static $array = array(
446 // scheme => default port
473 if ($port === '') return $port;
475 if ($scheme_normalize) $scheme = scheme_normalize($scheme);
476 if (isset($array[$scheme]) && $port == $array[$scheme])
477 $port = ''; // Ignore the defaults
482 // Path normalization
483 // http://example.org => http://example.org/
484 // http://example.org#hoge => http://example.org/#hoge
485 // http://example.org/path/a/b/./c////./d => http://example.org/path/a/b/c/d
486 // http://example.org/path/../../a/../back => http://example.org/back
487 function path_normalize($path = '', $divider = '/', $addroot = TRUE)
489 if (! is_string($path) || $path == '')
490 return $addroot ? $divider : '';
493 $last = ($path[strlen($path) - 1] == $divider) ? $divider : '';
494 $array = explode($divider, $path);
497 foreach(array_keys($array) as $key) {
498 if ($array[$key] == '' || $array[$key] == '.')
503 foreach($array as $value) {
504 if ($value == '..') {
507 array_push($tmp, $value);
512 $path = $addroot ? $divider : '';
513 if (! empty($array)) $path .= implode($divider, $array) . $last;
518 // DirectoryIndex normalize (Destructive and rough)
519 function file_normalize($string = 'index.html.en')
521 static $array = array(
522 'index' => TRUE, // Some system can omit the suffix
524 'index.html' => TRUE,
525 'index.shtml' => TRUE,
528 'index.php3' => TRUE,
529 'index.php4' => TRUE,
530 //'index.pl' => TRUE,
531 //'index.py' => TRUE,
532 //'index.rb' => TRUE,
534 'default.htm' => TRUE,
535 'default.html' => TRUE,
536 'default.asp' => TRUE,
537 'default.aspx' => TRUE,
540 // Content-negothiation filter:
541 // Roughly removing ISO 639 -like
542 // 2-letter suffixes (See RFC3066)
544 if (preg_match('/(.*)\.[a-z][a-z](?:-[a-z][a-z])?$/i', $string, $matches)) {
545 $_string = $matches[1];
547 $_string = & $string;
550 if (isset($array[strtolower($_string)])) {
557 // Sort query-strings if possible (Destructive and rough)
558 // [OK] &&&&f=d&b&d&c&a=0dd => a=0dd&b&c&d&f=d
559 // [OK] nothing==&eg=dummy&eg=padding&eg=foobar => eg=foobar
560 function query_normalize($string = '', $equal = FALSE, $equal_cutempty = TRUE)
562 $array = explode('&', $string);
564 // Remove '&' paddings
565 foreach(array_keys($array) as $key) {
566 if ($array[$key] == '') {
571 // Consider '='-sepalated input and paddings
573 $equals = $not_equals = array();
574 foreach ($array as $part) {
575 if (strpos($part, '=') === FALSE) {
576 $not_equals[] = $part;
578 list($key, $value) = explode('=', $part, 2);
579 $value = ltrim($value, '=');
580 if (! $equal_cutempty || $value != '') {
581 $equals[$key] = $value;
586 $array = & $not_equals;
587 foreach ($equals as $key => $value) {
588 $array[] = $key . '=' . $value;
594 return implode('&', $array);
597 // ---------------------
598 // Part One : Checker
600 function generate_glob_regex($string = '', $divider = '/')
602 static $from = array(
605 // 22 => '[', // Maybe cause regex compilation error (e.g. '[]')
621 if (is_array($string)) {
625 array_map('generate_glob_regex',
627 array_pad(array(), count($string), $divider)
632 $string = str_replace($from, $mid, $string); // Hide
633 $string = preg_quote($string, $divider);
634 $string = str_replace($mid, $to, $string); // Unhide
640 // TODO: preg_grep() ?
642 function is_badhost($hosts = '', $asap = TRUE)
646 if (! isset($regex)) {
648 $regex['badhost'] = array();
652 $blocklist['badhost'] = array(
653 //'*', // Deny all uri
654 //'10.20.*.*', // 10.20.example.com also matches
655 //'*.blogspot.com', // Blog services subdomains
656 //array('blogspot.com', '*.blogspot.com')
658 foreach ($blocklist['badhost'] as $part) {
659 $_part = is_array($part) ? implode('/', $part) : $part;
660 $regex['badhost'][$_part] = '/^' . generate_glob_regex($part) . '$/i';
665 if (file_exists(SPAM_INI_FILE)) {
666 $blocklist = array();
667 require(SPAM_INI_FILE);
668 foreach ($blocklist['badhost'] as $part) {
669 $_part = is_array($part) ? implode('/', $part) : $part;
670 $regex['badhost'][$_part] = '/^' . generate_glob_regex($part) . '$/i';
676 if (! is_array($hosts)) $hosts = array($hosts);
678 foreach($hosts as $host) {
679 if (! is_string($host)) $host = '';
680 foreach ($regex['badhost'] as $part => $_regex) {
681 if (preg_match($_regex, $host)) {
682 if (! isset($result[$part])) $result[$part] = array();
683 $result[$part][] = $host;
696 // Default (enabled) methods and thresholds (for content insertion)
697 function check_uri_spam_method($times = 1, $t_area = 0, $rule = TRUE)
699 $times = intval($times);
700 $t_area = intval($t_area);
704 'quantity' => 8 * $times, // Allow N URIs
705 'non_uniqhost' => 3 * $times, // Allow N duped (and normalized) Hosts
706 'non_uniquri' => 0 * $times, // Allow N duped (and normalized) URIs
709 'area_anchor' => $t_area, // Using <a href> HTML tag
710 'area_bbcode' => $t_area, // Using [url] or [link] BBCode
711 //'uri_anchor' => $t_area, // URI inside <a href> HTML tag
712 //'uri_bbcode' => $t_area, // URI inside [url] or [link] BBCode
717 //'asap' => TRUE, // Quit or return As Soon As Possible
718 'uniqhost' => TRUE, // Show uniq host (at block notification mail)
719 'badhost' => TRUE, // Check badhost
725 // Remove non-$positive values
726 foreach (array_keys($positive) as $key) {
727 if ($positive[$key] < 0) unset($positive[$key]);
730 return $positive + $bool;
733 // Simple/fast spam check
734 function check_uri_spam($target = '', $method = array())
736 if (! is_array($method) || empty($method)) {
737 $method = check_uri_spam_method();
751 'is_spam' => array(),
752 'method' => & $method,
754 $sum = & $progress['sum'];
755 $is_spam = & $progress['is_spam'];
756 $asap = isset($method['asap']);
759 if (is_array($target)) {
760 foreach($target as $str) {
762 $_progress = check_uri_spam($str, $method);
763 $_sum = & $_progress['sum'];
764 $_is_spam = & $_progress['is_spam'];
765 foreach (array_keys($_sum) as $key) {
766 $sum[$key] += $_sum[$key];
768 foreach(array_keys($_is_spam) as $key) {
769 if (is_array($_is_spam[$key])) {
770 // Marge keys (badhost)
771 foreach(array_keys($_is_spam[$key]) as $_key) {
772 if (! isset($is_spam[$key][$_key])) {
773 $is_spam[$key][$_key] = $_is_spam[$key][$_key];
775 $is_spam[$key][$_key] += $_is_spam[$key][$_key];
779 $is_spam[$key] = TRUE;
782 if ($asap && $is_spam) break;
787 // Area: There's HTML anchor tag
788 if ((! $asap || ! $is_spam) && isset($method['area_anchor'])) {
789 $key = 'area_anchor';
790 $_asap = isset($method['asap']) ? array('asap' => TRUE) : array();
791 $result = area_pickup($target, array($key => TRUE) + $_asap);
793 $sum[$key] = $result[$key];
794 if (isset($method[$key]) && $sum[$key] > $method[$key]) {
795 $is_spam[$key] = TRUE;
800 // Area: There's 'BBCode' linking tag
801 if ((! $asap || ! $is_spam) && isset($method['area_bbcode'])) {
802 $key = 'area_bbcode';
803 $_asap = isset($method['asap']) ? array('asap' => TRUE) : array();
804 $result = area_pickup($target, array($key => TRUE) + $_asap);
806 $sum[$key] = $result[$key];
807 if (isset($method[$key]) && $sum[$key] > $method[$key]) {
808 $is_spam[$key] = TRUE;
814 if ($asap && $is_spam) {
818 $pickups = spam_uri_pickup($target, $method);
819 if (empty($pickups)) {
823 // URI: Check quantity
824 $sum['quantity'] += count($pickups);
826 if ((! $asap || ! $is_spam) && isset($method['quantity']) &&
827 $sum['quantity'] > $method['quantity']) {
828 $is_spam['quantity'] = TRUE;
831 // URI: used inside HTML anchor tag pair
832 if ((! $asap || ! $is_spam) && isset($method['uri_anchor'])) {
834 foreach($pickups as $pickup) {
835 if (isset($pickup['area'][$key])) {
836 $sum[$key] += $pickup['area'][$key];
837 if(isset($method[$key]) &&
838 $sum[$key] > $method[$key]) {
839 $is_spam[$key] = TRUE;
840 if ($asap && $is_spam) break;
842 if ($asap && $is_spam) break;
847 // URI: used inside 'BBCode' pair
848 if ((! $asap || ! $is_spam) && isset($method['uri_bbcode'])) {
850 foreach($pickups as $pickup) {
851 if (isset($pickup['area'][$key])) {
852 $sum[$key] += $pickup['area'][$key];
853 if(isset($method[$key]) &&
854 $sum[$key] > $method[$key]) {
855 $is_spam[$key] = TRUE;
856 if ($asap && $is_spam) break;
858 if ($asap && $is_spam) break;
863 // URI: Uniqueness (and removing non-uniques)
864 if ((! $asap || ! $is_spam) && isset($method['non_uniquri'])) {
866 // Destructive normalize of URIs
867 uri_array_normalize($pickups);
870 foreach (array_keys($pickups) as $key) {
871 $uris[$key] = uri_array_implode($pickups[$key]);
873 $count = count($uris);
874 $uris = array_unique($uris);
875 $sum['non_uniquri'] += $count - count($uris);
876 if ($sum['non_uniquri'] > $method['non_uniquri']) {
877 $is_spam['non_uniquri'] = TRUE;
879 if (! $asap || ! $is_spam) {
880 foreach (array_diff(array_keys($pickups),
881 array_keys($uris)) as $remove) {
882 unset($pickups[$remove]);
889 if ($asap && $is_spam) {
893 // Host: Uniqueness (uniq / non-uniq)
895 foreach ($pickups as $pickup) $hosts[] = & $pickup['host'];
896 $hosts = array_unique($hosts);
897 $sum['uniqhost'] += count($hosts);
898 if ((! $asap || ! $is_spam) && isset($method['non_uniqhost'])) {
899 $sum['non_uniqhost'] = $sum['quantity'] - $sum['uniqhost'];
900 if ($sum['non_uniqhost'] > $method['non_uniqhost']) {
901 $is_spam['non_uniqhost'] = TRUE;
906 if ($asap && $is_spam) {
911 if ((! $asap || ! $is_spam) && isset($method['badhost'])) {
912 $badhost = is_badhost($hosts, $asap);
913 if (! empty($badhost)) {
914 $sum['badhost'] += array_count_leaves($badhost);
915 foreach(array_keys($badhost) as $keys) {
916 $is_spam['badhost'][$keys] =
917 array_count_leaves($badhost[$keys]);
927 function array_count_leaves($array = array(), $count_empty_array = FALSE)
929 if (! is_array($array) || (empty($array) && $count_empty_array))
934 foreach ($array as $part) {
935 $result += array_count_leaves($part, $count_empty_array);
940 // ---------------------
943 // TODO: Don't show unused $method!
944 // Summarize $progress (blocked only)
945 function summarize_spam_progress($progress = array(), $blockedonly = FALSE)
948 $tmp = array_keys($progress['is_spam']);
951 $method = & $progress['method'];
952 if (isset($progress['sum'])) {
953 foreach ($progress['sum'] as $key => $value) {
954 if (isset($method[$key])) {
955 $tmp[] = $key . '(' . $value . ')';
961 return implode(', ', $tmp);
964 // ---------------------
967 // Common bahavior for blocking
968 // NOTE: Call this function from various blocking feature, to disgueise the reason 'why blocked'
969 function spam_exit($mode = '', $data = array())
972 case '': echo("\n"); break;
974 echo('<pre>' . "\n");
975 echo htmlspecialchars(var_export($data, TRUE));
976 echo('</pre>' . "\n");
985 // ---------------------
989 // Simple/fast spam filter ($target: 'a string' or an array())
990 function pkwk_spamfilter($action, $page, $target = array('title' => ''), $method = array(), $exitmode = '')
992 $progress = check_uri_spam($target, $method);
994 if (! empty($progress['is_spam'])) {
995 // Mail to administrator(s)
996 pkwk_spamnotify($action, $page, $target, $progress, $method);
999 spam_exit($exitmode, $progress);
1003 // ---------------------
1004 // PukiWiki original
1006 // Mail to administrator(s)
1007 function pkwk_spamnotify($action, $page, $target = array('title' => ''), $progress = array(), $method = array())
1009 global $notify, $notify_subject;
1011 if (! $notify) return;
1013 $asap = isset($method['asap']);
1015 $summary['ACTION'] = 'Blocked by: ' . summarize_spam_progress($progress, TRUE);
1017 $summary['METRICS'] = summarize_spam_progress($progress);
1019 if (isset($progress['is_spam']['badhost'])) {
1021 foreach($progress['is_spam']['badhost'] as $glob=>$number) {
1022 $badhost[] = $glob . '(' . $number . ')';
1024 $summary['DETAIL_BADHOST'] = implode(', ', $badhost);
1026 $summary['COMMENT'] = $action;
1027 $summary['PAGE'] = '[blocked] ' . (is_pagename($page) ? $page : '');
1028 $summary['URI'] = get_script_uri() . '?' . rawurlencode($page);
1029 $summary['USER_AGENT'] = TRUE;
1030 $summary['REMOTE_ADDR'] = TRUE;
1031 pkwk_mail_notify($notify_subject, var_export($target, TRUE), $summary);