http://www.mediawiki.org/wiki/Special:Code/MediaWiki/76270

Revision: 76270
Author:   aaron
Date:     2010-11-07 21:53:26 +0000 (Sun, 07 Nov 2010)
Log Message:
-----------
Made sanitizeIP() handle CIDR IPs in IPv6 (fixes server-side stuff for bug 
24293)

Modified Paths:
--------------
    trunk/phase3/includes/IP.php

Modified: trunk/phase3/includes/IP.php
===================================================================
--- trunk/phase3/includes/IP.php        2010-11-07 21:05:01 UTC (rev 76269)
+++ trunk/phase3/includes/IP.php        2010-11-07 21:53:26 UTC (rev 76270)
@@ -163,7 +163,7 @@
        /**
         * Given an IPv6 address in octet notation, returns the expanded octet.
         * IPv4 IPs will be trimmed, thats it...
-        * @param $ip octet ipv6 IP address.
+        * @param $ip string IP address in quad or octet form (CIDR or not).
         * @return string
         */
        public static function sanitizeIP( $ip ) {
@@ -171,30 +171,31 @@
                if ( $ip === '' ) {
                        return null;
                }
-               // Trim and return IPv4 addresses
-               if ( self::isIPv4( $ip ) ) {
-                       return $ip;
+               if ( self::isIPv4( $ip ) || !self::isIPv6( $ip ) ) {
+                       return $ip; // nothing else to do for IPv4 addresses or 
invalid ones
                }
-               // Only IPv6 addresses can be expanded
-               if ( !self::isIPv6( $ip ) ) {
-                       return $ip;
-               }
                // Remove any whitespaces, convert to upper case
                $ip = strtoupper( $ip );
                // Expand zero abbreviations
                $abbrevPos = strpos( $ip, '::' );
                if ( $abbrevPos !== false ) {
+                       // We know this is valid IPv6. Find the last index of 
the
+                       // address before any CIDR number (e.g. "a:b:c::/24").
+                       $CIDRStart = strpos( $ip, "/" );
+                       $addressEnd = ( $CIDRStart !== false )
+                               ? $CIDRStart - 1
+                               : strlen( $ip ) - 1;
                        // If the '::' is at the beginning...
                        if( $abbrevPos == 0 ) {
                                $repeat = '0:';
                                $extra = '';
                                $pad = 9; // 7+2 (due to '::')
                        // If the '::' is at the end...
-                       } elseif( $abbrevPos == ( strlen( $ip ) - 2 ) ) {
+                       } elseif( $abbrevPos == ( $addressEnd - 1 ) ) {
                                $repeat = ':0';
                                $extra = '';
                                $pad = 9; // 7+2 (due to '::')
-                       // If the '::' is at the end...
+                       // If the '::' is in the middle...
                        } else {
                                $repeat = ':0';
                                $extra = ':';


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to