OSDN Git Service

Simplify.
authorhenoheno <henoheno>
Sat, 28 Apr 2007 03:00:43 +0000 (12:00 +0900)
committerhenoheno <henoheno>
Sat, 28 Apr 2007 03:00:43 +0000 (12:00 +0900)
Added function blocklist_distiller(). This is a backend function of is_badhost().
is_badhost(): Now array() grouping for good hosts are enabled.
array_count_leaves(): Just simplify
TODO: More useful summary

spam/spam.ini.php
spam/spam.php

index 607294d..19fdfee 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// $Id: spam.ini.php,v 1.90 2007/04/28 02:50:24 henoheno Exp $
+// $Id: spam.ini.php,v 1.91 2007/04/28 03:00:43 henoheno Exp $
 // Spam-related setting
 //
 // Reference:
 //  [3] ".example.org" prohibits ALL "example.org"-related FQDN
 //  (Now you know, [1] + [2] = [3])
 
-// NOTE for badhost (only):
-//  'unique_name' = array('a.example.org', 'b.example.org') is treated as a group 'unique_name'.
-
+// Grouping with array()
+//  'unique_name' => array('a.example.org', 'b.example.org')
+//
 // Guideline to keep group names unique
-//  [1] FQDN with capitalized letter(s)
-//  [2] Mail address of the domain-name owner
-//  [3] Unique name with capitalized letter(s) or spaces, commas, etc
+//   [1] FQDN with capitalized letter(s). Capitalized letters may suggest you this is a group.
+//   [2] Mail address of the domain-name owner
+//   [3] Unique name with capitalized letter(s) or spaces, commas, etc
+//   [4] IP address
 
 
 // List of the lists
index 7862f27..03be77c 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// $Id: spam.php,v 1.132 2007/04/22 21:49:08 henoheno Exp $
+// $Id: spam.php,v 1.133 2007/04/28 03:00:43 henoheno Exp $
 // Copyright (C) 2006-2007 PukiWiki Developers Team
 // License: GPL v2 or (at your option) any later version
 //
@@ -931,62 +931,66 @@ function get_blocklist_add(& $array, $key = 0, $value = '*.example.org')
        }
 }
 
-function is_badhost($hosts = array(), $asap = TRUE, & $remains)
+
+// Blocklist metrics: Separate $host, to $blocked and not blocked
+function blocklist_distiller(& $hosts, $keys = array('goodhost', 'badhost'), $asap = FALSE)
 {
-       $result = array();
        if (! is_array($hosts)) $hosts = array($hosts);
-       foreach(array_keys($hosts) as $key) {
-               if (! is_string($hosts[$key])) {
-                       unset($hosts[$key]);
-               }
-       }
-       if (empty($hosts)) return $result;
-
-       foreach(get_blocklist('list') as $key=>$value){
-               if ($value) {
-                       foreach (get_blocklist($key) as $label => $regex) {
-                               if (is_array($regex)) {
-                                       $result[$label] = array();
-                                       foreach($regex as $_label => $_regex) {
-                                               if (is_badhost_avail($_label, $_regex, $hosts, $result[$label]) && $asap) {
-                                                       break;
-                                               }
-                                       }
-                                       if (empty($result[$label])) unset($result[$label]);
-                               } else {
-                                       if (is_badhost_avail($label, $regex, $hosts, $result) && $asap) {
-                                               break;
+       if (! is_array($keys))  $keys  = array($keys);
+
+       $list = get_blocklist('list');
+       $blocked = array();
+
+       foreach($keys as $key){
+               foreach (get_blocklist($key) as $label => $regex) {
+                       if (is_array($regex)) {
+                               foreach($regex as $_label => $_regex) {
+                                       $group = preg_grep($_regex, $hosts);
+                                       if ($group) {
+                                               $hosts = array_diff($hosts, $group);
+                                               $blocked[$key][$label][$_label] = $group;
+                                               if ($asap && $list[$key]) break;
                                        }
                                }
+                       } else {
+                               $group = preg_grep($regex, $hosts);
+                               if ($group) {
+                                       $hosts = array_diff($hosts, $group);
+                                       $blocked[$key][$label] = $group;
+                                       if ($asap && $list[$key]) break;
+                               }
                        }
-               } else {
-                       foreach (get_blocklist($key) as $regex) {
-                               $hosts = preg_grep_invert($regex, $hosts);
-                       }
-                       if (empty($hosts)) return $result;
                }
        }
 
-       $remains = $hosts;
-       return $result;
+       return $blocked;
 }
 
-// Subroutine for is_badhost()
-function is_badhost_avail($label = '*.example.org', $regex = '/^.*\.example\.org$/', & $hosts, & $result)
+function is_badhost($hosts = array(), $asap = TRUE, & $remains)
 {
-       $group = preg_grep($regex, $hosts);
-       if ($group) {
+       $remains = array();
 
-               // DEBUG var_dump($group); // badhost detail
+       if (! is_array($hosts)) $hosts = array($hosts);
+       foreach(array_keys($hosts) as $key) {
+               if (! is_string($hosts[$key])) {
+                       unset($hosts[$key]);
+               }
+       }
+       if (empty($hosts)) return $result;
 
-               $result[$label] = & $group;
-               $hosts = array_diff($hosts, $result[$label]);
-               return TRUE;
-       } else {
-               return FALSE;
+       $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
+               }
        }
+       $remains = $hosts;
+
+       return $blocked;
 }
 
+
 // Default (enabled) methods and thresholds (for content insertion)
 function check_uri_spam_method($times = 1, $t_area = 0, $rule = TRUE)
 {
@@ -1225,7 +1229,9 @@ function check_uri_spam($target = '', $method = array())
                }
                unset($__remains);
                if (! empty($badhost)) {
+
                        //var_dump($badhost);   // BADHOST detail
+
                        $sum['badhost'] += array_count_leaves($badhost);
                        foreach(array_keys($badhost) as $keys) {
                                $is_spam['badhost'][$keys] =
@@ -1238,18 +1244,17 @@ function check_uri_spam($target = '', $method = array())
        return $progress;
 }
 
-// Count leaves
-function array_count_leaves($array = array(), $count_empty_array = FALSE)
+// 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_array))
-               return 1;
+       if (! is_array($array) || (empty($array) && $count_empty)) return 1;
 
        // Recurse
-       $result = 0;
+       $count = 0;
        foreach ($array as $part) {
-               $result += array_count_leaves($part, $count_empty_array);
+               $count += array_count_leaves($part, $count_empty);
        }
-       return $result;
+       return $count;
 }
 
 // ---------------------