OSDN Git Service

Added
[pukiwiki/pukiwiki_sandbox.git] / spam / spam.php
index 7e3d963..dbe5fdb 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// $Id: spam.php,v 1.154 2007/05/05 08:49:10 henoheno Exp $
+// $Id: spam.php,v 1.160 2007/05/12 09:19:49 henoheno Exp $
 // Copyright (C) 2006-2007 PukiWiki Developers Team
 // License: GPL v2 or (at your option) any later version
 //
@@ -41,7 +41,7 @@ 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)
+function var_export_shrink($expression, $return = FALSE, $ignore_numeric_keys = FALSE)
 {
        $result =preg_replace(
                // Remove a newline and spaces
@@ -49,6 +49,14 @@ function var_export_shrink($expression, $return = FALSE)
                var_export($expression, TRUE)
        );
 
+       if ($ignore_numeric_keys) {
+               $result =preg_replace(
+                       // Remove numeric keys
+                       '#^( *)[0-9]+ => #m', '$1',
+                       $result
+               );
+       }
+
        if ($return) {
                return $result;
        } else {
@@ -454,7 +462,7 @@ function spam_uri_pickup_preprocess($string = '')
        // http://victim.example.org/nasty.example.org/path#frag
        // => http://nasty.example.org/?refer=victim.example.org and original
        $string = preg_replace(
-               '#http://' .
+               '#h?ttp://' .
                '(' .
                        'ime\.nu' . '|' .       // 2ch.net
                        'ime\.st' . '|' .       // 2ch.net
@@ -466,6 +474,23 @@ function spam_uri_pickup_preprocess($string = '')
                $string
        );
 
+       // Domain exposure (gate-big5)
+       // http://victim.example.org/gate/big5/nasty.example.org/path
+       // => http://nasty.example.org/?refer=victim.example.org and original
+       $string = preg_replace(
+               '#h?ttp://' .
+               '(' .
+                       'big5.51job.com'         . '|' .
+                       'big5.china.com'         . '|' .
+                       'big5.xinhuanet.com' . '|' .
+               ')' .
+               '/gate/big5' .
+               '/([a-z0-9.%_-]+\.[a-z0-9.%_-]+)' .
+                '#i',  // nasty.example.org
+               'http://$2/?refer=$1 $0',                               // Preserve $0 or remove?
+               $string
+       );
+
        // Domain exposure (See _preg_replace_callback_domain_exposure())
        $string = preg_replace_callback(
                array(
@@ -1083,19 +1108,6 @@ function blocklist_distiller(& $hosts, $keys = array('goodhost', 'badhost'), $as
        return $blocked;
 }
 
-// Simple example for badhost (not used now)
-function is_badhost($hosts = array(), $asap = TRUE, $bool = TRUE)
-{
-       $list = get_blocklist('list');
-       $blocked = blocklist_distiller($hosts, array_keys($list), $asap);
-       foreach($list as $key=>$type){
-               if (! $type) unset($blocked[$key]); // Ignore goodhost etc
-       }
-
-       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)
 {
@@ -1210,18 +1222,14 @@ function check_uri_spam($target = '', $method = array())
                        if ($asap && $is_spam) break;
 
                        // Merge only
-                       $blocked = array_merge_leaves($blocked, $_progress['blocked'], FALSE, FALSE);
-                       $hosts   = array_merge_leaves($hosts,   $_progress['hosts'],   FALSE, FALSE);
+                       $blocked = array_merge_recursive($blocked, $_progress['blocked']);
+                       $hosts   = array_merge_recursive($hosts,   $_progress['hosts']);
                }
 
                // 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);
 
@@ -1372,49 +1380,50 @@ function array_count_leaves($array = array(), $count_empty = FALSE)
        return $count;
 }
 
-// Merge two leaves' value
-function array_merge_leaves(& $array1, & $array2, $unique_values = TRUE, $renumber_numeric = TRUE)
-{
-       $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;
-}
-
-// Shrink array('key' => array('key')) to array('key')
-function array_shrink_leaves(& $array)
+// An array-leaves to a flat array
+function array_flat_leaves($array, $unique = TRUE)
 {
        if (! is_array($array)) return $array;
 
-       foreach($array as $key => $value){
-               // Recurse. Removing more leaves beforehand
-               if (is_array($value)) array_shrink_leaves($array[$key]);
-       }
-
        $tmp = array();
-       foreach($array as $key => $value){
-               if (is_array($value)) {
-                       $count = count($value);
-                       if ($count == 1 && current($value) == $key) {
-                               unset($array[$key]);
-                               $array[] = $key;
+       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 $array;
+       return $unique ? array_values(array_unique($tmp)) : $tmp;
+}
+
+// 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);
+       }
+
+       return $leaf;   // array('A' => array('B' => 'C.D'))
 }
 
 
 // ---------------------
 // Reporting
 
-// TODO: Don't show unused $method!
 // Summarize $progress (blocked only)
 function summarize_spam_progress($progress = array(), $blockedonly = FALSE)
 {
@@ -1437,9 +1446,33 @@ function summarize_spam_progress($progress = array(), $blockedonly = FALSE)
 
 function summarize_detail_badhost($progress = array())
 {
-       if (! isset($progress['is_spam']['badhost'])) return '';
+       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;
+                       }
+               }
+       }
+
+       // 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);
+               }
+       }
 
-       return var_export_shrink(array_shrink_leaves($progress['blocked']), TRUE);
+       return var_export_shrink($blocked, TRUE, TRUE);
 }
 
 function summarize_detail_newtral($progress = array())
@@ -1448,14 +1481,37 @@ function summarize_detail_newtral($progress = array())
            ! is_array($progress['hosts']) ||
            empty($progress['hosts'])) return '';
 
-       // Sort by domain
-       $tmp = array();
-       foreach($progress['hosts'] as $value) {
-               $tmp[delimiter_reverse($value)] = $value;
+       $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)
+                       );
+               }
+               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));
+                       }
+               }
+               
+               //$tmp = array_unique_recursive($tmp); // Buggy?
+               $result = var_export_shrink($tmp, TRUE, TRUE);
        }
-       ksort($tmp);
 
-       return count($tmp) . ' (' .implode(', ', $tmp) . ')';
+       return $result;
 }