diff options
author | Fabian Becker <fabian.becker@uni-tuebingen.de> | 2013-09-04 01:27:51 +0400 |
---|---|---|
committer | Fabian Becker <fabian.becker@uni-tuebingen.de> | 2013-09-04 01:27:51 +0400 |
commit | 3f7a6795f1c61076bbd70720c5950fabf9bcddde (patch) | |
tree | f91d586773381c940d4c41c9fc98db0460efc377 /core | |
parent | 2c378a2f2bb55b81b8de6724d8fa87036fb3b4ac (diff) |
Remove compat code for PHP < 5.3. inet_pton/inet_ntop are available in Windows in PHP >= 5.3 so we don't need the compat code anymore.
refs #3741, #4113
Diffstat (limited to 'core')
-rw-r--r-- | core/IP.php | 176 |
1 files changed, 4 insertions, 172 deletions
diff --git a/core/IP.php b/core/IP.php index 751f6f69c7..025d18eda8 100644 --- a/core/IP.php +++ b/core/IP.php @@ -113,7 +113,7 @@ class IP } // single IP - if (($ip = @self::_inet_pton($ipRangeString)) === false) + if (($ip = @inet_pton($ipRangeString)) === false) return false; $maxbits = Common::strlen($ip) * 8; @@ -136,7 +136,7 @@ class IP public static function P2N($ipString) { // use @inet_pton() because it throws an exception and E_WARNING on invalid input - $ip = @self::_inet_pton($ipString); + $ip = @inet_pton($ipString); return $ip === false ? "\x00\x00\x00\x00" : $ip; } @@ -151,7 +151,7 @@ class IP public static function N2P($ip) { // use @inet_ntop() because it throws an exception and E_WARNING on invalid input - $ipStr = @self::_inet_ntop($ip); + $ipStr = @inet_ntop($ip); return $ipStr === false ? '0.0.0.0' : $ipStr; } @@ -272,7 +272,7 @@ class IP $bits = substr($ipRange, $pos + 1); $range = substr($ipRange, 0, $pos); - $high = $low = @self::_inet_pton($range); + $high = $low = @inet_pton($range); if ($low === false) { return false; } @@ -424,172 +424,4 @@ class IP $host = strtolower(@gethostbyaddr($ipStr)); return $host === '' ? $ipStr : $host; } - - static private function _inet_ntop($in_addr) - { - if (Common::isWindows() || !function_exists('inet_ntop')) { - return self::php_compat_inet_ntop($in_addr); - } else { - return inet_ntop($in_addr); - } - } - - static private function _inet_pton($address) - { - if (Common::isWindows() || !function_exists('inet_pton')) { - return self::php_compat_inet_pton($address); - } else { - return inet_pton($address); - } - } - - /** - * Converts a packed internet address to a human readable representation - * - * @link http://php.net/inet_ntop - * - * @param string $in_addr 32-bit IPv4 or 128-bit IPv6 address - * @return string|bool string representation of address or false on failure - */ - static public function php_compat_inet_ntop($in_addr) - { - $r = bin2hex($in_addr); - - switch (Common::strlen($in_addr)) { - case 4: - // IPv4 address - $prefix = ''; - break; - - case 16: - // IPv4-mapped address - if (substr_compare($r, '00000000000000000000ffff', 0, 24) === 0) { - $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); - $r = implode(':', $r); - - // compress leading zeros - $r = preg_replace( - '/(^|:)0{1,3}/', - '$1', - $r - ); - - // compress longest (and leftmost) consecutive groups of zeros - if (preg_match_all('/(?:^|:)(0(:|$))+/D', $r, $matches)) { - $longestMatch = 0; - foreach ($matches[0] as $aMatch) { - if (strlen($aMatch) > strlen($longestMatch)) { - $longestMatch = $aMatch; - } - } - $r = substr_replace($r, '::', strpos($r, $longestMatch), strlen($longestMatch)); - } - - return $r; - - default: - return false; - } - - $r = str_split($r, 2); - $r = array_map('hexdec', $r); - $r = implode('.', $r); - return $prefix . $r; - } - - /** - * Converts a human readable IP address to its packed in_addr representation - * - * @link http://php.net/inet_pton - * - * @param string $address a human readable IPv4 or IPv6 address - * @return string in_addr representation or false on failure - */ - static public function php_compat_inet_pton($address) - { - // IPv4 (or IPv4-compat, or IPv4-mapped) - if (preg_match('/(^|:)([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/iD', $address, $matches)) { - for ($i = count($matches); $i-- > 2;) { - if ($matches[$i] > 255 || - ($matches[$i][0] == '0' && strlen($matches[$i]) > 1) - ) { - return false; - } - } - - if (empty($matches[1])) { - $r = ip2long($address); - if ($r === false) { - return false; - } - - return pack('N', $r); - } - - $suffix = sprintf("%02x%02x:%02x%02x", $matches[2], $matches[3], $matches[4], $matches[5]); - $address = substr_replace($address, $matches[1] . $suffix, strrpos($address, $matches[0])); - } - - // IPv6 - if (strpos($address, ':') === false || - strspn($address, '01234567890abcdefABCDEF:') !== strlen($address) - ) { - return false; - } - - if (substr($address, 0, 2) == '::') { - $address = '0' . $address; - } - - if (substr($address, -2) == '::') { - $address .= '0'; - } - - $r = explode(':', $address); - $count = count($r); - - // grouped zeros - if (strpos($address, '::') !== false - && $count < 8 - ) { - $zeroGroup = array_search('', $r, 1); - - // we're replacing this cell, so we splice (8 - $count + 1) cells containing '0' - array_splice($r, $zeroGroup, 1, array_fill(0, 9 - $count, '0')); - } - - // guard against excessive ':' or '::' - if ($count > 8 || - array_search('', $r, 1) !== false - ) { - return false; - } - - // leading zeros - foreach ($r as $v) { - if (strlen(ltrim($v, '0')) > 4) { - return false; - } - } - - $r = array_map('hexdec', $r); - array_unshift($r, 'n*'); - $r = call_user_func_array('pack', $r); - - return $r; - } } |