}
// Remove leading zeros from each bloc as needed
$ip = preg_replace( '/(^|:)0+(' . RE_IPV6_WORD . ')/', '$1$2', $ip );
+
return $ip;
}
// Convert to lower case to make it more readable
$ip = strtolower( $ip );
}
+
return $ip;
}
return false;
}
}
+
// Plain hostname
return array( $both, false );
}
}
// NO leading zeroes
$ip_oct = preg_replace( '/(^|:)0+(' . RE_IPV6_WORD . ')/', '$1$2', $ip_oct );
+
return $ip_oct;
}
}
$s .= base_convert( substr( $ip_hex, $i * 2, 2 ), 16, 10 );
}
+
return $s;
}
/**
* Determine if an IP address really is an IP address, and if it is public,
* i.e. not RFC 1918 or similar
- * Comes from ProxyTools.php
*
* @param $ip String
* @return Boolean
return false;
}
}
+
return true;
}
return false;
}
}
+
return true;
}
$n = wfBaseConvert( $n, 10, 16, 8, false );
}
}
+
return $n;
}
foreach ( explode( ':', $ip ) as $v ) {
$r_ip .= str_pad( $v, 4, 0, STR_PAD_LEFT );
}
+
return $r_ip;
}
/**
* Given an IP address in dotted-quad/octet notation, returns an unsigned integer.
* Like ip2long() except that it actually works and has a consistent error return value.
- * Comes from ProxyTools.php
*
* @param string $ip quad dotted IP address.
* @return Mixed: string/int/false
if ( self::isIPv6( $ip ) ) {
$n = self::toUnsigned6( $ip );
} else {
+ // Bug 60035: an IP with leading 0's fails in ip2long sometimes (e.g. *.08)
+ $ip = preg_replace( '/(?<=\.)0+(?=[1-9])/', '', $ip );
$n = ip2long( $ip );
if ( $n < 0 ) {
$n += pow( 2, 32 );
}
}
}
+
return $n;
}
$network = false;
$bits = false;
}
+
return array( $network, $bits );
}
* 1.2.3.4 - 1.2.3.5 Explicit range
* 1.2.3.4 Single IP
*
- * 2001:0db8:85a3::7344/96 CIDR
+ * 2001:0db8:85a3::7344/96 CIDR
* 2001:0db8:85a3::7344 - 2001:0db8:85a3::7344 Explicit range
- * 2001:0db8:85a3::7344 Single IP
+ * 2001:0db8:85a3::7344 Single IP
* @param string $range IP range
* @return array(string, string)
*/
$network = false;
$bits = false;
}
+
return array( $network, (int)$bits );
}
* start and end of the range in hexadecimal. For IPv6.
*
* Formats are:
- * 2001:0db8:85a3::7344/96 CIDR
+ * 2001:0db8:85a3::7344/96 CIDR
* 2001:0db8:85a3::7344 - 2001:0db8:85a3::7344 Explicit range
- * 2001:0db8:85a3::7344/96 Single IP
+ * 2001:0db8:85a3::7344/96 Single IP
*
* @param $range
*
$start = "v6-$start";
$end = "v6-$end";
}
- // Explicit range notation...
+ // Explicit range notation...
} elseif ( strpos( $range, '-' ) !== false ) {
list( $start, $end ) = array_map( 'trim', explode( '-', $range, 2 ) );
$start = self::toUnsigned6( $start );
public static function isInRange( $addr, $range ) {
$hexIP = self::toHex( $addr );
list( $start, $end ) = self::parseRange( $range );
+
return ( strcmp( $hexIP, $start ) >= 0 &&
strcmp( $hexIP, $end ) <= 0 );
}
return $m[1];
}
if ( preg_match( '/^' . RE_IPV6_V4_PREFIX . RE_IPV6_WORD .
- ':' . RE_IPV6_WORD . '$/i', $addr, $m ) )
- {
+ ':' . RE_IPV6_WORD . '$/i', $addr, $m )
+ ) {
return long2ip( ( hexdec( $m[1] ) << 16 ) + hexdec( $m[2] ) );
}
if ( $bits === false ) {
return $start; // wasn't actually a range
}
+
return "$start/$bits";
}
}