mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Fix validation of IPv6 on IPv4 checks.
Separate IPv4 and IPv6 validation to allow strict checking.
This commit is contained in:
parent
71dbd9bd32
commit
1302fba632
2 changed files with 87 additions and 15 deletions
|
@ -541,21 +541,63 @@ class Validation extends Object {
|
|||
}
|
||||
|
||||
/**
|
||||
* Validation of an IPv4 address.
|
||||
* Validation of an IP address.
|
||||
*
|
||||
* Valid IP version strings for type restriction are:
|
||||
* - both: Check both IPv4 and IPv6, return true if the supplied address matches either version
|
||||
* - IPv4: Version 4 (Eg: 127.0.0.1, 192.168.10.123, 203.211.24.8)
|
||||
* - IPv6: Version 6 (Eg: ::1, 2001:0db8::1428:57ab)
|
||||
*
|
||||
* @param string $check The string to test.
|
||||
* @param string $type The IP Version to test against
|
||||
* @return boolean Success
|
||||
* @access public
|
||||
*/
|
||||
function ip($check, $type = 'IPv4') {
|
||||
if (function_exists('filter_var')) {
|
||||
return (boolean) filter_var($check, FILTER_VALIDATE_IP);
|
||||
}
|
||||
|
||||
function ip($check, $type = 'both') {
|
||||
$_this =& Validation::getInstance();
|
||||
$success = false;
|
||||
if ($type === 'IPv4' || $type === 'both') {
|
||||
$success |= $_this->_ipv4($check);
|
||||
}
|
||||
if ($type === 'IPv6' || $type === 'both') {
|
||||
$success |= $_this->_ipv6($check);
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validation of IPv4 addresses.
|
||||
*
|
||||
* @param string $check IP Address to test
|
||||
* @return boolean Success
|
||||
* @access protected
|
||||
*/
|
||||
function _ipv4($check) {
|
||||
if (function_exists('filter_var')) {
|
||||
return filter_var($check, FILTER_VALIDATE_IP, array('flags' => FILTER_FLAG_IPV4)) !== false;
|
||||
}
|
||||
$_this =& Validation::getInstance();
|
||||
$_this->check = $check;
|
||||
$_this->__populateIp();
|
||||
$_this->regex = '/^' . $_this->__pattern[$type] . '$/';
|
||||
$_this->check = $check;
|
||||
$_this->regex = '/^' . $_this->__pattern['IPv4'] . '$/';
|
||||
return $_this->_check();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validation of IPv6 addresses.
|
||||
*
|
||||
* @param string $check IP Address to test
|
||||
* @return boolean Success
|
||||
* @access protected
|
||||
*/
|
||||
function _ipv6($check) {
|
||||
if (function_exists('filter_var')) {
|
||||
return filter_var($check, FILTER_VALIDATE_IP, array('flags' => FILTER_FLAG_IPV6)) !== false;
|
||||
}
|
||||
$_this =& Validation::getInstance();
|
||||
$_this->__populateIp();
|
||||
$_this->check = $check;
|
||||
$_this->regex = '/^' . $_this->__pattern['IPv6'] . '$/';
|
||||
return $_this->_check();
|
||||
}
|
||||
|
||||
|
@ -954,6 +996,7 @@ class Validation extends Object {
|
|||
* Lazily popualate the IP address patterns used for validations
|
||||
*
|
||||
* @return void
|
||||
* @access private
|
||||
*/
|
||||
function __populateIp() {
|
||||
if (!isset($this->__pattern['IPv6'])) {
|
||||
|
|
|
@ -1725,13 +1725,16 @@ class ValidationTest extends CakeTestCase {
|
|||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testIp() {
|
||||
$this->assertTrue(Validation::ip('0.0.0.0'));
|
||||
$this->assertTrue(Validation::ip('192.168.1.156'));
|
||||
$this->assertTrue(Validation::ip('255.255.255.255'));
|
||||
$this->assertFalse(Validation::ip('127.0.0'));
|
||||
$this->assertFalse(Validation::ip('127.0.0.a'));
|
||||
$this->assertFalse(Validation::ip('127.0.0.256'));
|
||||
function testIpv4() {
|
||||
$this->assertTrue(Validation::ip('0.0.0.0', 'IPv4'));
|
||||
$this->assertTrue(Validation::ip('192.168.1.156', 'IPv4'));
|
||||
$this->assertTrue(Validation::ip('255.255.255.255', 'IPv4'));
|
||||
|
||||
$this->assertFalse(Validation::ip('127.0.0', 'IPv4'));
|
||||
$this->assertFalse(Validation::ip('127.0.0.a', 'IPv4'));
|
||||
$this->assertFalse(Validation::ip('127.0.0.256', 'IPv4'));
|
||||
|
||||
$this->assertFalse(Validation::ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'IPv4'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1772,6 +1775,32 @@ class ValidationTest extends CakeTestCase {
|
|||
$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('0.0.0.0', 'IPv6'));
|
||||
}
|
||||
|
||||
/**
|
||||
* testIpBoth method
|
||||
*
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
function testIpBoth() {
|
||||
$this->assertTrue(Validation::ip('0.0.0.0'));
|
||||
$this->assertTrue(Validation::ip('192.168.1.156'));
|
||||
$this->assertTrue(Validation::ip('255.255.255.255'));
|
||||
|
||||
$this->assertFalse(Validation::ip('127.0.0'));
|
||||
$this->assertFalse(Validation::ip('127.0.0.a'));
|
||||
$this->assertFalse(Validation::ip('127.0.0.256'));
|
||||
|
||||
$this->assertTrue(Validation::ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
|
||||
$this->assertTrue(Validation::ip('2001:db8:85a3:0:0:8a2e:370:7334'));
|
||||
$this->assertTrue(Validation::ip('2001:db8:85a3::8a2e:370:7334'));
|
||||
|
||||
$this->assertFalse(Validation::ip('2001:db8:85a3::8a2e:37023:7334'));
|
||||
$this->assertFalse(Validation::ip('2001:db8:85a3::8a2e:370k:7334'));
|
||||
$this->assertFalse(Validation::ip('1:2:3:4:5:6:7:8:9'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue