OSDN Git Service

is_ip(), whois_responsibility(): Moved from spam.php to spam_pickup.php
authorhenoheno <henoheno>
Fri, 2 Jan 2009 10:37:47 +0000 (19:37 +0900)
committerhenoheno <henoheno>
Fri, 2 Jan 2009 10:37:47 +0000 (19:37 +0900)
spam/SpamPickupTest.php
spam/SpamTest.php
spam/spam.php
spam/spam_pickup.php

index 4612faa..8c510f4 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// $Id: SpamPickupTest.php,v 1.9 2009/01/02 10:15:48 henoheno Exp $
+// $Id: SpamPickupTest.php,v 1.10 2009/01/02 10:37:47 henoheno Exp $
 // Copyright (C) 2007 heno
 //
 // Design test case for spam.php (called from runner.php)
@@ -321,7 +321,7 @@ EOF;
                $this->assertEquals('',               $results[5]['port']);
                $this->assertEquals('/',              $results[5]['path']);
                $this->assertEquals('',               $results[5]['file']);
-               $this->assertEquals('%2Fofficial&word=f', $results[5]['query']);
+               $this->assertEquals('%2fofficial&word=f', $results[5]['query']);
                $this->assertEquals('',               $results[5]['fragment']);
 
 
@@ -420,6 +420,72 @@ EOF;
                $this->assertEquals('nasty2.example.org', $results[0]['host']);
                $this->assertEquals('search2.example.org',  $results[1]['host']);
        }
+
+       function testFunc_is_ip()
+       {
+               // 1st argument: Null
+               foreach($this->setup_string_null() as $key => $value){
+                       $this->assertEquals(FALSE, is_ip($value), $key);
+               }
+
+               // IPv4
+               foreach(array(
+                               '192.168.1.1',
+                       ) as $value){
+                       $this->assertEquals(4,  is_ip($value), $key, '[' . $value . ']');
+               }
+
+               // IPv6
+               foreach(array(
+                               '::',                           // 0:0:0:0:0:0:0:0
+                               '::192.168.1.1',        // IPv4 within IPv6 network
+                       ) as $value){
+                       $this->assertEquals(6,  is_ip($value), $key, '[' . $value . ']');
+               }
+
+               // Invalid
+               foreach(array(
+                               '',
+                               '.',
+                       ) as $value){
+                       $this->assertEquals(FALSE,      is_ip($value), $key, '[' . $value . ']');
+               }
+       }
+
+       function testFunc_whois_responsibility()
+       {
+               // 1st argument: Null
+               foreach($this->setup_string_null() as $key => $value){
+                       $this->assertEquals('',        whois_responsibility($value), $key);
+               }
+
+               // 'act.edu.au' is known as 3rd level domain
+               $this->AssertEquals('bar.act.edu.au', whois_responsibility('foo.bar.act.edu.au'));
+               $this->AssertEquals('bar.act.edu.au', whois_responsibility('bar.act.edu.au'));
+               $this->AssertEquals('act.edu.au',  whois_responsibility('act.edu.au'));
+               $this->AssertEquals('edu.au',      whois_responsibility('edu.au'));
+               $this->AssertEquals('au',          whois_responsibility('au'));
+
+               // 'co.uk' is known as 2nd level domain
+               $this->AssertEquals('bar.co.uk',   whois_responsibility('foo.bar.co.uk'));
+               $this->AssertEquals('bar.co.uk',   whois_responsibility('bar.co.uk'));
+               $this->AssertEquals('co.uk',       whois_responsibility('co.uk'));
+               $this->AssertEquals('uk',          whois_responsibility('uk'));
+
+               // 'bar.uk' is not 2nd level (implicit responsibility)
+               $this->AssertEquals('bar.uk',      whois_responsibility('foo.bar.uk'));
+               $this->AssertEquals('bar.uk',      whois_responsibility('bar.uk'));
+
+               // IPv4
+               $this->AssertEquals('192.168.0.1', whois_responsibility('192.168.0.1'));
+
+               // Invalid Top-Level Domain (With implicit)
+               $this->AssertEquals('bar.local',  whois_responsibility('foo.bar.local'));       // Implicit responsibility
+               $this->AssertEquals('bar.local',  whois_responsibility('bar.local'));
+               $this->AssertEquals('local',      whois_responsibility('local'));
+               $this->AssertEquals('localhost',  whois_responsibility('localhost'));
+               $this->AssertEquals('s',          whois_responsibility('s'));
+       }
 }
 
 ?>
