Skip to content

Commit

Permalink
Fix Validation::ip() not respecting type.
Browse files Browse the repository at this point in the history
Apply patch from 'Xavier Franquet' to fix ip(), so that
it does not always validate both IP versions.

Fixes #2944
  • Loading branch information
markstory committed Jun 10, 2012
1 parent 56b2b8a commit 454fae9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/Cake/Test/Case/Utility/ValidationTest.php
Expand Up @@ -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');
}

/**
Expand Down Expand Up @@ -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');
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/Cake/Utility/Validation.php
Expand Up @@ -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));
}
Expand Down

0 comments on commit 454fae9

Please sign in to comment.