diff options
author | robocoder <anthon.pang@gmail.com> | 2011-04-27 05:30:50 +0400 |
---|---|---|
committer | robocoder <anthon.pang@gmail.com> | 2011-04-27 05:30:50 +0400 |
commit | 49ca6242caee17e55b7d928a3375bc3c80e53df7 (patch) | |
tree | 21b7ad9e275656f9bffc7ff6953aa7b4b7d21cf5 /core/IP.php | |
parent | 84e59bbbcbe2d9d8fdc71a4c5762a25c32cfbecf (diff) |
#2359 - clean up php_compat_inet_ntop
git-svn-id: http://dev.piwik.org/svn/trunk@4569 59fd770c-687e-43c8-a1e3-f5a4ff64c105
Diffstat (limited to 'core/IP.php')
-rw-r--r-- | core/IP.php | 41 |
1 files changed, 27 insertions, 14 deletions
diff --git a/core/IP.php b/core/IP.php index 051b3e6f8b..86fd282152 100644 --- a/core/IP.php +++ b/core/IP.php @@ -416,23 +416,30 @@ class Piwik_IP */ function php_compat_inet_ntop($in_addr) { + $r = bin2hex($in_addr); + switch (strlen($in_addr)) { case 4: - $r = str_split(bin2hex($in_addr), 2); - $r = array_map('hexdec', $r); - $r = implode('.', $r); - return $r; + // IPv4 address + $prefix = ''; + break; case 16: - $r = bin2hex($in_addr); - // IPv4-mapped address if(substr_compare($r, '00000000000000000000ffff', 0, 24) === 0) { - $r = str_split(substr($r, 24), 2); - $r = array_map('hexdec', $r); - $r = implode('.', $r); - return '::ffff:' . $r; + $prefix = '::ffff:'; + $r = substr($r, 24); + break; + } + + // IPv4-compat address + if(substr_compare($r, '000000000000000000000000', 0, 24) === 0 && + substr_compare($r, '0000', 24, 4) !== 0) + { + $prefix = '::'; + $r = substr($r, 24); + break; } $r = str_split($r, 4); @@ -445,7 +452,7 @@ function php_compat_inet_ntop($in_addr) $r ); - // compress groups of zeros + // compress longest (and leftmost) consecutive groups of zeros if(preg_match_all('/(?:^|:)(0(:|$))+/', $r, $matches)) { $longestMatch = 0; @@ -460,9 +467,15 @@ function php_compat_inet_ntop($in_addr) } return $r; + + default: + return false; } - return false; + $r = str_split($r, 2); + $r = array_map('hexdec', $r); + $r = implode('.', $r); + return $prefix . $r; } /** @@ -497,10 +510,10 @@ function php_compat_inet_pton($address) { return false; } - return pack('N', $r); + $suffix = pack('N', $r); + return $suffix; } - // IPv6 $colonCount = substr_count($address, ':'); if($colonCount < 2 || $colonCount > 7 || |