3 * Class to validate and to work with IPv6 addresses
6 * @subpackage Utilities
10 * Class to validate and to work with IPv6 addresses
12 * This was originally based on the PEAR class of the same name, but has been
16 * @subpackage Utilities
20 * Uncompresses an IPv6 address
22 * RFC 4291 allows you to compress consecutive zero pieces in an address to
23 * '::'. This method expects a valid IPv6 address and expands the '::' to
24 * the required number of zero pieces.
26 * Example: FF01::101 -> FF01:0:0:0:0:0:0:101
27 * ::1 -> 0:0:0:0:0:0:0:1
29 * @author Alexander Merz <alexander.merz@web.de>
30 * @author elfrink at introweb dot nl
31 * @author Josh Peck <jmp at joshpeck dot org>
32 * @copyright 2003-2005 The PHP Group
33 * @license http://www.opensource.org/licenses/bsd-license.php
34 * @param string $ip An IPv6 address
35 * @return string The uncompressed IPv6 address
37 public static function uncompress($ip) {
38 if (substr_count($ip, '::') !== 1) {
42 list($ip1, $ip2) = explode('::', $ip);
43 $c1 = ($ip1 === '') ? -1 : substr_count($ip1, ':');
44 $c2 = ($ip2 === '') ? -1 : substr_count($ip2, ':');
46 if (strpos($ip2, '.') !== false) {
50 if ($c1 === -1 && $c2 === -1) {
51 $ip = '0:0:0:0:0:0:0:0';
54 else if ($c1 === -1) {
55 $fill = str_repeat('0:', 7 - $c2);
56 $ip = str_replace('::', $fill, $ip);
59 else if ($c2 === -1) {
60 $fill = str_repeat(':0', 7 - $c1);
61 $ip = str_replace('::', $fill, $ip);
65 $fill = ':' . str_repeat('0:', 6 - $c2 - $c1);
66 $ip = str_replace('::', $fill, $ip);
72 * Compresses an IPv6 address
74 * RFC 4291 allows you to compress consecutive zero pieces in an address to
75 * '::'. This method expects a valid IPv6 address and compresses consecutive
76 * zero pieces to '::'.
78 * Example: FF01:0:0:0:0:0:0:101 -> FF01::101
79 * 0:0:0:0:0:0:0:1 -> ::1
82 * @param string $ip An IPv6 address
83 * @return string The compressed IPv6 address
85 public static function compress($ip) {
86 // Prepare the IP to be compressed
87 $ip = self::uncompress($ip);
88 $ip_parts = self::split_v6_v4($ip);
90 // Replace all leading zeros
91 $ip_parts[0] = preg_replace('/(^|:)0+([0-9])/', '\1\2', $ip_parts[0]);
93 // Find bunches of zeros
94 if (preg_match_all('/(?:^|:)(?:0(?::|$))+/', $ip_parts[0], $matches, PREG_OFFSET_CAPTURE)) {
97 foreach ($matches[0] as $match) {
98 if (strlen($match[0]) > $max) {
99 $max = strlen($match[0]);
104 $ip_parts[0] = substr_replace($ip_parts[0], '::', $pos, $max);
107 if ($ip_parts[1] !== '') {
108 return implode(':', $ip_parts);
116 * Splits an IPv6 address into the IPv6 and IPv4 representation parts
118 * RFC 4291 allows you to represent the last two parts of an IPv6 address
119 * using the standard IPv4 representation
121 * Example: 0:0:0:0:0:0:13.1.68.3
122 * 0:0:0:0:0:FFFF:129.144.52.38
124 * @param string $ip An IPv6 address
125 * @return string[] [0] contains the IPv6 represented part, and [1] the IPv4 represented part
127 protected static function split_v6_v4($ip) {
128 if (strpos($ip, '.') !== false) {
129 $pos = strrpos($ip, ':');
130 $ipv6_part = substr($ip, 0, $pos);
131 $ipv4_part = substr($ip, $pos + 1);
132 return array($ipv6_part, $ipv4_part);
135 return array($ip, '');
140 * Checks an IPv6 address
142 * Checks if the given IP is a valid IPv6 address
144 * @param string $ip An IPv6 address
145 * @return bool true if $ip is a valid IPv6 address
147 public static function check_ipv6($ip) {
148 $ip = self::uncompress($ip);
149 list($ipv6, $ipv4) = self::split_v6_v4($ip);
150 $ipv6 = explode(':', $ipv6);
151 $ipv4 = explode('.', $ipv4);
152 if (count($ipv6) === 8 && count($ipv4) === 1 || count($ipv6) === 6 && count($ipv4) === 4) {
153 foreach ($ipv6 as $ipv6_part) {
154 // The section can't be empty
155 if ($ipv6_part === '') {
159 // Nor can it be over four characters
160 if (strlen($ipv6_part) > 4) {
164 // Remove leading zeros (this is safe because of the above)
165 $ipv6_part = ltrim($ipv6_part, '0');
166 if ($ipv6_part === '') {
170 // Check the value is valid
171 $value = hexdec($ipv6_part);
172 if (dechex($value) !== strtolower($ipv6_part) || $value < 0 || $value > 0xFFFF) {
176 if (count($ipv4) === 4) {
177 foreach ($ipv4 as $ipv4_part) {
178 $value = (int) $ipv4_part;
179 if ((string) $value !== $ipv4_part || $value < 0 || $value > 0xFF) {