\ No newline at end of file
index 6239dd6..e1d96bd 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// $Id: SpamTest.php,v 1.24 2008/12/27 15:21:41 henoheno Exp $
+// $Id: SpamTest.php,v 1.25 2009/01/02 10:37:47 henoheno Exp $
 // Copyright (C) 2007 heno
 //
 // Design test case for spam.php (called from runner.php)
@@ -344,38 +344,6 @@ class SpamTest extends PHPUnit_TestCase
                //$this->assertEquals('192\.168\.',       generate_host_regex('192.168.'));
        }
 
-       function testFunc_is_ip()
-       {
-               // 1st argument: Null
-               foreach($this->setup_string_null() as $key => $value){
-                       $this->assertEquals(FALSE, is_ip($value), $key);
-               }
-
-               // IPv4
-               foreach(array(
-                               '192.168.1.1',
-                       ) as $value){
-                       $this->assertEquals(4,  is_ip($value), $key, '[' . $value . ']');
-               }
-
-               // IPv6
-               foreach(array(
-                               '::',                           // 0:0:0:0:0:0:0:0
-                               '::192.168.1.1',        // IPv4 within IPv6 network
-                       ) as $value){
-                       $this->assertEquals(6,  is_ip($value), $key, '[' . $value . ']');
-               }
-
-               // Invalid
-               foreach(array(
-                               '',
-                               '.',
-                       ) as $value){
-                       $this->assertEquals(FALSE,      is_ip($value), $key, '[' . $value . ']');
-               }
-
-       }
-
        function testFunc_get_blocklist()
        {
                if (! defined('SPAM_INI_FILE') || ! file_exists(SPAM_INI_FILE)) {
@@ -414,40 +382,6 @@ class SpamTest extends PHPUnit_TestCase
                $this->assertTrue(isset($array['IANA-examples']));
        }
 
-       function testFunc_whois_responsibility()
-       {
-               // 1st argument: Null
-               foreach($this->setup_string_null() as $key => $value){
-                       $this->assertEquals('',        whois_responsibility($value), $key);
-               }
-
-               // 'act.edu.au' is known as 3rd level domain
-               $this->AssertEquals('bar.act.edu.au', whois_responsibility('foo.bar.act.edu.au'));
-               $this->AssertEquals('bar.act.edu.au', whois_responsibility('bar.act.edu.au'));
-               $this->AssertEquals('act.edu.au',  whois_responsibility('act.edu.au'));
-               $this->AssertEquals('edu.au',      whois_responsibility('edu.au'));
-               $this->AssertEquals('au',          whois_responsibility('au'));
-
-               // 'co.uk' is known as 2nd level domain
-               $this->AssertEquals('bar.co.uk',   whois_responsibility('foo.bar.co.uk'));
-               $this->AssertEquals('bar.co.uk',   whois_responsibility('bar.co.uk'));
-               $this->AssertEquals('co.uk',       whois_responsibility('co.uk'));
-               $this->AssertEquals('uk',          whois_responsibility('uk'));
-
-               // 'bar.uk' is not 2nd level (implicit responsibility)
-               $this->AssertEquals('bar.uk',      whois_responsibility('foo.bar.uk'));
-               $this->AssertEquals('bar.uk',      whois_responsibility('bar.uk'));
-
-               // IPv4
-               $this->AssertEquals('192.168.0.1', whois_responsibility('192.168.0.1'));
-
-               // Invalid Top-Level Domain (With implicit)
-               $this->AssertEquals('bar.local',  whois_responsibility('foo.bar.local'));       // Implicit responsibility
-               $this->AssertEquals('bar.local',  whois_responsibility('bar.local'));
-               $this->AssertEquals('local',      whois_responsibility('local'));
-               $this->AssertEquals('localhost',  whois_responsibility('localhost'));
-               $this->AssertEquals('s',          whois_responsibility('s'));
-       }
 }
 
 ?>
