/wp-includes/Requests/IPv6.php

  1. <?php 
  2. /** 
  3. * Class to validate and to work with IPv6 addresses 
  4. * 
  5. * @package Requests 
  6. * @subpackage Utilities 
  7. */ 
  8.  
  9. /** 
  10. * Class to validate and to work with IPv6 addresses 
  11. * 
  12. * This was originally based on the PEAR class of the same name, but has been 
  13. * entirely rewritten. 
  14. * 
  15. * @package Requests 
  16. * @subpackage Utilities 
  17. */ 
  18. class Requests_IPv6 { 
  19. /** 
  20. * Uncompresses an IPv6 address 
  21. * 
  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. 
  25. * 
  26. * Example: FF01::101 -> FF01:0:0:0:0:0:0:101 
  27. * ::1 -> 0:0:0:0:0:0:0:1 
  28. * 
  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 
  36. */ 
  37. public static function uncompress($ip) { 
  38. if (substr_count($ip, '::') !== 1) { 
  39. return $ip; 
  40.  
  41. list($ip1, $ip2) = explode('::', $ip); 
  42. $c1 = ($ip1 === '') ? -1 : substr_count($ip1, ':'); 
  43. $c2 = ($ip2 === '') ? -1 : substr_count($ip2, ':'); 
  44.  
  45. if (strpos($ip2, '.') !== false) { 
  46. $c2++; 
  47. // :: 
  48. if ($c1 === -1 && $c2 === -1) { 
  49. $ip = '0:0:0:0:0:0:0:0'; 
  50. // ::xxx 
  51. else if ($c1 === -1) { 
  52. $fill = str_repeat('0:', 7 - $c2); 
  53. $ip = str_replace('::', $fill, $ip); 
  54. // xxx:: 
  55. else if ($c2 === -1) { 
  56. $fill = str_repeat(':0', 7 - $c1); 
  57. $ip = str_replace('::', $fill, $ip); 
  58. // xxx::xxx 
  59. else { 
  60. $fill = ':' . str_repeat('0:', 6 - $c2 - $c1); 
  61. $ip = str_replace('::', $fill, $ip); 
  62. return $ip; 
  63.  
  64. /** 
  65. * Compresses an IPv6 address 
  66. * 
  67. * RFC 4291 allows you to compress consecutive zero pieces in an address to 
  68. * '::'. This method expects a valid IPv6 address and compresses consecutive 
  69. * zero pieces to '::'. 
  70. * 
  71. * Example: FF01:0:0:0:0:0:0:101 -> FF01::101 
  72. * 0:0:0:0:0:0:0:1 -> ::1 
  73. * 
  74. * @see uncompress() 
  75. * @param string $ip An IPv6 address 
  76. * @return string The compressed IPv6 address 
  77. */ 
  78. public static function compress($ip) { 
  79. // Prepare the IP to be compressed 
  80. $ip = self::uncompress($ip); 
  81. $ip_parts = self::split_v6_v4($ip); 
  82.  
  83. // Replace all leading zeros 
  84. $ip_parts[0] = preg_replace('/(^|:)0+([0-9])/', '\1\2', $ip_parts[0]); 
  85.  
  86. // Find bunches of zeros 
  87. if (preg_match_all('/(?:^|:)(?:0(?::|$))+/', $ip_parts[0], $matches, PREG_OFFSET_CAPTURE)) { 
  88. $max = 0; 
  89. $pos = null; 
  90. foreach ($matches[0] as $match) { 
  91. if (strlen($match[0]) > $max) { 
  92. $max = strlen($match[0]); 
  93. $pos = $match[1]; 
  94.  
  95. $ip_parts[0] = substr_replace($ip_parts[0], '::', $pos, $max); 
  96.  
  97. if ($ip_parts[1] !== '') { 
  98. return implode(':', $ip_parts); 
  99. else { 
  100. return $ip_parts[0]; 
  101.  
  102. /** 
  103. * Splits an IPv6 address into the IPv6 and IPv4 representation parts 
  104. * 
  105. * RFC 4291 allows you to represent the last two parts of an IPv6 address 
  106. * using the standard IPv4 representation 
  107. * 
  108. * Example: 0:0:0:0:0:0:13.1.68.3 
  109. * 0:0:0:0:0:FFFF:129.144.52.38 
  110. * 
  111. * @param string $ip An IPv6 address 
  112. * @return string[] [0] contains the IPv6 represented part, and [1] the IPv4 represented part 
  113. */ 
  114. protected static function split_v6_v4($ip) { 
  115. if (strpos($ip, '.') !== false) { 
  116. $pos = strrpos($ip, ':'); 
  117. $ipv6_part = substr($ip, 0, $pos); 
  118. $ipv4_part = substr($ip, $pos + 1); 
  119. return array($ipv6_part, $ipv4_part); 
  120. else { 
  121. return array($ip, ''); 
  122.  
  123. /** 
  124. * Checks an IPv6 address 
  125. * 
  126. * Checks if the given IP is a valid IPv6 address 
  127. * 
  128. * @param string $ip An IPv6 address 
  129. * @return bool true if $ip is a valid IPv6 address 
  130. */ 
  131. public static function check_ipv6($ip) { 
  132. $ip = self::uncompress($ip); 
  133. list($ipv6, $ipv4) = self::split_v6_v4($ip); 
  134. $ipv6 = explode(':', $ipv6); 
  135. $ipv4 = explode('.', $ipv4); 
  136. if (count($ipv6) === 8 && count($ipv4) === 1 || count($ipv6) === 6 && count($ipv4) === 4) { 
  137. foreach ($ipv6 as $ipv6_part) { 
  138. // The section can't be empty 
  139. if ($ipv6_part === '') { 
  140. return false; 
  141.  
  142. // Nor can it be over four characters 
  143. if (strlen($ipv6_part) > 4) { 
  144. return false; 
  145.  
  146. // Remove leading zeros (this is safe because of the above) 
  147. $ipv6_part = ltrim($ipv6_part, '0'); 
  148. if ($ipv6_part === '') { 
  149. $ipv6_part = '0'; 
  150.  
  151. // Check the value is valid 
  152. $value = hexdec($ipv6_part); 
  153. if (dechex($value) !== strtolower($ipv6_part) || $value < 0 || $value > 0xFFFF) { 
  154. return false; 
  155. if (count($ipv4) === 4) { 
  156. foreach ($ipv4 as $ipv4_part) { 
  157. $value = (int) $ipv4_part; 
  158. if ((string) $value !== $ipv4_part || $value < 0 || $value > 0xFF) { 
  159. return false; 
  160. return true; 
  161. else { 
  162. return false; 
.