<?php
-// $Id: spam.php,v 1.55 2006/12/03 03:33:28 henoheno Exp $
+// $Id: spam.php,v 1.63 2006/12/10 02:31:18 henoheno Exp $
// Copyright (C) 2006 PukiWiki Developers Team
// License: GPL v2 or (at your option) any later version
if (! defined('SPAM_INI_FILE')) define('SPAM_INI_FILE', 'spam.ini.php');
+// ---------------------
+// URI pickup
+
// Return an array of URIs in the $string
// [OK] http://nasty.example.org#nasty_string
// [OK] http://nasty.example.org:80/foo/xxx#nasty_string/bar
$_uri['host'] = strtolower($_uri['host']);
$_uri['port'] = port_normalize($_uri['port'], $_uri['scheme'], FALSE);
$_uri['path'] = path_normalize($_uri['path']);
- $_uri['query'] = query_normalize($_uri['query']);
if ($preserve_rawuri) $_uri['rawuri'] = & $_uri[0];
// DEBUG
$_uri['fragment']
);
}
+
+ // Area offset for area_measure()
$_uri['area']['offset'] = $offset;
}
return $array;
}
+// Destructive normalize of URI array
+// NOTE: Give me the uri_pickup() result with chunks
+function uri_array_normalize(& $pickups, $preserve = TRUE)
+{
+ if (! is_array($pickups)) return $pickups;
+
+ foreach (array_keys($pickups) as $key) {
+ $_key = & $pickups[$key];
+ $_key['path'] = isset($_key['path']) ? strtolower($_key['path']) : '';
+ $_key['file'] = isset($_key['file']) ? file_normalize($_key['file']) : '';
+ $_key['query'] = isset($_key['query']) ? query_normalize(strtolower($_key['query']), TRUE) : '';
+ $_key['fragment'] = (isset($_key['fragment']) && $preserve) ?
+ strtolower($_key['fragment']) : ''; // Just ignore
+ }
+
+ return $pickups;
+}
+
+// An URI array => An URI (See uri_pickup())
+function uri_array_implode($uri = array())
+{
+ if (empty($uri) || ! is_array($uri)) return NULL;
+
+ $tmp = array();
+ if (isset($uri['scheme']) && $uri['scheme'] !== '') {
+ $tmp[] = & $uri['scheme'];
+ $tmp[] = '://';
+ }
+ if (isset($uri['userinfo']) && $uri['userinfo'] !== '') {
+ $tmp[] = & $uri['userinfo'];
+ $tmp[] = '@';
+ }
+ if (isset($uri['host']) && $uri['host'] !== '') {
+ $tmp[] = & $uri['host'];
+ }
+ if (isset($uri['port']) && $uri['port'] !== '') {
+ $tmp[] = ':';
+ $tmp[] = & $uri['port'];
+ }
+ if (isset($uri['path']) && $uri['path'] !== '') {
+ $tmp[] = & $uri['path'];
+ }
+ if (isset($uri['file']) && $uri['file'] !== '') {
+ $tmp[] = & $uri['file'];
+ }
+ if (isset($uri['query']) && $uri['query'] !== '') {
+ $tmp[] = '?';
+ $tmp[] = & $uri['query'];
+ }
+ if (isset($uri['fragment']) && $uri['fragment'] !== '') {
+ $tmp[] = '#';
+ $tmp[] = & $uri['fragment'];
+ }
+
+ return implode('', $tmp);
+}
+
+// $array['something'] => $array['wanted']
+function array_rename_keys(& $array, $keys = array('from' => 'to'), $force = FALSE, $default = '')
+{
+ if (! is_array($array) || ! is_array($keys)) return FALSE;
+
+ // Nondestructive test
+ if (! $force)
+ foreach(array_keys($keys) as $from)
+ if (! isset($array[$from]))
+ return FALSE;
+
+ foreach($keys as $from => $to) {
+ if ($from === $to) continue;
+ if (! $force || isset($array[$from])) {
+ $array[$to] = & $array[$from];
+ unset($array[$from]);
+ } else {
+ $array[$to] = $default;
+ }
+ }
+
+ return TRUE;
+}
+
+// ---------------------
+// Area pickup
+
+// Pickup all of markup areas
+function area_pickup($string = '', $method = array())
+{
+ $area = array();
+
+ // Anchor tags by preg_match_all()
+ // [OK] <a href=></a>
+ // [OK] <a href= >Good site!</a>
+ // [OK] <a href= "#" >test</a>
+ // [OK] <a href="http://nasty.example.com">visit http://nasty.example.com/</a>
+ // [OK] <a href=\'http://nasty.example.com/\' >discount foobar</a>
+ // [NG] <a href="http://ng.example.com">visit http://ng.example.com _not_ended_
+ if (isset($method['anchor'])) {
+ $areas = array();
+ preg_match_all('#<a\b[^>]*\bhref\b[^>]*>.*?</a\b[^>]*(>)#i',
+ $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
+ //var_dump(recursive_map('htmlspecialchars', $areas));
+ foreach(array_keys($areas) as $_area) {
+ $areas[$_area] = array(
+ $areas[$_area][0][1], // Area start (<a href>)
+ $areas[$_area][1][1], // Area end (</a>)
+ );
+ }
+ if (! empty($areas)) $area['anchor'] = $areas;
+ }
+
+ // phpBB's "BBCode" by preg_match_all()
+ // [OK] [url][/url]
+ // [OK] [url]http://nasty.example.com/[/url]
+ // [OK] [link]http://nasty.example.com/[/link]
+ // [OK] [url=http://nasty.example.com]visit http://nasty.example.com/[/url]
+ // [OK] [link http://nasty.example.com/]buy something[/link]
+ if (isset($method['bbcode'])) {
+ $areas = array();
+ preg_match_all('#\[(url|link)\b[^\]]*\].*?\[/\1\b[^\]]*(\])#i',
+ $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
+ //var_dump(recursive_map('htmlspecialchars', $areas));
+ foreach(array_keys($areas) as $_area) {
+ $areas[$_area] = array(
+ $areas[$_area][0][1], // Area start ([url])
+ $areas[$_area][2][1], // Area end ([/url])
+ );
+ }
+ if (! empty($areas)) $area['bbcode'] = $areas;
+ }
+
+ // Various Wiki syntax
+ // [text_or_uri>text_or_uri]
+ // [text_or_uri:text_or_uri]
+ // [text_or_uri|text_or_uri]
+ // [text_or_uri->text_or_uri]
+ // [text_or_uri text_or_uri] // MediaWiki
+ // MediaWiki: [http://nasty.example.com/ visit http://nasty.example.com/]
+
+ return $area;
+}
+
+// If in doubt, it's a little doubtful
+function area_measure($areas, & $array, $belief = -1, $a_key = 'area', $o_key = 'offset')
+{
+ if (! is_array($areas) || ! is_array($array)) return;
+
+ $areas_keys = array_keys($areas);
+ foreach(array_keys($array) as $u_index) {
+ $offset = isset($array[$u_index][$o_key]) ?
+ intval($array[$u_index][$o_key]) : 0;
+ foreach($areas_keys as $a_index) {
+ if (isset($array[$u_index][$a_key])) {
+ $offset_s = intval($areas[$a_index][0]);
+ $offset_e = intval($areas[$a_index][1]);
+ // [Area => inside <= Area]
+ if ($offset_s < $offset && $offset < $offset_e) {
+ $array[$u_index][$a_key] += $belief;
+ }
+ }
+ }
+ }
+}
+
+// ---------------------
+// Spam-uri pickup
+
// Domain exposure callback (See spam_uri_pickup_preprocess())
// http://victim.example.org/?foo+site:nasty.example.com+bar
// => http://nasty.example.com/?refer=victim.example.org
// Area elevation for '(especially external)link' intension
if (! empty($array)) {
-
- $area_shadow = array();
- foreach(array_keys($array) as $key){
- $area_shadow[$key] = & $array[$key]['area'];
- $area_shadow[$key]['anchor'] = 0;
- $area_shadow[$key]['bbcode'] = 0;
- }
-
- // Anchor tags by preg_match_all()
- // [OK] <a href="http://nasty.example.com">visit http://nasty.example.com/</a>
- // [OK] <a href=\'http://nasty.example.com/\' >discount foobar</a>
- // [NG] <a href="http://ng.example.com">visit http://ng.example.com _not_ended_
- // [NG] <a href= >Good site!</a> <a href= "#" >test</a>
- if (isset($area['anchor'])) {
- $areas = array();
- preg_match_all('#<a\b[^>]*href[^>]*>.*?</a\b[^>]*(>)#i',
- $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
- //var_dump(recursive_map('htmlspecialchars', $areas));
- foreach(array_keys($areas) as $_area) {
- $areas[$_area] = array(
- $areas[$_area][0][1], // Area start (<a href>)
- $areas[$_area][1][1], // Area end (</a>)
- );
+ $areas = area_pickup($string, $area);
+ if (! empty($areas)) {
+ $area_shadow = array();
+ foreach(array_keys($array) as $key){
+ $area_shadow[$key] = & $array[$key]['area'];
+ $area_shadow[$key]['anchor'] = 0;
+ $area_shadow[$key]['bbcode'] = 0;
}
- area_measure($areas, $area_shadow, 1, 'anchor');
- }
-
- // phpBB's "BBCode" by preg_match_all()
- // [url]http://nasty.example.com/[/url]
- // [link]http://nasty.example.com/[/link]
- // [url=http://nasty.example.com]visit http://nasty.example.com/[/url]
- // [link http://nasty.example.com/]buy something[/link]
- // ?? [url=][/url]
- if (isset($area['bbcode'])) {
- $areas = array();
- preg_match_all('#\[(url|link)\b[^\]]*\].*?\[/\1\b[^\]]*(\])#i',
- $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
- //var_dump(recursive_map('htmlspecialchars', $areas));
- foreach(array_keys($areas) as $_area) {
- $areas[$_area] = array(
- $areas[$_area][0][1], // Area start ([url])
- $areas[$_area][2][1], // Area end ([/url])
- );
+ if (isset($areas['anchor'])) {
+ area_measure($areas['anchor'], $area_shadow, 1, 'anchor');
+ }
+ if (isset($areas['bbcode'])) {
+ area_measure($areas['bbcode'], $area_shadow, 1, 'bbcode');
}
- area_measure($areas, $area_shadow, 1, 'bbcode');
- }
-
- // Various Wiki syntax
- // [text_or_uri>text_or_uri]
- // [text_or_uri:text_or_uri]
- // [text_or_uri|text_or_uri]
- // [text_or_uri->text_or_uri]
- // [text_or_uri text_or_uri] // MediaWiki
- // MediaWiki: [http://nasty.example.com/ visit http://nasty.example.com/]
-
- // Remove 'offset's for area_measure()
- foreach(array_keys($array) as $key)
- unset($array[$key]['area']['offset']);
- }
-
- return $array;
-}
-
-// $array['something'] => $array['wanted']
-function array_rename_keys(& $array, $keys = array('from' => 'to'), $force = FALSE, $default = '')
-{
- if (! is_array($array) || ! is_array($keys)) return FALSE;
-
- // Nondestructive test
- if (! $force)
- foreach(array_keys($keys) as $from)
- if (! isset($array[$from]))
- return FALSE;
-
- foreach($keys as $from => $to) {
- if ($from === $to) continue;
- if (! $force || isset($array[$from])) {
- $array[$to] = & $array[$from];
- unset($array[$from]);
- } else {
- $array[$to] = $default;
}
}
- return TRUE;
-}
-
-// If in doubt, it's a little doubtful
-function area_measure($areas, & $array, $belief = -1, $a_key = 'area', $o_key = 'offset')
-{
- if (! is_array($areas) || ! is_array($array)) return;
+ // Remove 'offset's for area_measure()
+ foreach(array_keys($array) as $key)
+ unset($array[$key]['area']['offset']);
- $areas_keys = array_keys($areas);
- foreach(array_keys($array) as $u_index) {
- $offset = isset($array[$u_index][$o_key]) ?
- intval($array[$u_index][$o_key]) : 0;
- foreach($areas_keys as $a_index) {
- if (isset($array[$u_index][$a_key])) {
- $offset_s = intval($areas[$a_index][0]);
- $offset_e = intval($areas[$a_index][1]);
- // [Area => inside <= Area]
- if ($offset_s < $offset && $offset < $offset_e) {
- $array[$u_index][$a_key] += $belief;
- }
- }
- }
- }
+ return $array;
}
// ---------------------
-// Part Two
+// Normalization
// Scheme normalization: Renaming the schemes
// snntp://example.org => nntps://example.org
// http://example.org/path/../../a/../back => http://example.org/back
function path_normalize($path = '', $divider = '/', $addroot = TRUE)
{
- if (! is_string($path) || $path == '') {
- $path = $addroot ? $divider : '';
- } else {
- $path = trim($path);
- $last = ($path[strlen($path) - 1] == $divider) ? $divider : '';
- $array = explode($divider, $path);
-
- // Remove paddings
- foreach(array_keys($array) as $key) {
- if ($array[$key] == '' || $array[$key] == '.')
- unset($array[$key]);
- }
- // Back-track
- $tmp = array();
- foreach($array as $value) {
- if ($value == '..') {
- array_pop($tmp);
- } else {
- array_push($tmp, $value);
- }
- }
- $array = & $tmp;
+ if (! is_string($path) || $path == '')
+ return $addroot ? $divider : '';
+
+ $path = trim($path);
+ $last = ($path[strlen($path) - 1] == $divider) ? $divider : '';
+ $array = explode($divider, $path);
- $path = $addroot ? $divider : '';
- if (! empty($array)) $path .= implode($divider, $array) . $last;
+ // Remove paddings
+ foreach(array_keys($array) as $key) {
+ if ($array[$key] == '' || $array[$key] == '.')
+ unset($array[$key]);
+ }
+ // Back-track
+ $tmp = array();
+ foreach($array as $value) {
+ if ($value == '..') {
+ array_pop($tmp);
+ } else {
+ array_push($tmp, $value);
+ }
}
+ $array = & $tmp;
+
+ $path = $addroot ? $divider : '';
+ if (! empty($array)) $path .= implode($divider, $array) . $last;
return $path;
}
-// Sort query-strings if possible
+// DirectoryIndex normalize (Destructive and rough)
+function file_normalize($string = 'index.html.en')
+{
+ static $array = array(
+ 'index' => TRUE, // Some system can omit the suffix
+ 'index.htm' => TRUE,
+ 'index.html' => TRUE,
+ 'index.shtml' => TRUE,
+ 'index.jsp' => TRUE,
+ 'index.php' => TRUE,
+ 'index.php3' => TRUE,
+ 'index.php4' => TRUE,
+ //'index.pl' => TRUE,
+ //'index.py' => TRUE,
+ //'index.rb' => TRUE,
+ 'index.cgi' => TRUE,
+ 'default.htm' => TRUE,
+ 'default.html' => TRUE,
+ 'default.asp' => TRUE,
+ 'default.aspx' => TRUE,
+ );
+
+ // Content-negothiation filter:
+ // Roughly removing ISO 639 -like
+ // 2-letter suffixes (See RFC3066)
+ $matches = array();
+ if (preg_match('/(.*)\.[a-z][a-z](?:-[a-z][a-z])?$/i', $string, $matches)) {
+ $_string = $matches[1];
+ } else {
+ $_string = & $string;
+ }
+
+ if (isset($array[strtolower($_string)])) {
+ return '';
+ } else {
+ return $string;
+ }
+}
+
+// Sort query-strings if possible (Destructive and rough)
// [OK] &&&&f=d&b&d&c&a=0dd => a=0dd&b&c&d&f=d
-function query_normalize($string = '')
+// [OK] nothing==&eg=dummy&eg=padding&eg=foobar => eg=foobar
+function query_normalize($string = '', $equal = FALSE, $equal_cutempty = TRUE)
{
$array = explode('&', $string);
- // Remove paddings
+ // Remove '&' paddings
foreach(array_keys($array) as $key) {
if ($array[$key] == '') {
unset($array[$key]);
}
}
- natsort($array);
- return implode('&', $array);
-}
+ // Consider '='-sepalated input and paddings
+ if ($equal) {
+ $equals = $not_equals = array();
+ foreach ($array as $part) {
+ if (strpos($part, '=') === FALSE) {
+ $not_equals[] = $part;
+ } else {
+ list($key, $value) = explode('=', $part, 2);
+ $value = ltrim($value, '=');
+ if (! $equal_cutempty || $value != '') {
+ $equals[$key] = $value;
+ }
+ }
+ }
-// An URI array => An URI (See uri_pickup())
-function uri_array_implode($uri = array())
-{
- if (empty($uri) || ! is_array($uri)) return NULL;
-
- $tmp = array();
- if (isset($uri['scheme']) && $uri['scheme'] !== '') {
- $tmp[] = & $uri['scheme'];
- $tmp[] = '://';
- }
- if (isset($uri['userinfo']) && $uri['userinfo'] !== '') {
- $tmp[] = & $uri['userinfo'];
- $tmp[] = '@';
- }
- if (isset($uri['host']) && $uri['host'] !== '') {
- $tmp[] = & $uri['host'];
- }
- if (isset($uri['port']) && $uri['port'] !== '') {
- $tmp[] = ':';
- $tmp[] = & $uri['port'];
- }
- if (isset($uri['path']) && $uri['path'] !== '') {
- $tmp[] = & $uri['path'];
- }
- if (isset($uri['file']) && $uri['file'] !== '') {
- $tmp[] = & $uri['file'];
- }
- if (isset($uri['query']) && $uri['query'] !== '') {
- $tmp[] = '?';
- $tmp[] = & $uri['query'];
- }
- if (isset($uri['fragment']) && $uri['fragment'] !== '') {
- $tmp[] = '#';
- $tmp[] = & $uri['fragment'];
+ $array = & $not_equals;
+ foreach ($equals as $key => $value) {
+ $array[] = $key . '=' . $value;
+ }
+ unset($equals);
}
- return implode('', $tmp);
+ natsort($array);
+ return implode('&', $array);
}
// ---------------------
// Areas
$area = array(
- //'total' => $t_area, // Allow N areas total, enabled below
- 'anchor' => $t_area, // Inside <a href> HTML tag
- 'bbcode' => $t_area, // Inside [url] or [link] BBCode
+ //'total' => $t_area, // Allow N areas total, enabled below
+ 'anchor' => $t_area, // Inside <a href> HTML tag
+ 'bbcode' => $t_area, // Inside [url] or [link] BBCode
);
// Rules
$rules = array(
+ 'asap' => FALSE, // Quit As Soon As Possible
+ 'uniqhost' => TRUE, // Show uniq host (at block notification mail)
'badhost' => TRUE, // Check badhost
);
return $method + $area + $rules;
}
-
-// TODO: Simplify. !empty(['_action']) just means $is_spam
+// TODO: Simplify $progress data structure
+// TODO: Simplify. !empty(['is_spam']) just means $is_spam
// Simple/fast spam check
-function check_uri_spam($target = '', $method = array(), $asap = TRUE)
+function check_uri_spam($target = '', $method = array())
{
$is_spam = FALSE;
- $progress = array(
- 'quantity' => 0,
- 'area' => array(
- 'total' => 0,
- 'anchor' => 0,
- 'bbcode' => 0,
- ),
- 'non_uniq' => 0,
- 'uniqhost' => 0,
- 'badhost' => 0,
- '_action' => array(),
- );
-
if (! is_array($method) || empty($method)) {
$method = check_uri_spam_method();
}
+ $asap = isset($method['asap']) ? $method['asap'] : TRUE;
+
+ $progress = array(
+ 'sum' => array(
+ 'quantity' => 0,
+ 'uniqhost' => 0,
+ 'non_uniq' => 0,
+ 'badhost' => 0,
+ 'area_total' => 0,
+ 'area_anchor' => 0,
+ 'area_bbcode' => 0,
+ ),
+ 'is_spam' => array(),
+ 'method' => & $method,
+ );
+
if (is_array($target)) {
// Recurse
foreach($target as $str) {
- list($_is_spam, $_progress) = check_uri_spam($str, $method, $asap);
+ list($_is_spam, $_progress) = check_uri_spam($str, $method);
$is_spam = $is_spam || $_is_spam;
- $progress['quantity'] += $_progress['quantity'];
- $progress['area']['total'] += $_progress['area']['total'];
- $progress['area']['anchor'] += $_progress['area']['anchor'];
- $progress['area']['bbcode'] += $_progress['area']['bbcode'];
- $progress['non_uniq'] += $_progress['non_uniq'];
- $progress['uniqhost'] += $_progress['uniqhost'];
- $progress['badhost'] += $_progress['badhost'];
- foreach($_progress['_action'] as $key => $value) {
- if (is_array($value)) {
- foreach(array_keys($value) as $_key) {
- $progress['_action'][$key][$_key] = TRUE;
- }
- } else {
- $progress['_action'][$key] = TRUE;
- }
+ foreach (array_keys($_progress['sum']) as $key) {
+ $progress['sum'][$key] += $_progress['sum'][$key];
+ }
+ foreach(array_keys($_progress['is_spam']) as $key) {
+ $progress['is_spam'][$key] = TRUE;
}
if ($is_spam && $asap) break;
}
} else {
$pickups = spam_uri_pickup($target);
if (! empty($pickups)) {
- $progress['quantity'] += count($pickups);
+ $progress['sum']['quantity'] += count($pickups);
// URI quantity
if ((! $is_spam || ! $asap) && isset($method['quantity']) &&
- $progress['quantity'] > $method['quantity']) {
+ $progress['sum']['quantity'] > $method['quantity']) {
$is_spam = TRUE;
- $progress['_action']['quantity'] = TRUE;
+ $progress['is_spam']['quantity'] = TRUE;
}
//var_dump($method['quantity'], $is_spam);
foreach($pickups as $pickup) {
foreach ($pickup['area'] as $key => $value) {
if ($key == 'offset') continue;
- $progress['area']['total'] += $value;
- $progress['area'][$key] += $value;
+ $p_key = 'area_' . $key;
+ $progress['sum']['area_total'] += $value;
+ $progress['sum'][$p_key] += $value;
if (isset($method['area']['total']) &&
- $progress['area']['total'] > $method['area']['total']) {
+ $progress['sum']['area_total'] > $method['area']['total']) {
$is_spam = TRUE;
- $progress['_action']['area']['total'] = TRUE;
+ $progress['is_spam']['area_total'] = TRUE;
if ($is_spam && $asap) break;
}
if(isset($method['area'][$key]) &&
- $progress['area'][$key] > $method['area'][$key]) {
+ $progress['sum'][$p_key] > $method['area'][$key]) {
$is_spam = TRUE;
- $progress['_action']['area'][$key] = TRUE;
+ $progress['is_spam'][$p_key] = TRUE;
if ($is_spam && $asap) break;
}
}
}
//var_dump($method['area'], $is_spam);
+
// URI uniqueness (and removing non-uniques)
if ((! $is_spam || ! $asap) && isset($method['non_uniq'])) {
// Destructive normalize of URIs
- foreach (array_keys($pickups) as $key) {
- $pickups[$key]['path'] = strtolower($pickups[$key]['path']);
- $pickups[$key]['file'] = strtolower($pickups[$key]['file']);
- $pickups[$key]['query'] = strtolower($pickups[$key]['query']);
- $pickups[$key]['fragment'] = ''; // Just ignore
- }
+ uri_array_normalize($pickups);
$uris = array();
foreach (array_keys($pickups) as $key) {
$uris[$key] = uri_array_implode($pickups[$key]);
}
$count = count($uris);
- $uris = array_unique($uris);
- $progress['non_uniq'] += $count - count($uris);
- if ($progress['non_uniq'] > $method['non_uniq']) {
+ $uris = array_unique($uris);
+ $progress['sum']['non_uniq'] += $count - count($uris);
+ if ($progress['sum']['non_uniq'] > $method['non_uniq']) {
$is_spam = TRUE;
- $progress['_action']['non_uniq'] = TRUE;
+ $progress['is_spam']['non_uniq'] = TRUE;
}
if (! $asap || ! $is_spam) {
foreach (array_diff(array_keys($pickups),
$hosts[] = & $pickup['host'];
}
$hosts = array_unique($hosts);
- $progress['uniqhost'] += count($hosts);
+ $progress['sum']['uniqhost'] += count($hosts);
//var_dump($method['uniqhost'], $is_spam);
// Bad host
if ((! $is_spam || ! $asap) && isset($method['badhost'])) {
$count = is_badhost($hosts, $asap);
- $progress['badhost'] += $count;
+ $progress['sum']['badhost'] += $count;
if ($count !== 0) {
- $progress['_action']['badhost'] = TRUE;
+ $progress['is_spam']['badhost'] = TRUE;
$is_spam = TRUE;
}
}
// ---------------------
// Reporting
-// TODO: Show all
+// TODO: Don't show unused $method!
// Summarize $progress (blocked only)
-function summarize_check_uri_spam_progress($progress = array(), $shownum = TRUE)
+function summarize_spam_progress($progress = array(), $blockedonly = FALSE)
{
- //$list = check_uri_spam_method();
+ $method = $progress['method'];
+ if (isset($method['area'])) {
+ foreach(array_keys($method['area']) as $key) {
+ $method['area_' . $key] = TRUE;
+ }
+ }
- $tmp = array();
- foreach (array_keys($progress['_action']) as $_action) {
- if (is_array($progress['_action'][$_action])) {
- foreach (array_keys($progress['_action'][$_action]) as $_area) {
- $tmp[] = $_action . '=>' . $_area .
- ($shownum ? '(' . $progress[$_action][$_area] . ')' : '');
+ if ($blockedonly) {
+ $tmp = array_keys($progress['is_spam']);
+ } else {
+ $tmp = array();
+ foreach ($progress['sum'] as $key => $value) {
+ if (isset($method[$key])) {
+ $tmp[] = $key . '(' . $value . ')';
}
- } else {
- $tmp[] = $_action .
- ($shownum ? '(' . $progress[$_action] . ')' : '');
}
}
// ---------------------
-//
+// Simple filtering
+// TODO: Record them
// Simple/fast spam filter ($target: 'a string' or an array())
-function pkwk_spamfilter($action, $page, $target = array('title' => ''), $method = array(), $asap = FALSE)
+function pkwk_spamfilter($action, $page, $target = array('title' => ''), $method = array())
{
global $notify;
- list($is_spam, $progress) = check_uri_spam($target, $method, $asap);
+ list($is_spam, $progress) = check_uri_spam($target, $method);
if ($is_spam) {
// Mail to administrator(s)
- if ($notify) pkwk_spamnotify($action, $page, $target, $progress);
+ if ($notify) pkwk_spamnotify($action, $page, $target, $progress, $method);
// End
spam_exit();
}
// PukiWiki original
// Mail to administrator(s)
-function pkwk_spamnotify($action, $page, $target = array('title' => ''), $progress = array())
+function pkwk_spamnotify($action, $page, $target = array('title' => ''), $progress = array(), $method = array())
{
global $notify_subject;
- $footer['ACTION'] = 'Blocked by: ' . summarize_check_uri_spam_progress($progress);
+ $asap = isset($method['asap']) ? $method['asap'] : TRUE;
+
+ $footer['ACTION'] = 'Blocked by: ' . summarize_spam_progress($progress, TRUE);
+
+ if (! $asap) {
+ $footer['METRICS'] = summarize_spam_progress($progress);
+ }
$footer['COMMENT'] = $action;
- $footer['PAGE'] = '[blocked] ' . $page;
- $footer['URI'] = get_script_uri() . '?' . rawurlencode($page);
+ $footer['PAGE'] = '[blocked] ' . $page;
+ $footer['URI'] = get_script_uri() . '?' . rawurlencode($page);
$footer['USER_AGENT'] = TRUE;
$footer['REMOTE_ADDR'] = TRUE;
pkwk_mail_notify($notify_subject, var_export($target, TRUE), $footer);