From 63601400e476c6cf43d985f3e7b9864681695ed4 Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Fri, 18 Jan 2013 16:46:04 +0100 Subject: Update to MediaWiki 1.20.2 this update includes: * adjusted Arch Linux skin * updated FluxBBAuthPlugin * patch for https://bugzilla.wikimedia.org/show_bug.cgi?id=44024 --- includes/IP.php | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) (limited to 'includes/IP.php') diff --git a/includes/IP.php b/includes/IP.php index e3f61214..10c707e7 100644 --- a/includes/IP.php +++ b/includes/IP.php @@ -133,7 +133,7 @@ class IP { } /** - * Convert an IP into a nice standard form. + * Convert an IP into a verbose, uppercase, normalized form. * IPv6 addresses in octet notation are expanded to 8 words. * IPv4 addresses are just trimmed. * @@ -185,6 +185,49 @@ class IP { return $ip; } + /** + * Prettify an IP for display to end users. + * This will make it more compact and lower-case. + * + * @param $ip string + * @return string + */ + public static function prettifyIP( $ip ) { + $ip = self::sanitizeIP( $ip ); // normalize (removes '::') + if ( self::isIPv6( $ip ) ) { + // Split IP into an address and a CIDR + if ( strpos( $ip, '/' ) !== false ) { + list( $ip, $cidr ) = explode( '/', $ip, 2 ); + } else { + list( $ip, $cidr ) = array( $ip, '' ); + } + // Get the largest slice of words with multiple zeros + $offset = 0; + $longest = $longestPos = false; + while ( preg_match( + '!(?:^|:)0(?::0)+(?:$|:)!', $ip, $m, PREG_OFFSET_CAPTURE, $offset + ) ) { + list( $match, $pos ) = $m[0]; // full match + if ( strlen( $match ) > strlen( $longest ) ) { + $longest = $match; + $longestPos = $pos; + } + $offset += ( $pos + strlen( $match ) ); // advance + } + if ( $longest !== false ) { + // Replace this portion of the string with the '::' abbreviation + $ip = substr_replace( $ip, '::', $longestPos, strlen( $longest ) ); + } + // Add any CIDR back on + if ( $cidr !== '' ) { + $ip = "{$ip}/{$cidr}"; + } + // Convert to lower case to make it more readable + $ip = strtolower( $ip ); + } + return $ip; + } + /** * Given a host/port string, like one might find in the host part of a URL * per RFC 2732, split the hostname part and the port part and return an @@ -198,7 +241,7 @@ class IP { * * A bare IPv6 address is accepted despite the lack of square brackets. * - * @param $both The string with the host and port + * @param $both string The string with the host and port * @return array */ public static function splitHostAndPort( $both ) { @@ -671,6 +714,7 @@ class IP { * @return String: valid dotted quad IPv4 address or null */ public static function canonicalize( $addr ) { + $addr = preg_replace( '/\%.*/','', $addr ); // remove zone info (bug 35738) if ( self::isValid( $addr ) ) { return $addr; } -- cgit v1.2.3-54-g00ecf