diff options
author | Andreas Fischer <bantu@phpbb.com> | 2010-07-22 00:18:46 +0200 |
---|---|---|
committer | Andreas Fischer <bantu@phpbb.com> | 2011-01-03 00:09:51 +0100 |
commit | 8032549a15609b48d0a0daf08bd541582126a4c4 (patch) | |
tree | 731683a1c1194983bfb46f1f109b0804ffce3078 | |
parent | dc7e3550ab6a4f95f12cbb366f3ad1a0568b8a2d (diff) | |
download | forums-8032549a15609b48d0a0daf08bd541582126a4c4.tar forums-8032549a15609b48d0a0daf08bd541582126a4c4.tar.gz forums-8032549a15609b48d0a0daf08bd541582126a4c4.tar.bz2 forums-8032549a15609b48d0a0daf08bd541582126a4c4.tar.xz forums-8032549a15609b48d0a0daf08bd541582126a4c4.zip |
[ticket/9746] Adding new function phpbb_ip_normalise().
This adds a function that normalises internet protocol addresses.
While there should be no problem at all when handling IPv4 addresses, the many
different representations of the exact same IPv6 address and webservers mapping
IPv4-addresses into the IPv6 space made it necessary to add such a function.
PHPBB3-9746
-rw-r--r-- | phpBB/includes/functions.php | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 285c3938c1..fad352f988 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -3281,6 +3281,59 @@ function short_ipv6($ip, $length) } /** +* Normalises an internet protocol address, +* also checks whether the specified address is valid. +* +* IPv4 addresses are returned 'as is'. +* +* IPv6 addresses are normalised according to +* A Recommendation for IPv6 Address Text Representation +* http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-07 +* +* @param string $address IP address +* +* @return mixed false if specified address is not valid, +* string otherwise +* +* @author bantu +*/ +function phpbb_ip_normalise($address) +{ + $address = trim($address); + + if (empty($address) || !is_string($address)) + { + return false; + } + + if (preg_match(get_preg_expression('ipv4'), $address)) + { + return $address; + } + else if (preg_match(get_preg_expression('ipv6'), $address)) + { + $address = inet_ntop(inet_pton($address)); + + if (strpos($address, '::ffff:') === 0) + { + // IPv4-mapped address + $address_ipv4 = substr($address, 7); + + if (preg_match(get_preg_expression('ipv4'), $address_ipv4)) + { + return $address_ipv4; + } + + return false; + } + + return $address; + } + + return false; +} + +/** * Wrapper for php's checkdnsrr function. * * @param string $host Fully-Qualified Domain Name |