PageRenderTime 28ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/simplepie/SimplePie/Net/IPv6.php

https://bitbucket.org/timgws/full-text-rss
PHP | 258 lines | 144 code | 7 blank | 107 comment | 25 complexity | 9ee0b9bc70e442ad440a67009da22756 MD5 | raw file
  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-2009, 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.3-dev
  37. * @copyright 2004-2010 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. * @todo phpDoc comments
  44. */
  45. /**
  46. * Class to validate and to work with IPv6 addresses.
  47. *
  48. * @package SimplePie
  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. * Removes a possible existing netmask specification of an IP address.
  61. *
  62. * @param string $ip the (compressed) IP as Hex representation
  63. * @return string the IP the without netmask
  64. * @since 1.1.0
  65. * @access public
  66. * @static
  67. */
  68. public static function removeNetmaskSpec($ip)
  69. {
  70. if (strpos($ip, '/') !== false)
  71. {
  72. list($addr, $nm) = explode('/', $ip);
  73. }
  74. else
  75. {
  76. $addr = $ip;
  77. }
  78. return $addr;
  79. }
  80. /**
  81. * Uncompresses an IPv6 address
  82. *
  83. * RFC 2373 allows you to compress zeros in an address to '::'. This
  84. * function expects an valid IPv6 address and expands the '::' to
  85. * the required zeros.
  86. *
  87. * Example: FF01::101 -> FF01:0:0:0:0:0:0:101
  88. * ::1 -> 0:0:0:0:0:0:0:1
  89. *
  90. * @access public
  91. * @static
  92. * @param string $ip a valid IPv6-address (hex format)
  93. * @return string the uncompressed IPv6-address (hex format)
  94. */
  95. public static function Uncompress($ip)
  96. {
  97. $uip = SimplePie_Net_IPv6::removeNetmaskSpec($ip);
  98. $c1 = -1;
  99. $c2 = -1;
  100. if (strpos($ip, '::') !== false)
  101. {
  102. list($ip1, $ip2) = explode('::', $ip);
  103. if ($ip1 === '')
  104. {
  105. $c1 = -1;
  106. }
  107. else
  108. {
  109. $pos = 0;
  110. if (($pos = substr_count($ip1, ':')) > 0)
  111. {
  112. $c1 = $pos;
  113. }
  114. else
  115. {
  116. $c1 = 0;
  117. }
  118. }
  119. if ($ip2 === '')
  120. {
  121. $c2 = -1;
  122. }
  123. else
  124. {
  125. $pos = 0;
  126. if (($pos = substr_count($ip2, ':')) > 0)
  127. {
  128. $c2 = $pos;
  129. }
  130. else
  131. {
  132. $c2 = 0;
  133. }
  134. }
  135. if (strstr($ip2, '.'))
  136. {
  137. $c2++;
  138. }
  139. // ::
  140. if ($c1 === -1 && $c2 === -1)
  141. {
  142. $uip = '0:0:0:0:0:0:0:0';
  143. }
  144. // ::xxx
  145. else if ($c1 === -1)
  146. {
  147. $fill = str_repeat('0:', 7 - $c2);
  148. $uip = str_replace('::', $fill, $uip);
  149. }
  150. // xxx::
  151. else if ($c2 === -1)
  152. {
  153. $fill = str_repeat(':0', 7 - $c1);
  154. $uip = str_replace('::', $fill, $uip);
  155. }
  156. // xxx::xxx
  157. else
  158. {
  159. $fill = str_repeat(':0:', 6 - $c2 - $c1);
  160. $uip = str_replace('::', $fill, $uip);
  161. $uip = str_replace('::', ':', $uip);
  162. }
  163. }
  164. return $uip;
  165. }
  166. /**
  167. * Splits an IPv6 address into the IPv6 and a possible IPv4 part
  168. *
  169. * RFC 2373 allows you to note the last two parts of an IPv6 address as
  170. * an IPv4 compatible address
  171. *
  172. * Example: 0:0:0:0:0:0:13.1.68.3
  173. * 0:0:0:0:0:FFFF:129.144.52.38
  174. *
  175. * @access public
  176. * @static
  177. * @param string $ip a valid IPv6-address (hex format)
  178. * @return array [0] contains the IPv6 part, [1] the IPv4 part (hex format)
  179. */
  180. public static function SplitV64($ip)
  181. {
  182. $ip = SimplePie_Net_IPv6::Uncompress($ip);
  183. if (strstr($ip, '.'))
  184. {
  185. $pos = strrpos($ip, ':');
  186. $ip[$pos] = '_';
  187. $ipPart = explode('_', $ip);
  188. return $ipPart;
  189. }
  190. else
  191. {
  192. return array($ip, '');
  193. }
  194. }
  195. /**
  196. * Checks an IPv6 address
  197. *
  198. * Checks if the given IP is IPv6-compatible
  199. *
  200. * @access public
  201. * @static
  202. * @param string $ip a valid IPv6-address
  203. * @return bool true if $ip is an IPv6 address
  204. */
  205. public static function checkIPv6($ip)
  206. {
  207. $ipPart = SimplePie_Net_IPv6::SplitV64($ip);
  208. $count = 0;
  209. if (!empty($ipPart[0]))
  210. {
  211. $ipv6 = explode(':', $ipPart[0]);
  212. for ($i = 0; $i < count($ipv6); $i++)
  213. {
  214. $dec = hexdec($ipv6[$i]);
  215. $hex = strtoupper(preg_replace('/^[0]{1,3}(.*[0-9a-fA-F])$/', '\\1', $ipv6[$i]));
  216. if ($ipv6[$i] >= 0 && $dec <= 65535 && $hex === strtoupper(dechex($dec)))
  217. {
  218. $count++;
  219. }
  220. }
  221. if ($count === 8)
  222. {
  223. return true;
  224. }
  225. elseif ($count === 6 && !empty($ipPart[1]))
  226. {
  227. $ipv4 = explode('.', $ipPart[1]);
  228. $count = 0;
  229. foreach ($ipv4 as $ipv4_part)
  230. {
  231. if ($ipv4_part >= 0 && $ipv4_part <= 255 && preg_match('/^\d{1,3}$/', $ipv4_part))
  232. {
  233. $count++;
  234. }
  235. }
  236. if ($count === 4)
  237. {
  238. return true;
  239. }
  240. }
  241. else
  242. {
  243. return false;
  244. }
  245. }
  246. else
  247. {
  248. return false;
  249. }
  250. }
  251. }