PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/library/SimplePie/Net/IPv6.php

https://github.com/elantrix/simplepie
PHP | 276 lines | 136 code | 13 blank | 127 comment | 28 complexity | a8af969e3755a7af0af1f22bf460a8b3 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * SimplePie
  4. *
  5. * A PHP-Based RSS and Atom Feed Framework.
  6. * Takes the hard work out of managing a complete RSS/Atom solution.
  7. *
  8. * Copyright (c) 2004-2012, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification, are
  12. * permitted provided that the following conditions are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright notice, this list of
  15. * conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright notice, this list
  18. * of conditions and the following disclaimer in the documentation and/or other materials
  19. * provided with the distribution.
  20. *
  21. * * Neither the name of the SimplePie Team nor the names of its contributors may be used
  22. * to endorse or promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  26. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  27. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
  28. * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  32. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * @package SimplePie
  36. * @version 1.4-dev
  37. * @copyright 2004-2012 Ryan Parman, Geoffrey Sneddon, Ryan McCue
  38. * @author Ryan Parman
  39. * @author Geoffrey Sneddon
  40. * @author Ryan McCue
  41. * @link http://simplepie.org/ SimplePie
  42. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  43. */
  44. /**
  45. * Class to validate and to work with IPv6 addresses.
  46. *
  47. * @package SimplePie
  48. * @subpackage HTTP
  49. * @copyright 2003-2005 The PHP Group
  50. * @license http://www.opensource.org/licenses/bsd-license.php
  51. * @link http://pear.php.net/package/Net_IPv6
  52. * @author Alexander Merz <alexander.merz@web.de>
  53. * @author elfrink at introweb dot nl
  54. * @author Josh Peck <jmp at joshpeck dot org>
  55. * @author Geoffrey Sneddon <geoffers@gmail.com>
  56. */
  57. class SimplePie_Net_IPv6
  58. {
  59. /**
  60. * Uncompresses an IPv6 address
  61. *
  62. * RFC 4291 allows you to compress concecutive zero pieces in an address to
  63. * '::'. This method expects a valid IPv6 address and expands the '::' to
  64. * the required number of zero pieces.
  65. *
  66. * Example: FF01::101 -> FF01:0:0:0:0:0:0:101
  67. * ::1 -> 0:0:0:0:0:0:0:1
  68. *
  69. * @author Alexander Merz <alexander.merz@web.de>
  70. * @author elfrink at introweb dot nl
  71. * @author Josh Peck <jmp at joshpeck dot org>
  72. * @copyright 2003-2005 The PHP Group
  73. * @license http://www.opensource.org/licenses/bsd-license.php
  74. * @param string $ip An IPv6 address
  75. * @return string The uncompressed IPv6 address
  76. */
  77. public static function uncompress($ip)
  78. {
  79. $c1 = -1;
  80. $c2 = -1;
  81. if (substr_count($ip, '::') === 1)
  82. {
  83. list($ip1, $ip2) = explode('::', $ip);
  84. if ($ip1 === '')
  85. {
  86. $c1 = -1;
  87. }
  88. else
  89. {
  90. $c1 = substr_count($ip1, ':');
  91. }
  92. if ($ip2 === '')
  93. {
  94. $c2 = -1;
  95. }
  96. else
  97. {
  98. $c2 = substr_count($ip2, ':');
  99. }
  100. if (strpos($ip2, '.') !== false)
  101. {
  102. $c2++;
  103. }
  104. // ::
  105. if ($c1 === -1 && $c2 === -1)
  106. {
  107. $ip = '0:0:0:0:0:0:0:0';
  108. }
  109. // ::xxx
  110. else if ($c1 === -1)
  111. {
  112. $fill = str_repeat('0:', 7 - $c2);
  113. $ip = str_replace('::', $fill, $ip);
  114. }
  115. // xxx::
  116. else if ($c2 === -1)
  117. {
  118. $fill = str_repeat(':0', 7 - $c1);
  119. $ip = str_replace('::', $fill, $ip);
  120. }
  121. // xxx::xxx
  122. else
  123. {
  124. $fill = ':' . str_repeat('0:', 6 - $c2 - $c1);
  125. $ip = str_replace('::', $fill, $ip);
  126. }
  127. }
  128. return $ip;
  129. }
  130. /**
  131. * Compresses an IPv6 address
  132. *
  133. * RFC 4291 allows you to compress concecutive zero pieces in an address to
  134. * '::'. This method expects a valid IPv6 address and compresses consecutive
  135. * zero pieces to '::'.
  136. *
  137. * Example: FF01:0:0:0:0:0:0:101 -> FF01::101
  138. * 0:0:0:0:0:0:0:1 -> ::1
  139. *
  140. * @see uncompress()
  141. * @param string $ip An IPv6 address
  142. * @return string The compressed IPv6 address
  143. */
  144. public static function compress($ip)
  145. {
  146. // Prepare the IP to be compressed
  147. $ip = self::uncompress($ip);
  148. $ip_parts = self::split_v6_v4($ip);
  149. // Replace all leading zeros
  150. $ip_parts[0] = preg_replace('/(^|:)0+([0-9])/', '\1\2', $ip_parts[0]);
  151. // Find bunches of zeros
  152. if (preg_match_all('/(?:^|:)(?:0(?::|$))+/', $ip_parts[0], $matches, PREG_OFFSET_CAPTURE))
  153. {
  154. $max = 0;
  155. $pos = null;
  156. foreach ($matches[0] as $match)
  157. {
  158. if (strlen($match[0]) > $max)
  159. {
  160. $max = strlen($match[0]);
  161. $pos = $match[1];
  162. }
  163. }
  164. $ip_parts[0] = substr_replace($ip_parts[0], '::', $pos, $max);
  165. }
  166. if ($ip_parts[1] !== '')
  167. {
  168. return implode(':', $ip_parts);
  169. }
  170. else
  171. {
  172. return $ip_parts[0];
  173. }
  174. }
  175. /**
  176. * Splits an IPv6 address into the IPv6 and IPv4 representation parts
  177. *
  178. * RFC 4291 allows you to represent the last two parts of an IPv6 address
  179. * using the standard IPv4 representation
  180. *
  181. * Example: 0:0:0:0:0:0:13.1.68.3
  182. * 0:0:0:0:0:FFFF:129.144.52.38
  183. *
  184. * @param string $ip An IPv6 address
  185. * @return array [0] contains the IPv6 represented part, and [1] the IPv4 represented part
  186. */
  187. private static function split_v6_v4($ip)
  188. {
  189. if (strpos($ip, '.') !== false)
  190. {
  191. $pos = strrpos($ip, ':');
  192. $ipv6_part = substr($ip, 0, $pos);
  193. $ipv4_part = substr($ip, $pos + 1);
  194. return array($ipv6_part, $ipv4_part);
  195. }
  196. else
  197. {
  198. return array($ip, '');
  199. }
  200. }
  201. /**
  202. * Checks an IPv6 address
  203. *
  204. * Checks if the given IP is a valid IPv6 address
  205. *
  206. * @param string $ip An IPv6 address
  207. * @return bool true if $ip is a valid IPv6 address
  208. */
  209. public static function check_ipv6($ip)
  210. {
  211. $ip = self::uncompress($ip);
  212. list($ipv6, $ipv4) = self::split_v6_v4($ip);
  213. $ipv6 = explode(':', $ipv6);
  214. $ipv4 = explode('.', $ipv4);
  215. if (count($ipv6) === 8 && count($ipv4) === 1 || count($ipv6) === 6 && count($ipv4) === 4)
  216. {
  217. foreach ($ipv6 as $ipv6_part)
  218. {
  219. // The section can't be empty
  220. if ($ipv6_part === '')
  221. return false;
  222. // Nor can it be over four characters
  223. if (strlen($ipv6_part) > 4)
  224. return false;
  225. // Remove leading zeros (this is safe because of the above)
  226. $ipv6_part = ltrim($ipv6_part, '0');
  227. if ($ipv6_part === '')
  228. $ipv6_part = '0';
  229. // Check the value is valid
  230. $value = hexdec($ipv6_part);
  231. if (dechex($value) !== strtolower($ipv6_part) || $value < 0 || $value > 0xFFFF)
  232. return false;
  233. }
  234. if (count($ipv4) === 4)
  235. {
  236. foreach ($ipv4 as $ipv4_part)
  237. {
  238. $value = (int) $ipv4_part;
  239. if ((string) $value !== $ipv4_part || $value < 0 || $value > 0xFF)
  240. return false;
  241. }
  242. }
  243. return true;
  244. }
  245. else
  246. {
  247. return false;
  248. }
  249. }
  250. /**
  251. * Checks if the given IP is a valid IPv6 address
  252. *
  253. * @codeCoverageIgnore
  254. * @deprecated Use {@see SimplePie_Net_IPv6::check_ipv6()} instead
  255. * @see check_ipv6
  256. * @param string $ip An IPv6 address
  257. * @return bool true if $ip is a valid IPv6 address
  258. */
  259. public static function checkIPv6($ip)
  260. {
  261. return self::check_ipv6($ip);
  262. }
  263. }