ipaddresses : add validIPAddressWithCIDR, validIPAddressIPv4WithCIDR and validIPAddressIPv6WithCIDR
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4039 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
@@ -49,6 +49,63 @@ class ipaddresses
|
|||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Return true if the provided IP address is valid (IPv4 or IPv6) and the
|
||||||
|
* provided CIDR is valid too
|
||||||
|
* @param string $ip The IP Address to validate with a CIDR
|
||||||
|
*/
|
||||||
|
public function validIPAddressWithCIDR ($ip)
|
||||||
|
{
|
||||||
|
if (!is_string ($ip) || $ip === "")
|
||||||
|
throw new \Exception (dgettext("domframework", "Invalid IP address"),
|
||||||
|
500);
|
||||||
|
$rc = $this->validIPv4AddressWithCIDR ($ip);
|
||||||
|
if ($rc === true)
|
||||||
|
return true;
|
||||||
|
$rc = $this->validIPv6AddressWithCIDR ($ip);
|
||||||
|
return $rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Return true if the provided IP address is valid and is IPv4 and the
|
||||||
|
* provided CIDR is valid too
|
||||||
|
* @param string $ip The IP Address to validate with a CIDR
|
||||||
|
*/
|
||||||
|
public function validIPv4AddressWithCIDR ($ip)
|
||||||
|
{
|
||||||
|
if (!is_string ($ip) || $ip === "")
|
||||||
|
throw new \Exception (dgettext("domframework", "Invalid IPv4 address"),
|
||||||
|
500);
|
||||||
|
@list ($ip, $cidr) = @explode ("/", $ip);
|
||||||
|
if ($cidr === null)
|
||||||
|
return false;
|
||||||
|
$cidr = intval ($cidr);
|
||||||
|
$rc = filter_var ($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
|
||||||
|
if ($rc === false)
|
||||||
|
return false;
|
||||||
|
if ($this->validIPv4CIDR ($cidr) === false)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Return true if the provided IP address is valid and is IPv6 and the
|
||||||
|
* provided CIDR is valid too
|
||||||
|
* @param string $ip The IP Address to validate with a CIDR
|
||||||
|
*/
|
||||||
|
public function validIPv6AddressWithCIDR ($ip)
|
||||||
|
{
|
||||||
|
if (!is_string ($ip) || $ip === "")
|
||||||
|
throw new \Exception (dgettext("domframework", "Invalid IPv6 address"),
|
||||||
|
500);
|
||||||
|
@list ($ip, $cidr) = @explode ("/", $ip);
|
||||||
|
if ($cidr === null)
|
||||||
|
return false;
|
||||||
|
$cidr = intval ($cidr);
|
||||||
|
$rc = filter_var ($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
|
||||||
|
if ($rc === false)
|
||||||
|
return false;
|
||||||
|
if ($this->validIPv6CIDR ($cidr) === false)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
/** Return true if the provided CIDR is valid. The CIDR can be valid in IPv4
|
/** Return true if the provided CIDR is valid. The CIDR can be valid in IPv4
|
||||||
* or IPv6
|
* or IPv6
|
||||||
* @param integer $cidr The CIDR to test
|
* @param integer $cidr The CIDR to test
|
||||||
|
|||||||
Reference in New Issue
Block a user