From 2e3b176678a8d38f2adf4e8fdeaacad8ad532407 Mon Sep 17 00:00:00 2001 From: henoheno Date: Fri, 2 Jan 2009 19:48:21 +0900 Subject: [PATCH] Separate some utility functions to spam_util.php --- spam/SpamTest.php | 294 +------------------------------------- spam/SpamUtilTest.php | 388 ++++++++++++++++++++++++++++++++++++++++++++++++++ spam/runner.php | 4 +- spam/spam.php | 297 +------------------------------------- spam/spam_util.php | 304 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 698 insertions(+), 589 deletions(-) create mode 100644 spam/SpamUtilTest.php create mode 100644 spam/spam_util.php diff --git a/spam/SpamTest.php b/spam/SpamTest.php index e1d96bd..ee5fbc6 100644 --- a/spam/SpamTest.php +++ b/spam/SpamTest.php @@ -1,5 +1,5 @@ assertEquals('com.example.bar.foo', - delimiter_reverse('foo.bar.example.com')); - - // A vector (an simple array) - $array = array('foo.ba2r', 'foo.bar2'); - $this->assertEquals(array('ba2r|foo', 'bar2|foo'), - delimiter_reverse($array, '.', '|')); - - // Note: array_map() vanishes all keys - $array = array('FB' => 'foo.ba2r', 'FB2' => 'foo.bar2'); - $this->assertEquals(array('ba2r|foo', 'bar2|foo'), - delimiter_reverse($array, '.', '|')); - - // A tree (recurse) - $array = array('foo.ba2r', 'foo.bar2', array('john.doe', 'bob.dude')); - $this->assertEquals(array('ba2r|foo', 'bar2|foo', array('doe|john', 'dude|bob')), - delimiter_reverse($array, '.', '|')); - - // Nothing changes - $this->assertEquals('100', delimiter_reverse('100')); - $this->assertEquals(array(), delimiter_reverse(array())); - - // Invalid cases - $this->assertEquals(FALSE, delimiter_reverse(TRUE)); - $this->assertEquals(FALSE, delimiter_reverse(FALSE)); - $this->assertEquals(FALSE, delimiter_reverse(NULL)); - $this->assertEquals(FALSE, delimiter_reverse(100)); - $this->assertEquals(FALSE, delimiter_reverse('100', FALSE)); - $this->assertEquals(FALSE, delimiter_reverse('100', 0)); - $this->assertEquals(FALSE, delimiter_reverse('100', '0', 0)); - } - + // Utility function setup_string_null() { return array( @@ -59,262 +25,6 @@ class SpamTest extends PHPUnit_TestCase ); } - function testFunc_strings() - { - // 1st argument: Null - $this->assertEquals('', strings(NULL, 0)); - $this->assertEquals('', strings(TRUE, 0)); - $this->assertEquals('', strings(FALSE, 0)); - $this->assertEquals('', strings('', 0)); - $this->assertEquals('0', strings(0, 0)); - $this->assertEquals('1', strings(1, 0)); - - // Setup - $t1 = '1' . "\n"; - $t2 = '12' . "\n"; - $t3 = '123' . "\n"; - $t4 = '1234' . "\n"; - $t5 = '12345'; - $test = $t1 . $t2 . $t3 . $t4 . $t5; - - // Minimum length - $this->assertEquals($t1 . $t2 . $t3 . $t4 . $t5, strings($test, -1)); - $this->assertEquals($t1 . $t2 . $t3 . $t4 . $t5, strings($test, 0)); - $this->assertEquals($t1 . $t2 . $t3 . $t4 . $t5, strings($test, 1)); - $this->assertEquals( $t2 . $t3 . $t4 . $t5, strings($test, 2)); - $this->assertEquals( $t3 . $t4 . $t5, strings($test, 3)); - $this->assertEquals( $t4 . $t5, strings($test, 4)); - $this->assertEquals( $t4 . $t5, strings($test)); // Default - $this->assertEquals( $t5, strings($test, 5)); - - // Preserve the last newline - $this->assertEquals($t4 . $t5, strings($test , 4)); - $this->assertEquals($t4 . $t5 . "\n", strings($test . "\n", 4)); - - // Ignore sequential spaces, and spaces at the beginning/end of lines - $test = ' A' . ' ' . ' ' . 'B '; - $this->assertEquals($test, strings($test, 0, FALSE)); - $this->assertEquals('A B', strings($test, 0, TRUE )); - } - - function testFunc_array_count_leaves() - { - // Empty array = 0, if option is not set - $array = array(); - $this->assertEquals(0, array_count_leaves($array, FALSE)); - $this->assertEquals(1, array_count_leaves($array, TRUE)); - $array = array( - array( - array() - ) - ); - $this->assertEquals(0, array_count_leaves($array, FALSE)); - $this->assertEquals(1, array_count_leaves($array, TRUE)); - - // One leaf = 1 - foreach(array(NULL, TRUE, FALSE, -1, 0, 1, '', 'foobar') as $value) { - $this->assertEquals(1, array_count_leaves($value, FALSE)); - $this->assertEquals(1, array_count_leaves($value, TRUE)); - } - - // Compisite - $array = array( - 1, - 'v1', - array(), // Empty array - array( - 2, - 'v2', - 'k1' => TRUE, - 'k2' => FALSE, - 'k3' => array(), // Empty array - 'k4' => array( - 3, - 'v3', - 'k5' => NULL, - 'k6' => array(), // Empty array - ), - ), - 'k7' => 4, - 'k8' => 'v4', - 'k9' => array(), // Empty array - 'k10' => array( - 5, - 'v5', - 'k11' => NULL, - 'k12' => array(), // Empty array - ), - ); - $this->assertEquals(14, array_count_leaves($array, FALSE)); - $this->assertEquals(19, array_count_leaves($array, TRUE)); - } - - function testPhpFunc_array_unique() - { - $this->assertEquals(array(1), array_unique(array(1, 1))); - - // Keys are preserved, array()s inside are preserved - $this->assertEquals( - array(0, 2 => array(1, 1), 3 => 2), - array_unique( - array(0, 0, array(1, 1), 2, 2) - ) - ); - - // Keys are preserved - $this->assertEquals( - array(0, 2 => array(1, 1), 3 => 2), - array_unique(array(0, 0, array(1, 1), 2, 2)) - ); - - // ONLY the first array() is preserved - $this->assertEquals( - array(0 => array(1, 1)), - array_unique(array_unique(array(0 => array(1, 1), 'a' => array(2,2), 'b' => array(3, 3)))) - ); - } - - function testFunc_array_merge_leaves() - { - // PHP array_unique_recursive(), PHP array_merge_leaves(), and array_merge_leaves() - $array1 = array(1); - $array2 = array(1); - $result = array_merge_recursive($array1, $array2); - $this->assertEquals(array(1, 1), $result); - $result = array_unique_recursive($result); - $this->assertEquals(array(1), $result); - $result = array_merge_leaves($array1, $array2); - $this->assertEquals(array(1), $result); - - $array1 = array(2); - $array2 = array(1); - $result = array_merge_recursive($array1, $array2); - $this->assertEquals(array(2, 1), $result); - $result = array_merge_leaves($array1, $array2); - $this->assertEquals(array(1), $result); - - // All NUMERIC keys are always renumbered from 0? - $array1 = array('10' => 'f3'); - $array2 = array('10' => 'f4'); - $result = array_merge_recursive($array1, $array2); - $this->assertEquals(array(0 => 'f3', 1 => 'f4'), $result); - $result = array_merge_leaves($array1, $array2); - $this->assertEquals(array(10 => 'f4'), $result); - - // One more thing ... - $array1 = array('20' => 'f5'); - $array2 = array(); - $result = array_merge_recursive($array1, $array2); - $this->assertEquals(array(0 => 'f5'), $result); - $result = array_merge_leaves($array1, $array2); - $this->assertEquals(array(20 => 'f5'), $result); - - // Non-numeric keys and values will be marged as you think? - $array1 = array('a' => 'f1'); - $array2 = array('a' => 'f2'); - $result = array_merge_recursive($array1, $array2); - $this->assertEquals(array('a' => array('f1', 'f2')), $result); - $result = array_merge_leaves($array1, $array2); - $this->assertEquals(array('a' => 'f2'), $result); - - // Non-numeric keys: An array and a value will be marged? - $array1 = array('b' => array('k1')); - $array2 = array('b' => 'k2'); - $result = array_merge_recursive($array1, $array2); - $this->assertEquals(array('b' => array(0 => 'k1', 1 => 'k2')), $result); - $result = array_merge_leaves($array1, $array2); - $this->assertEquals(array('b' => array(0 => 'k1')), $result); - - // Combination? - $array1 = array( - 2, - 'a' => 'f1', - '10' => 'f3', - '20' => 'f5', - 'b' => array('k1'), - ); - $array2 = array( - 1, - 'a' => 'f2', - '10' => 'f4', - 'b' => 'k2', - ); - $result = array ( - 2, - 'a' => array ( - 'f1', - 'f2', - ), - 'f3', - 'f5', - 'b' => array ( - 'k1', - 'k2', - ), - 1, - 'f4', - ); - $result2 = array ( - 0 => 1, - 10 => 'f4', - 20 => 'f5', - 'a' => 'f2', - 'b' => array ('k1'), - ); - $this->assertEquals($result, array_merge_recursive($array1, $array2)); - $this->assertEquals($result2, array_merge_leaves($array1, $array2)); - - // Values will not be unique? - $array1 = array(5, 4); - $array2 = array(4, 5); - $result = array_merge_recursive($array1, $array2); - $this->assertEquals(array(5, 4, 4, 5), $result); - $this->assertEquals(array(5, 4), array_unique_recursive($result)); - $this->assertEquals(array(0=>4, 1=>5), array_merge_leaves($array1, $array2)); - - // One more thing ...? - $array1 = array('b' => array('k3')); - $array2 = array('b' => 'k3'); - $result = array_merge_recursive($array1, $array2); - $this->assertEquals(array('b' => array('k3', 'k3')), $result); - $result = array_unique_recursive($result); - $this->assertEquals(array('b' => array('k3')), $result); - $result = array_merge_leaves($array1, $array2); - $this->assertEquals(array('b' => array('k3')), $result); - - // Preserve numeric keys? - $array1 = array('a' => array('' => NULL)); - $array2 = array('a' => array(5 => NULL)); - $array3 = array('a' => array(8 => NULL)); - // - // BAD: PHP array_merge_recursive() don't preserve numeric keys - $result = array_merge_recursive($array1, $array2); - $this->assertEquals(array('a' => array('' => NULL, 0 => NULL)), $result); // 0? - $result = array_merge_recursive($array2, $array3); - $this->assertEquals(array('a' => array(5 => NULL, 6 => NULL)), $result); // 6? - // - $result = array_merge_leaves($array1, $array2); - $this->assertEquals(array('a' => array('' => NULL, 5 => NULL)), $result); // 0? - $result = array_merge_leaves($array2, $array3); - $this->assertEquals(array('a' => array(5 => NULL, 8 => NULL)), $result); // 6? - - // Merging array leaves - $array1 = array('a' => TRUE); - $array2 = array('b' => FALSE); - $result = array_merge_leaves($array1, $array2); - $this->assertEquals(array('a' => TRUE, 'b' => FALSE), $result); - - $array1 = array('a' => TRUE); - $array2 = array('a' => array('aa' => TRUE)); - $this->assertEquals($array2, array_merge_leaves($array1, $array2)); - $this->assertEquals($array2, array_merge_leaves($array2, $array1)); - - $array1 = array('a' => array('a1' => TRUE)); - $array2 = array('a' => array('a2' => FALSE)); - $result = array_merge_leaves($array1, $array2); - $this->assertEquals(array('a' => array('a1' => TRUE, 'a2' => FALSE)), $result); - } - function testFunc_generate_glob_regex() { // 1st argument: Null diff --git a/spam/SpamUtilTest.php b/spam/SpamUtilTest.php new file mode 100644 index 0000000..299a001 --- /dev/null +++ b/spam/SpamUtilTest.php @@ -0,0 +1,388 @@ + NULL, + '[TRUE]' => TRUE, + '[FALSE]' => FALSE, + '[array(foobar)]' => array('foobar'), + '[]' => '', + '[0]' => 0, + '[1]' => 1 + ); + } + + function testFunc_delimiter_reverse() + { + // Simple + $this->assertEquals('com.example.bar.foo', + delimiter_reverse('foo.bar.example.com')); + + // A vector (an simple array) + $array = array('foo.ba2r', 'foo.bar2'); + $this->assertEquals(array('ba2r|foo', 'bar2|foo'), + delimiter_reverse($array, '.', '|')); + + // Note: array_map() vanishes all keys + $array = array('FB' => 'foo.ba2r', 'FB2' => 'foo.bar2'); + $this->assertEquals(array('ba2r|foo', 'bar2|foo'), + delimiter_reverse($array, '.', '|')); + + // A tree (recurse) + $array = array('foo.ba2r', 'foo.bar2', array('john.doe', 'bob.dude')); + $this->assertEquals(array('ba2r|foo', 'bar2|foo', array('doe|john', 'dude|bob')), + delimiter_reverse($array, '.', '|')); + + // Nothing changes + $this->assertEquals('100', delimiter_reverse('100')); + $this->assertEquals(array(), delimiter_reverse(array())); + + // Invalid cases + $this->assertEquals(FALSE, delimiter_reverse(TRUE)); + $this->assertEquals(FALSE, delimiter_reverse(FALSE)); + $this->assertEquals(FALSE, delimiter_reverse(NULL)); + $this->assertEquals(FALSE, delimiter_reverse(100)); + $this->assertEquals(FALSE, delimiter_reverse('100', FALSE)); + $this->assertEquals(FALSE, delimiter_reverse('100', 0)); + $this->assertEquals(FALSE, delimiter_reverse('100', '0', 0)); + } + + function testFunc_strings() + { + // 1st argument: Null + $this->assertEquals('', strings(NULL, 0)); + $this->assertEquals('', strings(TRUE, 0)); + $this->assertEquals('', strings(FALSE, 0)); + $this->assertEquals('', strings('', 0)); + $this->assertEquals('0', strings(0, 0)); + $this->assertEquals('1', strings(1, 0)); + + // Setup + $t1 = '1' . "\n"; + $t2 = '12' . "\n"; + $t3 = '123' . "\n"; + $t4 = '1234' . "\n"; + $t5 = '12345'; + $test = $t1 . $t2 . $t3 . $t4 . $t5; + + // Minimum length + $this->assertEquals($t1 . $t2 . $t3 . $t4 . $t5, strings($test, -1)); + $this->assertEquals($t1 . $t2 . $t3 . $t4 . $t5, strings($test, 0)); + $this->assertEquals($t1 . $t2 . $t3 . $t4 . $t5, strings($test, 1)); + $this->assertEquals( $t2 . $t3 . $t4 . $t5, strings($test, 2)); + $this->assertEquals( $t3 . $t4 . $t5, strings($test, 3)); + $this->assertEquals( $t4 . $t5, strings($test, 4)); + $this->assertEquals( $t4 . $t5, strings($test)); // Default + $this->assertEquals( $t5, strings($test, 5)); + + // Preserve the last newline + $this->assertEquals($t4 . $t5, strings($test , 4)); + $this->assertEquals($t4 . $t5 . "\n", strings($test . "\n", 4)); + + // Ignore sequential spaces, and spaces at the beginning/end of lines + $test = ' A' . ' ' . ' ' . 'B '; + $this->assertEquals($test, strings($test, 0, FALSE)); + $this->assertEquals('A B', strings($test, 0, TRUE )); + } + + function testFunc_array_count_leaves() + { + // Empty array = 0, if option is not set + $array = array(); + $this->assertEquals(0, array_count_leaves($array, FALSE)); + $this->assertEquals(1, array_count_leaves($array, TRUE)); + $array = array( + array( + array() + ) + ); + $this->assertEquals(0, array_count_leaves($array, FALSE)); + $this->assertEquals(1, array_count_leaves($array, TRUE)); + + // One leaf = 1 + foreach(array(NULL, TRUE, FALSE, -1, 0, 1, '', 'foobar') as $value) { + $this->assertEquals(1, array_count_leaves($value, FALSE)); + $this->assertEquals(1, array_count_leaves($value, TRUE)); + } + + // Compisite + $array = array( + 1, + 'v1', + array(), // Empty array + array( + 2, + 'v2', + 'k1' => TRUE, + 'k2' => FALSE, + 'k3' => array(), // Empty array + 'k4' => array( + 3, + 'v3', + 'k5' => NULL, + 'k6' => array(), // Empty array + ), + ), + 'k7' => 4, + 'k8' => 'v4', + 'k9' => array(), // Empty array + 'k10' => array( + 5, + 'v5', + 'k11' => NULL, + 'k12' => array(), // Empty array + ), + ); + $this->assertEquals(14, array_count_leaves($array, FALSE)); + $this->assertEquals(19, array_count_leaves($array, TRUE)); + } + + function testPhpFunc_array_unique() + { + $this->assertEquals(array(1), array_unique(array(1, 1))); + + // Keys are preserved, array()s inside are preserved + $this->assertEquals( + array(0, 2 => array(1, 1), 3 => 2), + array_unique( + array(0, 0, array(1, 1), 2, 2) + ) + ); + + // Keys are preserved + $this->assertEquals( + array(0, 2 => array(1, 1), 3 => 2), + array_unique(array(0, 0, array(1, 1), 2, 2)) + ); + + // ONLY the first array() is preserved + $this->assertEquals( + array(0 => array(1, 1)), + array_unique(array_unique(array(0 => array(1, 1), 'a' => array(2,2), 'b' => array(3, 3)))) + ); + } + + function testFunc_array_merge_leaves() + { + // PHP array_unique_recursive(), PHP array_merge_leaves(), and array_merge_leaves() + $array1 = array(1); + $array2 = array(1); + $result = array_merge_recursive($array1, $array2); + $this->assertEquals(array(1, 1), $result); + $result = array_unique_recursive($result); + $this->assertEquals(array(1), $result); + $result = array_merge_leaves($array1, $array2); + $this->assertEquals(array(1), $result); + + $array1 = array(2); + $array2 = array(1); + $result = array_merge_recursive($array1, $array2); + $this->assertEquals(array(2, 1), $result); + $result = array_merge_leaves($array1, $array2); + $this->assertEquals(array(1), $result); + + // All NUMERIC keys are always renumbered from 0? + $array1 = array('10' => 'f3'); + $array2 = array('10' => 'f4'); + $result = array_merge_recursive($array1, $array2); + $this->assertEquals(array(0 => 'f3', 1 => 'f4'), $result); + $result = array_merge_leaves($array1, $array2); + $this->assertEquals(array(10 => 'f4'), $result); + + // One more thing ... + $array1 = array('20' => 'f5'); + $array2 = array(); + $result = array_merge_recursive($array1, $array2); + $this->assertEquals(array(0 => 'f5'), $result); + $result = array_merge_leaves($array1, $array2); + $this->assertEquals(array(20 => 'f5'), $result); + + // Non-numeric keys and values will be marged as you think? + $array1 = array('a' => 'f1'); + $array2 = array('a' => 'f2'); + $result = array_merge_recursive($array1, $array2); + $this->assertEquals(array('a' => array('f1', 'f2')), $result); + $result = array_merge_leaves($array1, $array2); + $this->assertEquals(array('a' => 'f2'), $result); + + // Non-numeric keys: An array and a value will be marged? + $array1 = array('b' => array('k1')); + $array2 = array('b' => 'k2'); + $result = array_merge_recursive($array1, $array2); + $this->assertEquals(array('b' => array(0 => 'k1', 1 => 'k2')), $result); + $result = array_merge_leaves($array1, $array2); + $this->assertEquals(array('b' => array(0 => 'k1')), $result); + + // Combination? + $array1 = array( + 2, + 'a' => 'f1', + '10' => 'f3', + '20' => 'f5', + 'b' => array('k1'), + ); + $array2 = array( + 1, + 'a' => 'f2', + '10' => 'f4', + 'b' => 'k2', + ); + $result = array ( + 2, + 'a' => array ( + 'f1', + 'f2', + ), + 'f3', + 'f5', + 'b' => array ( + 'k1', + 'k2', + ), + 1, + 'f4', + ); + $result2 = array ( + 0 => 1, + 10 => 'f4', + 20 => 'f5', + 'a' => 'f2', + 'b' => array ('k1'), + ); + $this->assertEquals($result, array_merge_recursive($array1, $array2)); + $this->assertEquals($result2, array_merge_leaves($array1, $array2)); + + // Values will not be unique? + $array1 = array(5, 4); + $array2 = array(4, 5); + $result = array_merge_recursive($array1, $array2); + $this->assertEquals(array(5, 4, 4, 5), $result); + $this->assertEquals(array(5, 4), array_unique_recursive($result)); + $this->assertEquals(array(0=>4, 1=>5), array_merge_leaves($array1, $array2)); + + // One more thing ...? + $array1 = array('b' => array('k3')); + $array2 = array('b' => 'k3'); + $result = array_merge_recursive($array1, $array2); + $this->assertEquals(array('b' => array('k3', 'k3')), $result); + $result = array_unique_recursive($result); + $this->assertEquals(array('b' => array('k3')), $result); + $result = array_merge_leaves($array1, $array2); + $this->assertEquals(array('b' => array('k3')), $result); + + // Preserve numeric keys? + $array1 = array('a' => array('' => NULL)); + $array2 = array('a' => array(5 => NULL)); + $array3 = array('a' => array(8 => NULL)); + // + // BAD: PHP array_merge_recursive() don't preserve numeric keys + $result = array_merge_recursive($array1, $array2); + $this->assertEquals(array('a' => array('' => NULL, 0 => NULL)), $result); // 0? + $result = array_merge_recursive($array2, $array3); + $this->assertEquals(array('a' => array(5 => NULL, 6 => NULL)), $result); // 6? + // + $result = array_merge_leaves($array1, $array2); + $this->assertEquals(array('a' => array('' => NULL, 5 => NULL)), $result); // 0? + $result = array_merge_leaves($array2, $array3); + $this->assertEquals(array('a' => array(5 => NULL, 8 => NULL)), $result); // 6? + + // Merging array leaves + $array1 = array('a' => TRUE); + $array2 = array('b' => FALSE); + $result = array_merge_leaves($array1, $array2); + $this->assertEquals(array('a' => TRUE, 'b' => FALSE), $result); + + $array1 = array('a' => TRUE); + $array2 = array('a' => array('aa' => TRUE)); + $this->assertEquals($array2, array_merge_leaves($array1, $array2)); + $this->assertEquals($array2, array_merge_leaves($array2, $array1)); + + $array1 = array('a' => array('a1' => TRUE)); + $array2 = array('a' => array('a2' => FALSE)); + $result = array_merge_leaves($array1, $array2); + $this->assertEquals(array('a' => array('a1' => TRUE, 'a2' => FALSE)), $result); + } + + function testFunc_generate_glob_regex() + { + // 1st argument: Null + foreach($this->setup_string_null() as $key => $value){ + $this->assertEquals('', generate_glob_regex($value), $key); + } + + $this->assertEquals('.*\.txt', generate_glob_regex('*.txt')); + $this->assertEquals('A.A', generate_glob_regex('A?A')); + } + + function testFunc_generate_host_regex() + { + // 1st argument: Null + foreach($this->setup_string_null() as $key => $value){ + $this->assertEquals('', generate_host_regex($value), $key); + } + + $this->assertEquals('localhost', generate_host_regex('localhost')); + $this->assertEquals('example\.org', generate_host_regex('example.org')); + $this->assertEquals('(?:.*\.)?example\.org', generate_host_regex('.example.org')); + $this->assertEquals('.*\.example\.org', generate_host_regex('*.example.org')); + $this->assertEquals('.*\..*\.example\.org', generate_host_regex('*.*.example.org')); + $this->assertEquals('10\.20\.30\.40', generate_host_regex('10.20.30.40')); + + // Should match with 192.168.0.0/16 + //$this->assertEquals('192\.168\.', generate_host_regex('192.168.')); + } + + function testFunc_get_blocklist() + { + if (! defined('SPAM_INI_FILE') || ! file_exists(SPAM_INI_FILE)) { + $this->fail('SPAM_INI_FILE not defined or not found'); + return; + } + + // get_blocklist_add() + $array = array(); + // + get_blocklist_add($array, 'foo', 'bar'); + $this->assertEquals(1, count($array)); + $this->assertEquals('bar', $array['foo']); + // + get_blocklist_add($array, 'hoge', 'fuga'); + $this->assertEquals(2, count($array)); + $this->assertEquals('bar', $array['foo']); + $this->assertEquals('fuga', $array['hoge']); + // + get_blocklist_add($array, -1, '*.txt'); + $this->assertEquals(3, count($array)); + $this->assertEquals('bar', $array['foo']); + $this->assertEquals('fuga', $array['hoge']); + $this->assertEquals('#^.*\.txt$#i', $array['*.txt']); + + // get_blocklist() + // ALL + $array = get_blocklist(); + $this->assertTrue(isset($array['C'])); + $this->assertTrue(isset($array['goodhost'])); + // badhost + $array = get_blocklist('B-1'); + $this->assertTrue(isset($array['Google.com'])); + // goodhost + $array = get_blocklist('goodhost'); + $this->assertTrue(isset($array['IANA-examples'])); + } + +} + +?> diff --git a/spam/runner.php b/spam/runner.php index 8474d3b..3b0f346 100644 --- a/spam/runner.php +++ b/spam/runner.php @@ -1,5 +1,5 @@ show(); diff --git a/spam/spam.php b/spam/spam.php index bb43fea..eaf5952 100644 --- a/spam/spam.php +++ b/spam/spam.php @@ -1,5 +1,5 @@ = 4.2.0): var_export(): mail-reporting and dump related -if (! function_exists('var_export')) { - function var_export() { - return 'var_export() is not found on this server' . "\n"; - } -} - -// (PHP 4 >= 4.2.0): preg_grep() enables invert option -function preg_grep_invert($pattern = '//', $input = array()) -{ - static $invert; - if (! isset($invert)) $invert = defined('PREG_GREP_INVERT'); - - if ($invert) { - return preg_grep($pattern, $input, PREG_GREP_INVERT); - } else { - $result = preg_grep($pattern, $input); - if ($result) { - return array_diff($input, preg_grep($pattern, $input)); - } else { - return $input; - } - } -} - - -// --------------------- -// Utilities - -// 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 = var_export($expression, TRUE); - - $result = preg_replace( - // Remove a newline and spaces - '# => \n *array \(#', ' => array (', - $result - ); - - if ($ignore_numeric_keys) { - $result =preg_replace( - // Remove numeric keys - '#^( *)[0-9]+ => #m', '$1', - $result - ); - } - - if ($return) { - return $result; - } else { - echo $result; - return NULL; - } -} - -// Data structure: Create an array they _refer_only_one_ value -function one_value_array($num = 0, $value = NULL) -{ - $num = max(0, intval($num)); - $array = array(); - - for ($i = 0; $i < $num; $i++) { - $array[] = & $value; - } - - return $array; -} - -// Reverse $string with specified delimiter -function delimiter_reverse($string = 'foo.bar.example.com', $from_delim = '.', $to_delim = NULL) -{ - $to_null = ($to_delim === NULL); - - if (! is_string($from_delim) || (! $to_null && ! is_string($to_delim))) { - return FALSE; - } - if (is_array($string)) { - // Map, Recurse - $count = count($string); - $from = one_value_array($count, $from_delim); - if ($to_null) { - // Note: array_map() vanishes all keys - return array_map('delimiter_reverse', $string, $from); - } else { - $to = one_value_array($count, $to_delim); - // Note: array_map() vanishes all keys - return array_map('delimiter_reverse', $string, $from, $to); - } - } - if (! is_string($string)) { - return FALSE; - } - - // Returns com.example.bar.foo - if ($to_null) $to_delim = & $from_delim; - return implode($to_delim, array_reverse(explode($from_delim, $string))); -} - -// ksort() by domain -function ksort_by_domain(& $array) -{ - $sort = array(); - foreach(array_keys($array) as $key) { - $reversed = delimiter_reverse($key); - if ($reversed !== FALSE) { - $sort[$reversed] = $key; - } - } - ksort($sort, SORT_STRING); - - $result = array(); - foreach($sort as $key) { - $result[$key] = & $array[$key]; - } - - $array = $result; -} - -// Roughly strings(1) using PCRE -// This function is useful to: -// * Reduce the size of data, from removing unprintable binary data -// * Detect _bare_strings_ from binary data -// References: -// http://www.freebsd.org/cgi/man.cgi?query=strings (Man-page of GNU strings) -// http://www.pcre.org/pcre.txt -// Note: mb_ereg_replace() is one of mbstring extension's functions -// and need to init its encoding. -function strings($binary = '', $min_len = 4, $ignore_space = FALSE, $multibyte = FALSE) -{ - // String only - $binary = (is_array($binary) || $binary === TRUE) ? '' : strval($binary); - - $regex = $ignore_space ? - '[^[:graph:] \t\n]+' : // Remove "\0" etc, and readable spaces - '[^[:graph:][:space:]]+'; // Preserve readable spaces if possible - - $binary = $multibyte ? - mb_ereg_replace($regex, "\n", $binary) : - preg_replace('/' . $regex . '/s', "\n", $binary); - - if ($ignore_space) { - $binary = preg_replace( - array( - '/[ \t]{2,}/', - '/^[ \t]/m', - '/[ \t]$/m', - ), - array( - ' ', - '', - '' - ), - $binary); - } - - if ($min_len > 1) { - // The last character seems "\n" or not - $br = (! empty($binary) && $binary[strlen($binary) - 1] == "\n") ? "\n" : ''; - - $min_len = min(1024, intval($min_len)); - $regex = '/^.{' . $min_len . ',}/S'; - $binary = implode("\n", preg_grep($regex, explode("\n", $binary))) . $br; - } - - return $binary; -} - - -// --------------------- -// Utilities: Arrays - -// 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)) return 1; - - // Recurse - $count = 0; - foreach ($array as $part) { - $count += array_count_leaves($part, $count_empty); - } - 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) -{ - 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; -} - -// $array['something'] => $array['wanted'] -function array_rename_keys(& $array, $keys = array('from' => 'to'), $force = FALSE, $default = '') -{ - if (! is_array($array) || ! is_array($keys)) return FALSE; - - // Nondestructive test - if (! $force) { - foreach(array_keys($keys) as $from) { - if (! isset($array[$from])) { - return FALSE; - } - } - } - - foreach($keys as $from => $to) { - if ($from === $to) continue; - if (! $force || isset($array[$from])) { - $array[$to] = & $array[$from]; - unset($array[$from]); - } else { - $array[$to] = $default; - } - } - - return TRUE; -} - -// Remove redundant values from array() -function array_unique_recursive($array = array()) -{ - if (! is_array($array)) return $array; - - $tmp = array(); - foreach($array as $key => $value){ - if (is_array($value)) { - $array[$key] = array_unique_recursive($value); - } else { - if (isset($tmp[$value])) { - unset($array[$key]); - } else { - $tmp[$value] = TRUE; - } - } - } - - return $array; -} - // --------------------- // Part One : Checker diff --git a/spam/spam_util.php b/spam/spam_util.php new file mode 100644 index 0000000..217726a --- /dev/null +++ b/spam/spam_util.php @@ -0,0 +1,304 @@ += 4.2.0): var_export(): mail-reporting and dump related +if (! function_exists('var_export')) { + function var_export() { + return 'var_export() is not found on this server' . "\n"; + } +} + +// (PHP 4 >= 4.2.0): preg_grep() enables invert option +function preg_grep_invert($pattern = '//', $input = array()) +{ + static $invert; + if (! isset($invert)) $invert = defined('PREG_GREP_INVERT'); + + if ($invert) { + return preg_grep($pattern, $input, PREG_GREP_INVERT); + } else { + $result = preg_grep($pattern, $input); + if ($result) { + return array_diff($input, preg_grep($pattern, $input)); + } else { + return $input; + } + } +} + + +// --------------------- +// Utilities + +// 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 = var_export($expression, TRUE); + + $result = preg_replace( + // Remove a newline and spaces + '# => \n *array \(#', ' => array (', + $result + ); + + if ($ignore_numeric_keys) { + $result =preg_replace( + // Remove numeric keys + '#^( *)[0-9]+ => #m', '$1', + $result + ); + } + + if ($return) { + return $result; + } else { + echo $result; + return NULL; + } +} + +// Data structure: Create an array they _refer_only_one_ value +function one_value_array($num = 0, $value = NULL) +{ + $num = max(0, intval($num)); + $array = array(); + + for ($i = 0; $i < $num; $i++) { + $array[] = & $value; + } + + return $array; +} + +// Reverse $string with specified delimiter +function delimiter_reverse($string = 'foo.bar.example.com', $from_delim = '.', $to_delim = NULL) +{ + $to_null = ($to_delim === NULL); + + if (! is_string($from_delim) || (! $to_null && ! is_string($to_delim))) { + return FALSE; + } + if (is_array($string)) { + // Map, Recurse + $count = count($string); + $from = one_value_array($count, $from_delim); + if ($to_null) { + // Note: array_map() vanishes all keys + return array_map('delimiter_reverse', $string, $from); + } else { + $to = one_value_array($count, $to_delim); + // Note: array_map() vanishes all keys + return array_map('delimiter_reverse', $string, $from, $to); + } + } + if (! is_string($string)) { + return FALSE; + } + + // Returns com.example.bar.foo + if ($to_null) $to_delim = & $from_delim; + return implode($to_delim, array_reverse(explode($from_delim, $string))); +} + +// ksort() by domain +function ksort_by_domain(& $array) +{ + $sort = array(); + foreach(array_keys($array) as $key) { + $reversed = delimiter_reverse($key); + if ($reversed !== FALSE) { + $sort[$reversed] = $key; + } + } + ksort($sort, SORT_STRING); + + $result = array(); + foreach($sort as $key) { + $result[$key] = & $array[$key]; + } + + $array = $result; +} + +// Roughly strings(1) using PCRE +// This function is useful to: +// * Reduce the size of data, from removing unprintable binary data +// * Detect _bare_strings_ from binary data +// References: +// http://www.freebsd.org/cgi/man.cgi?query=strings (Man-page of GNU strings) +// http://www.pcre.org/pcre.txt +// Note: mb_ereg_replace() is one of mbstring extension's functions +// and need to init its encoding. +function strings($binary = '', $min_len = 4, $ignore_space = FALSE, $multibyte = FALSE) +{ + // String only + $binary = (is_array($binary) || $binary === TRUE) ? '' : strval($binary); + + $regex = $ignore_space ? + '[^[:graph:] \t\n]+' : // Remove "\0" etc, and readable spaces + '[^[:graph:][:space:]]+'; // Preserve readable spaces if possible + + $binary = $multibyte ? + mb_ereg_replace($regex, "\n", $binary) : + preg_replace('/' . $regex . '/s', "\n", $binary); + + if ($ignore_space) { + $binary = preg_replace( + array( + '/[ \t]{2,}/', + '/^[ \t]/m', + '/[ \t]$/m', + ), + array( + ' ', + '', + '' + ), + $binary); + } + + if ($min_len > 1) { + // The last character seems "\n" or not + $br = (! empty($binary) && $binary[strlen($binary) - 1] == "\n") ? "\n" : ''; + + $min_len = min(1024, intval($min_len)); + $regex = '/^.{' . $min_len . ',}/S'; + $binary = implode("\n", preg_grep($regex, explode("\n", $binary))) . $br; + } + + return $binary; +} + + +// --------------------- +// Utilities: Arrays + +// 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)) return 1; + + // Recurse + $count = 0; + foreach ($array as $part) { + $count += array_count_leaves($part, $count_empty); + } + 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) +{ + 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; +} + +// $array['something'] => $array['wanted'] +function array_rename_keys(& $array, $keys = array('from' => 'to'), $force = FALSE, $default = '') +{ + if (! is_array($array) || ! is_array($keys)) return FALSE; + + // Nondestructive test + if (! $force) { + foreach(array_keys($keys) as $from) { + if (! isset($array[$from])) { + return FALSE; + } + } + } + + foreach($keys as $from => $to) { + if ($from === $to) continue; + if (! $force || isset($array[$from])) { + $array[$to] = & $array[$from]; + unset($array[$from]); + } else { + $array[$to] = $default; + } + } + + return TRUE; +} + +// Remove redundant values from array() +function array_unique_recursive($array = array()) +{ + if (! is_array($array)) return $array; + + $tmp = array(); + foreach($array as $key => $value){ + if (is_array($value)) { + $array[$key] = array_unique_recursive($value); + } else { + if (isset($tmp[$value])) { + unset($array[$key]); + } else { + $tmp[$value] = TRUE; + } + } + } + + return $array; +} + +?> -- 2.11.0