chg: [metafield-type:ipv4] Usage of Cdir tool

pull/93/head
Sami Mokaddem 2022-02-28 09:42:49 +01:00
parent 97501642b8
commit 0363a91310
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
1 changed files with 3 additions and 85 deletions

View File

@ -8,6 +8,7 @@ use Cake\ORM\Query;
use MetaFieldsTypes\TextType;
use TypeError;
use App\Lib\Tools\CidrTool;
class IPv4Type extends TextType
{
@ -44,7 +45,8 @@ class IPv4Type extends TextType
}
foreach ($allMetaValues as $fieldID => $ip) {
if (!$this->IPInCidrBlock($searchValue, $ip)) {
$cidrTool = new CidrTool([$ip]);
if ($cidrTool->contains($searchValue) === false) {
if (!$isNegation) {
unset($allMetaValues[$fieldID]);
}
@ -89,92 +91,8 @@ class IPv4Type extends TextType
return $allMetaValues;
}
/**
* Convert a CIDR block to an array containing the minimum and maximum IP address for this block
*
* @param string $cidr an CIDR block with the form x.x.x.x/yy
* @return array
*/
protected function cidrToRange($cidr): array
{
$range = array();
$cidr = explode('/', $cidr);
if (count($cidr) == 1) { // No mask passed
$cidr[1] = '32';
}
$range[0] = long2ip((ip2long($cidr[0])) & ((-1 << (32 - (int)$cidr[1]))));
$range[1] = long2ip((ip2long($range[0])) + pow(2, (32 - (int)$cidr[1])) - 1);
return $range;
}
/**
* Check if the provided IP in contained by the CIDR block
*
* @param string $ip
* @param string $cidr an CIDR block with the form x.x.x.x/yy
* @return boolean
*/
protected function _IPInCidrBlock(string $ip, string $cidr): bool
{
$range = $this->cidrToRange($cidr);
return ip2long($range[0]) <= ip2long($ip) && ip2long($ip) <= ip2long($range[1]);
}
/**
* Check if the provided cidr block in contained by the CIDR block
*
* @param string $cidr1 an CIDR block with the form x.x.x.x/yy
* @param string $cidr2 an CIDR block with the form x.x.x.x/yy
* @return boolean
*/
protected function _cidrInCidrBlock(string $cidr1, string $cidr2): bool
{
$range = $this->cidrToRange($cidr1);
return $this->_IPInCidrBlock($range[0], $cidr2) && $this->_IPInCidrBlock($range[1], $cidr2);
}
protected function _isValidIP(string $value): bool
{
return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
}
protected function _isValidCidrBlock(string $value): bool
{
$explodedValue = explode('/', $value);
return $this->_isValidIP($explodedValue[0]);
}
protected function _isValidIPOrCidrBlock(string $value): bool
{
$explodedValue = explode('/', $value);
if (count($explodedValue) == 1) {
return $this->_isValidIP($value);
} else if (count($explodedValue) == 2) {
return $this->_isValidCidrBlock($value);
}
return false;
}
protected function IPInCidrBlock(string $ip, string $cidr, bool $throw=false): bool
{
if (!$this->_isValidCidrBlock($cidr)) {
if ($throw) {
throw new TypeError("Invalid CDIR block.");
}
return false;
}
if (!$this->_isValidIPOrCidrBlock($ip)) {
if ($throw) {
throw new TypeError("Invalid IP.");
}
return false;
}
$explodedIp = explode('/', $ip);
if (count($explodedIp) == 1) {
return $this->_IPInCidrBlock($ip, $cidr);
} else {
return $this->_cidrInCidrBlock($ip, $cidr);
}
}
}