[Phphtmllib-devel] SF.net SVN: phphtmllib:[3163] trunk/phphtmllib/src/validation/ NetworkValidator.
Status: Beta
Brought to you by:
hemna
From: <he...@us...> - 2008-10-06 23:00:24
|
Revision: 3163 http://phphtmllib.svn.sourceforge.net/phphtmllib/?rev=3163&view=rev Author: hemna Date: 2008-10-06 23:00:19 +0000 (Mon, 06 Oct 2008) Log Message: ----------- added is_ip_v6 Modified Paths: -------------- trunk/phphtmllib/src/validation/NetworkValidator.inc Modified: trunk/phphtmllib/src/validation/NetworkValidator.inc =================================================================== --- trunk/phphtmllib/src/validation/NetworkValidator.inc 2008-10-06 22:14:22 UTC (rev 3162) +++ trunk/phphtmllib/src/validation/NetworkValidator.inc 2008-10-06 23:00:19 UTC (rev 3163) @@ -230,9 +230,58 @@ self::error(phphtmllibException::STR_INVALID_IP); } } - - } + + /** + * This tries to validate an ip address as an IPv6 + * specific ip. + * + * @param string = the value to validate + * @return void + */ + public static function is_ip_v6($ip) { + //try and use filter_var if we have it + //requires php 5.2.0 or > + if (function_exixsts("filter_var")) { + if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === FALSE) { + self::error(phphtmllibException::STR_INVALID_IP); + } else { + return true; + } + } else { + if (substr_count($value, ":") < 2) { + // has to contain ":" at least twice like in ::1 or 1234::abcd + self::error(phphtmllibException::STR_INVALID_IP); + } + if (substr_count($value, "::") > 1) { + //only 1 double colon allowed. + self::error(phphtmllibException::STR_INVALID_IP); + } + + $groups = explode(':', $value); + $num_groups = count($groups); + if (($num_groups > 8) || ($num_groups < 3)) { + //3-8 groups of 0-4 digits (1 group has to be at leas 1 digit) + self::error(phphtmllibException::STR_INVALID_IP); + } + + $empty_groups = 0; + foreach ($groups as $group) { + $group = trim($group); + if (!empty($group) && !(is_numeric($group) && ($group == 0))) { + if (!preg_match('#([a-fA-F0-9]{0,4})#', $group)) { + self::error(phphtmllibException::STR_INVALID_IP); + } + } else { + ++$empty_groups; + } + } + + if ($empty_groups >= $num_groups) { + self::error(phphtmllibException::STR_INVALID_IP); + } + } + } /** * This validates a string as a portion This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |