OSDN Git Service

is_ip(), whois_responsibility(): Moved from spam.php to spam_pickup.php
[pukiwiki/pukiwiki_sandbox.git] / spam / spam.php
1 <?php
2 // $Id: spam.php,v 1.215 2009/01/02 10:37:47 henoheno Exp $
3 // Copyright (C) 2006-2007 PukiWiki Developers Team
4 // License: GPL v2 or (at your option) any later version
5 //
6 // Functions for Concept-work of spam-uri metrics
7 //
8 // (PHP 4 >= 4.3.0): preg_match_all(PREG_OFFSET_CAPTURE): $method['uri_XXX'] related feature
9
10 if (! defined('SPAM_INI_FILE'))   define('SPAM_INI_FILE',   'spam.ini.php');
11
12 // ---------------------
13 // Compat etc
14
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";
19         }
20 }
21
22 // (PHP 4 >= 4.2.0): preg_grep() enables invert option
23 function preg_grep_invert($pattern = '//', $input = array())
24 {
25         static $invert;
26         if (! isset($invert)) $invert = defined('PREG_GREP_INVERT');
27
28         if ($invert) {
29                 return preg_grep($pattern, $input, PREG_GREP_INVERT);
30         } else {
31                 $result = preg_grep($pattern, $input);
32                 if ($result) {
33                         return array_diff($input, preg_grep($pattern, $input));
34                 } else {
35                         return $input;
36                 }
37         }
38 }
39
40
41 // ---------------------
42 // Utilities
43
44 // Very roughly, shrink the lines of var_export()
45 // NOTE: If the same data exists, it must be corrupted.
46 function var_export_shrink($expression, $return = FALSE, $ignore_numeric_keys = FALSE)
47 {
48         $result = var_export($expression, TRUE);
49
50         $result = preg_replace(
51                 // Remove a newline and spaces
52                 '# => \n *array \(#', ' => array (',
53                 $result
54         );
55
56         if ($ignore_numeric_keys) {
57                 $result =preg_replace(
58                         // Remove numeric keys
59                         '#^( *)[0-9]+ => #m', '$1',
60                         $result
61                 );
62         }
63
64         if ($return) {
65                 return $result;
66         } else {
67                 echo   $result;
68                 return NULL;
69         }
70 }
71
72 // Data structure: Create an array they _refer_only_one_ value
73 function one_value_array($num = 0, $value = NULL)
74 {
75         $num   = max(0, intval($num));
76         $array = array();
77
78         for ($i = 0; $i < $num; $i++) {
79                 $array[] = & $value;
80         }
81
82         return $array;
83 }
84
85 // Reverse $string with specified delimiter
86 function delimiter_reverse($string = 'foo.bar.example.com', $from_delim = '.', $to_delim = NULL)
87 {
88         $to_null = ($to_delim === NULL);
89
90         if (! is_string($from_delim) || (! $to_null && ! is_string($to_delim))) {
91                 return FALSE;
92         }
93         if (is_array($string)) {
94                 // Map, Recurse
95                 $count = count($string);
96                 $from  = one_value_array($count, $from_delim);
97                 if ($to_null) {
98                         // Note: array_map() vanishes all keys
99                         return array_map('delimiter_reverse', $string, $from);
100                 } else {
101                         $to = one_value_array($count, $to_delim);
102                         // Note: array_map() vanishes all keys
103                         return array_map('delimiter_reverse', $string, $from, $to);
104                 }
105         }
106         if (! is_string($string)) {
107                 return FALSE;
108         }
109
110         // Returns com.example.bar.foo
111         if ($to_null) $to_delim = & $from_delim;
112         return implode($to_delim, array_reverse(explode($from_delim, $string)));
113 }
114
115 // ksort() by domain
116 function ksort_by_domain(& $array)
117 {
118         $sort = array();
119         foreach(array_keys($array) as $key) {
120                 $reversed = delimiter_reverse($key);
121                 if ($reversed !== FALSE) {
122                         $sort[$reversed] = $key;
123                 }
124         }
125         ksort($sort, SORT_STRING);
126
127         $result = array();
128         foreach($sort as $key) {
129                 $result[$key] = & $array[$key];
130         }
131
132         $array = $result;
133 }
134
135 // Roughly strings(1) using PCRE
136 // This function is useful to:
137 //   * Reduce the size of data, from removing unprintable binary data
138 //   * Detect _bare_strings_ from binary data
139 // References:
140 //   http://www.freebsd.org/cgi/man.cgi?query=strings (Man-page of GNU strings)
141 //   http://www.pcre.org/pcre.txt
142 // Note: mb_ereg_replace() is one of mbstring extension's functions
143 //   and need to init its encoding.
144 function strings($binary = '', $min_len = 4, $ignore_space = FALSE, $multibyte = FALSE)
145 {
146         // String only
147         $binary = (is_array($binary) || $binary === TRUE) ? '' : strval($binary);
148
149         $regex = $ignore_space ?
150                 '[^[:graph:] \t\n]+' :          // Remove "\0" etc, and readable spaces
151                 '[^[:graph:][:space:]]+';       // Preserve readable spaces if possible
152
153         $binary = $multibyte ?
154                 mb_ereg_replace($regex,           "\n",  $binary) :
155                 preg_replace('/' . $regex . '/s', "\n",  $binary);
156
157         if ($ignore_space) {
158                 $binary = preg_replace(
159                         array(
160                                 '/[ \t]{2,}/',
161                                 '/^[ \t]/m',
162                                 '/[ \t]$/m',
163                         ),
164                         array(
165                                 ' ',
166                                 '',
167                                 ''
168                         ),
169                          $binary);
170         }
171
172         if ($min_len > 1) {
173                 // The last character seems "\n" or not
174                 $br = (! empty($binary) && $binary[strlen($binary) - 1] == "\n") ? "\n" : '';
175
176                 $min_len = min(1024, intval($min_len));
177                 $regex = '/^.{' . $min_len . ',}/S';
178                 $binary = implode("\n", preg_grep($regex, explode("\n", $binary))) . $br;
179         }
180
181         return $binary;
182 }
183
184
185 // ---------------------
186 // Utilities: Arrays
187
188 // Count leaves (A leaf = value that is not an array, or an empty array)
189 function array_count_leaves($array = array(), $count_empty = FALSE)
190 {
191         if (! is_array($array) || (empty($array) && $count_empty)) return 1;
192
193         // Recurse
194         $count = 0;
195         foreach ($array as $part) {
196                 $count += array_count_leaves($part, $count_empty);
197         }
198         return $count;
199 }
200
201 // Merge two leaves
202 // Similar to PHP array_merge_leaves(), except strictly preserving keys as string
203 function array_merge_leaves($array1, $array2, $sort_keys = TRUE)
204 {
205         // Array(s) only 
206         $is_array1 = is_array($array1);
207         $is_array2 = is_array($array2);
208         if ($is_array1) {
209                 if ($is_array2) {
210                         ;       // Pass
211                 } else {
212                         return $array1;
213                 }
214         } else if ($is_array2) {
215                 return $array2;
216         } else {
217                 return $array2; // Not array ($array1 is overwritten)
218         }
219
220         $keys_all = array_merge(array_keys($array1), array_keys($array2));
221         if ($sort_keys) sort($keys_all, SORT_STRING);
222
223         $result = array();
224         foreach($keys_all as $key) {
225                 $isset1 = isset($array1[$key]);
226                 $isset2 = isset($array2[$key]);
227                 if ($isset1 && $isset2) {
228                         // Recurse
229                         $result[$key] = array_merge_leaves($array1[$key], $array2[$key], $sort_keys);
230                 } else if ($isset1) {
231                         $result[$key] = & $array1[$key];
232                 } else {
233                         $result[$key] = & $array2[$key];
234                 }
235         }
236         return $result;
237 }
238
239 // An array-leaves to a flat array
240 function array_flat_leaves($array, $unique = TRUE)
241 {
242         if (! is_array($array)) return $array;
243
244         $tmp = array();
245         foreach(array_keys($array) as $key) {
246                 if (is_array($array[$key])) {
247                         // Recurse
248                         foreach(array_flat_leaves($array[$key]) as $_value) {
249                                 $tmp[] = $_value;
250                         }
251                 } else {
252                         $tmp[] = & $array[$key];
253                 }
254         }
255
256         return $unique ? array_values(array_unique($tmp)) : $tmp;
257 }
258
259 // $array['something'] => $array['wanted']
260 function array_rename_keys(& $array, $keys = array('from' => 'to'), $force = FALSE, $default = '')
261 {
262         if (! is_array($array) || ! is_array($keys)) return FALSE;
263
264         // Nondestructive test
265         if (! $force) {
266                 foreach(array_keys($keys) as $from) {
267                         if (! isset($array[$from])) {
268                                 return FALSE;
269                         }
270                 }
271         }
272
273         foreach($keys as $from => $to) {
274                 if ($from === $to) continue;
275                 if (! $force || isset($array[$from])) {
276                         $array[$to] = & $array[$from];
277                         unset($array[$from]);
278                 } else  {
279                         $array[$to] = $default;
280                 }
281         }
282
283         return TRUE;
284 }
285
286 // Remove redundant values from array()
287 function array_unique_recursive($array = array())
288 {
289         if (! is_array($array)) return $array;
290
291         $tmp = array();
292         foreach($array as $key => $value){
293                 if (is_array($value)) {
294                         $array[$key] = array_unique_recursive($value);
295                 } else {
296                         if (isset($tmp[$value])) {
297                                 unset($array[$key]);
298                         } else {
299                                 $tmp[$value] = TRUE;
300                         }
301                 }
302         }
303
304         return $array;
305 }
306
307
308 // ---------------------
309 // Part One : Checker
310
311 // Rough implementation of globbing
312 //
313 // USAGE: $regex = '/^' . generate_glob_regex('*.txt', '/') . '$/i';
314 //
315 function generate_glob_regex($string = '', $divider = '/')
316 {
317         static $from = array(
318                          1 => '*',
319                         11 => '?',
320         //              22 => '[',      // Maybe cause regex compilation error (e.g. '[]')
321         //              23 => ']',      //
322                 );
323         static $mid = array(
324                          1 => '_AST_',
325                         11 => '_QUE_',
326         //              22 => '_RBR_',
327         //              23 => '_LBR_',
328                 );
329         static $to = array(
330                          1 => '.*',
331                         11 => '.',
332         //              22 => '[',
333         //              23 => ']',
334                 );
335
336         if (! is_string($string)) return '';
337
338         $string = str_replace($from, $mid, $string); // Hide
339         $string = preg_quote($string, $divider);
340         $string = str_replace($mid, $to, $string);   // Unhide
341
342         return $string;
343 }
344
345 // Generate host (FQDN, IPv4, ...) regex
346 // 'localhost'     : Matches with 'localhost' only
347 // 'example.org'   : Matches with 'example.org' only (See host_normalize() about 'www')
348 // '.example.org'  : Matches with ALL FQDN ended with '.example.org'
349 // '*.example.org' : Almost the same of '.example.org' except 'www.example.org'
350 // '10.20.30.40'   : Matches with IPv4 address '10.20.30.40' only
351 // [TODO] '192.'   : Matches with all IPv4 hosts started with '192.'
352 // TODO: IPv4, CIDR?, IPv6
353 function generate_host_regex($string = '', $divider = '/')
354 {
355         if (! is_string($string)) return '';
356
357         if (mb_strpos($string, '.') === FALSE || is_ip($string)) {
358                 // "localhost", IPv4, etc
359                 return generate_glob_regex($string, $divider);
360         }
361
362         // FQDN or something
363         $part = explode('.', $string, 2);
364         if ($part[0] == '') {
365                 // ".example.org"
366                 $part[0] = '(?:.*\.)?';
367         } else if ($part[0] == '*') {
368                 // "*.example.org"
369                 $part[0] = '.*\.';
370         } else {
371                 // example.org, etc
372                 return generate_glob_regex($string, $divider);
373         }
374
375         $part[1] = generate_glob_regex($part[1], $divider);
376
377         return implode('', $part);
378 }
379
380 // Load SPAM_INI_FILE and return parsed one
381 function get_blocklist($list = '')
382 {
383         static $regexes;
384
385         if ($list === NULL) {
386                 $regexes = NULL;        // Unset
387                 return array();
388         }
389
390         if (! isset($regexes)) {
391                 $regexes = array();
392                 if (file_exists(SPAM_INI_FILE)) {
393                         $blocklist = array();
394
395                         include(SPAM_INI_FILE);
396                         //      $blocklist['list'] = array(
397                         //      //'goodhost' => FALSE;
398                         //      'badhost' => TRUE;
399                         // );
400                         //      $blocklist['badhost'] = array(
401                         //              '*.blogspot.com',       // Blog services's subdomains (only)
402                         //              'IANA-examples' => '#^(?:.*\.)?example\.(?:com|net|org)$#',
403                         //      );
404
405                         foreach(array(
406                                         'pre',
407                                         'list',
408                                 ) as $special) {
409
410                                 if (! isset($blocklist[$special])) continue;
411
412                                 $regexes[$special] = $blocklist[$special];
413
414                                 foreach(array_keys($blocklist[$special]) as $_list) {
415                                         if (! isset($blocklist[$_list])) continue;
416
417                                         foreach ($blocklist[$_list] as $key => $value) {
418                                                 if (is_array($value)) {
419                                                         $regexes[$_list][$key] = array();
420                                                         foreach($value as $_key => $_value) {
421                                                                 get_blocklist_add($regexes[$_list][$key], $_key, $_value);
422                                                         }
423                                                 } else {
424                                                         get_blocklist_add($regexes[$_list], $key, $value);
425                                                 }
426                                         }
427
428                                         unset($blocklist[$_list]);
429                                 }
430                         }
431                 }
432         }
433
434         if ($list === '') {
435                 return $regexes;                // ALL of
436         } else if (isset($regexes[$list])) {
437                 return $regexes[$list]; // A part of
438         } else {
439                 return array();                 // Found nothing
440         }
441 }
442
443 // Subroutine of get_blocklist(): Add new regex to the $array
444 function get_blocklist_add(& $array, $key = 0, $value = '*.example.org/path/to/file.html')
445 {
446         if (is_string($key)) {
447                 $array[$key]   = & $value; // Treat $value as a regex for FQDN(host)s
448         } else {
449                 $regex = generate_host_regex($value, '#');
450                 if (! empty($regex)) {
451                         $array[$value] = '#^' . $regex . '$#i';
452                 }
453         }
454 }
455
456 // Blocklist metrics: Separate $host, to $blocked and not blocked
457 function blocklist_distiller(& $hosts, $keys = array('goodhost', 'badhost'), $asap = FALSE)
458 {
459         if (! is_array($hosts)) $hosts = array($hosts);
460         if (! is_array($keys))  $keys  = array($keys);
461
462         $list = get_blocklist('list');
463         $blocked = array();
464
465         foreach($keys as $key){
466                 foreach (get_blocklist($key) as $label => $regex) {
467                         if (is_array($regex)) {
468                                 foreach($regex as $_label => $_regex) {
469                                         $group = preg_grep($_regex, $hosts);
470                                         if ($group) {
471                                                 $hosts = array_diff($hosts, $group);
472                                                 $blocked[$key][$label][$_label] = $group;
473                                                 if ($asap && $list[$key]) break;
474                                         }
475                                 }
476                         } else {
477                                 $group = preg_grep($regex, $hosts);
478                                 if ($group) {
479                                         $hosts = array_diff($hosts, $group);
480                                         $blocked[$key][$label] = $group;
481                                         if ($asap && $list[$key]) break;
482                                 }
483                         }
484                 }
485         }
486
487         return $blocked;
488 }
489
490
491 // ---------------------
492
493
494 // Default (enabled) methods and thresholds (for content insertion)
495 function check_uri_spam_method($times = 1, $t_area = 0, $rule = TRUE)
496 {
497         $times  = intval($times);
498         $t_area = intval($t_area);
499
500         $positive = array(
501                 // Thresholds
502                 'quantity'     =>  8 * $times,  // Allow N URIs
503                 'non_uniqhost' =>  3 * $times,  // Allow N duped (and normalized) Hosts
504                 //'non_uniquri'=>  3 * $times,  // Allow N duped (and normalized) URIs
505
506                 // Areas
507                 'area_anchor'  => $t_area,      // Using <a href> HTML tag
508                 'area_bbcode'  => $t_area,      // Using [url] or [link] BBCode
509                 //'uri_anchor' => $t_area,      // URI inside <a href> HTML tag
510                 //'uri_bbcode' => $t_area,      // URI inside [url] or [link] BBCode
511         );
512         if ($rule) {
513                 $bool = array(
514                         // Rules
515                         //'asap'   => TRUE,     // Quit or return As Soon As Possible
516                         'uniqhost' => TRUE,     // Show uniq host (at block notification mail)
517                         'badhost'  => TRUE,     // Check badhost
518                 );
519         } else {
520                 $bool = array();
521         }
522
523         // Remove non-$positive values
524         foreach (array_keys($positive) as $key) {
525                 if ($positive[$key] < 0) unset($positive[$key]);
526         }
527
528         return $positive + $bool;
529 }
530
531 // Simple/fast spam check
532 function check_uri_spam($target = '', $method = array())
533 {
534         // Return value
535         $progress = array(
536                 'method'  => array(
537                         // Theme to do  => Dummy, optional value, or optional array()
538                         //'quantity'    => 8,
539                         //'uniqhost'    => TRUE,
540                         //'non_uniqhost'=> 3,
541                         //'non_uniquri' => 3,
542                         //'badhost'     => TRUE,
543                         //'area_anchor' => 0,
544                         //'area_bbcode' => 0,
545                         //'uri_anchor'  => 0,
546                         //'uri_bbcode'  => 0,
547                 ),
548                 'sum' => array(
549                         // Theme        => Volume found (int)
550                 ),
551                 'is_spam' => array(
552                         // Flag. If someting defined here,
553                         // one or more spam will be included
554                         // in this report
555                 ),
556                 'blocked' => array(
557                         // Hosts blocked
558                         //'category' => array(
559                         //      'host',
560                         //)
561                 ),
562                 'hosts' => array(
563                         // Hosts not blocked
564                 ),
565         );
566
567         // ----------------------------------------
568         // Aliases
569
570         $sum     = & $progress['sum'];
571         $is_spam = & $progress['is_spam'];
572         $progress['method'] = & $method;        // Argument
573         $blocked = & $progress['blocked'];
574         $hosts   = & $progress['hosts'];
575         $asap    = isset($method['asap']);
576
577         // ----------------------------------------
578         // Init
579
580         if (! is_array($method) || empty($method)) {
581                 $method = check_uri_spam_method();
582         }
583         foreach(array_keys($method) as $key) {
584                 if (! isset($sum[$key])) $sum[$key] = 0;
585         }
586         if (! isset($sum['quantity'])) $sum['quantity'] = 0;
587
588         // ----------------------------------------
589         // Recurse
590
591         if (is_array($target)) {
592                 foreach($target as $str) {
593                         if (! is_string($str)) continue;
594
595                         $_progress = check_uri_spam($str, $method);     // Recurse
596
597                         // Merge $sum
598                         $_sum = & $_progress['sum'];
599                         foreach (array_keys($_sum) as $key) {
600                                 if (! isset($sum[$key])) {
601                                         $sum[$key] = & $_sum[$key];
602                                 } else {
603                                         $sum[$key] += $_sum[$key];
604                                 }
605                         }
606
607                         // Merge $is_spam
608                         $_is_spam = & $_progress['is_spam'];
609                         foreach (array_keys($_is_spam) as $key) {
610                                 $is_spam[$key] = TRUE;
611                                 if ($asap) break;
612                         }
613                         if ($asap && $is_spam) break;
614
615                         // Merge only
616                         $blocked = array_merge_leaves($blocked, $_progress['blocked'], FALSE);
617                         $hosts   = array_merge_leaves($hosts,   $_progress['hosts'],   FALSE);
618                 }
619
620                 // Unique values
621                 $blocked = array_unique_recursive($blocked);
622                 $hosts   = array_unique_recursive($hosts);
623
624                 // Recount $sum['badhost']
625                 $sum['badhost'] = array_count_leaves($blocked);
626
627                 return $progress;
628         }
629
630         // ----------------------------------------
631         // Area measure
632
633         if (! $asap || ! $is_spam) {
634         
635                 // Method pickup
636                 $_method = array();
637                 foreach(array(
638                                 'area_anchor',  // There's HTML anchor tag
639                                 'area_bbcode',  // There's 'BBCode' linking tag
640                         ) as $key) {
641                         if (isset($method[$key])) $_method[$key] = TRUE;
642                 }
643
644                 if ($_method) {
645                         $_asap   = isset($method['asap']) ? array('asap' => TRUE) : array();
646                         $_result = area_pickup($target, $_method + $_asap);
647                         $_asap   = NULL;
648                 } else {
649                         $_result = FALSE;
650                 }
651
652                 if ($_result) {
653                         foreach(array_keys($_method) as $key) {
654                                 if (isset($_result[$key])) {
655                                         $sum[$key] = $_result[$key];
656                                         if (isset($method[$key]) && $sum[$key] > $method[$key]) {
657                                                 $is_spam[$key] = TRUE;
658                                         }
659                                 }
660                         }
661                 }
662
663                 unset($_asap, $_method, $_result);
664         }
665
666         // Return if ...
667         if ($asap && $is_spam) return $progress;
668
669         // ----------------------------------------
670         // URI: Pickup
671
672         $pickups = spam_uri_pickup($target, $method);
673
674
675         // Return if ...
676         if (empty($pickups)) return $progress;
677
678         // Normalize all
679         $pickups = uri_pickup_normalize($pickups);
680
681         // ----------------------------------------
682         // Pickup some part of URI
683
684         $hosts = array();
685         foreach ($pickups as $key => $pickup) {
686                 $hosts[$key] = & $pickup['host'];
687         }
688
689         // ----------------------------------------
690         // URI: Bad host <pre-filter> (Separate good/bad hosts from $hosts)
691
692         if ((! $asap || ! $is_spam) && isset($method['badhost'])) {
693                 $list    = get_blocklist('pre');
694                 $blocked = blocklist_distiller($hosts, array_keys($list), $asap);
695                 foreach($list as $key => $type){
696                         if (! $type) unset($blocked[$key]); // Ignore goodhost etc
697                 }
698                 unset($list);
699                 if (! empty($blocked)) $is_spam['badhost'] = TRUE;
700         }
701
702         // Return if ...
703         if ($asap && $is_spam) return $progress;
704
705         // Remove blocked from $pickups
706         foreach(array_keys($pickups) as $key) {
707                 if (! isset($hosts[$key])) {
708                         unset($pickups[$key]);
709                 }
710         }
711
712         // ----------------------------------------
713         // URI: Check quantity
714
715         $sum['quantity'] += count($pickups);
716                 // URI quantity
717         if ((! $asap || ! $is_spam) && isset($method['quantity']) &&
718                 $sum['quantity'] > $method['quantity']) {
719                 $is_spam['quantity'] = TRUE;
720         }
721
722         // ----------------------------------------
723         // URI: used inside HTML anchor tag pair
724
725         if ((! $asap || ! $is_spam) && isset($method['uri_anchor'])) {
726                 $key = 'uri_anchor';
727                 foreach($pickups as $pickup) {
728                         if (isset($pickup['area'][$key])) {
729                                 $sum[$key] += $pickup['area'][$key];
730                                 if(isset($method[$key]) &&
731                                         $sum[$key] > $method[$key]) {
732                                         $is_spam[$key] = TRUE;
733                                         if ($asap && $is_spam) break;
734                                 }
735                                 if ($asap && $is_spam) break;
736                         }
737                 }
738         }
739
740         // ----------------------------------------
741         // URI: used inside 'BBCode' pair
742
743         if ((! $asap || ! $is_spam) && isset($method['uri_bbcode'])) {
744                 $key = 'uri_bbcode';
745                 foreach($pickups as $pickup) {
746                         if (isset($pickup['area'][$key])) {
747                                 $sum[$key] += $pickup['area'][$key];
748                                 if(isset($method[$key]) &&
749                                         $sum[$key] > $method[$key]) {
750                                         $is_spam[$key] = TRUE;
751                                         if ($asap && $is_spam) break;
752                                 }
753                                 if ($asap && $is_spam) break;
754                         }
755                 }
756         }
757
758         // ----------------------------------------
759         // URI: Uniqueness (and removing non-uniques)
760
761         if ((! $asap || ! $is_spam) && isset($method['non_uniquri'])) {
762
763                 $uris = array();
764                 foreach (array_keys($pickups) as $key) {
765                         $uris[$key] = uri_pickup_implode($pickups[$key]);
766                 }
767                 $count = count($uris);
768                 $uris  = array_unique($uris);
769                 $sum['non_uniquri'] += $count - count($uris);
770                 if ($sum['non_uniquri'] > $method['non_uniquri']) {
771                         $is_spam['non_uniquri'] = TRUE;
772                 }
773                 if (! $asap || ! $is_spam) {
774                         foreach (array_diff(array_keys($pickups),
775                                 array_keys($uris)) as $remove) {
776                                 unset($pickups[$remove]);
777                         }
778                 }
779                 unset($uris);
780         }
781
782         // Return if ...
783         if ($asap && $is_spam) return $progress;
784
785         // ----------------------------------------
786         // Host: Uniqueness (uniq / non-uniq)
787
788         $hosts = array_unique($hosts);
789
790         if (isset($sum['uniqhost'])) $sum['uniqhost'] += count($hosts);
791         if ((! $asap || ! $is_spam) && isset($method['non_uniqhost'])) {
792                 $sum['non_uniqhost'] = $sum['quantity'] - $sum['uniqhost'];
793                 if ($sum['non_uniqhost'] > $method['non_uniqhost']) {
794                         $is_spam['non_uniqhost'] = TRUE;
795                 }
796         }
797
798         // Return if ...
799         if ($asap && $is_spam) return $progress;
800
801         // ----------------------------------------
802         // URI: Bad host (Separate good/bad hosts from $hosts)
803
804         if ((! $asap || ! $is_spam) && isset($method['badhost'])) {
805                 $list    = get_blocklist('list');
806                 $blocked = array_merge_leaves(
807                         $blocked,
808                         blocklist_distiller($hosts, array_keys($list), $asap),
809                         FALSE
810                 );
811                 foreach($list as $key=>$type){
812                         if (! $type) unset($blocked[$key]); // Ignore goodhost etc
813                 }
814                 unset($list);
815                 if (! empty($blocked)) $is_spam['badhost'] = TRUE;
816         }
817
818         // Return if ...
819         //if ($asap && $is_spam) return $progress;
820
821         // ----------------------------------------
822         // End
823
824         return $progress;
825 }
826
827 // ---------------------
828 // Reporting
829
830 // Summarize $progress (blocked only)
831 function summarize_spam_progress($progress = array(), $blockedonly = FALSE)
832 {
833         if ($blockedonly) {
834                 $tmp = array_keys($progress['is_spam']);
835         } else {
836                 $tmp = array();
837                 $method = & $progress['method'];
838                 if (isset($progress['sum'])) {
839                         foreach ($progress['sum'] as $key => $value) {
840                                 if (isset($method[$key]) && $value) {
841                                         $tmp[] = $key . '(' . $value . ')';
842                                 }
843                         }
844                 }
845         }
846
847         return implode(', ', $tmp);
848 }
849
850 function summarize_detail_badhost($progress = array())
851 {
852         if (! isset($progress['blocked']) || empty($progress['blocked'])) return '';
853
854         // Flat per group
855         $blocked = array();
856         foreach($progress['blocked'] as $list => $lvalue) {
857                 foreach($lvalue as $group => $gvalue) {
858                         $flat = implode(', ', array_flat_leaves($gvalue));
859                         if ($flat === $group) {
860                                 $blocked[$list][]       = $flat;
861                         } else {
862                                 $blocked[$list][$group] = $flat;
863                         }
864                 }
865         }
866
867         // Shrink per list
868         // From: 'A-1' => array('ie.to')
869         // To:   'A-1' => 'ie.to'
870         foreach($blocked as $list => $lvalue) {
871                 if (is_array($lvalue) &&
872                    count($lvalue) == 1 &&
873                    is_numeric(key($lvalue))) {
874                     $blocked[$list] = current($lvalue);
875                 }
876         }
877
878         return var_export_shrink($blocked, TRUE, TRUE);
879 }
880
881 function summarize_detail_newtral($progress = array())
882 {
883         if (! isset($progress['hosts'])    ||
884             ! is_array($progress['hosts']) ||
885             empty($progress['hosts'])) return '';
886
887         // Generate a responsible $trie
888         $trie = array();
889         foreach($progress['hosts'] as $value) {
890                 // 'A.foo.bar.example.com'
891                 $resp = whois_responsibility($value);   // 'example.com'
892                 if (empty($resp)) {
893                         // One or more test, or do nothing here
894                         $resp = strval($value);
895                         $rest = '';
896                 } else {
897                         $rest = rtrim(substr($value, 0, - strlen($resp)), '.'); // 'A.foo.bar'
898                 }
899                 $trie = array_merge_leaves($trie, array($resp => array($rest => NULL)), FALSE);
900         }
901
902         // Format: var_export_shrink() -like output
903         $result = array();
904         ksort_by_domain($trie);
905         foreach(array_keys($trie) as $key) {
906                 ksort_by_domain($trie[$key]);
907                 if (count($trie[$key]) == 1 && key($trie[$key]) == '') {
908                         // Just one 'responsibility.example.com'
909                         $result[] = '  \'' . $key . '\',';
910                 } else {
911                         // One subdomain-or-host, or several ones
912                         $subs = array();
913                         foreach(array_keys($trie[$key]) as $sub) {
914                                 if ($sub == '') {
915                                         $subs[] = $key;                 // 'example.com'
916                                 } else {
917                                         $subs[] = $sub . '. ';  // 'A.foo.bar. '
918                                 }
919                         }
920                         $result[] = '  \'' . $key . '\' => \'' . implode(', ', $subs) . '\',';
921                 }
922                 unset($trie[$key]);
923         }
924         return
925                 'array (' . "\n" .
926                         implode("\n", $result) . "\n" .
927                 ')';
928 }
929
930
931 // ---------------------
932 // Exit
933
934 // Freeing memories
935 function spam_dispose()
936 {
937         get_blocklist(NULL);
938         whois_responsibility(NULL);
939 }
940
941 // Common bahavior for blocking
942 // NOTE: Call this function from various blocking feature, to disgueise the reason 'why blocked'
943 function spam_exit($mode = '', $data = array())
944 {
945         $exit = TRUE;
946
947         switch ($mode) {
948                 case '':
949                         echo("\n");
950                         break;
951                 case 'dump':
952                         echo('<pre>' . "\n");
953                         echo htmlspecialchars(var_export($data, TRUE));
954                         echo('</pre>' . "\n");
955                         break;
956         };
957
958         if ($exit) exit;        // Force exit
959 }
960
961
962 // ---------------------
963 // Simple filtering
964
965 // TODO: Record them
966 // Simple/fast spam filter ($target: 'a string' or an array())
967 function pkwk_spamfilter($action, $page, $target = array('title' => ''), $method = array(), $exitmode = '')
968 {
969         $progress = check_uri_spam($target, $method);
970
971         if (empty($progress['is_spam'])) {
972                 spam_dispose();
973         } else {
974
975 // TODO: detect encoding from $target for mbstring functions
976 //              $tmp = array();
977 //              foreach(array_keys($target) as $key) {
978 //                      $tmp[strings($key, 0, FALSE, TRUE)] = strings($target[$key], 0, FALSE, TRUE);   // Removing "\0" etc
979 //              }
980 //              $target = & $tmp;
981
982                 pkwk_spamnotify($action, $page, $target, $progress, $method);
983                 spam_exit($exitmode, $progress);
984         }
985 }
986
987 // ---------------------
988 // PukiWiki original
989
990 // Mail to administrator(s)
991 function pkwk_spamnotify($action, $page, $target = array('title' => ''), $progress = array(), $method = array())
992 {
993         global $notify, $notify_subject;
994
995         if (! $notify) return;
996
997         $asap = isset($method['asap']);
998
999         $summary['ACTION']  = 'Blocked by: ' . summarize_spam_progress($progress, TRUE);
1000         if (! $asap) {
1001                 $summary['METRICS'] = summarize_spam_progress($progress);
1002         }
1003
1004         $tmp = summarize_detail_badhost($progress);
1005         if ($tmp != '') $summary['DETAIL_BADHOST'] = $tmp;
1006
1007         $tmp = summarize_detail_newtral($progress);
1008         if (! $asap && $tmp != '') $summary['DETAIL_NEUTRAL_HOST'] = $tmp;
1009
1010         $summary['COMMENT'] = $action;
1011         $summary['PAGE']    = '[blocked] ' . (is_pagename($page) ? $page : '');
1012         $summary['URI']     = get_script_uri() . '?' . rawurlencode($page);
1013         $summary['USER_AGENT']  = TRUE;
1014         $summary['REMOTE_ADDR'] = TRUE;
1015         pkwk_mail_notify($notify_subject,  var_export($target, TRUE), $summary, TRUE);
1016 }
1017
1018 ?>