*/
public function toUnsigned6( $ip ) {
if ( !$ip ) return null;
- $ip = explode(':', self::expandIP( $ip ) );
+ $ip = explode(':', self::sanitizeIP( $ip ) );
$r_ip = '';
foreach ($ip as $v) {
$r_ip .= wfBaseConvert( $v, 16, 2, 16);
* @param $ip octet ipv6 IP address.
* @return string
*/
- public function expandIP( $ip ) {
+ public function sanitizeIP( $ip ) {
if ( !$ip ) return null;
// Only IPv6 addresses can be expanded
if ( !self::isIPv6( $ip ) ) return $ip;
+ // Convert to upper case
+ $ip = strtoupper( $ip );
// Expand zero abbreviations
if ( substr_count($ip, '::') ) {
$ip = str_replace('::', str_repeat(':0000', 8 - substr_count($ip, ':')) . ':', $ip);
* @return string
*/
public function toOctet( $ip_int ) {
- $ip_int = strval($ip_int);
// Convert integer to binary
$ip_int = wfBaseConvert($ip_int, 10, 2, 128);
// Seperate into 8 octets
- $ip_oct = base_convert( substr( $ip_int, 0, 16 ), 2, 16 );
+ $ip_oct = wfBaseConvert( substr( $ip_int, 0, 16 ), 2, 16, 1, false );
for ($n=1; $n < 8; $n++) {
// Convert to hex, and add ":" marks
- $ip_oct .= ':' . base_convert( substr($ip_int, 16*$n, 16), 2, 16 );
+ $ip_oct .= ':' . wfBaseConvert( substr($ip_int, 16*$n, 16), 2, 16, 1, false );
}
return $ip_oct;
}
* @return array(string, int)
*/
public static function parseCIDR6( $range ) {
- $parts = explode( '/', $range, 2 );
+ # Expand any IPv6 IP
+ $parts = explode( '/', IP::sanitizeIP( $range ), 2 );
if ( count( $parts ) != 2 ) {
return array( false, false );
}
* @return array(string, int)
*/
public static function parseRange6( $range ) {
+ # Expand any IPv6 IP
+ $range = IP::sanitizeIP( $range );
if ( strpos( $range, '/' ) !== false ) {
# CIDR
list( $network, $bits ) = self::parseCIDR6( $range );
if ( $network === false ) {
$start = $end = false;
} else {
- $start = sprintf( '%08X', $network );
- $end = sprintf( '%08X', $network + pow( 2, (128 - $bits) ) - 1 );
+ $start = wfBaseConvert( $network, 10, 16, 1, false );
+ # Turn network to binary (again)
+ $end = wfBaseConvert( $network, 10, 2, 128 );
+ # Truncate the last (128-$bits) bits and replace them with ones
+ $end = str_pad( substr( $end, 0, $bits ), 128, 1, STR_PAD_RIGHT );
+ # Convert to hex
+ $end = wfBaseConvert( $end, 2, 16, 1, false );
}
} elseif ( strpos( $range, '-' ) !== false ) {
# Explicit range
if ( $start > $end ) {
$start = $end = false;
} else {
- $start = sprintf( '%08X', $start );
- $end = sprintf( '%08X', $end );
+ $start = wfBaseConvert( $start, 10, 16, 1, false );
+ $end = wfBaseConvert( $end, 10, 16, 1, false );
}
} else {
# Single IP
}
if ( $start === false || $end === false ) {
return array( false, false );
- } else {
+ } else {
return array( $start, $end );
}
}
// Use IPv6 functions if needed
$n = ( self::isIPv6($ip) ) ? self::toUnsigned6( $ip ) : self::toUnsigned( $ip );
if ( $n !== false ) {
- $n = sprintf( '%08X', $n );
+ $n = wfBaseConvert( $n, 10, 16, 1, false );
}
return $n;
}
<tr>
<td align=\"right\">{$mIpaddress}:</td>
<td align=\"left\">
- " . Xml::input( 'wpBlockAddress', 40, $this->BlockAddress,
+ " . Xml::input( 'wpBlockAddress', 45, $this->BlockAddress,
array(
'tabindex' => '1',
'id' => 'mw-bi-target',
<tr id='wpBlockOther'>
<td align=\"right\">{$mIpbother}:</td>
<td align=\"left\">
- " . Xml::input( 'wpBlockOther', 40, $this->BlockOther,
+ " . Xml::input( 'wpBlockOther', 45, $this->BlockOther,
array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
</td>
</tr>
<tr>
<td align=\"right\">{$mIpbreason}:</td>
<td align=\"left\">
- " . Xml::input( 'wpBlockReason', 40, $this->BlockReason,
+ " . Xml::input( 'wpBlockReason', 45, $this->BlockReason,
array( 'tabindex' => '3', 'id' => 'mw-bi-reason' ) ) . "
</td>
</tr>
$userId = 0;
$this->BlockAddress = trim( $this->BlockAddress );
# Expand valid IPv6 addresses, usernames are left as is
- $this->BlockAddress = IP::expandIP( $this->BlockAddress );
+ $this->BlockAddress = IP::sanitizeIP( $this->BlockAddress );
# isIPv4() and IPv6() are used for final validation
$rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
$rxIP6 = '\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}';