]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/simplepie/SimplePie/Parser.php
Merge pull request #181 from inthepoche/dev
[github/wallabag/wallabag.git] / inc / 3rdparty / simplepie / SimplePie / Parser.php
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 class SimplePie_Parser
48 {
49 var $error_code;
50 var $error_string;
51 var $current_line;
52 var $current_column;
53 var $current_byte;
54 var $separator = ' ';
55 var $namespace = array('');
56 var $element = array('');
57 var $xml_base = array('');
58 var $xml_base_explicit = array(false);
59 var $xml_lang = array('');
60 var $data = array();
61 var $datas = array(array());
62 var $current_xhtml_construct = -1;
63 var $encoding;
64
65 public function parse(&$data, $encoding)
66 {
67 // Use UTF-8 if we get passed US-ASCII, as every US-ASCII character is a UTF-8 character
68 if (strtoupper($encoding) === 'US-ASCII')
69 {
70 $this->encoding = 'UTF-8';
71 }
72 else
73 {
74 $this->encoding = $encoding;
75 }
76
77 // Strip BOM:
78 // UTF-32 Big Endian BOM
79 if (substr($data, 0, 4) === "\x00\x00\xFE\xFF")
80 {
81 $data = substr($data, 4);
82 }
83 // UTF-32 Little Endian BOM
84 elseif (substr($data, 0, 4) === "\xFF\xFE\x00\x00")
85 {
86 $data = substr($data, 4);
87 }
88 // UTF-16 Big Endian BOM
89 elseif (substr($data, 0, 2) === "\xFE\xFF")
90 {
91 $data = substr($data, 2);
92 }
93 // UTF-16 Little Endian BOM
94 elseif (substr($data, 0, 2) === "\xFF\xFE")
95 {
96 $data = substr($data, 2);
97 }
98 // UTF-8 BOM
99 elseif (substr($data, 0, 3) === "\xEF\xBB\xBF")
100 {
101 $data = substr($data, 3);
102 }
103
104 if (substr($data, 0, 5) === '<?xml' && strspn(substr($data, 5, 1), "\x09\x0A\x0D\x20") && ($pos = strpos($data, '?>')) !== false)
105 {
106 $declaration = new SimplePie_XML_Declaration_Parser(substr($data, 5, $pos - 5));
107 if ($declaration->parse())
108 {
109 $data = substr($data, $pos + 2);
110 $data = '<?xml version="' . $declaration->version . '" encoding="' . $encoding . '" standalone="' . (($declaration->standalone) ? 'yes' : 'no') . '"?>' . $data;
111 }
112 else
113 {
114 $this->error_string = 'SimplePie bug! Please report this!';
115 return false;
116 }
117 }
118
119 $return = true;
120
121 static $xml_is_sane = null;
122 if ($xml_is_sane === null)
123 {
124 $parser_check = xml_parser_create();
125 xml_parse_into_struct($parser_check, '<foo>&amp;</foo>', $values);
126 xml_parser_free($parser_check);
127 $xml_is_sane = isset($values[0]['value']);
128 }
129
130 // Create the parser
131 if ($xml_is_sane)
132 {
133 $xml = xml_parser_create_ns($this->encoding, $this->separator);
134 xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE, 1);
135 xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, 0);
136 xml_set_object($xml, $this);
137 xml_set_character_data_handler($xml, 'cdata');
138 xml_set_element_handler($xml, 'tag_open', 'tag_close');
139
140 // Parse!
141 if (!xml_parse($xml, $data, true))
142 {
143 $this->error_code = xml_get_error_code($xml);
144 $this->error_string = xml_error_string($this->error_code);
145 $return = false;
146 }
147 $this->current_line = xml_get_current_line_number($xml);
148 $this->current_column = xml_get_current_column_number($xml);
149 $this->current_byte = xml_get_current_byte_index($xml);
150 xml_parser_free($xml);
151 return $return;
152 }
153 else
154 {
155 libxml_clear_errors();
156 $xml = new XMLReader();
157 $xml->xml($data);
158 while (@$xml->read())
159 {
160 switch ($xml->nodeType)
161 {
162
163 case constant('XMLReader::END_ELEMENT'):
164 if ($xml->namespaceURI !== '')
165 {
166 $tagName = $xml->namespaceURI . $this->separator . $xml->localName;
167 }
168 else
169 {
170 $tagName = $xml->localName;
171 }
172 $this->tag_close(null, $tagName);
173 break;
174 case constant('XMLReader::ELEMENT'):
175 $empty = $xml->isEmptyElement;
176 if ($xml->namespaceURI !== '')
177 {
178 $tagName = $xml->namespaceURI . $this->separator . $xml->localName;
179 }
180 else
181 {
182 $tagName = $xml->localName;
183 }
184 $attributes = array();
185 while ($xml->moveToNextAttribute())
186 {
187 if ($xml->namespaceURI !== '')
188 {
189 $attrName = $xml->namespaceURI . $this->separator . $xml->localName;
190 }
191 else
192 {
193 $attrName = $xml->localName;
194 }
195 $attributes[$attrName] = $xml->value;
196 }
197 $this->tag_open(null, $tagName, $attributes);
198 if ($empty)
199 {
200 $this->tag_close(null, $tagName);
201 }
202 break;
203 case constant('XMLReader::TEXT'):
204
205 case constant('XMLReader::CDATA'):
206 $this->cdata(null, $xml->value);
207 break;
208 }
209 }
210 if ($error = libxml_get_last_error())
211 {
212 $this->error_code = $error->code;
213 $this->error_string = $error->message;
214 $this->current_line = $error->line;
215 $this->current_column = $error->column;
216 return false;
217 }
218 else
219 {
220 return true;
221 }
222 }
223 }
224
225 public function get_error_code()
226 {
227 return $this->error_code;
228 }
229
230 public function get_error_string()
231 {
232 return $this->error_string;
233 }
234
235 public function get_current_line()
236 {
237 return $this->current_line;
238 }
239
240 public function get_current_column()
241 {
242 return $this->current_column;
243 }
244
245 public function get_current_byte()
246 {
247 return $this->current_byte;
248 }
249
250 public function get_data()
251 {
252 return $this->data;
253 }
254
255 public function tag_open($parser, $tag, $attributes)
256 {
257 list($this->namespace[], $this->element[]) = $this->split_ns($tag);
258
259 $attribs = array();
260 foreach ($attributes as $name => $value)
261 {
262 list($attrib_namespace, $attribute) = $this->split_ns($name);
263 $attribs[$attrib_namespace][$attribute] = $value;
264 }
265
266 if (isset($attribs[SIMPLEPIE_NAMESPACE_XML]['base']))
267 {
268 $this->xml_base[] = SimplePie_Misc::absolutize_url($attribs[SIMPLEPIE_NAMESPACE_XML]['base'], end($this->xml_base));
269 $this->xml_base_explicit[] = true;
270 }
271 else
272 {
273 $this->xml_base[] = end($this->xml_base);
274 $this->xml_base_explicit[] = end($this->xml_base_explicit);
275 }
276
277 if (isset($attribs[SIMPLEPIE_NAMESPACE_XML]['lang']))
278 {
279 $this->xml_lang[] = $attribs[SIMPLEPIE_NAMESPACE_XML]['lang'];
280 }
281 else
282 {
283 $this->xml_lang[] = end($this->xml_lang);
284 }
285
286 if ($this->current_xhtml_construct >= 0)
287 {
288 $this->current_xhtml_construct++;
289 if (end($this->namespace) === SIMPLEPIE_NAMESPACE_XHTML)
290 {
291 $this->data['data'] .= '<' . end($this->element);
292 if (isset($attribs['']))
293 {
294 foreach ($attribs[''] as $name => $value)
295 {
296 $this->data['data'] .= ' ' . $name . '="' . htmlspecialchars($value, ENT_COMPAT, $this->encoding) . '"';
297 }
298 }
299 $this->data['data'] .= '>';
300 }
301 }
302 else
303 {
304 $this->datas[] =& $this->data;
305 $this->data =& $this->data['child'][end($this->namespace)][end($this->element)][];
306 $this->data = array('data' => '', 'attribs' => $attribs, 'xml_base' => end($this->xml_base), 'xml_base_explicit' => end($this->xml_base_explicit), 'xml_lang' => end($this->xml_lang));
307 if ((end($this->namespace) === SIMPLEPIE_NAMESPACE_ATOM_03 && in_array(end($this->element), array('title', 'tagline', 'copyright', 'info', 'summary', 'content')) && isset($attribs['']['mode']) && $attribs['']['mode'] === 'xml')
308 || (end($this->namespace) === SIMPLEPIE_NAMESPACE_ATOM_10 && in_array(end($this->element), array('rights', 'subtitle', 'summary', 'info', 'title', 'content')) && isset($attribs['']['type']) && $attribs['']['type'] === 'xhtml'))
309 {
310 $this->current_xhtml_construct = 0;
311 }
312 }
313 }
314
315 public function cdata($parser, $cdata)
316 {
317 if ($this->current_xhtml_construct >= 0)
318 {
319 $this->data['data'] .= htmlspecialchars($cdata, ENT_QUOTES, $this->encoding);
320 }
321 else
322 {
323 $this->data['data'] .= $cdata;
324 }
325 }
326
327 public function tag_close($parser, $tag)
328 {
329 if ($this->current_xhtml_construct >= 0)
330 {
331 $this->current_xhtml_construct--;
332 if (end($this->namespace) === SIMPLEPIE_NAMESPACE_XHTML && !in_array(end($this->element), array('area', 'base', 'basefont', 'br', 'col', 'frame', 'hr', 'img', 'input', 'isindex', 'link', 'meta', 'param')))
333 {
334 $this->data['data'] .= '</' . end($this->element) . '>';
335 }
336 }
337 if ($this->current_xhtml_construct === -1)
338 {
339 $this->data =& $this->datas[count($this->datas) - 1];
340 array_pop($this->datas);
341 }
342
343 array_pop($this->element);
344 array_pop($this->namespace);
345 array_pop($this->xml_base);
346 array_pop($this->xml_base_explicit);
347 array_pop($this->xml_lang);
348 }
349
350 public function split_ns($string)
351 {
352 static $cache = array();
353 if (!isset($cache[$string]))
354 {
355 if ($pos = strpos($string, $this->separator))
356 {
357 static $separator_length;
358 if (!$separator_length)
359 {
360 $separator_length = strlen($this->separator);
361 }
362 $namespace = substr($string, 0, $pos);
363 $local_name = substr($string, $pos + $separator_length);
364 if (strtolower($namespace) === SIMPLEPIE_NAMESPACE_ITUNES)
365 {
366 $namespace = SIMPLEPIE_NAMESPACE_ITUNES;
367 }
368
369 // Normalize the Media RSS namespaces
370 if ($namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG ||
371 $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG2 ||
372 $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG3 ||
373 $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG4 ||
374 $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG5 )
375 {
376 $namespace = SIMPLEPIE_NAMESPACE_MEDIARSS;
377 }
378 $cache[$string] = array($namespace, $local_name);
379 }
380 else
381 {
382 $cache[$string] = array('', $string);
383 }
384 }
385 return $cache[$string];
386 }
387 }