OSDN Git Service

Added
[pukiwiki/pukiwiki_sandbox.git] / spam / spam.php
index 27094f4..3cc34ff 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// $Id: spam.php,v 1.196 2007/07/02 14:51:40 henoheno Exp $
+// $Id: spam.php,v 1.201 2007/08/17 14:33:32 henoheno Exp $
 // Copyright (C) 2006-2007 PukiWiki Developers Team
 // License: GPL v2 or (at your option) any later version
 //
@@ -7,8 +7,6 @@
 //
 // (PHP 4 >= 4.3.0): preg_match_all(PREG_OFFSET_CAPTURE): $method['uri_XXX'] related feature
 
-require_once('spam_pickup.php');
-
 if (! defined('SPAM_INI_FILE'))   define('SPAM_INI_FILE',   'spam.ini.php');
 if (! defined('DOMAIN_INI_FILE')) define('DOMAIN_INI_FILE', 'domain.ini.php');
 
@@ -163,6 +161,44 @@ function array_count_leaves($array = array(), $count_empty = FALSE)
        return $count;
 }
 
+// Merge two leaves
+// Similar to PHP array_merge_leaves(), except strictly preserving keys as string
+function array_merge_leaves($array1, $array2, $sort_keys = TRUE)
+{
+       // Array(s) only 
+       $is_array1 = is_array($array1);
+       $is_array2 = is_array($array2);
+       if ($is_array1) {
+               if ($is_array2) {
+                       ;       // Pass
+               } else {
+                       return $array1;
+               }
+       } else if ($is_array2) {
+               return $array2;
+       } else {
+               return $array2; // Not array ($array1 is overwritten)
+       }
+
+       $keys_all = array_merge(array_keys($array1), array_keys($array2));
+       if ($sort_keys) sort($keys_all, SORT_STRING);
+
+       $result = array();
+       foreach($keys_all as $key) {
+               $isset1 = isset($array1[$key]);
+               $isset2 = isset($array2[$key]);
+               if ($isset1 && $isset2) {
+                       // Recurse
+                       $result[$key] = array_merge_leaves($array1[$key], $array2[$key], $sort_keys);
+               } else if ($isset1) {
+                       $result[$key] = & $array1[$key];
+               } else {
+                       $result[$key] = & $array2[$key];
+               }
+       }
+       return $result;
+}
+
 // An array-leaves to a flat array
 function array_flat_leaves($array, $unique = TRUE)
 {
@@ -189,10 +225,13 @@ function array_rename_keys(& $array, $keys = array('from' => 'to'), $force = FAL
        if (! is_array($array) || ! is_array($keys)) return FALSE;
 
        // Nondestructive test
-       if (! $force)
-               foreach(array_keys($keys) as $from)
-                       if (! isset($array[$from]))
+       if (! $force) {
+               foreach(array_keys($keys) as $from) {
+                       if (! isset($array[$from])) {
                                return FALSE;
+                       }
+               }
+       }
 
        foreach($keys as $from => $to) {
                if ($from === $to) continue;
@@ -281,7 +320,6 @@ function generate_host_regex($string = '', $divider = '/')
        if (mb_strpos($string, '.') === FALSE)
                return generate_glob_regex($string, $divider);
 
-       $result = '';
        if (is_ip($string)) {
                // IPv4
                return generate_glob_regex($string, $divider);
@@ -531,8 +569,8 @@ function check_uri_spam($target = '', $method = array())
                        if ($asap && $is_spam) break;
 
                        // Merge only
-                       $blocked = array_merge_recursive($blocked, $_progress['blocked']);
-                       $hosts   = array_merge_recursive($hosts,   $_progress['hosts']);
+                       $blocked = array_merge_leaves($blocked, $_progress['blocked'], FALSE);
+                       $hosts   = array_merge_leaves($hosts,   $_progress['hosts'],   FALSE);
                }
 
                // Unique values
@@ -648,7 +686,7 @@ function check_uri_spam($target = '', $method = array())
        // 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']) {
@@ -748,7 +786,7 @@ function summarize_detail_newtral($progress = array())
                } else {
                        $rest = rtrim(substr($value, 0, - strlen($resp)), '.'); // 'A.foo.bar'
                }
-               $trie = array_merge_recursive($trie, array($resp => array($rest => NULL)));
+               $trie = array_merge_leaves($trie, array($resp => array($rest => NULL)), FALSE);
        }
 
        // Format: var_export_shrink() -like output