chg: [metafield-types:ipv4] Improved logics

pull/93/head
Sami Mokaddem 2022-02-28 09:40:19 +01:00
parent 4089623eaa
commit 7c153e6164
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
1 changed files with 12 additions and 10 deletions

View File

@ -27,8 +27,7 @@ class IPv4Type extends TextType
*/ */
public function validate(string $value): bool public function validate(string $value): bool
{ {
return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) || return $this->_isValidIP($value) || $this->_isValidIP(explode('/', $value)[0]);
filter_var(explode('/', $value)[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
} }
public function setQueryExpression(QueryExpression $exp, string $searchValue, \App\Model\Entity\MetaTemplateField $metaTemplateField): QueryExpression public function setQueryExpression(QueryExpression $exp, string $searchValue, \App\Model\Entity\MetaTemplateField $metaTemplateField): QueryExpression
@ -62,13 +61,13 @@ class IPv4Type extends TextType
return $exp; return $exp;
} }
private function fetchAllMetatemplateFieldsIdForThisType(\App\Model\Entity\MetaTemplateField $metaTemplateField = null): Query protected function fetchAllMetatemplateFieldsIdForThisType(\App\Model\Entity\MetaTemplateField $metaTemplateField = null): Query
{ {
$conditions =[]; $conditions =[];
if (!is_null($metaTemplateField)) { if (!is_null($metaTemplateField)) {
$conditions['id'] = $metaTemplateField->id; $conditions['id'] = $metaTemplateField->id;
} else { } else {
$conditions['type'] = IPv4Type::TYPE; $conditions['type'] = $this::TYPE;
} }
$query = $this->MetaTemplateFields->find()->select(['id']) $query = $this->MetaTemplateFields->find()->select(['id'])
->distinct() ->distinct()
@ -76,7 +75,7 @@ class IPv4Type extends TextType
return $query; return $query;
} }
private function fetchAllValuesForThisType(array $conditions=[], \App\Model\Entity\MetaTemplateField $metaTemplateField=null): array protected function fetchAllValuesForThisType(array $conditions=[], \App\Model\Entity\MetaTemplateField $metaTemplateField=null): array
{ {
$metaTemplateFieldsIDs = $this->fetchAllMetatemplateFieldsIdForThisType($metaTemplateField); $metaTemplateFieldsIDs = $this->fetchAllMetatemplateFieldsIdForThisType($metaTemplateField);
if (empty($metaTemplateFieldsIDs)) { if (empty($metaTemplateFieldsIDs)) {
@ -100,6 +99,9 @@ class IPv4Type extends TextType
{ {
$range = array(); $range = array();
$cidr = explode('/', $cidr); $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[0] = long2ip((ip2long($cidr[0])) & ((-1 << (32 - (int)$cidr[1]))));
$range[1] = long2ip((ip2long($range[0])) + pow(2, (32 - (int)$cidr[1])) - 1); $range[1] = long2ip((ip2long($range[0])) + pow(2, (32 - (int)$cidr[1])) - 1);
return $range; return $range;
@ -112,7 +114,7 @@ class IPv4Type extends TextType
* @param string $cidr an CIDR block with the form x.x.x.x/yy * @param string $cidr an CIDR block with the form x.x.x.x/yy
* @return boolean * @return boolean
*/ */
private function _IPInCidrBlock(string $ip, string $cidr): bool protected function _IPInCidrBlock(string $ip, string $cidr): bool
{ {
$range = $this->cidrToRange($cidr); $range = $this->cidrToRange($cidr);
return ip2long($range[0]) <= ip2long($ip) && ip2long($ip) <= ip2long($range[1]); return ip2long($range[0]) <= ip2long($ip) && ip2long($ip) <= ip2long($range[1]);
@ -125,24 +127,24 @@ class IPv4Type extends TextType
* @param string $cidr2 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 * @return boolean
*/ */
private function _cidrInCidrBlock(string $cidr1, string $cidr2): bool protected function _cidrInCidrBlock(string $cidr1, string $cidr2): bool
{ {
$range = $this->cidrToRange($cidr1); $range = $this->cidrToRange($cidr1);
return $this->_IPInCidrBlock($range[0], $cidr2) && $this->_IPInCidrBlock($range[1], $cidr2); return $this->_IPInCidrBlock($range[0], $cidr2) && $this->_IPInCidrBlock($range[1], $cidr2);
} }
private function _isValidIP(string $value): bool protected function _isValidIP(string $value): bool
{ {
return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
} }
private function _isValidCidrBlock(string $value): bool protected function _isValidCidrBlock(string $value): bool
{ {
$explodedValue = explode('/', $value); $explodedValue = explode('/', $value);
return $this->_isValidIP($explodedValue[0]); return $this->_isValidIP($explodedValue[0]);
} }
private function _isValidIPOrCidrBlock(string $value): bool protected function _isValidIPOrCidrBlock(string $value): bool
{ {
$explodedValue = explode('/', $value); $explodedValue = explode('/', $value);
if (count($explodedValue) == 1) { if (count($explodedValue) == 1) {