Skip to content

Commit

Permalink
Simplify rcube_utils::check_ip()
Browse files Browse the repository at this point in the history
  • Loading branch information
alecpl committed Aug 13, 2015
1 parent 6b31846 commit 7a42173
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 33 deletions.
34 changes: 1 addition & 33 deletions program/lib/Roundcube/rcube_utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,39 +143,7 @@ public static function check_email($email, $dns_check=true)
*/
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;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/Framework/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ function data_valid_ip()
array('::1'),
array('::1.2.3.4'),
array('2001:2d12:c4fe:5afe::1'),
array('2001::'),
array('2001::1'),
);
}

Expand All @@ -110,7 +112,10 @@ function data_invalid_ip()
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'),
);
}

Expand Down

0 comments on commit 7a42173

Please sign in to comment.