HTML tag
+ //'uri_bbcode' => $t_area, // URI inside [url] or [link] BBCode
);
-
- // Rules
- $rules = array(
- 'badhost' => TRUE, // Check badhost
- );
-
- // Remove unused
- foreach (array_keys($method) as $key) {
- if ($method[$key] < 0) unset($method[$key]);
+ if ($rule) {
+ $bool = array(
+ // Rules
+ //'asap' => TRUE, // Quit or return As Soon As Possible
+ 'uniqhost' => TRUE, // Show uniq host (at block notification mail)
+ 'badhost' => TRUE, // Check badhost
+ );
+ } else {
+ $bool = array();
}
- foreach (array_keys($area) as $key) {
- if ($area[$key] < 0) unset($area[$key]);
+
+ // Remove non-$positive values
+ foreach (array_keys($positive) as $key) {
+ if ($positive[$key] < 0) unset($positive[$key]);
}
- $area = empty($area) ? array() : array('area' => $area);
- $rules = $rule ? $rules : array();
- return $method + $area + $rules;
+ return $positive + $bool;
}
-
-// TODO: Simplify. !empty(['_action']) 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();
}
+ $progress = array(
+ 'sum' => array(
+ 'quantity' => 0,
+ 'uniqhost' => 0,
+ 'non_uniqhost'=> 0,
+ 'non_uniquri' => 0,
+ 'badhost' => 0,
+ 'area_anchor' => 0,
+ 'area_bbcode' => 0,
+ 'uri_anchor' => 0,
+ 'uri_bbcode' => 0,
+ ),
+ 'is_spam' => array(),
+ 'method' => & $method,
+ 'remains' => array(),
+ );
+ $sum = & $progress['sum'];
+ $is_spam = & $progress['is_spam'];
+ $remains = & $progress['remains'];
+ $asap = isset($method['asap']);
+ // Return if ...
if (is_array($target)) {
- // Recurse
foreach($target as $str) {
- list($_is_spam, $_progress) = check_uri_spam($str, $method, $asap);
- $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;
+ // Recurse
+ $_progress = check_uri_spam($str, $method);
+ $_sum = & $_progress['sum'];
+ $_is_spam = & $_progress['is_spam'];
+ $_remains = & $_progress['remains'];
+ foreach (array_keys($_sum) as $key) {
+ $sum[$key] += $_sum[$key];
+ }
+ foreach (array_keys($_is_spam) as $key) {
+ if (is_array($_is_spam[$key])) {
+ // Marge keys (badhost)
+ foreach(array_keys($_is_spam[$key]) as $_key) {
+ if (! isset($is_spam[$key][$_key])) {
+ $is_spam[$key][$_key] = $_is_spam[$key][$_key];
+ } else {
+ $is_spam[$key][$_key] += $_is_spam[$key][$_key];
+ }
}
} else {
- $progress['_action'][$key] = TRUE;
+ $is_spam[$key] = TRUE;
}
}
- if ($is_spam && $asap) break;
+ foreach ($_remains as $key=>$value) {
+ foreach ($value as $_key=>$_value) {
+ $remains[$key][$_key] = $_value;
+ }
+ }
+ if ($asap && $is_spam) break;
}
- } else {
- $pickups = spam_uri_pickup($target);
- if (! empty($pickups)) {
- $progress['quantity'] += count($pickups);
-
- // URI quantity
- if ((! $is_spam || ! $asap) && isset($method['quantity']) &&
- $progress['quantity'] > $method['quantity']) {
- $is_spam = TRUE;
- $progress['_action']['quantity'] = TRUE;
+ return $progress;
+ }
+
+ // Area: There's HTML anchor tag
+ if ((! $asap || ! $is_spam) && isset($method['area_anchor'])) {
+ $key = 'area_anchor';
+ $_asap = isset($method['asap']) ? array('asap' => TRUE) : array();
+ $result = area_pickup($target, array($key => TRUE) + $_asap);
+ if ($result) {
+ $sum[$key] = $result[$key];
+ if (isset($method[$key]) && $sum[$key] > $method[$key]) {
+ $is_spam[$key] = TRUE;
}
- //var_dump($method['quantity'], $is_spam);
-
- // Using invalid area
- if ((! $is_spam || ! $asap) && isset($method['area'])) {
- foreach($pickups as $pickup) {
- foreach ($pickup['area'] as $key => $value) {
- if ($key == 'offset') continue;
- $progress['area']['total'] += $value;
- $progress['area'][$key] += $value;
- if (isset($method['area']['total']) &&
- $progress['area']['total'] > $method['area']['total']) {
- $is_spam = TRUE;
- $progress['_action']['area']['total'] = TRUE;
- if ($is_spam && $asap) break;
- }
- if(isset($method['area'][$key]) &&
- $progress['area'][$key] > $method['area'][$key]) {
- $is_spam = TRUE;
- $progress['_action']['area'][$key] = TRUE;
- if ($is_spam && $asap) break;
- }
- }
- if ($is_spam && $asap) break;
- }
+ }
+ }
+
+ // Area: There's 'BBCode' linking tag
+ if ((! $asap || ! $is_spam) && isset($method['area_bbcode'])) {
+ $key = 'area_bbcode';
+ $_asap = isset($method['asap']) ? array('asap' => TRUE) : array();
+ $result = area_pickup($target, array($key => TRUE) + $_asap);
+ if ($result) {
+ $sum[$key] = $result[$key];
+ if (isset($method[$key]) && $sum[$key] > $method[$key]) {
+ $is_spam[$key] = TRUE;
}
- //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(query_normalize($pickups[$key]['query'], TRUE));
- $pickups[$key]['fragment'] = ''; // Just ignore
- }
+ }
+ }
- $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']) {
- $is_spam = TRUE;
- $progress['_action']['non_uniq'] = TRUE;
+ // Return if ...
+ if ($asap && $is_spam) {
+ return $progress;
+ }
+ // URI Init
+ $pickups = spam_uri_pickup($target, $method);
+ if (empty($pickups)) {
+ return $progress;
+ }
+
+ // URI: Check quantity
+ $sum['quantity'] += count($pickups);
+ // URI quantity
+ if ((! $asap || ! $is_spam) && isset($method['quantity']) &&
+ $sum['quantity'] > $method['quantity']) {
+ $is_spam['quantity'] = TRUE;
+ }
+
+ // URI: used inside HTML anchor tag pair
+ if ((! $asap || ! $is_spam) && isset($method['uri_anchor'])) {
+ $key = 'uri_anchor';
+ foreach($pickups as $pickup) {
+ if (isset($pickup['area'][$key])) {
+ $sum[$key] += $pickup['area'][$key];
+ if(isset($method[$key]) &&
+ $sum[$key] > $method[$key]) {
+ $is_spam[$key] = TRUE;
+ if ($asap && $is_spam) break;
}
- if (! $asap || ! $is_spam) {
- foreach (array_diff(array_keys($pickups),
- array_keys($uris)) as $remove) {
- unset($pickups[$remove]);
- }
+ if ($asap && $is_spam) break;
+ }
+ }
+ }
+
+ // URI: used inside 'BBCode' pair
+ if ((! $asap || ! $is_spam) && isset($method['uri_bbcode'])) {
+ $key = 'uri_bbcode';
+ foreach($pickups as $pickup) {
+ if (isset($pickup['area'][$key])) {
+ $sum[$key] += $pickup['area'][$key];
+ if(isset($method[$key]) &&
+ $sum[$key] > $method[$key]) {
+ $is_spam[$key] = TRUE;
+ if ($asap && $is_spam) break;
}
- unset($uris);
+ if ($asap && $is_spam) break;
}
- //var_dump($method['non_uniq'], $is_spam);
+ }
+ }
+
+ // URI: Uniqueness (and removing non-uniques)
+ if ((! $asap || ! $is_spam) && isset($method['non_uniquri'])) {
- // Unique host
- $hosts = array();
- foreach ($pickups as $pickup) {
- $hosts[] = & $pickup['host'];
+ // Destructive normalize of URIs
+ 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);
+ $sum['non_uniquri'] += $count - count($uris);
+ if ($sum['non_uniquri'] > $method['non_uniquri']) {
+ $is_spam['non_uniquri'] = TRUE;
+ }
+ if (! $asap || ! $is_spam) {
+ foreach (array_diff(array_keys($pickups),
+ array_keys($uris)) as $remove) {
+ unset($pickups[$remove]);
}
- $hosts = array_unique($hosts);
- $progress['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;
- if ($count !== 0) {
- $progress['_action']['badhost'] = TRUE;
- $is_spam = TRUE;
+ }
+ unset($uris);
+ }
+
+ // Return if ...
+ if ($asap && $is_spam) {
+ return $progress;
+ }
+
+ // Host: Uniqueness (uniq / non-uniq)
+ $hosts = array();
+ foreach ($pickups as $pickup) $hosts[] = & $pickup['host'];
+ $hosts = array_unique($hosts);
+ $sum['uniqhost'] += count($hosts);
+ if ((! $asap || ! $is_spam) && isset($method['non_uniqhost'])) {
+ $sum['non_uniqhost'] = $sum['quantity'] - $sum['uniqhost'];
+ if ($sum['non_uniqhost'] > $method['non_uniqhost']) {
+ $is_spam['non_uniqhost'] = TRUE;
+ }
+ }
+
+ // Return if ...
+ if ($asap && $is_spam) {
+ return $progress;
+ }
+
+ // URI: Bad host
+ if ((! $asap || ! $is_spam) && isset($method['badhost'])) {
+ $__remains = array();
+ if ($asap) {
+ $badhost = is_badhost($hosts, $asap, $__remains);
+ } else {
+ $badhost = is_badhost($hosts, $asap, $__remains);
+ if ($__remains) {
+ $progress['remains']['badhost'] = array();
+ foreach ($__remains as $value) {
+ $progress['remains']['badhost'][$value] = TRUE;
}
}
- //var_dump($method['badhost'], $is_spam);
+ }
+ unset($__remains);
+ if (! empty($badhost)) {
+ $sum['badhost'] += array_count_leaves($badhost);
+ foreach(array_keys($badhost) as $keys) {
+ $is_spam['badhost'][$keys] =
+ array_count_leaves($badhost[$keys]);
+ }
+ unset($badhost);
}
}
- return array($is_spam, $progress);
+ return $progress;
+}
+
+// Count leaves
+function array_count_leaves($array = array(), $count_empty_array = FALSE)
+{
+ if (! is_array($array) || (empty($array) && $count_empty_array))
+ return 1;
+
+ // Recurse
+ $result = 0;
+ foreach ($array as $part) {
+ $result += array_count_leaves($part, $count_empty_array);
+ }
+ return $result;
}
// ---------------------
// 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();
-
- $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();
+ $method = & $progress['method'];
+ if (isset($progress['sum'])) {
+ foreach ($progress['sum'] as $key => $value) {
+ if (isset($method[$key])) {
+ $tmp[] = $key . '(' . $value . ')';
+ }
}
- } else {
- $tmp[] = $_action .
- ($shownum ? '(' . $progress[$_action] . ')' : '');
}
}
@@ -794,27 +1022,37 @@ function summarize_check_uri_spam_progress($progress = array(), $shownum = TRUE)
// Common bahavior for blocking
// NOTE: Call this function from various blocking feature, to disgueise the reason 'why blocked'
-function spam_exit()
+function spam_exit($mode = '', $data = array())
{
- die("\n");
+ switch ($mode) {
+ case '': echo("\n"); break;
+ case 'dump':
+ echo('' . "\n");
+ echo htmlspecialchars(var_export($data, TRUE));
+ echo('
' . "\n");
+ break;
+ };
+
+ // Force exit
+ exit;
}
// ---------------------
-//
+// 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(), $exitmode = '')
{
- global $notify;
-
- list($is_spam, $progress) = check_uri_spam($target, $method, $asap);
+ $progress = check_uri_spam($target, $method);
- if ($is_spam) {
+ if (! empty($progress['is_spam'])) {
// Mail to administrator(s)
- if ($notify) pkwk_spamnotify($action, $page, $target, $progress);
- // End
- spam_exit();
+ pkwk_spamnotify($action, $page, $target, $progress, $method);
+
+ // Exit
+ spam_exit($exitmode, $progress);
}
}
@@ -822,17 +1060,41 @@ function pkwk_spamfilter($action, $page, $target = array('title' => ''), $method
// 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);
- $footer['COMMENT'] = $action;
- $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);
+ global $notify, $notify_subject;
+
+ if (! $notify) return;
+
+ $asap = isset($method['asap']);
+
+ $summary['ACTION'] = 'Blocked by: ' . summarize_spam_progress($progress, TRUE);
+ if (! $asap) {
+ $summary['METRICS'] = summarize_spam_progress($progress);
+ }
+ if (isset($progress['is_spam']['badhost'])) {
+ $badhost = array();
+ foreach($progress['is_spam']['badhost'] as $glob=>$number) {
+ $badhost[] = $glob . '(' . $number . ')';
+ }
+ $summary['DETAIL_BADHOST'] = implode(', ', $badhost);
+ }
+ if (! $asap && $progress['remains']['badhost']) {
+ $count = count($progress['remains']['badhost']);
+ $summary['DETAIL_NEUTRAL_HOST'] = $count .
+ ' (' .
+ preg_replace(
+ '/[^, a-z0-9.-]/i', '',
+ implode(', ', array_keys($progress['remains']['badhost']))
+ ) .
+ ')';
+ }
+ $summary['COMMENT'] = $action;
+ $summary['PAGE'] = '[blocked] ' . (is_pagename($page) ? $page : '');
+ $summary['URI'] = get_script_uri() . '?' . rawurlencode($page);
+ $summary['USER_AGENT'] = TRUE;
+ $summary['REMOTE_ADDR'] = TRUE;
+ pkwk_mail_notify($notify_subject, var_export($target, TRUE), $summary);
}
?>