29 define(
'RE_IP_BYTE',
'(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[0-9])' );
32 define(
'RE_IP_PREFIX',
'(3[0-2]|[12]?\d)' );
37 define(
'RE_IPV6_WORD',
'([0-9A-Fa-f]{1,4})' );
38 define(
'RE_IPV6_PREFIX',
'(12[0-8]|1[01][0-9]|[1-9]?\d)' );
39 define(
'RE_IPV6_ADD',
53 define(
'RE_IPV6_GAP',
':(?:0+:)*(?::(?:0+:)*)?' );
54 define(
'RE_IPV6_V4_PREFIX',
'0*' .
RE_IPV6_GAP .
'(?:ffff:)?' );
57 define(
'IP_ADDRESS_STRING',
92 public static function isIPv6( $ip ) {
103 public static function isIPv4( $ip ) {
115 public static function isValid( $ip ) {
116 return ( preg_match(
'/^' .
RE_IP_ADD .
'$/', $ip )
130 || preg_match(
'/^' .
RE_IP_BLOCK .
'$/', $ipblock ) );
150 if ( !self::isIPAddress( $ip ) ) {
153 if ( self::isIPv4( $ip ) ) {
155 $ip = preg_replace(
'/(?:^|(?<=\.))0+(?=[1-9]|0\.|0$)/',
'', $ip );
159 $ip = strtoupper( $ip );
161 $abbrevPos = strpos( $ip,
'::' );
162 if ( $abbrevPos !==
false ) {
165 $CIDRStart = strpos( $ip,
"/" );
166 $addressEnd = ( $CIDRStart !==
false )
170 if ( $abbrevPos == 0 ) {
172 $extra = ( $ip ==
'::' ) ?
'0' :
'';
175 } elseif ( $abbrevPos == ( $addressEnd - 1 ) ) {
185 $ip = str_replace(
'::',
186 str_repeat( $repeat, $pad - substr_count( $ip,
':' ) ) . $extra,
191 $ip = preg_replace(
'/(^|:)0+(' .
RE_IPV6_WORD .
')/',
'$1$2', $ip );
205 if ( self::isIPv6( $ip ) ) {
207 if ( strpos( $ip,
'/' ) !==
false ) {
208 list( $ip, $cidr ) = explode(
'/', $ip, 2 );
210 list( $ip, $cidr ) = [ $ip,
'' ];
214 $longest = $longestPos =
false;
216 '!(?:^|:)0(?::0)+(?:$|:)!', $ip, $m, PREG_OFFSET_CAPTURE, $offset
218 list( $match, $pos ) = $m[0];
219 if ( strlen( $match ) > strlen( $longest ) ) {
223 $offset = ( $pos + strlen( $match ) );
225 if ( $longest !==
false ) {
227 $ip = substr_replace( $ip,
'::', $longestPos, strlen( $longest ) );
230 if ( $cidr !==
'' ) {
231 $ip =
"{$ip}/{$cidr}";
234 $ip = strtolower( $ip );
257 if ( substr( $both, 0, 1 ) ===
'[' ) {
258 if ( preg_match(
'/^\[(' .
RE_IPV6_ADD .
')\](?::(?P<port>\d+))?$/', $both, $m ) ) {
259 if ( isset( $m[
'port'] ) ) {
260 return [ $m[1], intval( $m[
'port'] ) ];
262 return [ $m[1],
false ];
269 $numColons = substr_count( $both,
':' );
270 if ( $numColons >= 2 ) {
272 if ( preg_match(
'/^' .
RE_IPV6_ADD .
'$/', $both ) ) {
273 return [ $both,
false ];
279 if ( $numColons >= 1 ) {
281 $bits = explode(
':', $both );
282 if ( preg_match(
'/^\d+/', $bits[1] ) ) {
283 return [ $bits[0], intval( $bits[1] ) ];
291 return [ $both,
false ];
306 if ( strpos( $host,
':' ) !==
false ) {
309 if ( $defaultPort !==
false && $port == $defaultPort ) {
312 return "$host:$port";
322 public static function formatHex( $hex ) {
323 if ( substr( $hex, 0, 3 ) ==
'v6-' ) {
336 public static function hexToOctet( $ip_hex ) {
338 $ip_hex = str_pad( strtoupper( $ip_hex ), 32,
'0', STR_PAD_LEFT );
340 $ip_oct = substr( $ip_hex, 0, 4 );
341 for ( $n = 1; $n < 8; $n++ ) {
342 $ip_oct .=
':' . substr( $ip_hex, 4 * $n, 4 );
345 $ip_oct = preg_replace(
'/(^|:)0+(' .
RE_IPV6_WORD .
')/',
'$1$2', $ip_oct );
356 public static function hexToQuad( $ip_hex ) {
358 $ip_hex = str_pad( strtoupper( $ip_hex ), 8,
'0', STR_PAD_LEFT );
361 for ( $i = 0; $i < 4; $i++ ) {
365 $s .= base_convert( substr( $ip_hex, $i * 2, 2 ), 16, 10 );
378 public static function isPublic( $ip ) {
379 static $privateSet =
null;
380 if ( !$privateSet ) {
381 $privateSet =
new IPSet( [
382 '10.0.0.0/8', # RFC 1918 (
private)
383 '172.16.0.0/12', # RFC 1918 (
private)
384 '192.168.0.0/16', # RFC 1918 (
private)
386 '127.0.0.0/8', # loopback
387 'fc00::/7', # RFC 4193 (local)
388 '0:0:0:0:0:0:0:1', # loopback
389 '169.254.0.0/16', #
link-local
390 'fe80::/10', #
link-local
393 return !$privateSet->match( $ip );
407 public static function toHex( $ip ) {
408 if ( self::isIPv6( $ip ) ) {
410 } elseif ( self::isIPv4( $ip ) ) {
417 # On 32-bit platforms (and on Windows), 2^32 does not fit into an int,
418 # so $n becomes a float. We convert it to string instead.
419 if ( is_float( $n ) ) {
423 if ( $n !==
false ) {
424 # Floating points can handle the conversion; faster than Wikimedia\base_convert()
425 $n = strtoupper( str_pad( base_convert( $n, 10, 16 ), 8,
'0', STR_PAD_LEFT ) );
446 foreach ( explode(
':', $ip )
as $v ) {
447 $r_ip .= str_pad( $v, 4, 0, STR_PAD_LEFT );
460 public static function parseCIDR( $range ) {
461 if ( self::isIPv6( $range ) ) {
464 $parts = explode(
'/', $range, 2 );
465 if (
count( $parts ) != 2 ) {
466 return [
false,
false ];
468 list( $network, $bits ) = $parts;
469 $network = ip2long( $network );
470 if ( $network !==
false && is_numeric( $bits ) && $bits >= 0 && $bits <= 32 ) {
474 $network &= ~( ( 1 << ( 32 - $bits ) ) - 1 );
476 # Convert to unsigned
477 if ( $network < 0 ) {
478 $network += pow( 2, 32 );
485 return [ $network, $bits ];
505 if ( strpos( $range,
'/' ) !==
false ) {
506 if ( self::isIPv6( $range ) ) {
510 if ( $network ===
false ) {
511 $start = $end =
false;
513 $start = sprintf(
'%08X', $network );
514 $end = sprintf(
'%08X', $network + pow( 2, ( 32 - $bits ) ) - 1 );
517 } elseif ( strpos( $range,
'-' ) !==
false ) {
518 list( $start, $end ) = array_map(
'trim', explode(
'-', $range, 2 ) );
519 if ( self::isIPv6( $start ) && self::isIPv6( $end ) ) {
522 if ( self::isIPv4( $start ) && self::isIPv4( $end ) ) {
525 if ( $start > $end ) {
526 $start = $end =
false;
529 $start = $end =
false;
535 if ( $start ===
false || $end ===
false ) {
536 return [
false,
false ];
538 return [ $start, $end ];
550 private static function parseCIDR6( $range ) {
551 # Explode into <expanded IP,range>
553 if (
count( $parts ) != 2 ) {
554 return [
false,
false ];
556 list( $network, $bits ) = $parts;
558 if ( $network !==
false && is_numeric( $bits ) && $bits >= 0 && $bits <= 128 ) {
562 # Native 32 bit functions WONT work here!!!
563 # Convert to a padded binary number
564 $network = Wikimedia\base_convert( $network, 16, 2, 128 );
565 # Truncate the last (128-$bits) bits and replace them with zeros
566 $network = str_pad( substr( $network, 0, $bits ), 128, 0, STR_PAD_RIGHT );
567 # Convert back to an integer
568 $network = Wikimedia\base_convert( $network, 2, 10 );
575 return [ $network, (int)$bits ];
595 if ( strpos( $range,
'/' ) !==
false ) {
597 if ( $network ===
false ) {
598 $start = $end =
false;
600 $start = Wikimedia\base_convert( $network, 10, 16, 32,
false );
601 # Turn network to binary (again)
602 $end = Wikimedia\base_convert( $network, 10, 2, 128 );
603 # Truncate the last (128-$bits) bits and replace them with ones
604 $end = str_pad( substr( $end, 0, $bits ), 128, 1, STR_PAD_RIGHT );
606 $end = Wikimedia\base_convert( $end, 2, 16, 32,
false );
607 # see toHex() comment
608 $start =
"v6-$start";
612 } elseif ( strpos( $range,
'-' ) !==
false ) {
613 list( $start, $end ) = array_map(
'trim', explode(
'-', $range, 2 ) );
616 if ( $start > $end ) {
617 $start = $end =
false;
623 if ( $start ===
false || $end ===
false ) {
624 return [
false,
false ];
626 return [ $start, $end ];
640 public static function isInRange( $addr, $range ) {
644 return ( strcmp( $hexIP, $start ) >= 0 &&
645 strcmp( $hexIP, $end ) <= 0 );
658 public static function isInRanges( $ip, $ranges ) {
659 foreach ( $ranges
as $range ) {
660 if ( self::isInRange( $ip, $range ) ) {
679 $addr = preg_replace(
'/\%.*/',
'', $addr );
681 if ( self::isValid( $addr ) ) {
685 if ( strpos( $addr,
':' ) !==
false && strpos( $addr,
'.' ) !==
false ) {
686 $addr = substr( $addr, strrpos( $addr,
':' ) + 1 );
687 if ( self::isIPv4( $addr ) ) {
693 if ( preg_match(
'/^0*' .
RE_IPV6_GAP .
'1$/', $addr, $m ) ) {
703 return long2ip( ( hexdec( $m[1] ) << 16 ) + hexdec( $m[2] ) );
719 if ( $bits ===
false ) {
723 return "$start/$bits";
732 public static function getSubnet( $ip ) {
738 } elseif ( preg_match(
'/^(\d+\.\d+\.\d+)\.\d+$/', $ip,
$matches ) ) {