Index: trunk/phase3/includes/IP.php |
— | — | @@ -163,7 +163,7 @@ |
164 | 164 | /** |
165 | 165 | * Given an IPv6 address in octet notation, returns the expanded octet. |
166 | 166 | * 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). |
168 | 168 | * @return string |
169 | 169 | */ |
170 | 170 | public static function sanitizeIP( $ip ) { |
— | — | @@ -171,30 +171,31 @@ |
172 | 172 | if ( $ip === '' ) { |
173 | 173 | return null; |
174 | 174 | } |
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 |
178 | 177 | } |
179 | | - // Only IPv6 addresses can be expanded |
180 | | - if ( !self::isIPv6( $ip ) ) { |
181 | | - return $ip; |
182 | | - } |
183 | 178 | // Remove any whitespaces, convert to upper case |
184 | 179 | $ip = strtoupper( $ip ); |
185 | 180 | // Expand zero abbreviations |
186 | 181 | $abbrevPos = strpos( $ip, '::' ); |
187 | 182 | 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; |
188 | 189 | // If the '::' is at the beginning... |
189 | 190 | if( $abbrevPos == 0 ) { |
190 | 191 | $repeat = '0:'; |
191 | 192 | $extra = ''; |
192 | 193 | $pad = 9; // 7+2 (due to '::') |
193 | 194 | // If the '::' is at the end... |
194 | | - } elseif( $abbrevPos == ( strlen( $ip ) - 2 ) ) { |
| 195 | + } elseif( $abbrevPos == ( $addressEnd - 1 ) ) { |
195 | 196 | $repeat = ':0'; |
196 | 197 | $extra = ''; |
197 | 198 | $pad = 9; // 7+2 (due to '::') |
198 | | - // If the '::' is at the end... |
| 199 | + // If the '::' is in the middle... |
199 | 200 | } else { |
200 | 201 | $repeat = ':0'; |
201 | 202 | $extra = ':'; |