Requests_IPv6

Class to validate and to work with IPv6 addresses.

Defined (1)

The class is defined in the following location(s).

/wp-includes/Requests/IPv6.php  
  1. class Requests_IPv6 { 
  2. /** 
  3. * Uncompresses an IPv6 address 
  4. * RFC 4291 allows you to compress consecutive zero pieces in an address to 
  5. * '::'. This method expects a valid IPv6 address and expands the '::' to 
  6. * the required number of zero pieces. 
  7. * Example: FF01::101 -> FF01:0:0:0:0:0:0:101 
  8. * ::1 -> 0:0:0:0:0:0:0:1 
  9. * @author Alexander Merz <alexander.merz@web.de> 
  10. * @author elfrink at introweb dot nl 
  11. * @author Josh Peck <jmp at joshpeck dot org> 
  12. * @copyright 2003-2005 The PHP Group 
  13. * @license http://www.opensource.org/licenses/bsd-license.php 
  14. * @param string $ip An IPv6 address 
  15. * @return string The uncompressed IPv6 address 
  16. */ 
  17. public static function uncompress($ip) { 
  18. if (substr_count($ip, '::') !== 1) { 
  19. return $ip; 
  20.  
  21. list($ip1, $ip2) = explode('::', $ip); 
  22. $c1 = ($ip1 === '') ? -1 : substr_count($ip1, ':'); 
  23. $c2 = ($ip2 === '') ? -1 : substr_count($ip2, ':'); 
  24.  
  25. if (strpos($ip2, '.') !== false) { 
  26. $c2++; 
  27. // :: 
  28. if ($c1 === -1 && $c2 === -1) { 
  29. $ip = '0:0:0:0:0:0:0:0'; 
  30. // ::xxx 
  31. else if ($c1 === -1) { 
  32. $fill = str_repeat('0:', 7 - $c2); 
  33. $ip = str_replace('::', $fill, $ip); 
  34. // xxx:: 
  35. else if ($c2 === -1) { 
  36. $fill = str_repeat(':0', 7 - $c1); 
  37. $ip = str_replace('::', $fill, $ip); 
  38. // xxx::xxx 
  39. else { 
  40. $fill = ':' . str_repeat('0:', 6 - $c2 - $c1); 
  41. $ip = str_replace('::', $fill, $ip); 
  42. return $ip; 
  43.  
  44. /** 
  45. * Compresses an IPv6 address 
  46. * RFC 4291 allows you to compress consecutive zero pieces in an address to 
  47. * '::'. This method expects a valid IPv6 address and compresses consecutive 
  48. * zero pieces to '::'. 
  49. * Example: FF01:0:0:0:0:0:0:101 -> FF01::101 
  50. * 0:0:0:0:0:0:0:1 -> ::1 
  51. * @see uncompress() 
  52. * @param string $ip An IPv6 address 
  53. * @return string The compressed IPv6 address 
  54. */ 
  55. public static function compress($ip) { 
  56. // Prepare the IP to be compressed 
  57. $ip = self::uncompress($ip); 
  58. $ip_parts = self::split_v6_v4($ip); 
  59.  
  60. // Replace all leading zeros 
  61. $ip_parts[0] = preg_replace('/(^|:)0+([0-9])/', '\1\2', $ip_parts[0]); 
  62.  
  63. // Find bunches of zeros 
  64. if (preg_match_all('/(?:^|:)(?:0(?::|$))+/', $ip_parts[0], $matches, PREG_OFFSET_CAPTURE)) { 
  65. $max = 0; 
  66. $pos = null; 
  67. foreach ($matches[0] as $match) { 
  68. if (strlen($match[0]) > $max) { 
  69. $max = strlen($match[0]); 
  70. $pos = $match[1]; 
  71.  
  72. $ip_parts[0] = substr_replace($ip_parts[0], '::', $pos, $max); 
  73.  
  74. if ($ip_parts[1] !== '') { 
  75. return implode(':', $ip_parts); 
  76. else { 
  77. return $ip_parts[0]; 
  78.  
  79. /** 
  80. * Splits an IPv6 address into the IPv6 and IPv4 representation parts 
  81. * RFC 4291 allows you to represent the last two parts of an IPv6 address 
  82. * using the standard IPv4 representation 
  83. * Example: 0:0:0:0:0:0:13.1.68.3 
  84. * 0:0:0:0:0:FFFF:129.144.52.38 
  85. * @param string $ip An IPv6 address 
  86. * @return string[] [0] contains the IPv6 represented part, and [1] the IPv4 represented part 
  87. */ 
  88. protected static function split_v6_v4($ip) { 
  89. if (strpos($ip, '.') !== false) { 
  90. $pos = strrpos($ip, ':'); 
  91. $ipv6_part = substr($ip, 0, $pos); 
  92. $ipv4_part = substr($ip, $pos + 1); 
  93. return array($ipv6_part, $ipv4_part); 
  94. else { 
  95. return array($ip, ''); 
  96.  
  97. /** 
  98. * Checks an IPv6 address 
  99. * Checks if the given IP is a valid IPv6 address 
  100. * @param string $ip An IPv6 address 
  101. * @return bool true if $ip is a valid IPv6 address 
  102. */ 
  103. public static function check_ipv6($ip) { 
  104. $ip = self::uncompress($ip); 
  105. list($ipv6, $ipv4) = self::split_v6_v4($ip); 
  106. $ipv6 = explode(':', $ipv6); 
  107. $ipv4 = explode('.', $ipv4); 
  108. if (count($ipv6) === 8 && count($ipv4) === 1 || count($ipv6) === 6 && count($ipv4) === 4) { 
  109. foreach ($ipv6 as $ipv6_part) { 
  110. // The section can't be empty 
  111. if ($ipv6_part === '') { 
  112. return false; 
  113.  
  114. // Nor can it be over four characters 
  115. if (strlen($ipv6_part) > 4) { 
  116. return false; 
  117.  
  118. // Remove leading zeros (this is safe because of the above) 
  119. $ipv6_part = ltrim($ipv6_part, '0'); 
  120. if ($ipv6_part === '') { 
  121. $ipv6_part = '0'; 
  122.  
  123. // Check the value is valid 
  124. $value = hexdec($ipv6_part); 
  125. if (dechex($value) !== strtolower($ipv6_part) || $value < 0 || $value > 0xFFFF) { 
  126. return false; 
  127. if (count($ipv4) === 4) { 
  128. foreach ($ipv4 as $ipv4_part) { 
  129. $value = (int) $ipv4_part; 
  130. if ((string) $value !== $ipv4_part || $value < 0 || $value > 0xFF) { 
  131. return false; 
  132. return true; 
  133. else { 
  134. return false;