/** * Checks an IPv6 adress * * Checks if the given IP is IPv6-compatible * * @param String $ip a valid IPv6-adress * * @return Boolean true if $ip is an IPv6 adress * @access public * @static */ public static function checkIPv6($ip) { $elements = Net_IPv6::separate($ip); $ip = $elements[0]; if ('' != $elements[1] && (!is_numeric($elements[1]) || 0 > $elements || 128 < $elements[1])) { return false; } $ipPart = Net_IPv6::SplitV64($ip); $count = 0; if (!empty($ipPart[0])) { $ipv6 = explode(':', $ipPart[0]); foreach ($ipv6 as $element) { // made a validate precheck if (!preg_match('/[0-9a-fA-F]*/', $element)) { return false; } } for ($i = 0; $i < count($ipv6); $i++) { if (4 < strlen($ipv6[$i])) { return false; } $dec = hexdec($ipv6[$i]); $hex = strtoupper(preg_replace("/^[0]{1,3}(.*[0-9a-fA-F])\$/", "\\1", $ipv6[$i])); if ($ipv6[$i] >= 0 && $dec <= 65535 && $hex == strtoupper(dechex($dec))) { $count++; } } if (8 == $count) { return true; } else { if (6 == $count and !empty($ipPart[1])) { $ipv4 = explode('.', $ipPart[1]); $count = 0; for ($i = 0; $i < count($ipv4); $i++) { if ($ipv4[$i] >= 0 && (int) $ipv4[$i] <= 255 && preg_match("/^\\d{1,3}\$/", $ipv4[$i])) { $count++; } } if (4 == $count) { return true; } } else { return false; } } } else { return false; } }
/** * Returns a possible existing prefix length/netmask specification on an IP addresse. * * @param String $ip the (compressed) IP as Hex representation * * @return String the netmask spec * @since 1.1.0 * @access public * @static */ function getNetmaskSpec($ip) { $elements = Net_IPv6::separate($ip); return $elements[1]; }