index b40a14f..bb43fea 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// $Id: spam.php,v 1.214 2009/01/02 10:29:02 henoheno Exp $
+// $Id: spam.php,v 1.215 2009/01/02 10:37:47 henoheno Exp $
 // Copyright (C) 2006-2007 PukiWiki Developers Team
 // License: GPL v2 or (at your option) any later version
 //
@@ -8,7 +8,6 @@
 // (PHP 4 >= 4.3.0): preg_match_all(PREG_OFFSET_CAPTURE): $method['uri_XXX'] related feature
 
 if (! defined('SPAM_INI_FILE'))   define('SPAM_INI_FILE',   'spam.ini.php');
-if (! defined('DOMAIN_INI_FILE')) define('DOMAIN_INI_FILE', 'domain.ini.php');
 
 // ---------------------
 // Compat etc
@@ -378,26 +377,6 @@ function generate_host_regex($string = '', $divider = '/')
        return implode('', $part);
 }
 
-// Rough hostname checker
-// TODO: Strict digit, 0x, CIDR, '999.999.999.999', ':', '::G'
-function is_ip($string = '')
-{
-       if (! is_string($string)) return FALSE;
-
-       if (strpos($string, ':') !== FALSE) {
-               return 6;       // Seems IPv6
-       }
-
-       if (preg_match('/^' .
-               '(?:[0-9]{1,3}\.){3}[0-9]{1,3}' . '|' .
-               '(?:[0-9]{1,3}\.){1,3}'         . '$/',
-               $string)) {
-               return 4;       // Seems IPv4(dot-decimal)
-       }
-
-       return FALSE;   // Seems not IP
-}
-
 // Load SPAM_INI_FILE and return parsed one
 function get_blocklist($list = '')
 {
@@ -949,59 +928,6 @@ function summarize_detail_newtral($progress = array())
 }
 
 
-// Check responsibility-root of the FQDN
-// 'foo.bar.example.com'        => 'example.com'        (.com        has the last whois for it)
-// 'foo.bar.example.au'         => 'example.au'         (.au         has the last whois for it)
-// 'foo.bar.example.edu.au'     => 'example.edu.au'     (.edu.au     has the last whois for it)
-// 'foo.bar.example.act.edu.au' => 'example.act.edu.au' (.act.edu.au has the last whois for it)
-function whois_responsibility($fqdn = 'foo.bar.example.com', $parent = FALSE, $implicit = TRUE)
-{
-       static $domain;
-
-       if ($fqdn === NULL) {
-               $domain = NULL; // Unset
-               return '';
-       }
-       if (! is_string($fqdn)) return '';
-
-       if (is_ip($fqdn)) return $fqdn;
-
-       if (! isset($domain)) {
-               $domain = array();
-               if (file_exists(DOMAIN_INI_FILE)) {
-                       include(DOMAIN_INI_FILE);       // Set
-               }
-       }
-
-       $result  = array();
-       $dcursor = & $domain;
-       $array   = array_reverse(explode('.', $fqdn));
-       $i = 0;
-       while(TRUE) {
-               if (! isset($array[$i])) break;
-               $acursor = $array[$i];
-               if (is_array($dcursor) && isset($dcursor[$acursor])) {
-                       $result[] = & $array[$i];
-                       $dcursor  = & $dcursor[$acursor];
-               } else {
-                       if (! $parent && isset($acursor)) {
-                               $result[] = & $array[$i];       // Whois servers must know this subdomain
-                       }
-                       break;
-               }
-               ++$i;
-       }
-
-       // Implicit responsibility: Top-Level-Domains must not be yours
-       // 'bar.foo.something' => 'foo.something'
-       if ($implicit && count($result) == 1 && count($array) > 1) {
-               $result[] = & $array[1];
-       }
-
-       return $result ? implode('.', array_reverse($result)) : '';
-}
-
-
 // ---------------------
 // Exit
 
