From c28b651b91367e86f1bd6ab7f0cd6c45e58811ab Mon Sep 17 00:00:00 2001 From: Joffrey Jaffeux Date: Wed, 6 Jun 2012 14:26:04 +0200 Subject: Add support for ipv6 --- system/core/Input.php | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/system/core/Input.php b/system/core/Input.php index 73f46ba6a..6a5a9d8f0 100755 --- a/system/core/Input.php +++ b/system/core/Input.php @@ -383,7 +383,27 @@ class CI_Input { */ public function valid_ip($ip) { - return (bool) filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); + if($this->ip_version($ip) === '4') + { + return (bool) filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); + } + else + { + return (bool) filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); + } + } + + // -------------------------------------------------------------------- + + /** + * Return ip version + * + * @param string + * @return string + */ + public function ip_version($ip) + { + return strpos($ip, ":") === false ? '4' : '6'; } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 83c62ccf881847c5f4eef83822de0f7b09f98491 Mon Sep 17 00:00:00 2001 From: Joffrey Jaffeux Date: Wed, 6 Jun 2012 14:26:31 +0200 Subject: add tests for ipv6 and new method ip_version() --- tests/codeigniter/core/Input_test.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/codeigniter/core/Input_test.php b/tests/codeigniter/core/Input_test.php index cfc80c950..27e152d6a 100644 --- a/tests/codeigniter/core/Input_test.php +++ b/tests/codeigniter/core/Input_test.php @@ -143,4 +143,31 @@ class Input_test extends CI_TestCase { $this->assertEquals("Hello, i try to your site", $harm); $this->assertEquals("Hello, i try to [removed]alert('Hack');[removed] your site", $harmless); } + + // -------------------------------------------------------------------- + + public function test_valid_ip() + { + $ip_v4 = '175.123.74.43'; + $this->assertTrue($this->input->valid_ip($ip_v4)); + + $ip_v6 = array('2001:0db8:0000:85a3:0000:0000:ac1f:8001', '2001:db8:0:85a3:0:0:ac1f:8001', '2001:db8:0:85a3::ac1f:8001'); + foreach($ip_v6 as $ip) { + $this->assertTrue($this->input->valid_ip($ip)); + } + } + + // -------------------------------------------------------------------- + + public function test_ip_version() + { + $ip_v4 = '175.123.74.43'; + $this->assertEquals('4', $this->input->ip_version($ip_v4)); + + $ip_v6 = array('2001:0db8:0000:85a3:0000:0000:ac1f:8001', '2001:db8:0:85a3:0:0:ac1f:8001', '2001:db8:0:85a3::ac1f:8001'); + foreach($ip_v6 as $ip) { + $this->assertEquals('6', $this->input->ip_version($ip)); + } + } + } \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 73ed3ed3e2cf3efeb8332c569e589f16468daf7d Mon Sep 17 00:00:00 2001 From: Joffrey Jaffeux Date: Wed, 6 Jun 2012 14:26:55 +0200 Subject: update changelog for ipv6 support --- user_guide_src/source/changelog.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index f4bba25ed..c12db1f27 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -135,6 +135,7 @@ Release Date: Not Released - Added a Wincache driver to the :doc:`Caching Library `. - Added dsn (delivery status notification) option to the :doc:`Email Library `. - Enabled public access to Email library's set_header() for adding additional headers to e-mails. Original function _set_header() now renamed to set_header(). + - Input library now supports IPv6 and has a ip_version() method. - Core -- cgit v1.2.3-24-g4f1b From 5f33f368ee4a635c93ffc73997969a2eabdf67b9 Mon Sep 17 00:00:00 2001 From: Joffrey Jaffeux Date: Wed, 6 Jun 2012 14:28:16 +0200 Subject: update documentation for ip_version() and tells valid_ip() now supports ipv6 --- user_guide_src/source/libraries/input.rst | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/user_guide_src/source/libraries/input.rst b/user_guide_src/source/libraries/input.rst index 432bac3c7..649fe43d6 100644 --- a/user_guide_src/source/libraries/input.rst +++ b/user_guide_src/source/libraries/input.rst @@ -228,7 +228,7 @@ $this->input->valid_ip($ip) ============================ Takes an IP address as input and returns TRUE or FALSE (boolean) if it -is valid or not. Note: The $this->input->ip_address() function above +is valid or not (works with IPv4 and IPv6). Note: The $this->input->ip_address() function above validates the IP automatically. :: @@ -242,6 +242,15 @@ validates the IP automatically. echo 'Valid'; } +$this->input->ip_version($ip) +============================ + +Takes an IP address as input and returns the version : 4 or 6. +:: + + $ip = '175.123.74.43'; + echo $this->input->ip_version($ip); // 4 + $this->input->user_agent() =========================== -- cgit v1.2.3-24-g4f1b From 6c8515459e4c631b0882532e8c44ee6435fd3809 Mon Sep 17 00:00:00 2001 From: Joffrey Jaffeux Date: Wed, 6 Jun 2012 14:30:11 +0200 Subject: bad spacing --- user_guide_src/source/changelog.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index c12db1f27..0ab18782e 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -135,7 +135,7 @@ Release Date: Not Released - Added a Wincache driver to the :doc:`Caching Library `. - Added dsn (delivery status notification) option to the :doc:`Email Library `. - Enabled public access to Email library's set_header() for adding additional headers to e-mails. Original function _set_header() now renamed to set_header(). - - Input library now supports IPv6 and has a ip_version() method. + - Input library now supports IPv6 and has a ip_version() method. - Core -- cgit v1.2.3-24-g4f1b From 0723617703dda3660597d9cdef59e7cdded1c497 Mon Sep 17 00:00:00 2001 From: Joffrey Jaffeux Date: Wed, 6 Jun 2012 14:39:02 +0200 Subject: follow styling guide --- system/core/Input.php | 2 +- tests/codeigniter/core/Input_test.php | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/system/core/Input.php b/system/core/Input.php index 6a5a9d8f0..ac67aaf4f 100755 --- a/system/core/Input.php +++ b/system/core/Input.php @@ -383,7 +383,7 @@ class CI_Input { */ public function valid_ip($ip) { - if($this->ip_version($ip) === '4') + if ($this->ip_version($ip) === '4') { return (bool) filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); } diff --git a/tests/codeigniter/core/Input_test.php b/tests/codeigniter/core/Input_test.php index 27e152d6a..2aa3a6246 100644 --- a/tests/codeigniter/core/Input_test.php +++ b/tests/codeigniter/core/Input_test.php @@ -152,7 +152,8 @@ class Input_test extends CI_TestCase { $this->assertTrue($this->input->valid_ip($ip_v4)); $ip_v6 = array('2001:0db8:0000:85a3:0000:0000:ac1f:8001', '2001:db8:0:85a3:0:0:ac1f:8001', '2001:db8:0:85a3::ac1f:8001'); - foreach($ip_v6 as $ip) { + foreach($ip_v6 as $ip) + { $this->assertTrue($this->input->valid_ip($ip)); } } @@ -165,7 +166,8 @@ class Input_test extends CI_TestCase { $this->assertEquals('4', $this->input->ip_version($ip_v4)); $ip_v6 = array('2001:0db8:0000:85a3:0000:0000:ac1f:8001', '2001:db8:0:85a3:0:0:ac1f:8001', '2001:db8:0:85a3::ac1f:8001'); - foreach($ip_v6 as $ip) { + foreach($ip_v6 as $ip) + { $this->assertEquals('6', $this->input->ip_version($ip)); } } -- cgit v1.2.3-24-g4f1b From 908f36a583384b0314d04c4cfe71992746e35daa Mon Sep 17 00:00:00 2001 From: Joffrey Jaffeux Date: Wed, 6 Jun 2012 15:47:22 +0200 Subject: ip_version() now returns int instead of string --- system/core/Input.php | 6 +++--- tests/codeigniter/core/Input_test.php | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/system/core/Input.php b/system/core/Input.php index ac67aaf4f..36ff96d03 100755 --- a/system/core/Input.php +++ b/system/core/Input.php @@ -383,7 +383,7 @@ class CI_Input { */ public function valid_ip($ip) { - if ($this->ip_version($ip) === '4') + if ($this->ip_version($ip) === 4) { return (bool) filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); } @@ -399,11 +399,11 @@ class CI_Input { * Return ip version * * @param string - * @return string + * @return int */ public function ip_version($ip) { - return strpos($ip, ":") === false ? '4' : '6'; + return strpos($ip, ":") === false ? 4 : 6; } // -------------------------------------------------------------------- diff --git a/tests/codeigniter/core/Input_test.php b/tests/codeigniter/core/Input_test.php index 2aa3a6246..98d6299f8 100644 --- a/tests/codeigniter/core/Input_test.php +++ b/tests/codeigniter/core/Input_test.php @@ -163,12 +163,12 @@ class Input_test extends CI_TestCase { public function test_ip_version() { $ip_v4 = '175.123.74.43'; - $this->assertEquals('4', $this->input->ip_version($ip_v4)); + $this->assertEquals(4, $this->input->ip_version($ip_v4)); $ip_v6 = array('2001:0db8:0000:85a3:0000:0000:ac1f:8001', '2001:db8:0:85a3:0:0:ac1f:8001', '2001:db8:0:85a3::ac1f:8001'); foreach($ip_v6 as $ip) { - $this->assertEquals('6', $this->input->ip_version($ip)); + $this->assertEquals(6, $this->input->ip_version($ip)); } } -- cgit v1.2.3-24-g4f1b From 1eb9b127cfb3aef5d89b86a48e35b2f35cd17f81 Mon Sep 17 00:00:00 2001 From: Joffrey Jaffeux Date: Wed, 6 Jun 2012 17:57:40 +0200 Subject: styling guide false -> FALSE --- system/core/Input.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/core/Input.php b/system/core/Input.php index 36ff96d03..c1f2086c4 100755 --- a/system/core/Input.php +++ b/system/core/Input.php @@ -403,7 +403,7 @@ class CI_Input { */ public function ip_version($ip) { - return strpos($ip, ":") === false ? 4 : 6; + return strpos($ip, ":") === FALSE ? 4 : 6; } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 70b5d3b4eb74ad27da7eac29ef5d349be944ba15 Mon Sep 17 00:00:00 2001 From: Joffrey Jaffeux Date: Wed, 6 Jun 2012 18:22:36 +0200 Subject: use an ip of the range 192.18.0.0/15 --- tests/codeigniter/core/Input_test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/codeigniter/core/Input_test.php b/tests/codeigniter/core/Input_test.php index 98d6299f8..3682d308d 100644 --- a/tests/codeigniter/core/Input_test.php +++ b/tests/codeigniter/core/Input_test.php @@ -148,7 +148,7 @@ class Input_test extends CI_TestCase { public function test_valid_ip() { - $ip_v4 = '175.123.74.43'; + $ip_v4 = '192.18.0.1'; $this->assertTrue($this->input->valid_ip($ip_v4)); $ip_v6 = array('2001:0db8:0000:85a3:0000:0000:ac1f:8001', '2001:db8:0:85a3:0:0:ac1f:8001', '2001:db8:0:85a3::ac1f:8001'); @@ -162,7 +162,7 @@ class Input_test extends CI_TestCase { public function test_ip_version() { - $ip_v4 = '175.123.74.43'; + $ip_v4 = '192.18.0.1'; $this->assertEquals(4, $this->input->ip_version($ip_v4)); $ip_v6 = array('2001:0db8:0000:85a3:0000:0000:ac1f:8001', '2001:db8:0:85a3:0:0:ac1f:8001', '2001:db8:0:85a3::ac1f:8001'); -- cgit v1.2.3-24-g4f1b From 1ab6f6520ebfc016c49cfbe3a4d9d009be5da268 Mon Sep 17 00:00:00 2001 From: Joffrey Jaffeux Date: Wed, 6 Jun 2012 20:14:13 +0200 Subject: removed ip_version() --- system/core/Input.php | 22 +--------------------- tests/codeigniter/core/Input_test.php | 14 -------------- user_guide_src/source/changelog.rst | 2 +- user_guide_src/source/libraries/input.rst | 9 --------- 4 files changed, 2 insertions(+), 45 deletions(-) diff --git a/system/core/Input.php b/system/core/Input.php index c1f2086c4..b986c4973 100755 --- a/system/core/Input.php +++ b/system/core/Input.php @@ -383,27 +383,7 @@ class CI_Input { */ public function valid_ip($ip) { - if ($this->ip_version($ip) === 4) - { - return (bool) filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); - } - else - { - return (bool) filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); - } - } - - // -------------------------------------------------------------------- - - /** - * Return ip version - * - * @param string - * @return int - */ - public function ip_version($ip) - { - return strpos($ip, ":") === FALSE ? 4 : 6; + return (bool) filter_var($ip, FILTER_VALIDATE_IP); } // -------------------------------------------------------------------- diff --git a/tests/codeigniter/core/Input_test.php b/tests/codeigniter/core/Input_test.php index 3682d308d..c9322c027 100644 --- a/tests/codeigniter/core/Input_test.php +++ b/tests/codeigniter/core/Input_test.php @@ -158,18 +158,4 @@ class Input_test extends CI_TestCase { } } - // -------------------------------------------------------------------- - - public function test_ip_version() - { - $ip_v4 = '192.18.0.1'; - $this->assertEquals(4, $this->input->ip_version($ip_v4)); - - $ip_v6 = array('2001:0db8:0000:85a3:0000:0000:ac1f:8001', '2001:db8:0:85a3:0:0:ac1f:8001', '2001:db8:0:85a3::ac1f:8001'); - foreach($ip_v6 as $ip) - { - $this->assertEquals(6, $this->input->ip_version($ip)); - } - } - } \ No newline at end of file diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 3118b7dc5..0b987b1be 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -135,7 +135,7 @@ Release Date: Not Released - Allowed for setting table class defaults in a config file. - Added a Wincache driver to the :doc:`Caching Library `. - Added dsn (delivery status notification) option to the :doc:`Email Library `. - - Input library now supports IPv6 and has a ip_version() method. + - Input library now supports IPv6. - Renamed method _set_header() to set_header() and made it public to enable adding custom headers in the :doc:`Email Library `. - Core diff --git a/user_guide_src/source/libraries/input.rst b/user_guide_src/source/libraries/input.rst index 649fe43d6..abdf87704 100644 --- a/user_guide_src/source/libraries/input.rst +++ b/user_guide_src/source/libraries/input.rst @@ -242,15 +242,6 @@ validates the IP automatically. echo 'Valid'; } -$this->input->ip_version($ip) -============================ - -Takes an IP address as input and returns the version : 4 or 6. -:: - - $ip = '175.123.74.43'; - echo $this->input->ip_version($ip); // 4 - $this->input->user_agent() =========================== -- cgit v1.2.3-24-g4f1b From 3dfa14b3831c6f22acdca3d3ae8f0cb48e66b782 Mon Sep 17 00:00:00 2001 From: Joffrey Jaffeux Date: Wed, 6 Jun 2012 22:21:31 +0200 Subject: remove text about ipv4/ipv6 support in the doc --- user_guide_src/source/libraries/input.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/libraries/input.rst b/user_guide_src/source/libraries/input.rst index abdf87704..432bac3c7 100644 --- a/user_guide_src/source/libraries/input.rst +++ b/user_guide_src/source/libraries/input.rst @@ -228,7 +228,7 @@ $this->input->valid_ip($ip) ============================ Takes an IP address as input and returns TRUE or FALSE (boolean) if it -is valid or not (works with IPv4 and IPv6). Note: The $this->input->ip_address() function above +is valid or not. Note: The $this->input->ip_address() function above validates the IP automatically. :: -- cgit v1.2.3-24-g4f1b