123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <?php
-
-
-
- class Requests_IPv6 {
-
-
- public static function uncompress($ip) {
- if (substr_count($ip, '::') !== 1) {
- return $ip;
- }
-
- list($ip1, $ip2) = explode('::', $ip);
- $c1 = ($ip1 === '') ? -1 : substr_count($ip1, ':');
- $c2 = ($ip2 === '') ? -1 : substr_count($ip2, ':');
-
- if (strpos($ip2, '.') !== false) {
- $c2++;
- }
-
- if ($c1 === -1 && $c2 === -1) {
- $ip = '0:0:0:0:0:0:0:0';
- }
-
- elseif ($c1 === -1) {
- $fill = str_repeat('0:', 7 - $c2);
- $ip = str_replace('::', $fill, $ip);
- }
-
- elseif ($c2 === -1) {
- $fill = str_repeat(':0', 7 - $c1);
- $ip = str_replace('::', $fill, $ip);
- }
-
- else {
- $fill = ':' . str_repeat('0:', 6 - $c2 - $c1);
- $ip = str_replace('::', $fill, $ip);
- }
- return $ip;
- }
-
-
-
- public static function compress($ip) {
-
- $ip = self::uncompress($ip);
- $ip_parts = self::split_v6_v4($ip);
-
-
- $ip_parts[0] = preg_replace('/(^|:)0+([0-9])/', '\1\2', $ip_parts[0]);
-
-
- if (preg_match_all('/(?:^|:)(?:0(?::|$))+/', $ip_parts[0], $matches, PREG_OFFSET_CAPTURE)) {
- $max = 0;
- $pos = null;
- foreach ($matches[0] as $match) {
- if (strlen($match[0]) > $max) {
- $max = strlen($match[0]);
- $pos = $match[1];
- }
- }
-
- $ip_parts[0] = substr_replace($ip_parts[0], '::', $pos, $max);
- }
-
- if ($ip_parts[1] !== '') {
- return implode(':', $ip_parts);
- }
- else {
- return $ip_parts[0];
- }
- }
-
-
-
- protected static function split_v6_v4($ip) {
- if (strpos($ip, '.') !== false) {
- $pos = strrpos($ip, ':');
- $ipv6_part = substr($ip, 0, $pos);
- $ipv4_part = substr($ip, $pos + 1);
- return array($ipv6_part, $ipv4_part);
- }
- else {
- return array($ip, '');
- }
- }
-
-
-
- public static function check_ipv6($ip) {
- $ip = self::uncompress($ip);
- list($ipv6, $ipv4) = self::split_v6_v4($ip);
- $ipv6 = explode(':', $ipv6);
- $ipv4 = explode('.', $ipv4);
- if (count($ipv6) === 8 && count($ipv4) === 1 || count($ipv6) === 6 && count($ipv4) === 4) {
- foreach ($ipv6 as $ipv6_part) {
-
- if ($ipv6_part === '') {
- return false;
- }
-
-
- if (strlen($ipv6_part) > 4) {
- return false;
- }
-
-
- $ipv6_part = ltrim($ipv6_part, '0');
- if ($ipv6_part === '') {
- $ipv6_part = '0';
- }
-
-
- $value = hexdec($ipv6_part);
- if (dechex($value) !== strtolower($ipv6_part) || $value < 0 || $value > 0xFFFF) {
- return false;
- }
- }
- if (count($ipv4) === 4) {
- foreach ($ipv4 as $ipv4_part) {
- $value = (int) $ipv4_part;
- if ((string) $value !== $ipv4_part || $value < 0 || $value > 0xFF) {
- return false;
- }
- }
- }
- return true;
- }
- else {
- return false;
- }
- }
- }
|