Skip to content
This repository
Browse code

Fix validation of IPv6 on IPv4 checks.

Separate IPv4 and IPv6 validation to allow strict checking.
  • Loading branch information...
commit 1302fba6326f854f3a587da421a0374c64f8dca5 1 parent 71dbd9b
Graham Weldon authored January 06, 2010
53  cake/libs/validation.php
@@ -541,21 +541,63 @@ function extension($check, $extensions = array('gif', 'jpeg', 'png', 'jpg')) {
541 541
 	}
542 542
 
543 543
 /**
544  
- * Validation of an IPv4 address.
  544
+ * Validation of an IP address.
  545
+ *
  546
+ * Valid IP version strings for type restriction are:
  547
+ * - both: Check both IPv4 and IPv6, return true if the supplied address matches either version
  548
+ * - IPv4: Version 4 (Eg: 127.0.0.1, 192.168.10.123, 203.211.24.8)
  549
+ * - IPv6: Version 6 (Eg: ::1, 2001:0db8::1428:57ab)
545 550
  *
546 551
  * @param string $check The string to test.
  552
+ * @param string $type The IP Version to test against
547 553
  * @return boolean Success
548 554
  * @access public
549 555
  */
550  
-	function ip($check, $type = 'IPv4') {
551  
-		if (function_exists('filter_var')) {
552  
-			return (boolean) filter_var($check, FILTER_VALIDATE_IP);
  556
+	function ip($check, $type = 'both') {
  557
+		$_this =& Validation::getInstance();
  558
+		$success = false;
  559
+		if ($type === 'IPv4' || $type === 'both') {
  560
+			$success |= $_this->_ipv4($check);
  561
+		}
  562
+		if ($type === 'IPv6' || $type === 'both') {
  563
+			$success |= $_this->_ipv6($check);
553 564
 		}
  565
+		return $success;
  566
+	}
554 567
 
  568
+/**
  569
+ * Validation of IPv4 addresses.
  570
+ *
  571
+ * @param string $check IP Address to test
  572
+ * @return boolean Success
  573
+ * @access protected
  574
+ */
  575
+	function _ipv4($check) {
  576
+		if (function_exists('filter_var')) {
  577
+			return filter_var($check, FILTER_VALIDATE_IP, array('flags' => FILTER_FLAG_IPV4)) !== false;
  578
+		}
555 579
 		$_this =& Validation::getInstance();
  580
+		$_this->__populateIp();
556 581
 		$_this->check = $check;
  582
+		$_this->regex = '/^' . $_this->__pattern['IPv4'] . '$/';
  583
+		return $_this->_check();
  584
+	}
  585
+
  586
+/**
  587
+ * Validation of IPv6 addresses.
  588
+ *
  589
+ * @param string $check IP Address to test
  590
+ * @return boolean Success
  591
+ * @access protected
  592
+ */
  593
+	function _ipv6($check) {
  594
+		if (function_exists('filter_var')) {
  595
+			return filter_var($check, FILTER_VALIDATE_IP, array('flags' => FILTER_FLAG_IPV6)) !== false;
  596
+		}
  597
+		$_this =& Validation::getInstance();
557 598
 		$_this->__populateIp();
558  
-		$_this->regex = '/^' . $_this->__pattern[$type] . '$/';
  599
+		$_this->check = $check;
  600
+		$_this->regex = '/^' . $_this->__pattern['IPv6'] . '$/';
559 601
 		return $_this->_check();
560 602
 	}
561 603
 
@@ -954,6 +996,7 @@ function _luhn() {
954 996
  * Lazily popualate the IP address patterns used for validations
955 997
  *
956 998
  * @return void
  999
+ * @access private
957 1000
  */
958 1001
 	function __populateIp() {
959 1002
 		if (!isset($this->__pattern['IPv6'])) {
43  cake/tests/cases/libs/validation.test.php
@@ -1725,13 +1725,16 @@ function testEqualTo() {
1725 1725
  * @access public
1726 1726
  * @return void
1727 1727
  */
1728  
-	function testIp() {
1729  
-		$this->assertTrue(Validation::ip('0.0.0.0'));
1730  
-		$this->assertTrue(Validation::ip('192.168.1.156'));
1731  
-		$this->assertTrue(Validation::ip('255.255.255.255'));
1732  
-		$this->assertFalse(Validation::ip('127.0.0'));
1733  
-		$this->assertFalse(Validation::ip('127.0.0.a'));
1734  
-		$this->assertFalse(Validation::ip('127.0.0.256'));
  1728
+	function testIpv4() {
  1729
+		$this->assertTrue(Validation::ip('0.0.0.0', 'IPv4'));
  1730
+		$this->assertTrue(Validation::ip('192.168.1.156', 'IPv4'));
  1731
+		$this->assertTrue(Validation::ip('255.255.255.255', 'IPv4'));
  1732
+
  1733
+		$this->assertFalse(Validation::ip('127.0.0', 'IPv4'));
  1734
+		$this->assertFalse(Validation::ip('127.0.0.a', 'IPv4'));
  1735
+		$this->assertFalse(Validation::ip('127.0.0.256', 'IPv4'));
  1736
+
  1737
+		$this->assertFalse(Validation::ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'IPv4'));
1735 1738
 	}
1736 1739
 
1737 1740
 /**
@@ -1772,6 +1775,32 @@ function testIpv6() {
1772 1775
 		$this->assertFalse(Validation::ip('1:2:3::4:5:6:7:8:9', 'IPv6'));
1773 1776
 		$this->assertFalse(Validation::ip('::ffff:2.3.4', 'IPv6'));
1774 1777
 		$this->assertFalse(Validation::ip('::ffff:257.1.2.3', 'IPv6'));
  1778
+
  1779
+		$this->assertFalse(Validation::ip('0.0.0.0', 'IPv6'));
  1780
+	}
  1781
+
  1782
+/**
  1783
+ * testIpBoth method
  1784
+ *
  1785
+ * @return void
  1786
+ * @access public
  1787
+ */	
  1788
+	function testIpBoth() {
  1789
+		$this->assertTrue(Validation::ip('0.0.0.0'));
  1790
+		$this->assertTrue(Validation::ip('192.168.1.156'));
  1791
+		$this->assertTrue(Validation::ip('255.255.255.255'));
  1792
+		
  1793
+		$this->assertFalse(Validation::ip('127.0.0'));
  1794
+		$this->assertFalse(Validation::ip('127.0.0.a'));
  1795
+		$this->assertFalse(Validation::ip('127.0.0.256'));
  1796
+
  1797
+		$this->assertTrue(Validation::ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
  1798
+		$this->assertTrue(Validation::ip('2001:db8:85a3:0:0:8a2e:370:7334'));
  1799
+		$this->assertTrue(Validation::ip('2001:db8:85a3::8a2e:370:7334'));
  1800
+
  1801
+		$this->assertFalse(Validation::ip('2001:db8:85a3::8a2e:37023:7334'));
  1802
+		$this->assertFalse(Validation::ip('2001:db8:85a3::8a2e:370k:7334'));
  1803
+		$this->assertFalse(Validation::ip('1:2:3:4:5:6:7:8:9'));
1775 1804
 	}
1776 1805
 
1777 1806
 /**

0 notes on commit 1302fba

Please sign in to comment.
Something went wrong with that request. Please try again.