SimplePie_Net_IPv6

Class to validate and to work with IPv6 addresses.

Defined (1)

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

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