From 37d83947aacd30780ca72cc49d529f1b9746ccb4 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Sun, 9 Jun 2019 19:18:37 +0000 Subject: [PATCH] ipaddresses : add folding git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@5338 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- ipaddresses.php | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/ipaddresses.php b/ipaddresses.php index 5210fae..a0bdf75 100644 --- a/ipaddresses.php +++ b/ipaddresses.php @@ -10,6 +10,7 @@ class ipaddresses * @param string $ip The IP Address to validate */ public function validIPAddress ($ip) + // {{{ { if (!is_string ($ip) || $ip === "") throw new \Exception (dgettext ("domframework", "Invalid IP address"), @@ -20,11 +21,13 @@ class ipaddresses $rc = $this->validIPv6Address ($ip); return $rc; } + // }}} /** Return true if the provided IP address is valid and is IPv4 * @param string $ip The IP Address to validate */ public function validIPv4Address ($ip) + // {{{ { if (!is_string ($ip) || $ip === "") throw new \Exception (dgettext ("domframework", "Invalid IPv4 address"), @@ -34,11 +37,13 @@ class ipaddresses return FALSE; return TRUE; } + // }}} /** Return true if the provided IP address is valid and is IPv6 * @param string $ip The IP Address to validate */ public function validIPv6Address ($ip) + // {{{ { if (!is_string ($ip) || $ip === "") throw new \Exception (dgettext ("domframework", "Invalid IPv6 address"), @@ -48,12 +53,14 @@ class ipaddresses return FALSE; 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"), @@ -64,12 +71,14 @@ class ipaddresses $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"), @@ -84,12 +93,14 @@ class ipaddresses 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"), @@ -104,12 +115,15 @@ class ipaddresses 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 * @return boolean The CIDR is valid */ public function validCIDR ($cidr) + // {{{ { if (! is_integer ($cidr) && ! is_integer ($cidr)) throw new \Exception (dgettext ("domframework", "Invalid CIDR provided"), @@ -120,12 +134,14 @@ class ipaddresses return false; return true; } + // }}} /** Return true if the provided CIDR is valid. The CIDR can be valid in IPv4. * @param integer $cidr The CIDR to test * @return boolean The CIDR is valid */ public function validIPv4CIDR ($cidr) + // {{{ { if (! is_integer ($cidr) && ! is_string ($cidr)) throw new \Exception (dgettext ("domframework", "Invalid CIDR provided"), @@ -136,12 +152,14 @@ class ipaddresses return false; return true; } + // }}} /** Return true if the provided CIDR is valid. The CIDR can be valid in IPv6. * @param integer $cidr The CIDR to test * @return boolean The CIDR is valid */ public function validIPv6CIDR ($cidr) + // {{{ { if (! is_integer ($cidr) && ! is_string ($cidr)) throw new \Exception (dgettext ("domframework", "Invalid CIDR provided"), @@ -152,6 +170,7 @@ class ipaddresses return false; return true; } + // }}} /** Return the IPv6 to compressed (or compact) form. * Remove the 0 when they are placed on the begin of the nibble. @@ -161,6 +180,7 @@ class ipaddresses * @param string $ip The IP to compress */ public function compressIP ($ip) + // {{{ { if (strpos ($ip, ":") === false) return $ip; @@ -215,6 +235,7 @@ class ipaddresses $ipHex = implode (":", $ipArr); return $ipHex; } + // }}} /** Return the IPv6 uncompressed (all the fields exists). They are not filled * by zeros @@ -227,6 +248,7 @@ class ipaddresses * return "0:0:0:0:0:0:ffff:7f00:1" */ public function uncompressIPv6 ($ip) + // {{{ { if (! is_string ($ip) || $ip === "" || $this->validIPAddress ($ip) === false) @@ -273,6 +295,7 @@ class ipaddresses } return $ip; } + // }}} /** Get an IPv6 address with the format * x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x @@ -282,6 +305,7 @@ class ipaddresses * @param string $ipv6 The IPv6 to group */ public function groupIPv6 ($ipv6) + // {{{ { if (! is_string ($ipv6) || $ipv6 === "") throw new \Exception (dgettext ("domframework", "Invalid IPv6 address"), @@ -301,6 +325,7 @@ class ipaddresses } return $new; } + // }}} /** Return the IP adddress with filling the fields with the missing zeros. * Valid only on IPv6 (but don't change anything if the provided address is @@ -312,6 +337,7 @@ class ipaddresses * return "0000:0000:0000:0000:0000:0000:ffff:7f00:0001" */ public function completeAddressWithZero ($ip) + // {{{ { if (! is_string ($ip) || $ip === "") throw new \Exception (dgettext ("domframework", "Invalid IP address"), @@ -340,6 +366,7 @@ class ipaddresses } throw new \Exception (dgettext ("domframework", "Invalid IP address"), 500); } + // }}} /** Return the provided CIDR in binary. Length must be in bytes. * Return FALSE if the parameters are invalid @@ -347,6 +374,7 @@ class ipaddresses * @param integer $length The length to use */ public function cidrToBin ($cidr, $length) + // {{{ { if (! is_numeric ($cidr) || $cidr < 0 || $cidr > 128) throw new \Exception (dgettext ("domframework", "Invalid CIDR"), 500); @@ -360,6 +388,7 @@ class ipaddresses } return pack ('H*', $this->str_base_convert ($val, 2, 16)); } + // }}} /** Base conversion with 128 bits support for IPv6 * Based on http://fr2.php.net/manual/en/function.base-convert.php#109660 @@ -370,6 +399,7 @@ class ipaddresses * default) */ public function str_base_convert($str, $frombase=10, $tobase=36) + // {{{ { $str = trim ($str); if (intval ($frombase) != 10) @@ -404,6 +434,7 @@ class ipaddresses return $s; } + // }}} /** Reverse the provided IP address * The IPv6 are returned in format : @@ -411,6 +442,7 @@ class ipaddresses * @param string $ipReverse The IPv6 to reverse */ function reverseIPAddress ($ipReverse) + // {{{ { if (!is_string ($ipReverse) || $ipReverse === "") throw new \Exception (dgettext ("domframework", "Invalid IP address"), @@ -441,6 +473,7 @@ class ipaddresses } throw new \Exception (dgettext ("domframework", "Invalid IP address"), 500); } + // }}} /** This function return the CIDR associated to the provided netmask * Ex. Return 24 for a mask 255.255.255.0 @@ -449,6 +482,7 @@ class ipaddresses * @param string $netmask The mask to convert in CIDR */ public function netmask2cidr ($netmask) + // {{{ { $netmask = ip2long ($netmask); if ($netmask === FALSE) @@ -463,6 +497,7 @@ class ipaddresses } return $i; } + // }}} /** This function return true if the provided address is in the provided * network with the associated cidr @@ -472,6 +507,7 @@ class ipaddresses * @return boolean True if $ip is in $network/$cidr */ public function ipInNetwork ($ip, $network, $cidr) + // {{{ { if ($this->validIPAddress ($ip) === false) throw new \Exception (dgettext ("domframework", "Invalid IP address"), @@ -499,6 +535,7 @@ class ipaddresses return ($this->networkFirstIP ($ip, $cidr) === $this->networkFirstIP ($network, $cidr)); } + // }}} /** Get the first IP of a network. * IPv4 and IPv6 compatible @@ -508,9 +545,11 @@ class ipaddresses * Example : $ip="192.168.1.2", $cidr=24 => return "192.168.1.0" */ public function networkFirstIP ($ip, $cidr) + // {{{ { return $this->networkFirstLastIP ($ip, $cidr, "0"); } + // }}} /** Get the last IP of a network. * IPv4 and IPv6 compatible @@ -520,9 +559,11 @@ class ipaddresses * Example : $ip="192.168.1.2", $cidr=24 => return "192.168.1.255" */ public function networkLastIP ($ip, $cidr) + // {{{ { return $this->networkFirstLastIP ($ip, $cidr, "1"); } + // }}} /** Get the network first IP. * IPv4 and IPv6 compatible @@ -534,6 +575,7 @@ class ipaddresses * Example : $ip="192.168.1.2", $cidr=24 => return "192.168.1.0" */ private function networkFirstLastIP ($ip, $cidr, $map) + // {{{ { if ($this->validIPAddress ($ip) === false) throw new \Exception (dgettext ("domframework", "Invalid IP address"), @@ -611,4 +653,5 @@ class ipaddresses } return $ipBase; } + // }}} }