2 // $Id: spam.php,v 1.206 2008/12/27 15:11:37 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');
11 if (! defined('DOMAIN_INI_FILE')) define('DOMAIN_INI_FILE', 'domain.ini.php');
13 // ---------------------
16 // (PHP 4 >= 4.2.0): var_export(): mail-reporting and dump related
17 if (! function_exists('var_export')) {
18 function var_export() {
19 return 'var_export() is not found on this server' . "\n";
23 // (PHP 4 >= 4.2.0): preg_grep() enables invert option
24 function preg_grep_invert($pattern = '//', $input = array())
27 if (! isset($invert)) $invert = defined('PREG_GREP_INVERT');
30 return preg_grep($pattern, $input, PREG_GREP_INVERT);
32 $result = preg_grep($pattern, $input);
34 return array_diff($input, preg_grep($pattern, $input));
42 // ---------------------
45 // Very roughly, shrink the lines of var_export()
46 // NOTE: If the same data exists, it must be corrupted.
47 function var_export_shrink($expression, $return = FALSE, $ignore_numeric_keys = FALSE)
49 $result = var_export($expression, TRUE);
51 $result = preg_replace(
52 // Remove a newline and spaces
53 '# => \n *array \(#', ' => array (',
57 if ($ignore_numeric_keys) {
58 $result =preg_replace(
59 // Remove numeric keys
60 '#^( *)[0-9]+ => #m', '$1',
73 // Data structure: Create an array they _refer_only_one_ value
74 function one_value_array($num = 0, $value = NULL)
76 $num = max(0, intval($num));
79 for ($i = 0; $i < $num; $i++) {
86 // Reverse $string with specified delimiter
87 function delimiter_reverse($string = 'foo.bar.example.com', $from_delim = '.', $to_delim = NULL)
89 $to_null = ($to_delim === NULL);
91 if (! is_string($from_delim) || (! $to_null && ! is_string($to_delim))) {
94 if (is_array($string)) {
96 $count = count($string);
97 $from = one_value_array($count, $from_delim);
99 // Note: array_map() vanishes all keys
100 return array_map('delimiter_reverse', $string, $from);
102 $to = one_value_array($count, $to_delim);
103 // Note: array_map() vanishes all keys
104 return array_map('delimiter_reverse', $string, $from, $to);
107 if (! is_string($string)) {
111 // Returns com.example.bar.foo
112 if ($to_null) $to_delim = & $from_delim;
113 return implode($to_delim, array_reverse(explode($from_delim, $string)));
117 function ksort_by_domain(& $array)
120 foreach(array_keys($array) as $key) {
121 $reversed = delimiter_reverse($key);
122 if ($reversed !== FALSE) {
123 $sort[$reversed] = $key;
126 ksort($sort, SORT_STRING);
129 foreach($sort as $key) {
130 $result[$key] = & $array[$key];
136 // Roughly strings(1) using PCRE
137 // This function is useful to:
138 // * Reduce the size of data, from removing unprintable binary data
139 // * Detect _bare_strings_ from binary data
141 // http://www.freebsd.org/cgi/man.cgi?query=strings (Man-page of GNU strings)
142 // http://www.pcre.org/pcre.txt
143 // Note: mb_ereg_replace() is one of mbstring extension's functions
144 // and need to init its encoding.
145 function strings($binary = '', $min_len = 4, $ignore_space = FALSE, $multibyte = FALSE)
148 $binary = (is_array($binary) || $binary === TRUE) ? '' : strval($binary);
150 $regex = $ignore_space ?
151 '[^[:graph:] \t\n]+' : // Remove "\0" etc, and readable spaces
152 '[^[:graph:][:space:]]+'; // Preserve readable spaces if possible
154 $binary = $multibyte ?
155 mb_ereg_replace($regex, "\n", $binary) :
156 preg_replace('/' . $regex . '/s', "\n", $binary);
159 $binary = preg_replace(
174 // The last character seems "\n" or not
175 $br = (! empty($binary) && $binary[strlen($binary) - 1] == "\n") ? "\n" : '';
177 $min_len = min(1024, intval($min_len));
178 $regex = '/^.{' . $min_len . ',}/S';
179 $binary = implode("\n", preg_grep($regex, explode("\n", $binary))) . $br;
186 // ---------------------
189 // Count leaves (A leaf = value that is not an array, or an empty array)
190 function array_count_leaves($array = array(), $count_empty = FALSE)
192 if (! is_array($array) || (empty($array) && $count_empty)) return 1;
196 foreach ($array as $part) {
197 $count += array_count_leaves($part, $count_empty);
203 // Similar to PHP array_merge_leaves(), except strictly preserving keys as string
204 function array_merge_leaves($array1, $array2, $sort_keys = TRUE)
207 $is_array1 = is_array($array1);
208 $is_array2 = is_array($array2);
215 } else if ($is_array2) {
218 return $array2; // Not array ($array1 is overwritten)
221 $keys_all = array_merge(array_keys($array1), array_keys($array2));
222 if ($sort_keys) sort($keys_all, SORT_STRING);
225 foreach($keys_all as $key) {
226 $isset1 = isset($array1[$key]);
227 $isset2 = isset($array2[$key]);
228 if ($isset1 && $isset2) {
230 $result[$key] = array_merge_leaves($array1[$key], $array2[$key], $sort_keys);
231 } else if ($isset1) {
232 $result[$key] = & $array1[$key];
234 $result[$key] = & $array2[$key];
240 // An array-leaves to a flat array
241 function array_flat_leaves($array, $unique = TRUE)
243 if (! is_array($array)) return $array;
246 foreach(array_keys($array) as $key) {
247 if (is_array($array[$key])) {
249 foreach(array_flat_leaves($array[$key]) as $_value) {
253 $tmp[] = & $array[$key];
257 return $unique ? array_values(array_unique($tmp)) : $tmp;
260 // $array['something'] => $array['wanted']
261 function array_rename_keys(& $array, $keys = array('from' => 'to'), $force = FALSE, $default = '')
263 if (! is_array($array) || ! is_array($keys)) return FALSE;
265 // Nondestructive test
267 foreach(array_keys($keys) as $from) {
268 if (! isset($array[$from])) {
274 foreach($keys as $from => $to) {
275 if ($from === $to) continue;
276 if (! $force || isset($array[$from])) {
277 $array[$to] = & $array[$from];
278 unset($array[$from]);
280 $array[$to] = $default;
287 // Remove redundant values from array()
288 function array_unique_recursive($array = array())
290 if (! is_array($array)) return $array;
293 foreach($array as $key => $value){
294 if (is_array($value)) {
295 $array[$key] = array_unique_recursive($value);
297 if (isset($tmp[$value])) {
309 // ---------------------
310 // Part One : Checker
312 // Rough implementation of globbing
314 // USAGE: $regex = '/^' . generate_glob_regex('*.txt', '/') . '$/i';
316 function generate_glob_regex($string = '', $divider = '/')
318 static $from = array(
321 // 22 => '[', // Maybe cause regex compilation error (e.g. '[]')
337 if (! is_string($string)) return '';
339 $string = str_replace($from, $mid, $string); // Hide
340 $string = preg_quote($string, $divider);
341 $string = str_replace($mid, $to, $string); // Unhide
346 // Generate host (FQDN, IPv4, ...) regex
347 // 'localhost' : Matches with 'localhost' only
348 // 'example.org' : Matches with 'example.org' only (See host_normalize() about 'www')
349 // '.example.org' : Matches with ALL FQDN ended with '.example.org'
350 // '*.example.org' : Almost the same of '.example.org' except 'www.example.org'
351 // '10.20.30.40' : Matches with IPv4 address '10.20.30.40' only
352 // [TODO] '192.' : Matches with all IPv4 hosts started with '192.'
353 // TODO: IPv4, CIDR?, IPv6
354 function generate_host_regex($string = '', $divider = '/')
356 if (! is_string($string)) return '';
358 if (mb_strpos($string, '.') === FALSE) {
360 return generate_glob_regex($string, $divider);
363 if (is_ip($string)) {
365 return generate_glob_regex($string, $divider);
368 $part = explode('.', $string, 2);
369 if ($part[0] == '') {
371 $part[0] = '(?:.*\.)?';
372 } else if ($part[0] == '*') {
377 return generate_glob_regex($string, $divider);
379 $part[1] = generate_glob_regex($part[1], $divider);
380 return implode('', $part);
384 // Rough hostname checker
385 // TODO: Strict digit, 0x, CIDR, '999.999.999.999', ':', '::G'
386 function is_ip($string = '')
388 if (! is_string($string)) return FALSE;
390 if (strpos($string, ':') !== FALSE) {
391 return 6; // Seems IPv6
394 if (preg_match('/^' .
395 '(?:[0-9]{1,3}\.){3}[0-9]{1,3}' . '|' .
396 '(?:[0-9]{1,3}\.){1,3}' . '$/',
398 return 4; // Seems IPv4(dot-decimal)
401 return FALSE; // Seems not IP
404 // Load SPAM_INI_FILE and return parsed one
405 function get_blocklist($list = '')
409 if ($list === NULL) {
410 $regexes = NULL; // Unset
414 if (! isset($regexes)) {
416 if (file_exists(SPAM_INI_FILE)) {
417 $blocklist = array();
419 include(SPAM_INI_FILE);
420 // $blocklist['list'] = array(
421 // //'goodhost' => FALSE;
422 // 'badhost' => TRUE;
424 // $blocklist['badhost'] = array(
425 // '*.blogspot.com', // Blog services's subdomains (only)
426 // 'IANA-examples' => '#^(?:.*\.)?example\.(?:com|net|org)$#',
434 if (! isset($blocklist[$special])) continue;
436 $regexes[$special] = $blocklist[$special];
438 foreach(array_keys($blocklist[$special]) as $_list) {
439 if (! isset($blocklist[$_list])) continue;
441 foreach ($blocklist[$_list] as $key => $value) {
442 if (is_array($value)) {
443 $regexes[$_list][$key] = array();
444 foreach($value as $_key => $_value) {
445 get_blocklist_add($regexes[$_list][$key], $_key, $_value);
448 get_blocklist_add($regexes[$_list], $key, $value);
451 unset($blocklist[$_list]);
458 return $regexes; // ALL of
459 } else if (isset($regexes[$list])) {
460 return $regexes[$list]; // A part of
462 return array(); // Found nothing
466 // Subroutine of get_blocklist()
467 function get_blocklist_add(& $array, $key = 0, $value = '*.example.org')
469 if (is_string($key)) {
470 $array[$key] = & $value; // Treat $value as a regex for FQDN(host)s
472 $array[$value] = '/^' . generate_host_regex($value, '/') . '$/i';
476 // Blocklist metrics: Separate $host, to $blocked and not blocked
477 function blocklist_distiller(& $hosts, $keys = array('goodhost', 'badhost'), $asap = FALSE)
479 if (! is_array($hosts)) $hosts = array($hosts);
480 if (! is_array($keys)) $keys = array($keys);
482 $list = get_blocklist('list');
485 foreach($keys as $key){
486 foreach (get_blocklist($key) as $label => $regex) {
487 if (is_array($regex)) {
488 foreach($regex as $_label => $_regex) {
489 $group = preg_grep($_regex, $hosts);
491 $hosts = array_diff($hosts, $group);
492 $blocked[$key][$label][$_label] = $group;
493 if ($asap && $list[$key]) break;
497 $group = preg_grep($regex, $hosts);
499 $hosts = array_diff($hosts, $group);
500 $blocked[$key][$label] = $group;
501 if ($asap && $list[$key]) break;
511 // ---------------------
514 // Default (enabled) methods and thresholds (for content insertion)
515 function check_uri_spam_method($times = 1, $t_area = 0, $rule = TRUE)
517 $times = intval($times);
518 $t_area = intval($t_area);
522 'quantity' => 8 * $times, // Allow N URIs
523 'non_uniqhost' => 3 * $times, // Allow N duped (and normalized) Hosts
524 //'non_uniquri'=> 3 * $times, // Allow N duped (and normalized) URIs
527 'area_anchor' => $t_area, // Using <a href> HTML tag
528 'area_bbcode' => $t_area, // Using [url] or [link] BBCode
529 //'uri_anchor' => $t_area, // URI inside <a href> HTML tag
530 //'uri_bbcode' => $t_area, // URI inside [url] or [link] BBCode
535 //'asap' => TRUE, // Quit or return As Soon As Possible
536 'uniqhost' => TRUE, // Show uniq host (at block notification mail)
537 'badhost' => TRUE, // Check badhost
543 // Remove non-$positive values
544 foreach (array_keys($positive) as $key) {
545 if ($positive[$key] < 0) unset($positive[$key]);
548 return $positive + $bool;
551 // Simple/fast spam check
552 function check_uri_spam($target = '', $method = array())
557 // Theme to do => Dummy, optional value, or optional array()
559 //'uniqhost' => TRUE,
560 //'non_uniqhost'=> 3,
561 //'non_uniquri' => 3,
563 //'area_anchor' => 0,
564 //'area_bbcode' => 0,
569 // Theme => Volume found (int)
572 // Flag. If someting defined here,
573 // one or more spam will be included
578 //'category' => array(
587 // ----------------------------------------
590 $sum = & $progress['sum'];
591 $is_spam = & $progress['is_spam'];
592 $progress['method'] = & $method; // Argument
593 $blocked = & $progress['blocked'];
594 $hosts = & $progress['hosts'];
595 $asap = isset($method['asap']);
597 // ----------------------------------------
600 if (! is_array($method) || empty($method)) {
601 $method = check_uri_spam_method();
603 foreach(array_keys($method) as $key) {
604 if (! isset($sum[$key])) $sum[$key] = 0;
606 if (! isset($sum['quantity'])) $sum['quantity'] = 0;
608 // ----------------------------------------
611 if (is_array($target)) {
612 foreach($target as $str) {
613 if (! is_string($str)) continue;
615 $_progress = check_uri_spam($str, $method); // Recurse
618 $_sum = & $_progress['sum'];
619 foreach (array_keys($_sum) as $key) {
620 if (! isset($sum[$key])) {
621 $sum[$key] = & $_sum[$key];
623 $sum[$key] += $_sum[$key];
628 $_is_spam = & $_progress['is_spam'];
629 foreach (array_keys($_is_spam) as $key) {
630 $is_spam[$key] = TRUE;
633 if ($asap && $is_spam) break;
636 $blocked = array_merge_leaves($blocked, $_progress['blocked'], FALSE);
637 $hosts = array_merge_leaves($hosts, $_progress['hosts'], FALSE);
641 $blocked = array_unique_recursive($blocked);
642 $hosts = array_unique_recursive($hosts);
644 // Recount $sum['badhost']
645 $sum['badhost'] = array_count_leaves($blocked);
650 // ----------------------------------------
653 // Area: There's HTML anchor tag
654 if ((! $asap || ! $is_spam) && isset($method['area_anchor'])) {
655 $key = 'area_anchor';
656 $_asap = isset($method['asap']) ? array('asap' => TRUE) : array();
657 $result = area_pickup($target, array($key => TRUE) + $_asap);
659 $sum[$key] = $result[$key];
660 if (isset($method[$key]) && $sum[$key] > $method[$key]) {
661 $is_spam[$key] = TRUE;
666 // Area: There's 'BBCode' linking tag
667 if ((! $asap || ! $is_spam) && isset($method['area_bbcode'])) {
668 $key = 'area_bbcode';
669 $_asap = isset($method['asap']) ? array('asap' => TRUE) : array();
670 $result = area_pickup($target, array($key => TRUE) + $_asap);
672 $sum[$key] = $result[$key];
673 if (isset($method[$key]) && $sum[$key] > $method[$key]) {
674 $is_spam[$key] = TRUE;
680 if ($asap && $is_spam) return $progress;
682 // ----------------------------------------
685 $pickups = uri_pickup_normalize(spam_uri_pickup($target, $method));
687 foreach ($pickups as $key => $pickup) {
688 $hosts[$key] = & $pickup['host'];
692 if (empty($pickups)) return $progress;
694 // ----------------------------------------
695 // URI: Bad host <pre-filter> (Separate good/bad hosts from $hosts)
697 if ((! $asap || ! $is_spam) && isset($method['badhost'])) {
698 $list = get_blocklist('pre');
699 $blocked = blocklist_distiller($hosts, array_keys($list), $asap);
700 foreach($list as $key=>$type){
701 if (! $type) unset($blocked[$key]); // Ignore goodhost etc
704 if (! empty($blocked)) $is_spam['badhost'] = TRUE;
708 if ($asap && $is_spam) return $progress;
710 // Remove blocked from $pickups
711 foreach(array_keys($pickups) as $key) {
712 if (! isset($hosts[$key])) {
713 unset($pickups[$key]);
717 // ----------------------------------------
718 // URI: Check quantity
720 $sum['quantity'] += count($pickups);
722 if ((! $asap || ! $is_spam) && isset($method['quantity']) &&
723 $sum['quantity'] > $method['quantity']) {
724 $is_spam['quantity'] = TRUE;
727 // ----------------------------------------
728 // URI: used inside HTML anchor tag pair
730 if ((! $asap || ! $is_spam) && isset($method['uri_anchor'])) {
732 foreach($pickups as $pickup) {
733 if (isset($pickup['area'][$key])) {
734 $sum[$key] += $pickup['area'][$key];
735 if(isset($method[$key]) &&
736 $sum[$key] > $method[$key]) {
737 $is_spam[$key] = TRUE;
738 if ($asap && $is_spam) break;
740 if ($asap && $is_spam) break;
745 // ----------------------------------------
746 // URI: used inside 'BBCode' pair
748 if ((! $asap || ! $is_spam) && isset($method['uri_bbcode'])) {
750 foreach($pickups as $pickup) {
751 if (isset($pickup['area'][$key])) {
752 $sum[$key] += $pickup['area'][$key];
753 if(isset($method[$key]) &&
754 $sum[$key] > $method[$key]) {
755 $is_spam[$key] = TRUE;
756 if ($asap && $is_spam) break;
758 if ($asap && $is_spam) break;
763 // ----------------------------------------
764 // URI: Uniqueness (and removing non-uniques)
766 if ((! $asap || ! $is_spam) && isset($method['non_uniquri'])) {
769 foreach (array_keys($pickups) as $key) {
770 $uris[$key] = uri_pickup_implode($pickups[$key]);
772 $count = count($uris);
773 $uris = array_unique($uris);
774 $sum['non_uniquri'] += $count - count($uris);
775 if ($sum['non_uniquri'] > $method['non_uniquri']) {
776 $is_spam['non_uniquri'] = TRUE;
778 if (! $asap || ! $is_spam) {
779 foreach (array_diff(array_keys($pickups),
780 array_keys($uris)) as $remove) {
781 unset($pickups[$remove]);
788 if ($asap && $is_spam) return $progress;
790 // ----------------------------------------
791 // Host: Uniqueness (uniq / non-uniq)
793 $hosts = array_unique($hosts);
795 if (isset($sum['uniqhost'])) $sum['uniqhost'] += count($hosts);
796 if ((! $asap || ! $is_spam) && isset($method['non_uniqhost'])) {
797 $sum['non_uniqhost'] = $sum['quantity'] - $sum['uniqhost'];
798 if ($sum['non_uniqhost'] > $method['non_uniqhost']) {
799 $is_spam['non_uniqhost'] = TRUE;
804 if ($asap && $is_spam) return $progress;
806 // ----------------------------------------
807 // URI: Bad host (Separate good/bad hosts from $hosts)
809 if ((! $asap || ! $is_spam) && isset($method['badhost'])) {
810 $list = get_blocklist('list');
811 $blocked = array_merge_leaves(
813 blocklist_distiller($hosts, array_keys($list), $asap),
816 foreach($list as $key=>$type){
817 if (! $type) unset($blocked[$key]); // Ignore goodhost etc
820 if (! empty($blocked)) $is_spam['badhost'] = TRUE;
824 //if ($asap && $is_spam) return $progress;
826 // ----------------------------------------
832 // ---------------------
835 // Summarize $progress (blocked only)
836 function summarize_spam_progress($progress = array(), $blockedonly = FALSE)
839 $tmp = array_keys($progress['is_spam']);
842 $method = & $progress['method'];
843 if (isset($progress['sum'])) {
844 foreach ($progress['sum'] as $key => $value) {
845 if (isset($method[$key]) && $value) {
846 $tmp[] = $key . '(' . $value . ')';
852 return implode(', ', $tmp);
855 function summarize_detail_badhost($progress = array())
857 if (! isset($progress['blocked']) || empty($progress['blocked'])) return '';
861 foreach($progress['blocked'] as $list => $lvalue) {
862 foreach($lvalue as $group => $gvalue) {
863 $flat = implode(', ', array_flat_leaves($gvalue));
864 if ($flat === $group) {
865 $blocked[$list][] = $flat;
867 $blocked[$list][$group] = $flat;
873 // From: 'A-1' => array('ie.to')
874 // To: 'A-1' => 'ie.to'
875 foreach($blocked as $list => $lvalue) {
876 if (is_array($lvalue) &&
877 count($lvalue) == 1 &&
878 is_numeric(key($lvalue))) {
879 $blocked[$list] = current($lvalue);
883 return var_export_shrink($blocked, TRUE, TRUE);
886 function summarize_detail_newtral($progress = array())
888 if (! isset($progress['hosts']) ||
889 ! is_array($progress['hosts']) ||
890 empty($progress['hosts'])) return '';
892 // Generate a responsible $trie
894 foreach($progress['hosts'] as $value) {
895 // 'A.foo.bar.example.com'
896 $resp = whois_responsibility($value); // 'example.com'
898 // One or more test, or do nothing here
899 $resp = strval($value);
902 $rest = rtrim(substr($value, 0, - strlen($resp)), '.'); // 'A.foo.bar'
904 $trie = array_merge_leaves($trie, array($resp => array($rest => NULL)), FALSE);
907 // Format: var_export_shrink() -like output
909 ksort_by_domain($trie);
910 foreach(array_keys($trie) as $key) {
911 ksort_by_domain($trie[$key]);
912 if (count($trie[$key]) == 1 && key($trie[$key]) == '') {
913 // Just one 'responsibility.example.com'
914 $result[] = ' \'' . $key . '\',';
916 // One subdomain-or-host, or several ones
918 foreach(array_keys($trie[$key]) as $sub) {
922 $subs[] = $sub . '.' . $key;
925 $result[] = ' \'' . $key . '\' => \'' . implode(', ', $subs) . '\',';
931 implode("\n", $result) . "\n" .
936 // Check responsibility-root of the FQDN
937 // 'foo.bar.example.com' => 'example.com' (.com has the last whois for it)
938 // 'foo.bar.example.au' => 'example.au' (.au has the last whois for it)
939 // 'foo.bar.example.edu.au' => 'example.edu.au' (.edu.au has the last whois for it)
940 // 'foo.bar.example.act.edu.au' => 'example.act.edu.au' (.act.edu.au has the last whois for it)
941 function whois_responsibility($fqdn = 'foo.bar.example.com', $parent = FALSE, $implicit = TRUE)
945 if ($fqdn === NULL) {
946 $domain = NULL; // Unset
949 if (! is_string($fqdn)) return '';
951 if (is_ip($fqdn)) return $fqdn;
953 if (! isset($domain)) {
955 if (file_exists(DOMAIN_INI_FILE)) {
956 include(DOMAIN_INI_FILE); // Set
961 $dcursor = & $domain;
962 $array = array_reverse(explode('.', $fqdn));
965 if (! isset($array[$i])) break;
966 $acursor = $array[$i];
967 if (is_array($dcursor) && isset($dcursor[$acursor])) {
968 $result[] = & $array[$i];
969 $dcursor = & $dcursor[$acursor];
971 if (! $parent && isset($acursor)) {
972 $result[] = & $array[$i]; // Whois servers must know this subdomain
979 // Implicit responsibility: Top-Level-Domains must not be yours
980 // 'bar.foo.something' => 'foo.something'
981 if ($implicit && count($result) == 1 && count($array) > 1) {
982 $result[] = & $array[1];
985 return $result ? implode('.', array_reverse($result)) : '';
989 // ---------------------
993 function spam_dispose()
996 whois_responsibility(NULL);
999 // Common bahavior for blocking
1000 // NOTE: Call this function from various blocking feature, to disgueise the reason 'why blocked'
1001 function spam_exit($mode = '', $data = array())
1010 echo('<pre>' . "\n");
1011 echo htmlspecialchars(var_export($data, TRUE));
1012 echo('</pre>' . "\n");
1016 if ($exit) exit; // Force exit
1020 // ---------------------
1023 // TODO: Record them
1024 // Simple/fast spam filter ($target: 'a string' or an array())
1025 function pkwk_spamfilter($action, $page, $target = array('title' => ''), $method = array(), $exitmode = '')
1027 $progress = check_uri_spam($target, $method);
1029 if (empty($progress['is_spam'])) {
1033 // TODO: detect encoding from $target for mbstring functions
1035 // foreach(array_keys($target) as $key) {
1036 // $tmp[strings($key, 0, FALSE, TRUE)] = strings($target[$key], 0, FALSE, TRUE); // Removing "\0" etc
1038 // $target = & $tmp;
1040 pkwk_spamnotify($action, $page, $target, $progress, $method);
1041 spam_exit($exitmode, $progress);
1045 // ---------------------
1046 // PukiWiki original
1048 // Mail to administrator(s)
1049 function pkwk_spamnotify($action, $page, $target = array('title' => ''), $progress = array(), $method = array())
1051 global $notify, $notify_subject;
1053 if (! $notify) return;
1055 $asap = isset($method['asap']);
1057 $summary['ACTION'] = 'Blocked by: ' . summarize_spam_progress($progress, TRUE);
1059 $summary['METRICS'] = summarize_spam_progress($progress);
1062 $tmp = summarize_detail_badhost($progress);
1063 if ($tmp != '') $summary['DETAIL_BADHOST'] = $tmp;
1065 $tmp = summarize_detail_newtral($progress);
1066 if (! $asap && $tmp != '') $summary['DETAIL_NEUTRAL_HOST'] = $tmp;
1068 $summary['COMMENT'] = $action;
1069 $summary['PAGE'] = '[blocked] ' . (is_pagename($page) ? $page : '');
1070 $summary['URI'] = get_script_uri() . '?' . rawurlencode($page);
1071 $summary['USER_AGENT'] = TRUE;
1072 $summary['REMOTE_ADDR'] = TRUE;
1073 pkwk_mail_notify($notify_subject, var_export($target, TRUE), $summary, TRUE);