define( 'RE_IPV6_V4_PREFIX', '0*' . RE_IPV6_GAP . '(?:ffff:)?' );
// An IPv6 block is an IP address and a prefix (d1 to d128)
define( 'RE_IPV6_PREFIX', '(12[0-8]|1[01][0-9]|[1-9]?\d)');
-// An IPv6 IP is made up of 8 octets. However abbreviations like "::" can be used. This is lax!
-define( 'RE_IPV6_ADD', '(:(:' . RE_IPV6_WORD . '){1,7}|' . RE_IPV6_WORD . '(:{1,2}' . RE_IPV6_WORD . '|::$){1,7})' );
+// An IPv6 IP is made up of 8 octets. However abbreviations like "::" can be used.
+// This is lax! Number of octets/double colons validation not done.
+define( 'RE_IPV6_ADD',
+ '(' .
+ ':(:' . RE_IPV6_WORD . '){1,7}' . // IPs that start with ":"
+ '|' .
+ RE_IPV6_WORD . '(:{1,2}' . RE_IPV6_WORD . '|::$){1,7}' . // IPs that don't start with ":"
+ ')'
+);
define( 'RE_IPV6_BLOCK', RE_IPV6_ADD . '\/' . RE_IPV6_PREFIX );
// This might be useful for regexps used elsewhere, matches any IPv6 or IPv6 address or network
define( 'IP_ADDRESS_STRING',
'(?:' .
- RE_IP_ADD . '(\/' . RE_IP_PREFIX . '|)' .
+ RE_IP_ADD . '(\/' . RE_IP_PREFIX . '|)' . // IPv4
'|' .
- RE_IPV6_ADD . '(\/' . RE_IPV6_PREFIX . '|)' .
- ')' );
+ RE_IPV6_ADD . '(\/' . RE_IPV6_PREFIX . '|)' . // IPv6
+ ')'
+);
/**
* A collection of public static functions to play with IP address
public static function isIPv6( $ip ) {
if ( !$ip ) return false;
if( is_array( $ip ) ) {
- throw new MWException( "invalid value passed to " . __METHOD__ );
+ throw new MWException( "invalid value passed to " . __METHOD__ );
}
+ $doubleColons = substr_count($ip, '::');
// IPv6 IPs with two "::" strings are ambiguous and thus invalid
- return preg_match( '/^' . RE_IPV6_ADD . '(\/' . RE_IPV6_PREFIX . '|)$/', $ip) && ( substr_count($ip, '::') < 2);
+ return preg_match( '/^' . RE_IPV6_ADD . '(\/' . RE_IPV6_PREFIX . '|)$/', $ip)
+ && ( $doubleColons == 1 || substr_count($ip,':') == 7 );
}
public static function isIPv4( $ip ) {
// Remove any whitespaces, convert to upper case
$ip = strtoupper( $ip );
// Expand zero abbreviations
- if ( strpos( $ip, '::' ) !== false ) {
- $ip = str_replace('::', str_repeat(':0', 8 - substr_count($ip, ':')) . ':', $ip);
+ $abbrevPos = strpos( $ip, '::' );
+ if ( $abbrevPos !== false ) {
+ // If the '::' is at the beginning...
+ if( $abbrevPos == 0 ) {
+ $repeat = '0:'; $extra = ''; $pad = 9; // 7+2 (due to '::')
+ // If the '::' is at the end...
+ } else if( $abbrevPos == (strlen($ip)-2) ) {
+ $repeat = ':0'; $extra = ''; $pad = 9; // 7+2 (due to '::')
+ // If the '::' is at the end...
+ } else {
+ $repeat = ':0'; $extra = ':'; $pad = 8; // 6+2 (due to '::')
+ }
+ $ip = str_replace('::', str_repeat($repeat, $pad-substr_count($ip,':')).$extra, $ip);
}
- // For IPs that start with "::", correct the final IP so that it starts with '0' and not ':'
- if ( $ip[0] == ':' ) $ip = "0$ip";
// Remove leading zereos from each bloc as needed
$ip = preg_replace( '/(^|:)0+' . RE_IPV6_WORD . '/', '$1$2', $ip );
return $ip;
public static function toOctet( $ip_int ) {
// Convert to padded uppercase hex
$ip_hex = wfBaseConvert($ip_int, 10, 16, 32, false);
- // Seperate into 8 octets
+ // Separate into 8 octets
$ip_oct = substr( $ip_hex, 0, 4 );
for ($n=1; $n < 8; $n++) {
$ip_oct .= ':' . substr($ip_hex, 4*$n, 4);
$ip_oct = preg_replace( '/(^|:)0+' . RE_IPV6_WORD . '/', '$1$2', $ip_oct );
return $ip_oct;
}
+
+ /**
+ * Convert an IPv4 or IPv6 hexadecimal representation back to readable format
+ */
+ public static function formatHex( $hex ) {
+ if ( substr( $hex, 0, 3 ) == 'v6-' ) {
+ return self::hexToOctet( $hex );
+ } else {
+ return self::hexToQuad( $hex );
+ }
+ }
/**
- * Given an unsigned integer, returns an IPv6 address in octet notation
+ * Given a hexadecimal number, returns to an IPv6 address in octet notation
* @param $ip string hex IP
* @return string
*/
- public static function HextoOctet( $ip_hex ) {
+ public static function hextoOctet( $ip_hex ) {
// Convert to padded uppercase hex
$ip_hex = str_pad( strtoupper($ip_hex), 32, '0');
- // Seperate into 8 octets
+ // Separate into 8 octets
$ip_oct = substr( $ip_hex, 0, 4 );
for ($n=1; $n < 8; $n++) {
$ip_oct .= ':' . substr($ip_hex, 4*$n, 4);
$ip_oct = preg_replace( '/(^|:)0+' . RE_IPV6_WORD . '/', '$1$2', $ip_oct );
return $ip_oct;
}
+
+ /**
+ * Converts a hexadecimal number to an IPv4 address in octet notation
+ * @param $ip string Hex IP
+ * @return string
+ */
+ public static function hexToQuad( $ip ) {
+ // Converts a hexadecimal IP to nnn.nnn.nnn.nnn format
+ $s = '';
+ for ( $i = 0; $i < 4; $i++ ) {
+ if ( $s !== '' ) {
+ $s .= '.';
+ }
+ $s .= base_convert( substr( $ip, $i * 2, 2 ), 16, 10 );
+ }
+ return $s;
+ }
/**
* Convert a network specification in IPv6 CIDR notation to an integer network and a number of bits
public static function toHex( $ip ) {
$n = self::toUnsigned( $ip );
if ( $n !== false ) {
- $n = ( self::isIPv6($ip) ) ? "v6-" . wfBaseConvert( $n, 10, 16, 32, false ) : wfBaseConvert( $n, 10, 16, 8, false );
+ $n = self::isIPv6($ip) ? "v6-" . wfBaseConvert( $n, 10, 16, 32, false ) : wfBaseConvert( $n, 10, 16, 8, false );
}
return $n;
}
*/
public static function isInRange( $addr, $range ) {
// Convert to IPv6 if needed
- $unsignedIP = self::toHex( $addr );
+ $hexIP = self::toHex( $addr );
list( $start, $end ) = self::parseRange( $range );
- return (($unsignedIP >= $start) && ($unsignedIP <= $end));
+ return (strcmp($hexIP, $start) >= 0 &&
+ strcmp($hexIP, $end) <= 0);
}
/**
if ( self::isValid( $addr ) )
return $addr;
- // Annoying IPv6 representations like ::ffff:1.2.3.4
+ // Turn mapped addresses from ::ce:ffff:1.2.3.4 to 1.2.3.4
if ( strpos($addr,':') !==false && strpos($addr,'.') !==false ) {
- $addr = str_replace( '.', ':', $addr );
- if( IP::isIPv6( $addr ) )
- return $addr;
+ $addr = substr( $addr, strrpos($addr,':')+1 );
+ if( self::isIPv4($addr) ) return $addr;
}
// IPv6 loopback address