]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/simplepie/SimplePie/Net/IPv6.php
5 * A PHP-Based RSS and Atom Feed Framework.
6 * Takes the hard work out of managing a complete RSS/Atom solution.
8 * Copyright (c) 2004-2009, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
11 * Redistribution and use in source and binary forms, with or without modification, are
12 * permitted provided that the following conditions are met:
14 * * Redistributions of source code must retain the above copyright notice, this list of
15 * conditions and the following disclaimer.
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.
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
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.
37 * @copyright 2004-2010 Ryan Parman, Geoffrey Sneddon, Ryan McCue
39 * @author Geoffrey Sneddon
41 * @link http://simplepie.org/ SimplePie
42 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 * @todo phpDoc comments
48 * Class to validate and to work with IPv6 addresses.
51 * @copyright 2003-2005 The PHP Group
52 * @license http://www.opensource.org/licenses/bsd-license.php
53 * @link http://pear.php.net/package/Net_IPv6
54 * @author Alexander Merz <alexander.merz@web.de>
55 * @author elfrink at introweb dot nl
56 * @author Josh Peck <jmp at joshpeck dot org>
57 * @author Geoffrey Sneddon <geoffers@gmail.com>
59 class SimplePie_Net_IPv6
62 * Removes a possible existing netmask specification of an IP address.
64 * @param string $ip the (compressed) IP as Hex representation
65 * @return string the IP the without netmask
70 public static function removeNetmaskSpec($ip)
72 if (strpos($ip, '/') !== false)
74 list($addr, $nm) = explode('/', $ip);
84 * Uncompresses an IPv6 address
86 * RFC 2373 allows you to compress zeros in an address to '::'. This
87 * function expects an valid IPv6 address and expands the '::' to
90 * Example: FF01::101 -> FF01:0:0:0:0:0:0:101
91 * ::1 -> 0:0:0:0:0:0:0:1
95 * @param string $ip a valid IPv6-address (hex format)
96 * @return string the uncompressed IPv6-address (hex format)
98 public static function Uncompress($ip)
100 $uip = SimplePie_Net_IPv6
::removeNetmaskSpec($ip);
103 if (strpos($ip, '::') !== false)
105 list($ip1, $ip2) = explode('::', $ip);
113 if (($pos = substr_count($ip1, ':')) > 0)
129 if (($pos = substr_count($ip2, ':')) > 0)
138 if (strstr($ip2, '.'))
143 if ($c1 === -1 && $c2 === -1)
145 $uip = '0:0:0:0:0:0:0:0';
150 $fill = str_repeat('0:', 7 - $c2);
151 $uip = str_replace('::', $fill, $uip);
156 $fill = str_repeat(':0', 7 - $c1);
157 $uip = str_replace('::', $fill, $uip);
162 $fill = str_repeat(':0:', 6 - $c2 - $c1);
163 $uip = str_replace('::', $fill, $uip);
164 $uip = str_replace('::', ':', $uip);
171 * Splits an IPv6 address into the IPv6 and a possible IPv4 part
173 * RFC 2373 allows you to note the last two parts of an IPv6 address as
174 * an IPv4 compatible address
176 * Example: 0:0:0:0:0:0:13.1.68.3
177 * 0:0:0:0:0:FFFF:129.144.52.38
181 * @param string $ip a valid IPv6-address (hex format)
182 * @return array [0] contains the IPv6 part, [1] the IPv4 part (hex format)
184 public static function SplitV64($ip)
186 $ip = SimplePie_Net_IPv6
::Uncompress($ip);
187 if (strstr($ip, '.'))
189 $pos = strrpos($ip, ':');
191 $ipPart = explode('_', $ip);
196 return array($ip, '');
201 * Checks an IPv6 address
203 * Checks if the given IP is IPv6-compatible
207 * @param string $ip a valid IPv6-address
208 * @return bool true if $ip is an IPv6 address
210 public static function checkIPv6($ip)
212 $ipPart = SimplePie_Net_IPv6
::SplitV64($ip);
214 if (!empty($ipPart[0]))
216 $ipv6 = explode(':', $ipPart[0]);
217 for ($i = 0; $i < count($ipv6); $i++
)
219 $dec = hexdec($ipv6[$i]);
220 $hex = strtoupper(preg_replace('/^[0]{1,3}(.*[0-9a-fA-F])$/', '\\1', $ipv6[$i]));
221 if ($ipv6[$i] >= 0 && $dec <= 65535 && $hex === strtoupper(dechex($dec)))
230 elseif ($count === 6 && !empty($ipPart[1]))
232 $ipv4 = explode('.', $ipPart[1]);
234 foreach ($ipv4 as $ipv4_part)
236 if ($ipv4_part >= 0 && $ipv4_part <= 255 && preg_match('/^\d{1,3}$/', $ipv4_part))