OSDN Git Service

90b97358c2ee88dd426364b6abd53a70ad7091b1
[pukiwiki/pukiwiki_sandbox.git] / spam.php
1 <?php
2 // $Id: spam.php,v 1.40 2006/11/26 02:57:26 henoheno Exp $
3 // Copyright (C) 2006 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 // Return an array of URIs in the $string
9 // [OK] http://nasty.example.org#nasty_string
10 // [OK] http://nasty.example.org:80/foo/xxx#nasty_string/bar
11 // [OK] ftp://nasty.example.org:80/dfsdfs
12 // [OK] ftp://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htm (from RFC3986)
13 function uri_pickup($string = '', $normalize = TRUE,
14         $preserve_rawuri = FALSE, $preserve_chunk = TRUE)
15 {
16         // Not available for: IDN(ignored)
17         $array = array();
18         preg_match_all(
19                 // scheme://userinfo@host:port/path/or/pathinfo/maybefile.and?query=string#fragment
20                 // Refer RFC3986 (Regex below is not strict)
21                 '#(\b[a-z][a-z0-9.+-]{1,8})://' .       // 1: Scheme
22                 '(?:' .
23                         '([^\s<>"\'\[\]/\#?@]*)' .              // 2: Userinfo (Username)
24                 '@)?' .
25                 '(' .
26                         // 3: Host
27                         '\[[0-9a-f:.]+\]' . '|' .                               // IPv6([colon-hex and dot]): RFC2732
28                         '(?:[0-9]{1-3}\.){3}[0-9]{1-3}' . '|' . // IPv4(dot-decimal): 001.22.3.44
29                         '[^\s<>"\'\[\]:/\#?]+' .                                // FQDN: foo.example.org
30                 ')' .
31                 '(?::([0-9]*))?' .                                      // 4: Port
32                 '((?:/+[^\s<>"\'\[\]/\#]+)*/+)?' .      // 5: Directory path or path-info
33                 '([^\s<>"\'\[\]\#]+)?' .                        // 6: File and query string
34                 '(?:\#([a-z0-9._~%!$&\'()*+,;=:@-]*))?' .       // 7: Fragment
35                 '#i',
36                  $string, $array, PREG_SET_ORDER | PREG_OFFSET_CAPTURE
37         );
38         //var_dump(recursive_map('htmlspecialchars', $array));
39
40         // Shrink $array
41         static $parts = array(
42                 1 => 'scheme', 2 => 'userinfo', 3 => 'host', 4 => 'port',
43                 5 => 'path', 6 => 'file', 7 => 'fragment'
44         );
45         $default = array('');
46         foreach(array_keys($array) as $uri) {
47                 array_rename_keys($array[$uri], $parts, TRUE, $default);
48                 $offset = $array[$uri]['scheme'][1]; // Scheme's offset
49
50                 foreach(array_keys($array[$uri]) as $part) {
51                         // Remove offsets for each part
52                         $array[$uri][$part] = & $array[$uri][$part][0];
53                 }
54
55                 if ($normalize) {
56                         $array[$uri]['scheme'] = scheme_normalize($array[$uri]['scheme']);
57                         //if ($array[$uri]['scheme'] === '') {
58                         //      // Ignore
59                         //      unset ($array[$uri]);
60                         //      continue;
61                         //}
62                         
63                         $array[$uri]['host']   = strtolower($array[$uri]['host']);
64                         $array[$uri]['port']   = port_normalize($array[$uri]['port'], $array[$uri]['scheme'], FALSE);
65                         $array[$uri]['path']   = path_normalize($array[$uri]['path']);
66                         //$array[$uri]['uri']    = uri_array_implode($array[$uri]);
67                         if ($preserve_rawuri) $array[$uri]['rawuri'] = & $array[$uri][0];
68                 } else {
69                         $array[$uri]['uri'] = & $array[$uri][0]; // Raw
70                 }
71                 unset($array[$uri][0]); // Matched string itself
72                 if (! $preserve_chunk) {
73                         unset(
74                                 $array[$uri]['scheme'],
75                                 $array[$uri]['userinfo'],
76                                 $array[$uri]['host'],
77                                 $array[$uri]['port'],
78                                 $array[$uri]['path'],
79                                 $array[$uri]['file'],
80                                 $array[$uri]['fragment']
81                         );
82                 }
83
84                 $array[$uri]['area']['offset'] = $offset;
85         }
86
87         return $array;
88 }
89
90 // Domain exposure callback (See spam_uri_pickup_preprocess())
91 // http://victim.example.org/?foo+site:nasty.example.com+bar
92 // => http://nasty.example.com/?refer=victim.example.org
93 // NOTE: 'refer=' is not so good for (at this time).
94 // Consider about using IP address of the victim, try to avoid that.
95 function _preg_replace_callback_domain_exposure($matches = array())
96 {
97         $result = '';
98
99         // Preserve the victim URI as a complicity or ...
100         if (isset($matches[5])) {
101                 $result =
102                         $matches[1] . '://' .   // scheme
103                         $matches[2] . '/' .             // victim.example.org
104                         $matches[3];                    // The rest of all (before victim)
105         }
106
107         // Flipped URI
108         $result = 
109                 $matches[1] . '://' .   // scheme
110                 $matches[4] .                   // nasty.example.com
111                 '/?refer=' . strtolower($matches[2]) .  // victim.example.org
112                 ' ' . $result;
113
114         return $result;
115 }
116
117 // Preprocess: rawurldecode() and adding space(s) to detect/count some URIs _if possible_
118 // NOTE: It's maybe danger to var_dump(result). [e.g. 'javascript:']
119 // [OK] http://victim.example.org/go?http%3A%2F%2Fnasty.example.org
120 // [OK] http://victim.example.org/http://nasty.example.org
121 function spam_uri_pickup_preprocess($string = '')
122 {
123         if (! is_string($string)) return '';
124
125         $string = rawurldecode($string);
126
127         // Domain exposure (See _preg_replace_callback_domain_exposure())
128         $string = preg_replace_callback(
129                 array(
130                         // Something Google: http://www.google.com/supported_domains
131                         '#(http)://([a-z0-9.]+\.google\.[a-z]{2,3}(?:\.[a-z]{2})?)/' .
132                         '([a-z0-9?=&.%_+-]+)' .         // ?query=foo+
133                         '\bsite:([a-z0-9.%_-]+)' .      // site:nasty.example.com
134                         '()' .  // Preserve?
135                         '#i',
136                 ),
137                 '_preg_replace_callback_domain_exposure',
138                 $string
139         );
140
141         // URI exposure (uriuri => uri uri)
142         $string = preg_replace(
143                 array(
144                         '#(?<! )(?:https?|ftp):/#',
145                 //      '#[a-z][a-z0-9.+-]{1,8}://#i',
146                 //      '#[a-z][a-z0-9.+-]{1,8}://#i'
147                 ),
148                 ' $0',
149                 $string
150         );
151
152         return $string;
153 }
154
155 // Main function of spam-uri pickup
156 function spam_uri_pickup($string = '', $area = array())
157 {
158         if (! is_array($area) || empty($area)) {
159                 $area = array(
160                                 'anchor' => TRUE,
161                                 'bbcode' => TRUE,
162                         );
163         }
164
165         $string = spam_uri_pickup_preprocess($string);
166
167         $array  = uri_pickup($string);
168
169         // Area elevation for '(especially external)link' intension
170         if (! empty($array)) {
171         
172                 $area_shadow = array();
173                 foreach(array_keys($array) as $key){
174                         $area_shadow[$key] = & $array[$key]['area'];
175                         $area_shadow[$key]['anchor'] = 0;
176                         $area_shadow[$key]['bbcode'] = 0;
177                 }
178
179                 // Anchor tags by preg_match_all()
180                 // [OK] <a href="http://nasty.example.com">visit http://nasty.example.com/</a>
181                 // [OK] <a href=\'http://nasty.example.com/\' >discount foobar</a> 
182                 // [NG] <a href="http://ng.example.com">visit http://ng.example.com _not_ended_
183                 // [NG] <a href=  >Good site!</a> <a href= "#" >test</a>
184                 if (isset($area['anchor'])) {
185                         $areas = array();
186                         preg_match_all('#<a\b[^>]*href[^>]*>.*?</a\b[^>]*(>)#i',
187                                  $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
188                         //var_dump(recursive_map('htmlspecialchars', $areas));
189                         foreach(array_keys($areas) as $_area) {
190                                 $areas[$_area] =  array(
191                                         $areas[$_area][0][1], // Area start (<a href>)
192                                         $areas[$_area][1][1], // Area end   (</a>)
193                                 );
194                         }
195                         area_measure($areas, $area_shadow, -1, 'anchor');
196                 }
197
198                 // phpBB's "BBCode" by preg_match_all()
199                 // [url]http://nasty.example.com/[/url]
200                 // [link]http://nasty.example.com/[/link]
201                 // [url=http://nasty.example.com]visit http://nasty.example.com/[/url]
202                 // [link http://nasty.example.com/]buy something[/link]
203                 // ?? [url=][/url]
204                 if (isset($area['bbcode'])) {
205                         $areas = array();
206                         preg_match_all('#\[(url|link)\b[^\]]*\].*?\[/\1\b[^\]]*(\])#i',
207                                  $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
208                         //var_dump(recursive_map('htmlspecialchars', $areas));
209                         foreach(array_keys($areas) as $_area) {
210                                 $areas[$_area] = array(
211                                         $areas[$_area][0][1], // Area start ([url])
212                                         $areas[$_area][2][1], // Area end   ([/url])
213                                 );
214                         }
215                         area_measure($areas, $area_shadow, -1, 'bbcode');
216                 }
217
218                 // Various Wiki syntax
219                 // [text_or_uri>text_or_uri]
220                 // [text_or_uri:text_or_uri]
221                 // [text_or_uri|text_or_uri]
222                 // [text_or_uri->text_or_uri]
223                 // [text_or_uri text_or_uri] // MediaWiki
224                 // MediaWiki: [http://nasty.example.com/ visit http://nasty.example.com/]
225
226                 // Remove 'offset's for area_measure()
227                 //foreach(array_keys($array) as $key)
228                 //      unset($array[$key]['offset']);
229         }
230
231         return $array;
232 }
233
234 // $array['something'] => $array['wanted']
235 function array_rename_keys(& $array, $keys = array('from' => 'to'), $force = FALSE, $default = '')
236 {
237         if (! is_array($array) || ! is_array($keys))
238                 return FALSE;
239
240         // Nondestructive test
241         if (! $force)
242                 foreach(array_keys($keys) as $from)
243                         if (! isset($array[$from]))
244                                 return FALSE;
245
246         foreach($keys as $from => $to) {
247                 if ($from === $to) continue;
248                 if (! $force || isset($array[$from])) {
249                         $array[$to] = & $array[$from];
250                         unset($array[$from]);
251                 } else  {
252                         $array[$to] = $default;
253                 }
254         }
255
256         return TRUE;
257 }
258
259 // If in doubt, it's a little doubtful
260 function area_measure($areas, & $array, $belief = -1, $a_key = 'area', $o_key = 'offset')
261 {
262         if (! is_array($areas) || ! is_array($array)) return;
263
264         $areas_keys = array_keys($areas);
265         foreach(array_keys($array) as $u_index) {
266                 $offset = isset($array[$u_index][$o_key]) ?
267                         intval($array[$u_index][$o_key]) : 0;
268                 foreach($areas_keys as $a_index) {
269                         if (isset($array[$u_index][$a_key])) {
270                                 $offset_s = intval($areas[$a_index][0]);
271                                 $offset_e = intval($areas[$a_index][1]);
272                                 // [Area => inside <= Area]
273                                 if ($offset_s < $offset && $offset < $offset_e) {
274                                         $array[$u_index][$a_key] += $belief;
275                                 }
276                         }
277                 }
278         }
279 }
280
281
282 // ---------------------
283 // Part Two
284
285 // Scheme normalization: Renaming the schemes
286 // snntp://example.org =>  nntps://example.org
287 // NOTE: Keep the static lists simple. See also port_normalize().
288 function scheme_normalize($scheme = '', $considerd_harmfull = TRUE)
289 {
290         // Abbreviations considerable they don't have link intension
291         static $abbrevs = array(
292                 'ttp'   => 'http',
293                 'ttps'  => 'https',
294         );
295
296         // Alias => normalized
297         static $aliases = array(
298                 'pop'   => 'pop3',
299                 'news'  => 'nntp',
300                 'imap4' => 'imap',
301                 'snntp' => 'nntps',
302                 'snews' => 'nntps',
303                 'spop3' => 'pop3s',
304                 'pops'  => 'pop3s',
305         );
306
307         $scheme = strtolower(trim($scheme));
308         if (isset($abbrevs[$scheme])) {
309                 if ($considerd_harmfull) {
310                         $scheme = $abbrevs[$scheme];
311                 } else {
312                         $scheme = '';
313                 }
314         }
315         if (isset($aliases[$scheme])) $scheme = $aliases[$scheme];
316
317         return $scheme;
318 }
319
320 // Port normalization: Suppress the (redundant) default port
321 // HTTP://example.org:80/ => http://example.org/
322 // HTTP://example.org:8080/ => http://example.org:8080/
323 // HTTPS://example.org:443/ => https://example.org/
324 function port_normalize($port, $scheme, $scheme_normalize = TRUE)
325 {
326         // Schemes that users _maybe_ want to add protocol-handlers
327         // to their web browsers. (and attackers _maybe_ want to use ...)
328         // Reference: http://www.iana.org/assignments/port-numbers
329         static $array = array(
330                 // scheme => default port
331                 'ftp'     =>    21,
332                 'ssh'     =>    22,
333                 'telnet'  =>    23,
334                 'smtp'    =>    25,
335                 'tftp'    =>    69,
336                 'gopher'  =>    70,
337                 'finger'  =>    79,
338                 'http'    =>    80,
339                 'pop3'    =>   110,
340                 'sftp'    =>   115,
341                 'nntp'    =>   119,
342                 'imap'    =>   143,
343                 'irc'     =>   194,
344                 'wais'    =>   210,
345                 'https'   =>   443,
346                 'nntps'   =>   563,
347                 'rsync'   =>   873,
348                 'ftps'    =>   990,
349                 'telnets' =>   992,
350                 'imaps'   =>   993,
351                 'ircs'    =>   994,
352                 'pop3s'   =>   995,
353                 'mysql'   =>  3306,
354         );
355
356         $port = trim($port);
357         if ($port === '') return $port;
358
359         if ($scheme_normalize) $scheme = scheme_normalize($scheme);
360         if (isset($array[$scheme]) && $port == $array[$scheme])
361                 $port = ''; // Ignore the defaults
362
363         return $port;
364 }
365
366 // Path normalization
367 // http://example.org => http://example.org/
368 // http://example.org#hoge => http://example.org/#hoge
369 // http://example.org/path/a/b/./c////./d => http://example.org/path/a/b/c/d
370 // http://example.org/path/../../a/../back => http://example.org/back
371 function path_normalize($path = '', $divider = '/', $addroot = TRUE)
372 {
373         if (! is_string($path) || $path == '') {
374                 $path = $addroot ? $divider : '';
375         } else {
376                 $path = trim($path);
377                 $last = ($path[strlen($path) - 1] == $divider) ? $divider : '';
378                 $array = explode($divider, $path);
379
380                 // Remove paddings
381                 foreach(array_keys($array) as $key) {
382                         if ($array[$key] == '' || $array[$key] == '.')
383                                  unset($array[$key]);
384                 }
385                 // Back-track
386                 $tmp = array();
387                 foreach($array as $value) {
388                         if ($value == '..') {
389                                 array_pop($tmp);
390                         } else {
391                                 array_push($tmp, $value);
392                         }
393                 }
394                 $array = & $tmp;
395
396                 $path = $addroot ? $divider : '';
397                 if (! empty($array)) $path .= implode($divider, $array) . $last;
398         }
399
400         return $path;
401 }
402
403 // An URI array => An URI (See uri_pickup())
404 function uri_array_implode($uri = array())
405 {
406         if (empty($uri) || ! is_array($uri)) return NULL;
407         
408         $tmp = array();
409         if (isset($uri['scheme']) && $uri['scheme'] !== '') {
410                 $tmp[] = & $uri['scheme'];
411                 $tmp[] = '://';
412         }
413         if (isset($uri['userinfo']) && $uri['userinfo'] !== '') {
414                 $tmp[] = & $uri['userinfo'];
415                 $tmp[] = '@';
416         }
417         if (isset($uri['host']) && $uri['host'] !== '') {
418                 $tmp[] = & $uri['host'];
419         }
420         if (isset($uri['port']) && $uri['port'] !== '') {
421                 $tmp[] = ':';
422                 $tmp[] = & $uri['port'];
423         }
424         if (isset($uri['path']) && $uri['path'] !== '') {
425                 $tmp[] = & $uri['path'];
426         }
427         if (isset($uri['file']) && $uri['file'] !== '') {
428                 $tmp[] = & $uri['file'];
429         }
430         if (isset($uri['fragment']) && $uri['fragment'] !== '') {
431                 $tmp[] = '#';
432                 $tmp[] = & $uri['fragment'];
433         }
434
435         return implode('', $tmp);
436 }
437
438 // ---------------------
439 // Part One : Checker
440
441 function generate_glob_regex($string = '', $divider = '/')
442 {
443         static $from = array(
444                         0 => '*',
445                         1 => '?',
446                         2 => '\[',
447                         3 => '\]',
448                         4 => '[',
449                         5 => ']',
450                 );
451         static $mid = array(
452                         0 => '_AST_',
453                         1 => '_QUE_',
454                         2 => '_eRBR_',
455                         3 => '_eLBR_',
456                         4 => '_RBR_',
457                         5 => '_LBR_',
458                 );
459         static $to = array(
460                         0 => '.*',
461                         1 => '.',
462                         2 => '\[',
463                         3 => '\]',
464                         4 => '[',
465                         5 => ']',
466                 );
467
468         $string = str_replace($from, $mid, $string); // Hide
469         $string = preg_quote($string, $divider);
470         $string = str_replace($mid, $to, $string);   // Unhide
471
472         return $string;
473 }
474
475 // TODO: Ignore list
476 // TODO: require_or_include_once(another file)
477 function is_badhost($hosts = '', $asap = TRUE)
478 {
479         static $blocklist_regex;
480
481         if (! isset($blocklist_regex)) {
482                 $blocklist_regex = array();
483                 $blocklist = array(
484                         // Deny all uri
485                         //'*',
486
487                         // IP address or ...
488                         //'10.20.*.*',  // 10.20.example.com also matches
489                         //'\[1\]',
490                         
491                         // Too much malicious sub-domains
492                         //'*.blogspot.com',
493
494                         // 2006-11 dev
495                         'wwwtahoo.com',
496
497                         // 2006-11 dev
498                         '*.infogami.com',
499
500                         // 2006/11/19 17:50 dev
501                         //'*.google0site.org',
502                         //'*.bigpricesearch.org',
503                         //'*.osfind.org',
504                         //'*.bablomira.biz',
505                 );
506                 foreach ($blocklist as $part) {
507                         $blocklist_regex[] = '#^' . generate_glob_regex($part, '#') . '$#i';
508                 }
509         }
510
511         $result = 0;
512         if (! is_array($hosts)) $hosts = array($hosts);
513         foreach($hosts as $host) {
514                 if (! is_string($host)) $host = '';
515                 foreach ($blocklist_regex as $regex) {
516                         if (preg_match($regex, $host)) {
517                                 ++$result;
518                                 if ($asap) {
519                                         return $result;
520                                 } else {
521                                         break; // Check next host
522                                 }
523                         }
524                 }
525         }
526
527         return $result;
528 }
529
530 // Default method and threshold
531 function check_uri_spam_method()
532 {
533         return array(
534                 'quantity' => 8,                // Allow N URIs
535                 'area'     => array(
536                         'total'  => 0,          // Allow N areas
537                         'anchor' => 0,          // <a href> HTML tag
538                         'bbcode' => 0,          // [url] or [link] BBCode
539                         ),
540                 'non_uniq' => 3,                // Allow N duped (and normalized) URIs
541                 'badhost'  => TRUE,
542                 );
543 }
544
545 // TODO return TRUE or FALSE!
546 // Simple/fast spam check
547 function check_uri_spam($target = '', $method = array(), $asap = TRUE)
548 {
549         $is_spam  = FALSE;
550         $progress = array(
551                 'quantity' => 0,
552                 'area'     => array(
553                         'total'  => 0,
554                         'anchor' => 0,
555                         'bbcode' => 0,
556                         ),
557                 'non_uniq' => 0,
558                 'uniqhost' => 0,
559                 'badhost'  => 0,
560                 );
561
562         if (! is_array($method) || empty($method)) {
563                 $method = check_uri_spam_method();
564         }
565
566         if (is_array($target)) {
567                 foreach($target as $str) {
568                         // Recurse
569                         list($is_spam, $_progress) = check_uri_spam($str, $method);
570                         $progress['quantity'] += $_progress['quantity'];
571                         $progress['area']['total']  += $_progress['area']['total'];
572                         $progress['area']['anchor'] += $_progress['area']['anchor'];
573                         $progress['area']['bbcode'] += $_progress['area']['bbcode'];
574                         $progress['non_uniq'] += $_progress['non_uniq'];
575                         $progress['uniqhost'] += $_progress['uniqhost'];
576                         $progress['badhost']  += $_progress['badhost'];
577                         if ($asap || $is_spam) break;
578                 }
579         } else {
580                 $pickups = spam_uri_pickup($target);
581                 $progress['quantity'] += count($pickups);
582
583                 if (! empty($pickups)) {
584
585                         // URI quantity
586                         if ((! $is_spam || ! $asap) && isset($method['quantity']) &&
587                                 $progress['quantity'] > $method['quantity']) {
588                                 $is_spam = TRUE;
589                         }
590                         //var_dump($method['quantity'], $is_spam);
591
592                         // Using invalid area
593                         if ((! $is_spam || ! $asap) && isset($method['area'])) {
594                                 foreach($pickups as $pickup) {
595                                         // Total
596                                         $total = 0;
597                                         foreach ($pickup['area'] as $key => $value) {
598                                                 if ($key == 'offset') continue;
599                                                 $progress['area']['total'] += $value;
600                                                 $progress['area'][$key]    += $value;
601                                                 if ($value < 0) {
602                                                         $is_spam = TRUE;
603                                                         if ($is_spam && $asap) break;
604                                                 }
605                                                 if ($asap) break;
606                                         }
607                                 }
608                         }
609                         //var_dump($method['area'], $is_spam);
610
611                         // URI uniqueness (and removing non-uniques)
612                         if ((! $is_spam || ! $asap) && isset($method['non_uniq'])) {
613                                 $uris = array();
614                                 foreach ($pickups as $key => $pickup) {
615                                         $uris[$key] = uri_array_implode($pickup);
616                                 }
617                                 $count = count($uris);
618                                 $uris = array_unique($uris);
619                                 $progress['non_uniq'] += $count - count($uris);
620                                 if ($progress['non_uniq'] > $method['non_uniq']) {
621                                         $is_spam = TRUE;
622                                 }
623                                 if (! $asap || ! $is_spam) {
624                                         foreach (array_diff(array_keys($pickups),
625                                                 array_keys($uris)) as $remove) {
626                                                 unset($pickups[$remove]);
627                                         }
628                                 }
629                                 unset($uris);
630                                 //var_dump($uris, $pickups);
631                         }
632                         //var_dump($method['non_uniq'], $is_spam);
633
634                         // Unique host
635                         $hosts = array();
636                         foreach ($pickups as $pickup) {
637                                 $hosts[] = & $pickup['host'];
638                         }
639                         $hosts = array_unique($hosts);
640                         $progress['uniqhost'] += count($hosts);
641                         //var_dump($method['uniqhost'], $is_spam);
642
643                         // Bad host
644                         if ((! $is_spam || ! $asap) && isset($method['badhost'])) {
645                                 $count = is_badhost($hosts, $asap);
646                                 $progress['badhost'] += $count;
647                                 if ($count !== 0) $is_spam = TRUE;
648                         }
649                         //var_dump($method['badhost'], $is_spam);
650                 }
651         }
652
653         return array($is_spam, $progress);
654 }
655
656 // ---------------------
657
658 // Check User-Agent (not testing yet)
659 function is_invalid_useragent($ua_name = '' /*, $ua_vars = ''*/ )
660 {
661         return $ua_name === '';
662 }
663
664 // ---------------------
665
666 // TODO: Separate check-part(s) and mail part
667 // TODO: Mail to administrator with more measurement data?
668 // Simple/fast spam filter ($target: 'a string' or an array())
669 function pkwk_spamfilter($action, $page, $target = array('title' => ''), $method = array())
670 {
671         $is_spam = FALSE;
672
673         //$is_spam =  is_invalid_useragent('NOTYET');
674         if ($is_spam) {
675                 $action .= ' (Invalid User-Agent)';
676         } else {
677                 list($is_spam) = check_uri_spam($target, $method);
678         }
679
680         if ($is_spam) {
681                 // Mail to administrator(s)
682                 global $notify, $notify_subject;
683                 if ($notify) {
684                         $footer['ACTION'] = $action;
685                         $footer['PAGE']   = '[blocked] ' . $page;
686                         $footer['URI']    = get_script_uri() . '?' . rawurlencode($page);
687                         $footer['USER_AGENT']  = TRUE;
688                         $footer['REMOTE_ADDR'] = TRUE;
689                         pkwk_mail_notify($notify_subject,  var_export($target, TRUE), $footer);
690                         unset($footer);
691                 }
692         }
693
694         if ($is_spam) spam_exit();
695 }
696
697 // ---------------------
698
699 // Common bahavior for blocking
700 // NOTE: Call this function from various blocking feature, to disgueise the reason 'why blocked'
701 function spam_exit()
702 {
703         die("\n");
704 }
705
706 ?>