From 7a42173a16fcf7c53023aee4610ac42523e86226 Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Thu, 13 Aug 2015 09:04:19 +0200 Subject: [PATCH] Simplify rcube_utils::check_ip() --- program/lib/Roundcube/rcube_utils.php | 34 +-------------------------- tests/Framework/Utils.php | 5 ++++ 2 files changed, 6 insertions(+), 33 deletions(-) diff --git a/program/lib/Roundcube/rcube_utils.php b/program/lib/Roundcube/rcube_utils.php index 162d4cb8d..924b0d63c 100644 --- a/program/lib/Roundcube/rcube_utils.php +++ b/program/lib/Roundcube/rcube_utils.php @@ -143,39 +143,7 @@ class rcube_utils */ public static function check_ip($ip) { - // IPv6, but there's no build-in IPv6 support - if (strpos($ip, ':') !== false && !defined('AF_INET6')) { - $parts = explode(':', $ip); - $count = count($parts); - - if ($count > 8 || $count < 2) { - return false; - } - - foreach ($parts as $idx => $part) { - $length = strlen($part); - if (!$length) { - // there can be only one :: - if ($found_empty && $idx > 2) { - return false; - } - $found_empty = true; - } - // last part can be an IPv4 address - else if ($idx == $count - 1) { - if (!preg_match('/^[0-9a-f]{1,4}$/i', $part)) { - return @inet_pton($part) !== false; - } - } - else if (!preg_match('/^[0-9a-f]{1,4}$/i', $part)) { - return false; - } - } - - return true; - } - - return @inet_pton($ip) !== false; + return filter_var($ip, FILTER_VALIDATE_IP) !== false; } /** diff --git a/tests/Framework/Utils.php b/tests/Framework/Utils.php index d5b853db3..dd0ff72d8 100644 --- a/tests/Framework/Utils.php +++ b/tests/Framework/Utils.php @@ -95,6 +95,8 @@ class Framework_Utils extends PHPUnit_Framework_TestCase array('::1'), array('::1.2.3.4'), array('2001:2d12:c4fe:5afe::1'), + array('2001::'), + array('2001::1'), ); } @@ -110,7 +112,10 @@ class Framework_Utils extends PHPUnit_Framework_TestCase array('1.1.1.1.1'), array('::1.2.3.260'), array('::1.0'), + array(':::1'), + array('2001:::1'), array('2001::c4fe:5afe::1'), + array(':c4fe:5afe:1'), ); }