r76270 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76269‎ | r76270 | r76271 >
Date:21:53, 7 November 2010
Author:aaron
Status:ok
Tags:
Comment:
Made sanitizeIP() handle CIDR IPs in IPv6 (fixes server-side stuff for bug 24293)
Modified paths:
  • /trunk/phase3/includes/IP.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/IP.php
@@ -163,7 +163,7 @@
164164 /**
165165 * Given an IPv6 address in octet notation, returns the expanded octet.
166166 * IPv4 IPs will be trimmed, thats it...
167 - * @param $ip octet ipv6 IP address.
 167+ * @param $ip string IP address in quad or octet form (CIDR or not).
168168 * @return string
169169 */
170170 public static function sanitizeIP( $ip ) {
@@ -171,30 +171,31 @@
172172 if ( $ip === '' ) {
173173 return null;
174174 }
175 - // Trim and return IPv4 addresses
176 - if ( self::isIPv4( $ip ) ) {
177 - return $ip;
 175+ if ( self::isIPv4( $ip ) || !self::isIPv6( $ip ) ) {
 176+ return $ip; // nothing else to do for IPv4 addresses or invalid ones
178177 }
179 - // Only IPv6 addresses can be expanded
180 - if ( !self::isIPv6( $ip ) ) {
181 - return $ip;
182 - }
183178 // Remove any whitespaces, convert to upper case
184179 $ip = strtoupper( $ip );
185180 // Expand zero abbreviations
186181 $abbrevPos = strpos( $ip, '::' );
187182 if ( $abbrevPos !== false ) {
 183+ // We know this is valid IPv6. Find the last index of the
 184+ // address before any CIDR number (e.g. "a:b:c::/24").
 185+ $CIDRStart = strpos( $ip, "/" );
 186+ $addressEnd = ( $CIDRStart !== false )
 187+ ? $CIDRStart - 1
 188+ : strlen( $ip ) - 1;
188189 // If the '::' is at the beginning...
189190 if( $abbrevPos == 0 ) {
190191 $repeat = '0:';
191192 $extra = '';
192193 $pad = 9; // 7+2 (due to '::')
193194 // If the '::' is at the end...
194 - } elseif( $abbrevPos == ( strlen( $ip ) - 2 ) ) {
 195+ } elseif( $abbrevPos == ( $addressEnd - 1 ) ) {
195196 $repeat = ':0';
196197 $extra = '';
197198 $pad = 9; // 7+2 (due to '::')
198 - // If the '::' is at the end...
 199+ // If the '::' is in the middle...
199200 } else {
200201 $repeat = ':0';
201202 $extra = ':';

Follow-up revisions

RevisionCommit summaryAuthorDate
r76275Similar to r76267 but for JS. Should finish bug 24293.aaron22:46, 7 November 2010

Status & tagging log