index 402d9d8..da32b11 100644 (file)
@@ -1,11 +1,13 @@
 <?php
-// $Id: spam_pickup.php,v 1.67 2009/01/02 10:18:38 henoheno Exp $
+// $Id: spam_pickup.php,v 1.68 2009/01/02 10:37:47 henoheno Exp $
 // Copyright (C) 2006-2007 PukiWiki Developers Team
 // License: GPL v2 or (at your option) any later version
 //
 // Functions for Concept-work of spam-uri metrics
 //
 
+if (! defined('DOMAIN_INI_FILE')) define('DOMAIN_INI_FILE', 'domain.ini.php');
+
 // ---------------------
 // URI pickup
 
@@ -821,4 +823,76 @@ function spam_uri_pickup($string = '', $method = array())
        return $array;
 }
 
+// Rough hostname checker
+// TODO: Strict digit, 0x, CIDR, '999.999.999.999', ':', '::G'
+function is_ip($string = '')
+{
+       if (! is_string($string)) return FALSE;
+
+       if (strpos($string, ':') !== FALSE) {
+               return 6;       // Seems IPv6
+       }
+
+       if (preg_match('/^' .
+               '(?:[0-9]{1,3}\.){3}[0-9]{1,3}' . '|' .
+               '(?:[0-9]{1,3}\.){1,3}'         . '$/',
+               $string)) {
+               return 4;       // Seems IPv4(dot-decimal)
+       }
+
+       return FALSE;   // Seems not IP
+}
+
+// Check responsibility-root of the FQDN
+// 'foo.bar.example.com'        => 'example.com'        (.com        has the last whois for it)
+// 'foo.bar.example.au'         => 'example.au'         (.au         has the last whois for it)
+// 'foo.bar.example.edu.au'     => 'example.edu.au'     (.edu.au     has the last whois for it)
+// 'foo.bar.example.act.edu.au' => 'example.act.edu.au' (.act.edu.au has the last whois for it)
+function whois_responsibility($fqdn = 'foo.bar.example.com', $parent = FALSE, $implicit = TRUE)
+{
+       static $domain;
+
+       if ($fqdn === NULL) {
+               $domain = NULL; // Unset
+               return '';
+       }
+       if (! is_string($fqdn)) return '';
+
+       if (is_ip($fqdn)) return $fqdn;
+
+       if (! isset($domain)) {
+               $domain = array();
+               if (file_exists(DOMAIN_INI_FILE)) {
+                       include(DOMAIN_INI_FILE);       // Set
+               }
+       }
+
+       $result  = array();
+       $dcursor = & $domain;
+       $array   = array_reverse(explode('.', $fqdn));
+       $i = 0;
+       while(TRUE) {
+               if (! isset($array[$i])) break;
+               $acursor = $array[$i];
+               if (is_array($dcursor) && isset($dcursor[$acursor])) {
+                       $result[] = & $array[$i];
+                       $dcursor  = & $dcursor[$acursor];
+               } else {
+                       if (! $parent && isset($acursor)) {
+                               $result[] = & $array[$i];       // Whois servers must know this subdomain
+                       }
+                       break;
+               }
+               ++$i;
+       }
+
+       // Implicit responsibility: Top-Level-Domains must not be yours
+       // 'bar.foo.something' => 'foo.something'
+       if ($implicit && count($result) == 1 && count($array) > 1) {
+               $result[] = & $array[1];
+       }
+
+       return $result ? implode('.', array_reverse($result)) : '';
+}
+
 ?>