// An IPv6 block is an IP address and a prefix (d1 to d128)
define( 'RE_IPV6_PREFIX', '(12[0-8]|1[01][0-9]|[1-9]?\d)');
// An IPv6 IP is made up of 8 octets. However abbreviations like "::" can be used. This is lax!
-define( 'RE_IPV6_ADD', RE_IPV6_WORD . '(:{1,2}' . RE_IPV6_WORD . '|::$){1,7}' );
+define( 'RE_IPV6_ADD', '(:(:' . RE_IPV6_WORD . '){1,7}|' . RE_IPV6_WORD . '(:{1,2}' . RE_IPV6_WORD . '|::$){1,7})' );
define( 'RE_IPV6_BLOCK', RE_IPV6_ADD . '\/' . RE_IPV6_PREFIX );
// This might be useful for regexps used elsewhere, matches any IPv6 or IPv6 address or network
define( 'IP_ADDRESS_STRING', RE_IP_ADD . '(\/' . RE_IP_PREFIX . '|)|' . RE_IPV6_ADD . '(\/' . RE_IPV6_PREFIX . '|)');
} elseif ( strpos( $range, '-' ) !== false ) {
# Explicit range
list( $start, $end ) = array_map( 'trim', explode( '-', $range, 2 ) );
- $start = self::toUnsigned6( $start ); $end = self::toUnsigned6( $end );
+ $start = self::toUnsigned6( $start ); $end = self::toUnsigned6( $end );
if ( $start > $end ) {
$start = $end = false;
} else {
}
}
-function wfGetLastIPfromXFF( $xff )
-{
+function wfGetLastIPfromXFF( $xff ) {
if ( $xff ) {
// Avoid annoyingly long xff hacks
- $xff = substr( $xff, 0, 255 );
- // Look for the last IP, assuming they are separated by commas
- $n = strrpos( $xff, ',' );
- if ( strrpos !== false ) {
- $last = substr( $xff, $n + 1 );
- // Make sure it is an IP
- $m = preg_match('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#', $last, $last_ip);
- if ( $m > 0 )
- $xff_ip = $last_ip;
- else
- $xff_ip = null;
+ $xff = substr( $xff, 0, 255 );
+ // Look for the last IP, assuming they are separated by commas or spaces
+ $n = ( strrpos($xff, ',') ) ? strrpos($xff, ',') : strrpos($xff, ' ');
+ if ( strrpos !== false ) {
+ $last = trim( substr( $xff, $n + 1 ) );
+ // Make sure it is an IP
+ $m = preg_match('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#', $last, $last_ip4);
+ $n = preg_match('#:(:[0-9A-Fa-f]{1,4}){1,7}|[0-9A-Fa-f]{1,4}(:{1,2}[0-9A-Fa-f]{1,4}|::$){1,7}#', $last, $last_ip6);
+ if ( $m > 0 )
+ $xff_ip = $last_ip4;
+ else if ( $n > 0 )
+ $xff_ip = $last_ip6;
+ else
+ $xff_ip = null;
} else {
- $xff_ip = null;
+ $xff_ip = null;
}
} else {
- $xff_ip = null;
+ $xff_ip = null;
}
return $xff_ip;
}