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