(Separate good/bad hosts from $hosts)
+
+ if ((! $asap || ! $is_spam) && isset($method['badhost'])) {
+ $list = get_blocklist('pre');
+ $blocked = blocklist_distiller($hosts, array_keys($list), $asap);
+ foreach($list as $key => $type){
+ if (! $type) unset($blocked[$key]); // Ignore goodhost etc
+ }
+ unset($list);
+ if (! empty($blocked)) $is_spam['badhost'] = TRUE;
+ }
+
+ // Return if ...
+ if ($asap && $is_spam) return $progress;
+
+ // Remove blocked from $pickups
+ foreach(array_keys($pickups) as $key) {
+ if (! isset($hosts[$key])) {
+ unset($pickups[$key]);
+ }
+ }
+
+ // ----------------------------------------
// URI: Check quantity
+
$sum['quantity'] += count($pickups);
// URI quantity
if ((! $asap || ! $is_spam) && isset($method['quantity']) &&
@@ -1279,7 +433,9 @@ function check_uri_spam($target = '', $method = array())
$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) {
@@ -1295,7 +451,9 @@ function check_uri_spam($target = '', $method = array())
}
}
+ // ----------------------------------------
// URI: used inside 'BBCode' pair
+
if ((! $asap || ! $is_spam) && isset($method['uri_bbcode'])) {
$key = 'uri_bbcode';
foreach($pickups as $pickup) {
@@ -1311,7 +469,9 @@ function check_uri_spam($target = '', $method = array())
}
}
+ // ----------------------------------------
// URI: Uniqueness (and removing non-uniques)
+
if ((! $asap || ! $is_spam) && isset($method['non_uniquri'])) {
$uris = array();
@@ -1336,10 +496,12 @@ function check_uri_spam($target = '', $method = array())
// Return if ...
if ($asap && $is_spam) return $progress;
+ // ----------------------------------------
// Host: Uniqueness (uniq / non-uniq)
- foreach ($pickups as $pickup) $hosts[] = & $pickup['host'];
+
$hosts = array_unique($hosts);
- $sum['uniqhost'] += count($hosts);
+
+ 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']) {
@@ -1350,77 +512,32 @@ function check_uri_spam($target = '', $method = array())
// Return if ...
if ($asap && $is_spam) return $progress;
+ // ----------------------------------------
// URI: Bad host (Separate good/bad hosts from $hosts)
- if ((! $asap || ! $is_spam) && isset($method['badhost'])) {
- // is_badhost()
- $list = get_blocklist('list');
- $blocked = blocklist_distiller($hosts, array_keys($list), $asap);
+ 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;
}
- return $progress;
-}
-
-// Count leaves (A leaf = value that is not an array, or an empty array)
-function array_count_leaves($array = array(), $count_empty = FALSE)
-{
- if (! is_array($array) || (empty($array) && $count_empty)) return 1;
-
- // Recurse
- $count = 0;
- foreach ($array as $part) {
- $count += array_count_leaves($part, $count_empty);
- }
- return $count;
-}
-
-// An array-leaves to a flat array
-function array_flat_leaves($array, $unique = TRUE)
-{
- if (! is_array($array)) return $array;
-
- $tmp = array();
- foreach(array_keys($array) as $key) {
- if (is_array($array[$key])) {
- // Recurse
- foreach(array_flat_leaves($array[$key]) as $_value) {
- $tmp[] = $_value;
- }
- } else {
- $tmp[] = & $array[$key];
- }
- }
-
- return $unique ? array_values(array_unique($tmp)) : $tmp;
-}
+ // Return if ...
+ //if ($asap && $is_spam) return $progress;
-// An array() to an array leaf
-function array_leaf($array = array('A', 'B', 'C.D'), $stem = FALSE, $edge = array())
-{
- $leaf = array();
- $tmp = & $leaf;
- foreach($array as $arg) {
- if (! is_string($arg) && ! is_int($arg)) continue;
- $tmp[$arg] = array();
- $parent = & $tmp;
- $tmp = & $tmp[$arg];
- }
- if ($stem) {
- $parent[key($parent)] = & $edge;
- } else {
- $parent = key($parent);
- }
+ // ----------------------------------------
+ // End
- return $leaf; // array('A' => array('B' => 'C.D'))
+ return $progress;
}
-
// ---------------------
// Reporting
@@ -1453,7 +570,7 @@ function summarize_detail_badhost($progress = array())
foreach($progress['blocked'] as $list => $lvalue) {
foreach($lvalue as $group => $gvalue) {
$flat = implode(', ', array_flat_leaves($gvalue));
- if ($flat == $group) {
+ if ($flat === $group) {
$blocked[$list][] = $flat;
} else {
$blocked[$list][$group] = $flat;
@@ -1481,49 +598,70 @@ function summarize_detail_newtral($progress = array())
! is_array($progress['hosts']) ||
empty($progress['hosts'])) return '';
- $result = '';
- if (FALSE) {
- // Sort by domain
- $tmp = array();
- foreach($progress['hosts'] as $value) {
- $tmp[delimiter_reverse($value)] = $value;
- }
- ksort($tmp, SORT_STRING);
- $result = count($tmp) . ' (' .implode(', ', $tmp) . ')';
- } else {
- $tmp = array();
- foreach($progress['hosts'] as $value) {
- $tmp = array_merge_recursive(
- $tmp,
- array_leaf(explode('.', delimiter_reverse($value) . '.'), TRUE, $value)
- );
+ // 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 {
+ $rest = rtrim(substr($value, 0, - strlen($resp)), '.'); // 'A.foo.bar'
}
- ksort($tmp);
- foreach($tmp as $key => $value) {
- ksort($tmp[$key]);
- foreach($value as $_key => $_value) {
- ksort($tmp[$key][$_key]);
- $tmp[$key][$_key] = implode(', ', array_flat_leaves($_value));
+ $trie = array_merge_leaves($trie, array($resp => array($rest => NULL)), FALSE);
+ }
+
+ // 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) . '\',';
}
-
- //$tmp = array_unique_recursive($tmp); // Buggy?
- $result = var_export_shrink($tmp, TRUE, TRUE);
+ unset($trie[$key]);
}
-
- return $result;
+ 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($mode = '', $data = array())
{
+ $exit = TRUE;
+
switch ($mode) {
- case '': echo("\n"); break;
+ case '':
+ echo("\n");
+ break;
case 'dump':
echo('' . "\n");
echo htmlspecialchars(var_export($data, TRUE));
@@ -1531,8 +669,7 @@ function spam_exit($mode = '', $data = array())
break;
};
- // Force exit
- exit;
+ if ($exit) exit; // Force exit
}
@@ -1545,11 +682,18 @@ function pkwk_spamfilter($action, $page, $target = array('title' => ''), $method
{
$progress = check_uri_spam($target, $method);
- if (! empty($progress['is_spam'])) {
- // Mail to administrator(s)
- pkwk_spamnotify($action, $page, $target, $progress, $method);
+ if (empty($progress['is_spam'])) {
+ spam_dispose();
+ } else {
- // 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);
}
}