123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534 |
- <?php
- define( 'RE_IP_BYTE', '(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[0-9])');
- define( 'RE_IP_ADD' , RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE );
- define( 'RE_IP_PREFIX', '(3[0-2]|[12]?\d)');
- define( 'RE_IP_BLOCK', RE_IP_ADD . '\/' . RE_IP_PREFIX);
- define( 'RE_IPV6_WORD', '([0-9A-Fa-f]{1,4})' );
- define( 'RE_IPV6_GAP', ':(?:0+:)*(?::(?:0+:)*)?' );
- define( 'RE_IPV6_V4_PREFIX', '0*' . RE_IPV6_GAP . '(?:ffff:)?' );
- define( 'RE_IPV6_PREFIX', '(12[0-8]|1[01][0-9]|[1-9]?\d)');
- 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 );
- define( 'IP_ADDRESS_STRING',
- '(?:' .
- RE_IP_ADD . '(\/' . RE_IP_PREFIX . '|)' .
- '|' .
- RE_IPV6_ADD . '(\/' . RE_IPV6_PREFIX . '|)' .
- ')' );
- class IP {
-
- public static function isIPAddress( $ip ) {
- if ( !$ip ) return false;
- if ( is_array( $ip ) ) {
- throw new MWException( "invalid value passed to " . __METHOD__ );
- }
-
- return preg_match( '/^' . IP_ADDRESS_STRING . '$/', $ip) && ( substr_count($ip, '::') < 2 );
- }
- public static function isIPv6( $ip ) {
- if ( !$ip ) return false;
- if( is_array( $ip ) ) {
- throw new MWException( "invalid value passed to " . __METHOD__ );
- }
-
- return preg_match( '/^' . RE_IPV6_ADD . '(\/' . RE_IPV6_PREFIX . '|)$/', $ip) && ( substr_count($ip, '::') < 2);
- }
- public static function isIPv4( $ip ) {
- if ( !$ip ) return false;
- return preg_match( '/^' . RE_IP_ADD . '(\/' . RE_IP_PREFIX . '|)$/', $ip);
- }
-
- public static function IPv4toIPv6( $ip ) {
- if ( !$ip ) return null;
-
- if ( self::isIPv6( $ip ) ) return $ip;
-
- if ( strpos( $ip, '/' ) !== false ) {
- $parts = explode( '/', $ip, 2 );
- if ( count( $parts ) != 2 ) {
- return false;
- }
- $network = self::toUnsigned( $parts[0] );
- if ( $network !== false && is_numeric( $parts[1] ) && $parts[1] >= 0 && $parts[1] <= 32 ) {
- $bits = $parts[1] + 96;
- return self::toOctet( $network ) . "/$bits";
- } else {
- return false;
- }
- }
- return self::toOctet( self::toUnsigned( $ip ) );
- }
-
- public static function toUnsigned6( $ip ) {
- if ( !$ip ) return null;
- $ip = explode(':', self::sanitizeIP( $ip ) );
- $r_ip = '';
- foreach ($ip as $v) {
- $r_ip .= str_pad( $v, 4, 0, STR_PAD_LEFT );
- }
- $r_ip = wfBaseConvert( $r_ip, 16, 10 );
- return $r_ip;
- }
-
- public static function sanitizeIP( $ip ) {
- $ip = trim( $ip );
- if ( $ip === '' ) return null;
-
- if ( self::isIPv4($ip) ) return $ip;
-
- if ( !self::isIPv6($ip) ) return $ip;
-
- $ip = strtoupper( $ip );
-
- if ( strpos( $ip, '::' ) !== false ) {
- $ip = str_replace('::', str_repeat(':0', 8 - substr_count($ip, ':')) . ':', $ip);
- }
-
- if ( $ip[0] == ':' ) $ip = "0$ip";
-
- $ip = preg_replace( '/(^|:)0+' . RE_IPV6_WORD . '/', '$1$2', $ip );
- return $ip;
- }
-
- public static function toOctet( $ip_int ) {
-
- $ip_hex = wfBaseConvert($ip_int, 10, 16, 32, false);
-
- $ip_oct = substr( $ip_hex, 0, 4 );
- for ($n=1; $n < 8; $n++) {
- $ip_oct .= ':' . substr($ip_hex, 4*$n, 4);
- }
-
- $ip_oct = preg_replace( '/(^|:)0+' . RE_IPV6_WORD . '/', '$1$2', $ip_oct );
- return $ip_oct;
- }
-
-
- public static function HextoOctet( $ip_hex ) {
-
- $ip_hex = str_pad( strtoupper($ip_hex), 32, '0');
-
- $ip_oct = substr( $ip_hex, 0, 4 );
- for ($n=1; $n < 8; $n++) {
- $ip_oct .= ':' . substr($ip_hex, 4*$n, 4);
- }
-
- $ip_oct = preg_replace( '/(^|:)0+' . RE_IPV6_WORD . '/', '$1$2', $ip_oct );
- return $ip_oct;
- }
-
-
-
- public static function hexToQuad( $ip ) {
-
- $dec = wfBaseConvert( $ip, 16, 10 );
- $parts[3] = $dec % 256;
- $dec /= 256;
- $parts[2] = $dec % 256;
- $dec /= 256;
- $parts[1] = $dec % 256;
- $parts[0] = $dec / 256;
- return implode( '.', array_reverse( $parts ) );
- }
-
- public static function parseCIDR6( $range ) {
-
- $parts = explode( '/', IP::sanitizeIP( $range ), 2 );
- if ( count( $parts ) != 2 ) {
- return array( false, false );
- }
- $network = self::toUnsigned6( $parts[0] );
- if ( $network !== false && is_numeric( $parts[1] ) && $parts[1] >= 0 && $parts[1] <= 128 ) {
- $bits = $parts[1];
- if ( $bits == 0 ) {
- $network = 0;
- } else {
-
-
- $network = wfBaseConvert( $network, 10, 2, 128 );
-
- $network = str_pad( substr( $network, 0, $bits ), 128, 0, STR_PAD_RIGHT );
-
- $network = wfBaseConvert( $network, 2, 10 );
- }
- } else {
- $network = false;
- $bits = false;
- }
- return array( $network, $bits );
- }
-
- public static function parseRange6( $range ) {
-
- $range = IP::sanitizeIP( $range );
- if ( strpos( $range, '/' ) !== false ) {
-
- list( $network, $bits ) = self::parseCIDR6( $range );
- if ( $network === false ) {
- $start = $end = false;
- } else {
- $start = wfBaseConvert( $network, 10, 16, 32, false );
-
- $end = wfBaseConvert( $network, 10, 2, 128 );
-
- $end = str_pad( substr( $end, 0, $bits ), 128, 1, STR_PAD_RIGHT );
-
- $end = wfBaseConvert( $end, 2, 16, 32, false );
-
- $start = "v6-$start"; $end = "v6-$end";
- }
- } elseif ( strpos( $range, '-' ) !== false ) {
-
- list( $start, $end ) = array_map( 'trim', explode( '-', $range, 2 ) );
- $start = self::toUnsigned6( $start ); $end = self::toUnsigned6( $end );
- if ( $start > $end ) {
- $start = $end = false;
- } else {
- $start = wfBaseConvert( $start, 10, 16, 32, false );
- $end = wfBaseConvert( $end, 10, 16, 32, false );
- }
-
- $start = "v6-$start"; $end = "v6-$end";
- } else {
-
- $start = $end = self::toHex( $range );
- }
- if ( $start === false || $end === false ) {
- return array( false, false );
- } else {
- return array( $start, $end );
- }
- }
-
- public static function isValid( $ip ) {
- return ( preg_match( '/^' . RE_IP_ADD . '$/', $ip) || preg_match( '/^' . RE_IPV6_ADD . '$/', $ip) );
- }
-
- public static function isValidBlock( $ipblock ) {
- return ( count(self::toArray($ipblock)) == 1 + 5 );
- }
-
- public static function isPublic( $ip ) {
- $n = self::toUnsigned( $ip );
- if ( !$n ) {
- return false;
- }
-
-
- if( $ip != long2ip( $n ) ) {
- return false;
- }
- static $privateRanges = false;
- if ( !$privateRanges ) {
- $privateRanges = array(
- array( '10.0.0.0', '10.255.255.255' ),
- array( '172.16.0.0', '172.31.255.255' ),
- array( '192.168.0.0', '192.168.255.255' ),
- array( '0.0.0.0', '0.255.255.255' ),
- array( '127.0.0.0', '127.255.255.255' ),
- );
- }
- foreach ( $privateRanges as $r ) {
- $start = self::toUnsigned( $r[0] );
- $end = self::toUnsigned( $r[1] );
- if ( $n >= $start && $n <= $end ) {
- return false;
- }
- }
- return true;
- }
-
- public static function toArray( $ipblock ) {
- $matches = array();
- if( preg_match( '/^' . RE_IP_ADD . '(?:\/(?:'.RE_IP_PREFIX.'))?' . '$/', $ipblock, $matches ) ) {
- return $matches;
- } else if ( preg_match( '/^' . RE_IPV6_ADD . '(?:\/(?:'.RE_IPV6_PREFIX.'))?' . '$/', $ipblock, $matches ) ) {
- return $matches;
- } else {
- return false;
- }
- }
-
- public static function toHex( $ip ) {
- $n = self::toUnsigned( $ip );
- if ( $n !== false ) {
- $n = self::isIPv6($ip) ? "v6-" . wfBaseConvert( $n, 10, 16, 32, false ) : wfBaseConvert( $n, 10, 16, 8, false );
- }
- return $n;
- }
-
- public static function toUnsigned( $ip ) {
-
- if ( self::isIPv6( $ip ) ) {
- return self::toUnsigned6( $ip );
- }
- if ( $ip == '255.255.255.255' ) {
- $n = -1;
- } else {
- $n = ip2long( $ip );
- if ( $n == -1 || $n === false ) {
- $n = false;
- }
- }
- if ( $n < 0 ) {
- $n += pow( 2, 32 );
- }
- return $n;
- }
-
- public static function toSigned( $ip ) {
- if ( $ip == '255.255.255.255' ) {
- $n = -1;
- } else {
- $n = ip2long( $ip );
- if ( $n == -1 ) {
- $n = false;
- }
- }
- return $n;
- }
-
- public static function parseCIDR( $range ) {
- $parts = explode( '/', $range, 2 );
- if ( count( $parts ) != 2 ) {
- return array( false, false );
- }
- $network = self::toSigned( $parts[0] );
- if ( $network !== false && is_numeric( $parts[1] ) && $parts[1] >= 0 && $parts[1] <= 32 ) {
- $bits = $parts[1];
- if ( $bits == 0 ) {
- $network = 0;
- } else {
- $network &= ~((1 << (32 - $bits)) - 1);
- }
-
- if ( $network < 0 ) {
- $network += pow( 2, 32 );
- }
- } else {
- $network = false;
- $bits = false;
- }
- return array( $network, $bits );
- }
-
- public static function parseRange( $range ) {
-
- if ( self::isIPv6( $range ) ) {
- return self::parseRange6( $range );
- }
- if ( strpos( $range, '/' ) !== false ) {
-
- list( $network, $bits ) = self::parseCIDR( $range );
- if ( $network === false ) {
- $start = $end = false;
- } else {
- $start = sprintf( '%08X', $network );
- $end = sprintf( '%08X', $network + pow( 2, (32 - $bits) ) - 1 );
- }
- } elseif ( strpos( $range, '-' ) !== false ) {
-
- list( $start, $end ) = array_map( 'trim', explode( '-', $range, 2 ) );
- if( self::isIPAddress( $start ) && self::isIPAddress( $end ) ) {
- $start = self::toUnsigned( $start ); $end = self::toUnsigned( $end );
- if ( $start > $end ) {
- $start = $end = false;
- } else {
- $start = sprintf( '%08X', $start );
- $end = sprintf( '%08X', $end );
- }
- } else {
- $start = $end = false;
- }
- } else {
-
- $start = $end = self::toHex( $range );
- }
- if ( $start === false || $end === false ) {
- return array( false, false );
- } else {
- return array( $start, $end );
- }
- }
-
- public static function isInRange( $addr, $range ) {
-
- $unsignedIP = self::toHex( $addr );
- list( $start, $end ) = self::parseRange( $range );
- return (($unsignedIP >= $start) && ($unsignedIP <= $end));
- }
-
- public static function canonicalize( $addr ) {
- if ( self::isValid( $addr ) )
- return $addr;
-
- if ( strpos($addr,':') !==false && strpos($addr,'.') !==false ) {
- $addr = str_replace( '.', ':', $addr );
- if( IP::isIPv6( $addr ) )
- return $addr;
- }
-
- $m = array();
- if ( preg_match( '/^0*' . RE_IPV6_GAP . '1$/', $addr, $m ) )
- return '127.0.0.1';
-
- if ( preg_match( '/^' . RE_IPV6_V4_PREFIX . '(' . RE_IP_ADD . ')$/i', $addr, $m ) )
- return $m[1];
- if ( preg_match( '/^' . RE_IPV6_V4_PREFIX . RE_IPV6_WORD . ':' . RE_IPV6_WORD . '$/i', $addr, $m ) )
- return long2ip( ( hexdec( $m[1] ) << 16 ) + hexdec( $m[2] ) );
- return null;
- }
- }
|