HTML tag
- 'bbcode' => $t_area, // Inside [url] or [link] BBCode
- );
+ if (isset($sum['uniqhost'])) $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;
+ }
+ }
- // Rules
- $rules = array(
- 'badhost' => TRUE, // Check badhost
- );
+ // Return if ...
+ if ($asap && $is_spam) return $progress;
- // Remove unused
- foreach (array_keys($method) as $key) {
- if ($method[$key] < 0) unset($method[$key]);
- }
- foreach (array_keys($area) as $key) {
- if ($area[$key] < 0) unset($area[$key]);
+ // ----------------------------------------
+ // URI: Bad host (Separate good/bad hosts from $hosts)
+
+ if ((! $asap || ! $is_spam) && isset($method['badhost'])) {
+ $list = get_blocklist('list');
+ $blocked = array_merge_leaves(
+ $blocked,
+ blocklist_distiller($hosts, array_keys($list), $asap),
+ FALSE
+ );
+ foreach($list as $key=>$type){
+ if (! $type) unset($blocked[$key]); // Ignore goodhost etc
+ }
+ unset($list);
+ if (! empty($blocked)) $is_spam['badhost'] = TRUE;
}
- $area = empty($area) ? array() : array('area' => $area);
- $rules = $rule ? $rules : array();
- return $method + $area + $rules;
-}
+ // Return if ...
+ //if ($asap && $is_spam) return $progress;
+ // ----------------------------------------
+ // End
-// TODO return TRUE or FALSE!
-// Simple/fast spam check
-function check_uri_spam($target = '', $method = array(), $asap = TRUE)
-{
- $is_spam = FALSE;
- $progress = array(
- 'quantity' => 0,
- 'area' => array(
- 'total' => 0,
- 'anchor' => 0,
- 'bbcode' => 0,
- ),
- 'non_uniq' => 0,
- 'uniqhost' => 0,
- 'badhost' => 0,
- '_action' => array(),
- );
+ return $progress;
+}
- if (! is_array($method) || empty($method)) {
- $method = check_uri_spam_method();
- }
+// ---------------------
+// Reporting
- 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;
- }
- } else {
- $progress['_action'][$key] = TRUE;
- }
- }
- if ($is_spam && $asap) break;
- }
+// Summarize $progress (blocked only)
+function summarize_spam_progress($progress = array(), $blockedonly = FALSE)
+{
+ if ($blockedonly) {
+ $tmp = array_keys($progress['is_spam']);
} 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;
- }
- //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;
+ $tmp = array();
+ $method = & $progress['method'];
+ if (isset($progress['sum'])) {
+ foreach ($progress['sum'] as $key => $value) {
+ if (isset($method[$key]) && $value) {
+ $tmp[] = $key . '(' . $value . ')';
}
}
- //var_dump($method['area'], $is_spam);
+ }
+ }
- // URI uniqueness (and removing non-uniques)
- if ((! $is_spam || ! $asap) && isset($method['non_uniq'])) {
- $uris = array();
- foreach ($pickups as $key => $pickup) {
- $uris[$key] = uri_array_implode($pickup);
- }
- $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;
- }
- if (! $asap || ! $is_spam) {
- foreach (array_diff(array_keys($pickups),
- array_keys($uris)) as $remove) {
- unset($pickups[$remove]);
- }
- }
- unset($uris);
- }
- //var_dump($method['non_uniq'], $is_spam);
+ return implode(', ', $tmp);
+}
- // Unique host
- $hosts = array();
- foreach ($pickups as $pickup) {
- $hosts[] = & $pickup['host'];
- }
- $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;
- }
+function summarize_detail_badhost($progress = array())
+{
+ if (! isset($progress['blocked']) || empty($progress['blocked'])) return '';
+
+ // Flat per group
+ $blocked = array();
+ foreach($progress['blocked'] as $list => $lvalue) {
+ foreach($lvalue as $group => $gvalue) {
+ $flat = implode(', ', array_flat_leaves($gvalue));
+ if ($flat === $group) {
+ $blocked[$list][] = $flat;
+ } else {
+ $blocked[$list][$group] = $flat;
}
- //var_dump($method['badhost'], $is_spam);
}
}
- return array($is_spam, $progress);
-}
+ // Shrink per list
+ // From: 'A-1' => array('ie.to')
+ // To: 'A-1' => 'ie.to'
+ foreach($blocked as $list => $lvalue) {
+ if (is_array($lvalue) &&
+ count($lvalue) == 1 &&
+ is_numeric(key($lvalue))) {
+ $blocked[$list] = current($lvalue);
+ }
+ }
-// ---------------------
-// Reporting
+ return var_export_shrink($blocked, TRUE, TRUE);
+}
-// TODO: Show all
-// Summarize $progress (blocked only)
-function summarize_check_uri_spam_progress($progress = array(), $shownum = TRUE)
+function summarize_detail_newtral($progress = array())
{
- //$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 (! isset($progress['hosts']) ||
+ ! is_array($progress['hosts']) ||
+ empty($progress['hosts'])) return '';
+
+ // Generate a responsible $trie
+ $trie = array();
+ foreach($progress['hosts'] as $value) {
+ // 'A.foo.bar.example.com'
+ $resp = whois_responsibility($value); // 'example.com'
+ if (empty($resp)) {
+ // One or more test, or do nothing here
+ $resp = strval($value);
+ $rest = '';
} else {
- $tmp[] = $_action .
- ($shownum ? '(' . $progress[$_action] . ')' : '');
+ $rest = rtrim(substr($value, 0, - strlen($resp)), '.'); // 'A.foo.bar'
}
+ $trie = array_merge_leaves($trie, array($resp => array($rest => NULL)), FALSE);
}
- return implode(', ', $tmp);
+ // Format: var_export_shrink() -like output
+ $result = array();
+ ksort_by_domain($trie);
+ foreach(array_keys($trie) as $key) {
+ ksort_by_domain($trie[$key]);
+ if (count($trie[$key]) == 1 && key($trie[$key]) == '') {
+ // Just one 'responsibility.example.com'
+ $result[] = ' \'' . $key . '\',';
+ } else {
+ // One subdomain-or-host, or several ones
+ $subs = array();
+ foreach(array_keys($trie[$key]) as $sub) {
+ if ($sub == '') {
+ $subs[] = $key; // 'example.com'
+ } else {
+ $subs[] = $sub . '. '; // 'A.foo.bar. '
+ }
+ }
+ $result[] = ' \'' . $key . '\' => \'' . implode(', ', $subs) . '\',';
+ }
+ unset($trie[$key]);
+ }
+ return
+ 'array (' . "\n" .
+ implode("\n", $result) . "\n" .
+ ')';
}
+
// ---------------------
// Exit
+// Freeing memories
+function spam_dispose()
+{
+ get_blocklist(NULL);
+ whois_responsibility(NULL);
+}
+
// 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");
+ $exit = TRUE;
+
+ switch ($mode) {
+ case '':
+ echo("\n");
+ break;
+ case 'dump':
+ echo('' . "\n");
+ echo htmlsc(var_export($data, TRUE));
+ echo('
' . "\n");
+ break;
+ };
+
+ if ($exit) exit; // Force 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;
+ $progress = check_uri_spam($target, $method);
- list($is_spam, $progress) = check_uri_spam($target, $method, $asap);
+ if (empty($progress['is_spam'])) {
+ spam_dispose();
+ } else {
- // Mail to administrator(s)
- if ($is_spam) {
- if ($notify) {
- pkwk_spamnotify($action, $page, $target, $progress);
- }
- spam_exit();
+// TODO: detect encoding from $target for mbstring functions
+// $tmp = array();
+// foreach(array_keys($target) as $key) {
+// $tmp[strings($key, 0, FALSE, TRUE)] = strings($target[$key], 0, FALSE, TRUE); // Removing "\0" etc
+// }
+// $target = & $tmp;
+
+ pkwk_spamnotify($action, $page, $target, $progress, $method);
+ spam_exit($exitmode, $progress);
}
}
@@ -763,18 +702,31 @@ 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['BLOCKED'] = 'Blocked by: ' .
- summarize_check_uri_spam_progress($progress);
- $footer['ACTION'] = 'Blocked: ' . $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);
+ }
+
+ $tmp = summarize_detail_badhost($progress);
+ if ($tmp != '') $summary['DETAIL_BADHOST'] = $tmp;
+
+ $tmp = summarize_detail_newtral($progress);
+ if (! $asap && $tmp != '') $summary['DETAIL_NEUTRAL_HOST'] = $tmp;
+
+ $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, TRUE);
}
?>