Skip to content
Browse files

Fix Validation::ip() not respecting type.

Apply patch from 'Xavier Franquet' to fix ip(), so that
it does not always validate both IP versions.

Fixes #2944
  • Loading branch information...
1 parent 56b2b8a commit 454fae9bd0bb0bc46e1408851dfc52b1528025ff @markstory markstory committed Jun 10, 2012
Showing with 5 additions and 3 deletions.
  1. +2 −0 lib/Cake/Test/Case/Utility/ValidationTest.php
  2. +3 −3 lib/Cake/Utility/Validation.php
View
2 lib/Cake/Test/Case/Utility/ValidationTest.php
@@ -1663,6 +1663,7 @@ public function testIpV4() {
$this->assertFalse(Validation::ip('127.0.0'));
$this->assertFalse(Validation::ip('127.0.0.a'));
$this->assertFalse(Validation::ip('127.0.0.256'));
+ $this->assertFalse(Validation::ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'ipv4'), 'IPv6 is not valid IPv4');
}
/**
@@ -1702,6 +1703,7 @@ public function testIpv6() {
$this->assertFalse(Validation::ip('1:2:3::4:5:6:7:8:9', 'IPv6'));
$this->assertFalse(Validation::ip('::ffff:2.3.4', 'IPv6'));
$this->assertFalse(Validation::ip('::ffff:257.1.2.3', 'IPv6'));
+ $this->assertFalse(Validation::ip('255.255.255.255', 'ipv6'), 'IPv4 is not valid IPv6');
}
/**
View
6 lib/Cake/Utility/Validation.php
@@ -467,12 +467,12 @@ public static function extension($check, $extensions = array('gif', 'jpeg', 'png
*/
public static function ip($check, $type = 'both') {
$type = strtolower($type);
- $flags = array();
+ $flags = null;
if ($type === 'ipv4' || $type === 'both') {
- $flags[] = FILTER_FLAG_IPV4;
+ $flags |= FILTER_FLAG_IPV4;
}
if ($type === 'ipv6' || $type === 'both') {
- $flags[] = FILTER_FLAG_IPV6;
+ $flags |= FILTER_FLAG_IPV6;
}
return (boolean)filter_var($check, FILTER_VALIDATE_IP, array('flags' => $flags));
}

0 comments on commit 454fae9

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