mirror of
https://github.com/Respect/Validation.git
synced 2024-06-09 01:02:16 +02:00
Fix for 32bit integers
This commit is contained in:
parent
5f93aa1040
commit
e08212ed17
|
@ -27,7 +27,7 @@ class Ip extends AbstractRule
|
|||
|| $input == '0.0.0.0-255.255.255.255')
|
||||
return null;
|
||||
|
||||
$range = array('min' => null, 'max' => null);
|
||||
$range = array('min' => null, 'max' => null, 'mask' => null);
|
||||
|
||||
if (strpos($input, '-') !== false)
|
||||
list($range['min'], $range['max']) = explode('-', $input);
|
||||
|
@ -73,7 +73,7 @@ class Ip extends AbstractRule
|
|||
$isAddressMask = strpos($input[1], '.') !== false;
|
||||
|
||||
if ($isAddressMask && $this->verifyAddress($input[1])) {
|
||||
$range['mask'] = ip2long($input[1]);
|
||||
$range['mask'] = sprintf('%032b', ip2long($input[1]));
|
||||
|
||||
return ;
|
||||
}
|
||||
|
@ -82,7 +82,7 @@ class Ip extends AbstractRule
|
|||
throw new ComponentException('Invalid network mask');
|
||||
}
|
||||
|
||||
$range['mask'] = ~ (pow(2, (32 - $input[1])) - 1);
|
||||
$range['mask'] = sprintf('%032b', ip2long(long2ip(~(pow(2, (32 - $input[1])) - 1))));
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
|
@ -106,15 +106,23 @@ class Ip extends AbstractRule
|
|||
if ($this->networkRange === null)
|
||||
return true;
|
||||
|
||||
$input = ip2long($input);
|
||||
$range = $this->networkRange;
|
||||
|
||||
if (isset($range['mask'])) {
|
||||
return ($input & $range['mask']) == (ip2long($range['min']) & $range['mask']);
|
||||
if (isset($this->networkRange['mask'])) {
|
||||
return $this->compareRange($input);
|
||||
}
|
||||
|
||||
return $input >= ip2long($range['min'])
|
||||
&& $input <= ip2long($range['max']);
|
||||
$input = sprintf('%u', ip2long($input));
|
||||
|
||||
return bccomp($input, sprintf('%u', ip2long($this->networkRange['min']))) >= 0
|
||||
&& bccomp($input, sprintf('%u', ip2long($this->networkRange['max']))) <= 0;
|
||||
}
|
||||
|
||||
protected function compareRange($input)
|
||||
{
|
||||
$range = $this->networkRange;
|
||||
$min = sprintf('%032b', ip2long($range['min']));
|
||||
$input = sprintf('%032b', ip2long($input));
|
||||
|
||||
return ($input & $range['mask']) === ($min & $range['mask']);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -75,6 +75,7 @@ class IpTest extends \PHPUnit_Framework_TestCase
|
|||
array('192.168.255.156', '0.0.0.0-255.255.255.255'),
|
||||
array('220.78.173.2', '220.78.168/21'),
|
||||
array('220.78.173.2', '220.78.168.0/21'),
|
||||
array('220.78.173.2', '220.78.168.0/255.255.248.0'),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -101,6 +102,7 @@ class IpTest extends \PHPUnit_Framework_TestCase
|
|||
array('192.10.2.6', '193.168.0.0-193.255.255.255'),
|
||||
array('220.78.176.1', '220.78.168/21'),
|
||||
array('220.78.176.2', '220.78.168.0/21'),
|
||||
array('220.78.176.3', '220.78.168.0/255.255.248.0'),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue