From 8503ca915e6774f95e4a2bf408b58d9c442255e4 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Tue, 23 Jan 2018 09:49:14 +0000 Subject: [PATCH] ipaddresses : add validIPAddressWithCIDR, validIPAddressIPv4WithCIDR and validIPAddressIPv6WithCIDR git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4039 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- ipaddresses.php | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/ipaddresses.php b/ipaddresses.php index 4e5bf59..02c1d93 100644 --- a/ipaddresses.php +++ b/ipaddresses.php @@ -49,6 +49,63 @@ class ipaddresses 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 * or IPv6 * @param integer $cidr The CIDR to test