From 47aa28c663ec92bb908c58b2578c4d184bb0c146 Mon Sep 17 00:00:00 2001 From: Jared Hancock <gravydish@gmail.com> Date: Mon, 2 Jul 2012 09:26:06 -0500 Subject: [PATCH] Support validation of IPv4 and IPv6 addresses --- include/class.validator.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/include/class.validator.php b/include/class.validator.php index d41bf027f..2cec3fa3f 100644 --- a/include/class.validator.php +++ b/include/class.validator.php @@ -172,10 +172,15 @@ class Validator { return false; $ip=trim($ip); - if(preg_match("/^[0-9]{1,3}(.[0-9]{1,3}){3}$/",$ip)) { - foreach(explode(".", $ip) as $block) - if($block<0 || $block>255 ) - return false; + # Thanks to http://stackoverflow.com/a/1934546 + if (function_exists('inet_pton')) { # PHP 5.1.0 + # Let the built-in library parse the IP address + return @inet_pton($ip) !== false; + } else if (preg_match( + '/^(?>(?>([a-f0-9]{1,4})(?>:(?1)){7}|(?!(?:.*[a-f0-9](?>:|$)){7,})' + .'((?1)(?>:(?1)){0,5})?::(?2)?)|(?>(?>(?1)(?>:(?1)){5}:|(?!(?:.*[a-f0-9]:){5,})' + .'(?3)?::(?>((?1)(?>:(?1)){0,3}):)?)?(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])' + .'(?>\.(?4)){3}))$/iD', $ip)) { return true; } return false; -- GitLab