followup r91869: validate id chars for incoming prefs tabs in hash ([\w-]+ is suffici...
[mediawiki.git] / includes / IP.php
blob055fe85c05630d0c8e042a5617ab660b90a1b584
1 <?php
2 /**
3 * Functions and constants to play with IP addresses and ranges
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @author Ashar Voultoiz <hashar at free dot fr>, Aaron Schulz
24 // Some regex definition to "play" with IP address and IP address blocks
26 // An IPv4 address is made of 4 bytes from x00 to xFF which is d0 to d255
27 define( 'RE_IP_BYTE', '(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[0-9])' );
28 define( 'RE_IP_ADD', RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE );
29 // An IPv4 block is an IP address and a prefix (d1 to d32)
30 define( 'RE_IP_PREFIX', '(3[0-2]|[12]?\d)' );
31 define( 'RE_IP_BLOCK', RE_IP_ADD . '\/' . RE_IP_PREFIX );
33 // An IPv6 address is made up of 8 words (each x0000 to xFFFF).
34 // However, the "::" abbreviation can be used on consecutive x0000 words.
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',
38 '(?:' . // starts with "::" (including "::")
39 ':(?::|(?::' . RE_IPV6_WORD . '){1,7})' .
40 '|' . // ends with "::" (except "::")
41 RE_IPV6_WORD . '(?::' . RE_IPV6_WORD . '){0,6}::' .
42 '|' . // contains one "::" in the middle, ending in "::WORD"
43 RE_IPV6_WORD . '(?::' . RE_IPV6_WORD . '){0,5}' . '::' . RE_IPV6_WORD .
44 '|' . // contains one "::" in the middle, not ending in "::WORD" (regex for PCRE 4.0+)
45 RE_IPV6_WORD . '(?::(?P<abn>:(?P<iabn>))?' . RE_IPV6_WORD . '(?!:(?P=abn))){1,5}' .
46 ':' . RE_IPV6_WORD . '(?P=iabn)' .
47 // NOTE: (?!(?P=abn)) fails iff "::" used twice; (?P=iabn) passes iff a "::" was found.
48 '|' . // contains no "::"
49 RE_IPV6_WORD . '(?::' . RE_IPV6_WORD . '){7}' .
50 ')'
51 // NOTE: With PCRE 7.2+, we can combine the two '"::" in the middle' cases into:
52 // RE_IPV6_WORD . '(?::((?(-1)|:))?' . RE_IPV6_WORD . '){1,6}(?(-2)|^)'
53 // This also improves regex concatenation by using relative references.
55 // An IPv6 block is an IP address and a prefix (d1 to d128)
56 define( 'RE_IPV6_BLOCK', RE_IPV6_ADD . '\/' . RE_IPV6_PREFIX );
57 // For IPv6 canonicalization (NOT for strict validation; these are quite lax!)
58 define( 'RE_IPV6_GAP', ':(?:0+:)*(?::(?:0+:)*)?' );
59 define( 'RE_IPV6_V4_PREFIX', '0*' . RE_IPV6_GAP . '(?:ffff:)?' );
61 // This might be useful for regexps used elsewhere, matches any IPv6 or IPv6 address or network
62 define( 'IP_ADDRESS_STRING',
63 '(?:' .
64 RE_IP_ADD . '(?:\/' . RE_IP_PREFIX . ')?' . // IPv4
65 '|' .
66 RE_IPV6_ADD . '(?:\/' . RE_IPV6_PREFIX . ')?' . // IPv6
67 ')'
70 /**
71 * A collection of public static functions to play with IP address
72 * and IP blocks.
74 class IP {
75 /**
76 * Determine if a string is as valid IP address or network (CIDR prefix).
77 * SIIT IPv4-translated addresses are rejected.
78 * Note: canonicalize() tries to convert translated addresses to IPv4.
80 * @param $ip String: possible IP address
81 * @return Boolean
83 public static function isIPAddress( $ip ) {
84 return (bool)preg_match( '/^' . IP_ADDRESS_STRING . '$/', $ip );
87 /**
88 * Given a string, determine if it as valid IP in IPv6 only.
89 * Note: Unlike isValid(), this looks for networks too.
91 * @param $ip String: possible IP address
92 * @return Boolean
94 public static function isIPv6( $ip ) {
95 return (bool)preg_match( '/^' . RE_IPV6_ADD . '(?:\/' . RE_IPV6_PREFIX . ')?$/', $ip );
98 /**
99 * Given a string, determine if it as valid IP in IPv4 only.
100 * Note: Unlike isValid(), this looks for networks too.
102 * @param $ip String: possible IP address
103 * @return Boolean
105 public static function isIPv4( $ip ) {
106 return (bool)preg_match( '/^' . RE_IP_ADD . '(?:\/' . RE_IP_PREFIX . ')?$/', $ip );
110 * Validate an IP address. Ranges are NOT considered valid.
111 * SIIT IPv4-translated addresses are rejected.
112 * Note: canonicalize() tries to convert translated addresses to IPv4.
114 * @param $ip String
115 * @return Boolean: True if it is valid.
117 public static function isValid( $ip ) {
118 return ( preg_match( '/^' . RE_IP_ADD . '$/', $ip )
119 || preg_match( '/^' . RE_IPV6_ADD . '$/', $ip ) );
123 * Validate an IP Block (valid address WITH a valid prefix).
124 * SIIT IPv4-translated addresses are rejected.
125 * Note: canonicalize() tries to convert translated addresses to IPv4.
127 * @param $ipblock String
128 * @return Boolean: True if it is valid.
130 public static function isValidBlock( $ipblock ) {
131 return ( preg_match( '/^' . RE_IPV6_BLOCK . '$/', $ipblock )
132 || preg_match( '/^' . RE_IP_BLOCK . '$/', $ipblock ) );
136 * Convert an IP into a nice standard form.
137 * IPv6 addresses in octet notation are expanded to 8 words.
138 * IPv4 addresses are just trimmed.
140 * @param $ip String: IP address in quad or octet form (CIDR or not).
141 * @return String
143 public static function sanitizeIP( $ip ) {
144 $ip = trim( $ip );
145 if ( $ip === '' ) {
146 return null;
148 if ( self::isIPv4( $ip ) || !self::isIPv6( $ip ) ) {
149 return $ip; // nothing else to do for IPv4 addresses or invalid ones
151 // Remove any whitespaces, convert to upper case
152 $ip = strtoupper( $ip );
153 // Expand zero abbreviations
154 $abbrevPos = strpos( $ip, '::' );
155 if ( $abbrevPos !== false ) {
156 // We know this is valid IPv6. Find the last index of the
157 // address before any CIDR number (e.g. "a:b:c::/24").
158 $CIDRStart = strpos( $ip, "/" );
159 $addressEnd = ( $CIDRStart !== false )
160 ? $CIDRStart - 1
161 : strlen( $ip ) - 1;
162 // If the '::' is at the beginning...
163 if ( $abbrevPos == 0 ) {
164 $repeat = '0:';
165 $extra = ( $ip == '::' ) ? '0' : ''; // for the address '::'
166 $pad = 9; // 7+2 (due to '::')
167 // If the '::' is at the end...
168 } elseif ( $abbrevPos == ( $addressEnd - 1 ) ) {
169 $repeat = ':0';
170 $extra = '';
171 $pad = 9; // 7+2 (due to '::')
172 // If the '::' is in the middle...
173 } else {
174 $repeat = ':0';
175 $extra = ':';
176 $pad = 8; // 6+2 (due to '::')
178 $ip = str_replace( '::',
179 str_repeat( $repeat, $pad - substr_count( $ip, ':' ) ) . $extra,
183 // Remove leading zereos from each bloc as needed
184 $ip = preg_replace( '/(^|:)0+(' . RE_IPV6_WORD . ')/', '$1$2', $ip );
185 return $ip;
189 * Given a host/port string, like one might find in the host part of a URL
190 * per RFC 2732, split the hostname part and the port part and return an
191 * array with an element for each. If there is no port part, the array will
192 * have false in place of the port. If the string was invalid in some way,
193 * false is returned.
195 * This was easy with IPv4 and was generally done in an ad-hoc way, but
196 * with IPv6 it's somewhat more complicated due to the need to parse the
197 * square brackets and colons.
199 * A bare IPv6 address is accepted despite the lack of square brackets.
201 * @param $both The string with the host and port
202 * @return array
204 public static function splitHostAndPort( $both ) {
205 if ( substr( $both, 0, 1 ) === '[' ) {
206 if ( preg_match( '/^\[(' . RE_IPV6_ADD . ')\](?::(?P<port>\d+))?$/', $both, $m ) ) {
207 if ( isset( $m['port'] ) ) {
208 return array( $m[1], intval( $m['port'] ) );
209 } else {
210 return array( $m[1], false );
212 } else {
213 // Square bracket found but no IPv6
214 return false;
217 $numColons = substr_count( $both, ':' );
218 if ( $numColons >= 2 ) {
219 // Is it a bare IPv6 address?
220 if ( preg_match( '/^' . RE_IPV6_ADD . '$/', $both ) ) {
221 return array( $both, false );
222 } else {
223 // Not valid IPv6, but too many colons for anything else
224 return false;
227 if ( $numColons >= 1 ) {
228 // Host:port?
229 $bits = explode( ':', $both );
230 if ( preg_match( '/^\d+/', $bits[1] ) ) {
231 return array( $bits[0], intval( $bits[1] ) );
232 } else {
233 // Not a valid port
234 return false;
237 // Plain hostname
238 return array( $both, false );
242 * Given a host name and a port, combine them into host/port string like
243 * you might find in a URL. If the host contains a colon, wrap it in square
244 * brackets like in RFC 2732. If the port matches the default port, omit
245 * the port specification
247 public static function combineHostAndPort( $host, $port, $defaultPort = false ) {
248 if ( strpos( $host, ':' ) !== false ) {
249 $host = "[$host]";
251 if ( $defaultPort !== false && $port == $defaultPort ) {
252 return $host;
253 } else {
254 return "$host:$port";
259 * Given an unsigned integer, returns an IPv6 address in octet notation
261 * @param $ip_int String: IP address.
262 * @return String
264 public static function toOctet( $ip_int ) {
265 return self::hexToOctet( wfBaseConvert( $ip_int, 10, 16, 32, false ) );
269 * Convert an IPv4 or IPv6 hexadecimal representation back to readable format
271 * @param $hex String: number, with "v6-" prefix if it is IPv6
272 * @return String: quad-dotted (IPv4) or octet notation (IPv6)
274 public static function formatHex( $hex ) {
275 if ( substr( $hex, 0, 3 ) == 'v6-' ) { // IPv6
276 return self::hexToOctet( substr( $hex, 3 ) );
277 } else { // IPv4
278 return self::hexToQuad( $hex );
283 * Converts a hexadecimal number to an IPv6 address in octet notation
285 * @param $ip_hex String: pure hex (no v6- prefix)
286 * @return String (of format a:b:c:d:e:f:g:h)
288 public static function hexToOctet( $ip_hex ) {
289 // Pad hex to 32 chars (128 bits)
290 $ip_hex = str_pad( strtoupper( $ip_hex ), 32, '0', STR_PAD_LEFT );
291 // Separate into 8 words
292 $ip_oct = substr( $ip_hex, 0, 4 );
293 for ( $n = 1; $n < 8; $n++ ) {
294 $ip_oct .= ':' . substr( $ip_hex, 4 * $n, 4 );
296 // NO leading zeroes
297 $ip_oct = preg_replace( '/(^|:)0+(' . RE_IPV6_WORD . ')/', '$1$2', $ip_oct );
298 return $ip_oct;
302 * Converts a hexadecimal number to an IPv4 address in quad-dotted notation
304 * @param $ip_hex String: pure hex
305 * @return String (of format a.b.c.d)
307 public static function hexToQuad( $ip_hex ) {
308 // Pad hex to 8 chars (32 bits)
309 $ip_hex = str_pad( strtoupper( $ip_hex ), 8, '0', STR_PAD_LEFT );
310 // Separate into four quads
311 $s = '';
312 for ( $i = 0; $i < 4; $i++ ) {
313 if ( $s !== '' ) {
314 $s .= '.';
316 $s .= base_convert( substr( $ip_hex, $i * 2, 2 ), 16, 10 );
318 return $s;
322 * Determine if an IP address really is an IP address, and if it is public,
323 * i.e. not RFC 1918 or similar
324 * Comes from ProxyTools.php
326 * @param $ip String
327 * @return Boolean
329 public static function isPublic( $ip ) {
330 if ( self::isIPv6( $ip ) ) {
331 return self::isPublic6( $ip );
333 $n = self::toUnsigned( $ip );
334 if ( !$n ) {
335 return false;
338 // ip2long accepts incomplete addresses, as well as some addresses
339 // followed by garbage characters. Check that it's really valid.
340 if ( $ip != long2ip( $n ) ) {
341 return false;
344 static $privateRanges = false;
345 if ( !$privateRanges ) {
346 $privateRanges = array(
347 array( '10.0.0.0', '10.255.255.255' ), # RFC 1918 (private)
348 array( '172.16.0.0', '172.31.255.255' ), # "
349 array( '192.168.0.0', '192.168.255.255' ), # "
350 array( '0.0.0.0', '0.255.255.255' ), # this network
351 array( '127.0.0.0', '127.255.255.255' ), # loopback
355 foreach ( $privateRanges as $r ) {
356 $start = self::toUnsigned( $r[0] );
357 $end = self::toUnsigned( $r[1] );
358 if ( $n >= $start && $n <= $end ) {
359 return false;
362 return true;
366 * Determine if an IPv6 address really is an IP address, and if it is public,
367 * i.e. not RFC 4193 or similar
369 * @param $ip String
370 * @return Boolean
372 private static function isPublic6( $ip ) {
373 static $privateRanges = false;
374 if ( !$privateRanges ) {
375 $privateRanges = array(
376 array( 'fc::', 'fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff' ), # RFC 4193 (local)
377 array( '0:0:0:0:0:0:0:1', '0:0:0:0:0:0:0:1' ), # loopback
380 $n = self::toHex( $ip );
381 foreach ( $privateRanges as $r ) {
382 $start = self::toHex( $r[0] );
383 $end = self::toHex( $r[1] );
384 if ( $n >= $start && $n <= $end ) {
385 return false;
388 return true;
392 * Return a zero-padded upper case hexadecimal representation of an IP address.
394 * Hexadecimal addresses are used because they can easily be extended to
395 * IPv6 support. To separate the ranges, the return value from this
396 * function for an IPv6 address will be prefixed with "v6-", a non-
397 * hexadecimal string which sorts after the IPv4 addresses.
399 * @param $ip String: quad dotted/octet IP address.
400 * @return String
402 public static function toHex( $ip ) {
403 if ( self::isIPv6( $ip ) ) {
404 $n = 'v6-' . self::IPv6ToRawHex( $ip );
405 } else {
406 $n = self::toUnsigned( $ip );
407 if ( $n !== false ) {
408 $n = wfBaseConvert( $n, 10, 16, 8, false );
411 return $n;
415 * Given an IPv6 address in octet notation, returns a pure hex string.
417 * @param $ip String: octet ipv6 IP address.
418 * @return String: pure hex (uppercase)
420 private static function IPv6ToRawHex( $ip ) {
421 $ip = self::sanitizeIP( $ip );
422 if ( !$ip ) {
423 return null;
425 $r_ip = '';
426 foreach ( explode( ':', $ip ) as $v ) {
427 $r_ip .= str_pad( $v, 4, 0, STR_PAD_LEFT );
429 return $r_ip;
433 * Given an IP address in dotted-quad/octet notation, returns an unsigned integer.
434 * Like ip2long() except that it actually works and has a consistent error return value.
435 * Comes from ProxyTools.php
437 * @param $ip String: quad dotted IP address.
438 * @return Mixed: string/int/false
440 public static function toUnsigned( $ip ) {
441 if ( self::isIPv6( $ip ) ) {
442 $n = self::toUnsigned6( $ip );
443 } else {
444 $n = ip2long( $ip );
445 if ( $n < 0 ) {
446 $n += pow( 2, 32 );
449 return $n;
452 private static function toUnsigned6( $ip ) {
453 return wfBaseConvert( self::IPv6ToRawHex( $ip ), 16, 10 );
457 * Convert a network specification in CIDR notation
458 * to an integer network and a number of bits
460 * @param $range String: IP with CIDR prefix
461 * @return array(int or string, int)
463 public static function parseCIDR( $range ) {
464 if ( self::isIPv6( $range ) ) {
465 return self::parseCIDR6( $range );
467 $parts = explode( '/', $range, 2 );
468 if ( count( $parts ) != 2 ) {
469 return array( false, false );
471 list( $network, $bits ) = $parts;
472 $network = ip2long( $network );
473 if ( $network !== false && is_numeric( $bits ) && $bits >= 0 && $bits <= 32 ) {
474 if ( $bits == 0 ) {
475 $network = 0;
476 } else {
477 $network &= ~( ( 1 << ( 32 - $bits ) ) - 1);
479 # Convert to unsigned
480 if ( $network < 0 ) {
481 $network += pow( 2, 32 );
483 } else {
484 $network = false;
485 $bits = false;
487 return array( $network, $bits );
491 * Given a string range in a number of formats,
492 * return the start and end of the range in hexadecimal.
494 * Formats are:
495 * 1.2.3.4/24 CIDR
496 * 1.2.3.4 - 1.2.3.5 Explicit range
497 * 1.2.3.4 Single IP
499 * 2001:0db8:85a3::7344/96 CIDR
500 * 2001:0db8:85a3::7344 - 2001:0db8:85a3::7344 Explicit range
501 * 2001:0db8:85a3::7344 Single IP
502 * @param $range String: IP range
503 * @return array(string, string)
505 public static function parseRange( $range ) {
506 // CIDR notation
507 if ( strpos( $range, '/' ) !== false ) {
508 if ( self::isIPv6( $range ) ) {
509 return self::parseRange6( $range );
511 list( $network, $bits ) = self::parseCIDR( $range );
512 if ( $network === false ) {
513 $start = $end = false;
514 } else {
515 $start = sprintf( '%08X', $network );
516 $end = sprintf( '%08X', $network + pow( 2, ( 32 - $bits ) ) - 1 );
518 // Explicit range
519 } elseif ( strpos( $range, '-' ) !== false ) {
520 list( $start, $end ) = array_map( 'trim', explode( '-', $range, 2 ) );
521 if ( self::isIPv6( $start ) && self::isIPv6( $end ) ) {
522 return self::parseRange6( $range );
524 if ( self::isIPv4( $start ) && self::isIPv4( $end ) ) {
525 $start = self::toUnsigned( $start );
526 $end = self::toUnsigned( $end );
527 if ( $start > $end ) {
528 $start = $end = false;
529 } else {
530 $start = sprintf( '%08X', $start );
531 $end = sprintf( '%08X', $end );
533 } else {
534 $start = $end = false;
536 } else {
537 # Single IP
538 $start = $end = self::toHex( $range );
540 if ( $start === false || $end === false ) {
541 return array( false, false );
542 } else {
543 return array( $start, $end );
548 * Convert a network specification in IPv6 CIDR notation to an
549 * integer network and a number of bits
551 * @return array(string, int)
553 private static function parseCIDR6( $range ) {
554 # Explode into <expanded IP,range>
555 $parts = explode( '/', IP::sanitizeIP( $range ), 2 );
556 if ( count( $parts ) != 2 ) {
557 return array( false, false );
559 list( $network, $bits ) = $parts;
560 $network = self::IPv6ToRawHex( $network );
561 if ( $network !== false && is_numeric( $bits ) && $bits >= 0 && $bits <= 128 ) {
562 if ( $bits == 0 ) {
563 $network = "0";
564 } else {
565 # Native 32 bit functions WONT work here!!!
566 # Convert to a padded binary number
567 $network = wfBaseConvert( $network, 16, 2, 128 );
568 # Truncate the last (128-$bits) bits and replace them with zeros
569 $network = str_pad( substr( $network, 0, $bits ), 128, 0, STR_PAD_RIGHT );
570 # Convert back to an integer
571 $network = wfBaseConvert( $network, 2, 10 );
573 } else {
574 $network = false;
575 $bits = false;
577 return array( $network, (int)$bits );
581 * Given a string range in a number of formats, return the
582 * start and end of the range in hexadecimal. For IPv6.
584 * Formats are:
585 * 2001:0db8:85a3::7344/96 CIDR
586 * 2001:0db8:85a3::7344 - 2001:0db8:85a3::7344 Explicit range
587 * 2001:0db8:85a3::7344/96 Single IP
588 * @return array(string, string)
590 private static function parseRange6( $range ) {
591 # Expand any IPv6 IP
592 $range = IP::sanitizeIP( $range );
593 // CIDR notation...
594 if ( strpos( $range, '/' ) !== false ) {
595 list( $network, $bits ) = self::parseCIDR6( $range );
596 if ( $network === false ) {
597 $start = $end = false;
598 } else {
599 $start = wfBaseConvert( $network, 10, 16, 32, false );
600 # Turn network to binary (again)
601 $end = wfBaseConvert( $network, 10, 2, 128 );
602 # Truncate the last (128-$bits) bits and replace them with ones
603 $end = str_pad( substr( $end, 0, $bits ), 128, 1, STR_PAD_RIGHT );
604 # Convert to hex
605 $end = wfBaseConvert( $end, 2, 16, 32, false );
606 # see toHex() comment
607 $start = "v6-$start";
608 $end = "v6-$end";
610 // Explicit range notation...
611 } elseif ( strpos( $range, '-' ) !== false ) {
612 list( $start, $end ) = array_map( 'trim', explode( '-', $range, 2 ) );
613 $start = self::toUnsigned6( $start );
614 $end = self::toUnsigned6( $end );
615 if ( $start > $end ) {
616 $start = $end = false;
617 } else {
618 $start = wfBaseConvert( $start, 10, 16, 32, false );
619 $end = wfBaseConvert( $end, 10, 16, 32, false );
621 # see toHex() comment
622 $start = "v6-$start";
623 $end = "v6-$end";
624 } else {
625 # Single IP
626 $start = $end = self::toHex( $range );
628 if ( $start === false || $end === false ) {
629 return array( false, false );
630 } else {
631 return array( $start, $end );
636 * Determine if a given IPv4/IPv6 address is in a given CIDR network
638 * @param $addr String: the address to check against the given range.
639 * @param $range String: the range to check the given address against.
640 * @return Boolean: whether or not the given address is in the given range.
642 public static function isInRange( $addr, $range ) {
643 $hexIP = self::toHex( $addr );
644 list( $start, $end ) = self::parseRange( $range );
645 return ( strcmp( $hexIP, $start ) >= 0 &&
646 strcmp( $hexIP, $end ) <= 0 );
650 * Convert some unusual representations of IPv4 addresses to their
651 * canonical dotted quad representation.
653 * This currently only checks a few IPV4-to-IPv6 related cases. More
654 * unusual representations may be added later.
656 * @param $addr String: something that might be an IP address
657 * @return String: valid dotted quad IPv4 address or null
659 public static function canonicalize( $addr ) {
660 if ( self::isValid( $addr ) ) {
661 return $addr;
663 // Turn mapped addresses from ::ce:ffff:1.2.3.4 to 1.2.3.4
664 if ( strpos( $addr, ':' ) !== false && strpos( $addr, '.' ) !== false ) {
665 $addr = substr( $addr, strrpos( $addr, ':' ) + 1 );
666 if ( self::isIPv4( $addr ) ) {
667 return $addr;
670 // IPv6 loopback address
671 $m = array();
672 if ( preg_match( '/^0*' . RE_IPV6_GAP . '1$/', $addr, $m ) ) {
673 return '127.0.0.1';
675 // IPv4-mapped and IPv4-compatible IPv6 addresses
676 if ( preg_match( '/^' . RE_IPV6_V4_PREFIX . '(' . RE_IP_ADD . ')$/i', $addr, $m ) ) {
677 return $m[1];
679 if ( preg_match( '/^' . RE_IPV6_V4_PREFIX . RE_IPV6_WORD .
680 ':' . RE_IPV6_WORD . '$/i', $addr, $m ) )
682 return long2ip( ( hexdec( $m[1] ) << 16 ) + hexdec( $m[2] ) );
685 return null; // give up
689 * Gets rid of uneeded numbers in quad-dotted/octet IP strings
690 * For example, 127.111.113.151/24 -> 127.111.113.0/24
691 * @param $range String: IP address to normalize
692 * @return string
694 public static function sanitizeRange( $range ){
695 list( /*...*/, $bits ) = self::parseCIDR( $range );
696 list( $start, /*...*/ ) = self::parseRange( $range );
697 $start = self::formatHex( $start );
698 return "$start/$bits";