27 define(
'RE_IP_BYTE',
'(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[0-9])' );
30 define(
'RE_IP_PREFIX',
'(3[0-2]|[12]?\d)' );
35 define(
'RE_IPV6_WORD',
'([0-9A-Fa-f]{1,4})' );
36 define(
'RE_IPV6_PREFIX',
'(12[0-8]|1[01][0-9]|[1-9]?\d)' );
37 define(
'RE_IPV6_ADD',
51 define(
'RE_IPV6_GAP',
':(?:0+:)*(?::(?:0+:)*)?' );
52 define(
'RE_IPV6_V4_PREFIX',
'0*' .
RE_IPV6_GAP .
'(?:ffff:)?' );
55 define(
'IP_ADDRESS_STRING',
87 public static function isIPv6( $ip ) {
98 public static function isIPv4( $ip ) {
110 public static function isValid( $ip ) {
111 return ( preg_match(
'/^' .
RE_IP_ADD .
'$/', $ip )
125 || preg_match(
'/^' .
RE_IP_BLOCK .
'$/', $ipblock ) );
145 if ( !self::isIPAddress($ip) ) {
148 if ( self::isIPv4( $ip ) ) {
150 $ip = preg_replace(
'/(?:^|(?<=\.))0+(?=[1-9]|0\.|0$)/',
'', $ip );
154 $ip = strtoupper( $ip );
156 $abbrevPos = strpos( $ip,
'::' );
157 if ( $abbrevPos !==
false ) {
160 $CIDRStart = strpos( $ip,
"/" );
161 $addressEnd = ( $CIDRStart !==
false )
165 if ( $abbrevPos == 0 ) {
167 $extra = ( $ip ==
'::' ) ?
'0' :
'';
170 } elseif ( $abbrevPos == ( $addressEnd - 1 ) ) {
180 $ip = str_replace(
'::',
181 str_repeat( $repeat, $pad - substr_count( $ip,
':' ) ) . $extra,
186 $ip = preg_replace(
'/(^|:)0+(' .
RE_IPV6_WORD .
')/',
'$1$2', $ip );
200 if ( self::isIPv6( $ip ) ) {
202 if ( strpos( $ip,
'/' ) !==
false ) {
203 list( $ip, $cidr ) = explode(
'/', $ip, 2 );
209 $longest = $longestPos =
false;
211 '!(?:^|:)0(?::0)+(?:$|:)!', $ip, $m, PREG_OFFSET_CAPTURE, $offset
213 list( $match, $pos ) = $m[0];
214 if ( strlen( $match ) > strlen( $longest ) ) {
218 $offset = ( $pos + strlen( $match ) );
220 if ( $longest !==
false ) {
222 $ip = substr_replace( $ip,
'::', $longestPos, strlen( $longest ) );
225 if ( $cidr !==
'' ) {
226 $ip =
"{$ip}/{$cidr}";
229 $ip = strtolower( $ip );
252 if ( substr( $both, 0, 1 ) ===
'[' ) {
253 if ( preg_match(
'/^\[(' .
RE_IPV6_ADD .
')\](?::(?P<port>\d+))?$/', $both, $m ) ) {
254 if ( isset( $m[
'port'] ) ) {
255 return array( $m[1], intval( $m[
'port'] ) );
257 return array( $m[1],
false );
264 $numColons = substr_count( $both,
':' );
265 if ( $numColons >= 2 ) {
267 if ( preg_match(
'/^' .
RE_IPV6_ADD .
'$/', $both ) ) {
268 return array( $both,
false );
274 if ( $numColons >= 1 ) {
276 $bits = explode(
':', $both );
277 if ( preg_match(
'/^\d+/', $bits[1] ) ) {
278 return array( $bits[0], intval( $bits[1] ) );
286 return array( $both,
false );
301 if ( strpos( $host,
':' ) !==
false ) {
304 if ( $defaultPort !==
false && $port == $defaultPort ) {
307 return "$host:$port";
317 public static function toOctet( $ip_int ) {
327 public static function formatHex( $hex ) {
328 if ( substr( $hex, 0, 3 ) ==
'v6-' ) {
341 public static function hexToOctet( $ip_hex ) {
343 $ip_hex = str_pad( strtoupper( $ip_hex ), 32,
'0', STR_PAD_LEFT );
345 $ip_oct = substr( $ip_hex, 0, 4 );
346 for (
$n = 1;
$n < 8;
$n++ ) {
347 $ip_oct .=
':' . substr( $ip_hex, 4 *
$n, 4 );
350 $ip_oct = preg_replace(
'/(^|:)0+(' .
RE_IPV6_WORD .
')/',
'$1$2', $ip_oct );
361 public static function hexToQuad( $ip_hex ) {
363 $ip_hex = str_pad( strtoupper( $ip_hex ), 8,
'0', STR_PAD_LEFT );
366 for ( $i = 0; $i < 4; $i++ ) {
370 $s .= base_convert( substr( $ip_hex, $i * 2, 2 ), 16, 10 );
383 public static function isPublic( $ip ) {
384 if ( self::isIPv6( $ip ) ) {
394 if ( $ip != long2ip(
$n ) ) {
398 static $privateRanges =
false;
399 if ( !$privateRanges ) {
400 $privateRanges =
array(
401 array(
'10.0.0.0',
'10.255.255.255' ), # RFC 1918 (
private)
402 array(
'172.16.0.0',
'172.31.255.255' ), # RFC 1918 (
private)
403 array(
'192.168.0.0',
'192.168.255.255' ), # RFC 1918 (
private)
405 array(
'127.0.0.0',
'127.255.255.255' ), # loopback
409 foreach ( $privateRanges
as $r ) {
412 if (
$n >= $start &&
$n <= $end ) {
427 private static function isPublic6( $ip ) {
428 static $privateRanges =
false;
429 if ( !$privateRanges ) {
430 $privateRanges =
array(
431 array(
'fc00::',
'fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff' ), # RFC 4193 (local)
432 array(
'0:0:0:0:0:0:0:1',
'0:0:0:0:0:0:0:1' ), # loopback
436 foreach ( $privateRanges
as $r ) {
439 if (
$n >= $start &&
$n <= $end ) {
458 public static function toHex( $ip ) {
459 if ( self::isIPv6( $ip ) ) {
463 if (
$n !==
false ) {
483 foreach ( explode(
':', $ip )
as $v ) {
484 $r_ip .= str_pad( $v, 4, 0, STR_PAD_LEFT );
498 if ( self::isIPv6( $ip ) ) {
507 # On 32-bit platforms (and on Windows), 2^32 does not fit into an int,
508 # so $n becomes a float. We convert it to string instead.
509 if ( is_float(
$n ) ) {
533 public static function parseCIDR( $range ) {
534 if ( self::isIPv6( $range ) ) {
537 $parts = explode(
'/', $range, 2 );
538 if ( count( $parts ) != 2 ) {
539 return array(
false,
false );
541 list( $network, $bits ) = $parts;
542 $network = ip2long( $network );
543 if ( $network !==
false && is_numeric( $bits ) && $bits >= 0 && $bits <= 32 ) {
547 $network &= ~( ( 1 << ( 32 - $bits ) ) - 1 );
549 # Convert to unsigned
550 if ( $network < 0 ) {
551 $network += pow( 2, 32 );
558 return array( $network, $bits );
578 if ( strpos( $range,
'/' ) !==
false ) {
579 if ( self::isIPv6( $range ) ) {
583 if ( $network ===
false ) {
584 $start = $end =
false;
586 $start = sprintf(
'%08X', $network );
587 $end = sprintf(
'%08X', $network + pow( 2, ( 32 - $bits ) ) - 1 );
590 } elseif ( strpos( $range,
'-' ) !==
false ) {
591 list( $start, $end ) = array_map(
'trim', explode(
'-', $range, 2 ) );
592 if ( self::isIPv6( $start ) && self::isIPv6( $end ) ) {
595 if ( self::isIPv4( $start ) && self::isIPv4( $end ) ) {
598 if ( $start > $end ) {
599 $start = $end =
false;
601 $start = sprintf(
'%08X', $start );
602 $end = sprintf(
'%08X', $end );
605 $start = $end =
false;
611 if ( $start ===
false || $end ===
false ) {
612 return array(
false,
false );
614 return array( $start, $end );
626 private static function parseCIDR6( $range ) {
627 # Explode into <expanded IP,range>
629 if ( count( $parts ) != 2 ) {
630 return array(
false,
false );
632 list( $network, $bits ) = $parts;
634 if ( $network !==
false && is_numeric( $bits ) && $bits >= 0 && $bits <= 128 ) {
638 # Native 32 bit functions WONT work here!!!
639 # Convert to a padded binary number
641 # Truncate the last (128-$bits) bits and replace them with zeros
642 $network = str_pad( substr( $network, 0, $bits ), 128, 0, STR_PAD_RIGHT );
643 # Convert back to an integer
651 return array( $network, (
int)$bits );
671 if ( strpos( $range,
'/' ) !==
false ) {
673 if ( $network ===
false ) {
674 $start = $end =
false;
677 # Turn network to binary (again)
679 # Truncate the last (128-$bits) bits and replace them with ones
680 $end = str_pad( substr( $end, 0, $bits ), 128, 1, STR_PAD_RIGHT );
683 # see toHex() comment
684 $start =
"v6-$start";
688 } elseif ( strpos( $range,
'-' ) !==
false ) {
689 list( $start, $end ) = array_map(
'trim', explode(
'-', $range, 2 ) );
692 if ( $start > $end ) {
693 $start = $end =
false;
698 # see toHex() comment
699 $start =
"v6-$start";
705 if ( $start ===
false || $end ===
false ) {
706 return array(
false,
false );
708 return array( $start, $end );
719 public static function isInRange( $addr, $range ) {
723 return ( strcmp( $hexIP, $start ) >= 0 &&
724 strcmp( $hexIP, $end ) <= 0 );
739 $addr = preg_replace(
'/\%.*/',
'', $addr );
741 if ( self::isValid( $addr ) ) {
745 if ( strpos( $addr,
':' ) !==
false && strpos( $addr,
'.' ) !==
false ) {
746 $addr = substr( $addr, strrpos( $addr,
':' ) + 1 );
747 if ( self::isIPv4( $addr ) ) {
753 if ( preg_match(
'/^0*' .
RE_IPV6_GAP .
'1$/', $addr, $m ) ) {
763 return long2ip( ( hexdec( $m[1] ) << 16 ) + hexdec( $m[2] ) );
779 if ( $bits ===
false ) {
783 return "$start/$bits";