PageRenderTime 48ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/packages/simplepie/classes/simplepie/net/ipv6.php

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