[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
This commit is contained in:
Andreas Fischer 2010-07-22 00:18:46 +02:00
parent dc7e3550ab
commit 8032549a15

View file

@ -3280,6 +3280,59 @@ function short_ipv6($ip, $length)
return $ip;
}
/**
* 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.
*