OSDN Git Service

Remove unused array_shrink_leaves().
[pukiwiki/pukiwiki_sandbox.git] / spam / spam.php
index 90bcf5f..846acba 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// $Id: spam.php,v 1.152 2007/05/05 07:28:33 henoheno Exp $
+// $Id: spam.php,v 1.157 2007/05/05 13:58:39 henoheno Exp $
 // Copyright (C) 2006-2007 PukiWiki Developers Team
 // License: GPL v2 or (at your option) any later version
 //
@@ -37,6 +37,34 @@ function preg_grep_invert($pattern = '//', $input = array())
        }
 }
 
+// ----
+
+// Very roughly, shrink the lines of var_export()
+// NOTE: If the same data exists, it must be corrupted.
+function var_export_shrink($expression, $return = FALSE, $ignore_numeric_keys = FALSE)
+{
+       $result =preg_replace(
+               // Remove a newline and spaces
+               '# => \n *array \(#', ' => array (',
+               var_export($expression, TRUE)
+       );
+
+       if ($ignore_numeric_keys) {
+               $result =preg_replace(
+                       // Remove numeric keys
+                       '#^( *)[0-9]+ => #m', '$1',
+                       $result
+               );
+       }
+
+       if ($return) {
+               return $result;
+       } else {
+               echo   $result;
+               return NULL;
+       }
+}
+
 // Remove redundant values from array()
 function array_unique_recursive($array = array())
 {
@@ -54,7 +82,23 @@ function array_unique_recursive($array = array())
                        }
                }
        }
-       
+
+       return $array;
+}
+
+// Renumber all numeric keys from 0
+function array_renumber_numeric_keys(& $array)
+{
+       if (! is_array($array)) return $array;
+
+       $count = -1;
+       $tmp = array();
+       foreach($array as $key => $value){
+               if (is_array($value)) array_renumber_numeric_keys($array[$key]);        // Recurse
+               if (is_numeric($key)) $tmp[$key] = ++$count;
+       }
+       array_rename_keys($array, $tmp);
+
        return $array;
 }
 
@@ -99,6 +143,16 @@ function strings($binary = '', $min_len = 4, $ignore_space = FALSE)
        return $binary;
 }
 
+// Reverse $string with specified delimiter
+function delimiter_reverse($string = 'foo.bar.example.com', $from_delim = '.', $to_delim = '.')
+{
+       if (! is_string($string) || ! is_string($from_delim) || ! is_string($to_delim))
+               return $string;
+
+       // com.example.bar.foo
+       return implode($to_delim, array_reverse(explode($from_delim, $string)));
+}
+
 
 // ---------------------
 // URI pickup
@@ -1049,7 +1103,6 @@ function is_badhost($hosts = array(), $asap = TRUE, $bool = TRUE)
        return $bool ? ! empty($blocked) : $blocked;
 }
 
-
 // Default (enabled) methods and thresholds (for content insertion)
 function check_uri_spam_method($times = 1, $t_area = 0, $rule = TRUE)
 {
@@ -1163,13 +1216,19 @@ function check_uri_spam($target = '', $method = array())
                        }
                        if ($asap && $is_spam) break;
 
-                       // Merge $blocked
-                       $blocked = array_merge_leaves($blocked, $_progress['blocked']);
-
-                       // Merge $hosts
-                       $hosts   = array_merge_leaves($hosts,   $_progress['hosts']);
+                       // Merge only
+                       $blocked = array_merge_leaves($blocked, $_progress['blocked'], FALSE, FALSE);
+                       $hosts   = array_merge_leaves($hosts,   $_progress['hosts'],   FALSE, FALSE);
                }
 
+               // Unique values
+               $blocked = array_unique_recursive($blocked);
+               $hosts   = array_unique_recursive($hosts);
+
+               // Renumber numeric keys
+               array_renumber_numeric_keys($blocked);
+               array_renumber_numeric_keys($hosts);
+
                // Recount $sum['badhost']
                $sum['badhost'] = array_count_leaves($blocked);
 
@@ -1321,17 +1380,38 @@ function array_count_leaves($array = array(), $count_empty = FALSE)
 }
 
 // Merge two leaves' value
-function array_merge_leaves(& $array1, & $array2, $unique_values = TRUE)
+function array_merge_leaves(& $array1, & $array2, $unique_values = TRUE, $renumber_numeric = TRUE)
 {
-       // All NUMERIC keys are always renumbered from 0
        $array = array_merge_recursive($array1, $array2);
 
        // Redundant values (and keys) are vanished
        if ($unique_values) $array = array_unique_recursive($array);
 
+       // All NUMERIC keys are always renumbered from 0
+       if ($renumber_numeric) array_renumber_numeric_keys($array);
+
        return $array;
 }
 
+// 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;
+}
 
 // ---------------------
 // Reporting
@@ -1357,39 +1437,35 @@ function summarize_spam_progress($progress = array(), $blockedonly = FALSE)
        return implode(', ', $tmp);
 }
 
-// Very roughly, shrink the lines of var_export()
-// NOTE: If the same data exists, it must be corrupted.
-function var_export_shrink($expression, $return = FALSE)
-{
-       $result =preg_replace(
-               // Remove a newline and spaces
-               '# => \n *array \(#', ' => array (',
-               var_export($expression, TRUE)
-       );
-
-       if ($return) {
-               return $result;
-       } else {
-               echo   $result;
-               return NULL;
-       }
-}
-
 function summarize_detail_badhost($progress = array())
 {
-       if (! isset($progress['is_spam']['badhost'])) return '';
+       if (! isset($progress['blocked']) || empty($progress['blocked'])) return '';
 
-       return var_export_shrink($progress['blocked'], TRUE);
-}
+       // 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;
+                       }
+               }
+       }
 
-// Reverse $string with specified delimiter
-function delimiter_reverse($string = 'foo.bar.example.com', $from_delim = '.', $to_delim = '.')
-{
-       if (! is_string($string) || ! is_string($from_delim) || ! is_string($to_delim))
-               return $string;
+       // 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);
+               }
+       }
 
-       // com.example.bar.foo
-       return implode($to_delim, array_reverse(explode($from_delim, $string)));
+       return var_export_shrink($blocked, TRUE, TRUE);
 }
 
 function summarize_detail_newtral($progress = array())