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 ) );
141 if ( self::isIPv4( $ip ) || !self::isIPv6( $ip ) ) {
145 $ip = strtoupper( $ip );
147 $abbrevPos = strpos( $ip,
'::' );
148 if ( $abbrevPos !==
false ) {
151 $CIDRStart = strpos( $ip,
"/" );
152 $addressEnd = ( $CIDRStart !==
false )
156 if ( $abbrevPos == 0 ) {
158 $extra = ( $ip ==
'::' ) ?
'0' :
'';
161 } elseif ( $abbrevPos == ( $addressEnd - 1 ) ) {
171 $ip = str_replace(
'::',
172 str_repeat( $repeat, $pad - substr_count( $ip,
':' ) ) . $extra,
177 $ip = preg_replace(
'/(^|:)0+(' .
RE_IPV6_WORD .
')/',
'$1$2', $ip );
191 if ( self::isIPv6( $ip ) ) {
193 if ( strpos( $ip,
'/' ) !==
false ) {
194 list( $ip, $cidr ) = explode(
'/', $ip, 2 );
200 $longest = $longestPos =
false;
202 '!(?:^|:)0(?::0)+(?:$|:)!', $ip, $m, PREG_OFFSET_CAPTURE, $offset
204 list( $match, $pos ) = $m[0];
205 if ( strlen( $match ) > strlen( $longest ) ) {
209 $offset = ( $pos + strlen( $match ) );
211 if ( $longest !==
false ) {
213 $ip = substr_replace( $ip,
'::', $longestPos, strlen( $longest ) );
216 if ( $cidr !==
'' ) {
217 $ip =
"{$ip}/{$cidr}";
220 $ip = strtolower( $ip );
243 if ( substr( $both, 0, 1 ) ===
'[' ) {
244 if ( preg_match(
'/^\[(' .
RE_IPV6_ADD .
')\](?::(?P<port>\d+))?$/', $both, $m ) ) {
245 if ( isset( $m[
'port'] ) ) {
246 return array( $m[1], intval( $m[
'port'] ) );
248 return array( $m[1],
false );
255 $numColons = substr_count( $both,
':' );
256 if ( $numColons >= 2 ) {
258 if ( preg_match(
'/^' .
RE_IPV6_ADD .
'$/', $both ) ) {
259 return array( $both,
false );
265 if ( $numColons >= 1 ) {
267 $bits = explode(
':', $both );
268 if ( preg_match(
'/^\d+/', $bits[1] ) ) {
269 return array( $bits[0], intval( $bits[1] ) );
277 return array( $both,
false );
292 if ( strpos( $host,
':' ) !==
false ) {
295 if ( $defaultPort !==
false && $port == $defaultPort ) {
298 return "$host:$port";
308 public static function toOctet( $ip_int ) {
318 public static function formatHex( $hex ) {
319 if ( substr( $hex, 0, 3 ) ==
'v6-' ) {
332 public static function hexToOctet( $ip_hex ) {
334 $ip_hex = str_pad( strtoupper( $ip_hex ), 32,
'0', STR_PAD_LEFT );
336 $ip_oct = substr( $ip_hex, 0, 4 );
337 for (
$n = 1;
$n < 8;
$n++ ) {
338 $ip_oct .=
':' . substr( $ip_hex, 4 *
$n, 4 );
341 $ip_oct = preg_replace(
'/(^|:)0+(' .
RE_IPV6_WORD .
')/',
'$1$2', $ip_oct );
352 public static function hexToQuad( $ip_hex ) {
354 $ip_hex = str_pad( strtoupper( $ip_hex ), 8,
'0', STR_PAD_LEFT );
357 for ( $i = 0; $i < 4; $i++ ) {
361 $s .= base_convert( substr( $ip_hex, $i * 2, 2 ), 16, 10 );
374 public static function isPublic( $ip ) {
375 if ( self::isIPv6( $ip ) ) {
385 if ( $ip != long2ip(
$n ) ) {
389 static $privateRanges =
false;
390 if ( !$privateRanges ) {
391 $privateRanges =
array(
392 array(
'10.0.0.0',
'10.255.255.255' ), # RFC 1918 (
private)
393 array(
'172.16.0.0',
'172.31.255.255' ), # RFC 1918 (
private)
394 array(
'192.168.0.0',
'192.168.255.255' ), # RFC 1918 (
private)
396 array(
'127.0.0.0',
'127.255.255.255' ), # loopback
400 foreach ( $privateRanges
as $r ) {
403 if (
$n >= $start &&
$n <= $end ) {
418 private static function isPublic6( $ip ) {
419 static $privateRanges =
false;
420 if ( !$privateRanges ) {
421 $privateRanges =
array(
422 array(
'fc00::',
'fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff' ), # RFC 4193 (local)
423 array(
'0:0:0:0:0:0:0:1',
'0:0:0:0:0:0:0:1' ), # loopback
427 foreach ( $privateRanges
as $r ) {
430 if (
$n >= $start &&
$n <= $end ) {
449 public static function toHex( $ip ) {
450 if ( self::isIPv6( $ip ) ) {
454 if (
$n !==
false ) {
474 foreach ( explode(
':', $ip )
as $v ) {
475 $r_ip .= str_pad( $v, 4, 0, STR_PAD_LEFT );
489 if ( self::isIPv6( $ip ) ) {
493 $ip = preg_replace(
'/(?<=\.)0+(?=[1-9])/',
'', $ip );
497 # On 32-bit platforms (and on Windows), 2^32 does not fit into an int,
498 # so $n becomes a float. We convert it to string instead.
499 if ( is_float(
$n ) ) {
523 public static function parseCIDR( $range ) {
524 if ( self::isIPv6( $range ) ) {
527 $parts = explode(
'/', $range, 2 );
528 if ( count( $parts ) != 2 ) {
529 return array(
false,
false );
531 list( $network, $bits ) = $parts;
532 $network = ip2long( $network );
533 if ( $network !==
false && is_numeric( $bits ) && $bits >= 0 && $bits <= 32 ) {
537 $network &= ~( ( 1 << ( 32 - $bits ) ) - 1 );
539 # Convert to unsigned
540 if ( $network < 0 ) {
541 $network += pow( 2, 32 );
548 return array( $network, $bits );
568 if ( strpos( $range,
'/' ) !==
false ) {
569 if ( self::isIPv6( $range ) ) {
573 if ( $network ===
false ) {
574 $start = $end =
false;
576 $start = sprintf(
'%08X', $network );
577 $end = sprintf(
'%08X', $network + pow( 2, ( 32 - $bits ) ) - 1 );
580 } elseif ( strpos( $range,
'-' ) !==
false ) {
581 list( $start, $end ) = array_map(
'trim', explode(
'-', $range, 2 ) );
582 if ( self::isIPv6( $start ) && self::isIPv6( $end ) ) {
585 if ( self::isIPv4( $start ) && self::isIPv4( $end ) ) {
588 if ( $start > $end ) {
589 $start = $end =
false;
591 $start = sprintf(
'%08X', $start );
592 $end = sprintf(
'%08X', $end );
595 $start = $end =
false;
601 if ( $start ===
false || $end ===
false ) {
602 return array(
false,
false );
604 return array( $start, $end );
616 private static function parseCIDR6( $range ) {
617 # Explode into <expanded IP,range>
619 if ( count( $parts ) != 2 ) {
620 return array(
false,
false );
622 list( $network, $bits ) = $parts;
624 if ( $network !==
false && is_numeric( $bits ) && $bits >= 0 && $bits <= 128 ) {
628 # Native 32 bit functions WONT work here!!!
629 # Convert to a padded binary number
631 # Truncate the last (128-$bits) bits and replace them with zeros
632 $network = str_pad( substr( $network, 0, $bits ), 128, 0, STR_PAD_RIGHT );
633 # Convert back to an integer
641 return array( $network, (
int)$bits );
661 if ( strpos( $range,
'/' ) !==
false ) {
663 if ( $network ===
false ) {
664 $start = $end =
false;
667 # Turn network to binary (again)
669 # Truncate the last (128-$bits) bits and replace them with ones
670 $end = str_pad( substr( $end, 0, $bits ), 128, 1, STR_PAD_RIGHT );
673 # see toHex() comment
674 $start =
"v6-$start";
678 } elseif ( strpos( $range,
'-' ) !==
false ) {
679 list( $start, $end ) = array_map(
'trim', explode(
'-', $range, 2 ) );
682 if ( $start > $end ) {
683 $start = $end =
false;
688 # see toHex() comment
689 $start =
"v6-$start";
695 if ( $start ===
false || $end ===
false ) {
696 return array(
false,
false );
698 return array( $start, $end );
709 public static function isInRange( $addr, $range ) {
713 return ( strcmp( $hexIP, $start ) >= 0 &&
714 strcmp( $hexIP, $end ) <= 0 );
729 $addr = preg_replace(
'/\%.*/',
'', $addr );
731 if ( self::isValid( $addr ) ) {
735 if ( strpos( $addr,
':' ) !==
false && strpos( $addr,
'.' ) !==
false ) {
736 $addr = substr( $addr, strrpos( $addr,
':' ) + 1 );
737 if ( self::isIPv4( $addr ) ) {
743 if ( preg_match(
'/^0*' .
RE_IPV6_GAP .
'1$/', $addr, $m ) ) {
753 return long2ip( ( hexdec( $m[1] ) << 16 ) + hexdec( $m[2] ) );
769 if ( $bits ===
false ) {
773 return "$start/$bits";