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-2012, 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-2012 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
46 * Parses XML into something sane
49 * This class can be overloaded with {@see SimplePie::set_parser_class()}
54 class SimplePie_Parser
62 var $namespace = array('');
63 var $element = array('');
64 var $xml_base = array('');
65 var $xml_base_explicit = array(false);
66 var $xml_lang = array('');
68 var $datas = array(array());
69 var $current_xhtml_construct = -1;
73 public function set_registry(SimplePie_Registry
$registry)
75 $this->registry
= $registry;
78 public function parse(&$data, $encoding)
80 // Use UTF-8 if we get passed US-ASCII, as every US-ASCII character is a UTF-8 character
81 if (strtoupper($encoding) === 'US-ASCII')
83 $this->encoding
= 'UTF-8';
87 $this->encoding
= $encoding;
91 // UTF-32 Big Endian BOM
92 if (substr($data, 0, 4) === "\x00\x00\xFE\xFF")
94 $data = substr($data, 4);
96 // UTF-32 Little Endian BOM
97 elseif (substr($data, 0, 4) === "\xFF\xFE\x00\x00")
99 $data = substr($data, 4);
101 // UTF-16 Big Endian BOM
102 elseif (substr($data, 0, 2) === "\xFE\xFF")
104 $data = substr($data, 2);
106 // UTF-16 Little Endian BOM
107 elseif (substr($data, 0, 2) === "\xFF\xFE")
109 $data = substr($data, 2);
112 elseif (substr($data, 0, 3) === "\xEF\xBB\xBF")
114 $data = substr($data, 3);
117 if (substr($data, 0, 5) === '<?xml' && strspn(substr($data, 5, 1), "\x09\x0A\x0D\x20") && ($pos = strpos($data, '?>')) !== false)
119 $declaration = $this->registry
->create('XML_Declaration_Parser', array(substr($data, 5, $pos - 5)));
120 if ($declaration->parse())
122 $data = substr($data, $pos +
2);
123 $data = '<?xml version="' . $declaration->version
. '" encoding="' . $encoding . '" standalone="' . (($declaration->standalone
) ? 'yes' : 'no') . '"?>' . $data;
127 $this->error_string
= 'SimplePie bug! Please report this!';
134 static $xml_is_sane = null;
135 if ($xml_is_sane === null)
137 $parser_check = xml_parser_create();
138 xml_parse_into_struct($parser_check, '<foo>&</foo>', $values);
139 xml_parser_free($parser_check);
140 $xml_is_sane = isset($values[0]['value']);
146 $xml = xml_parser_create_ns($this->encoding
, $this->separator
);
147 xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE
, 1);
148 xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING
, 0);
149 xml_set_object($xml, $this);
150 xml_set_character_data_handler($xml, 'cdata');
151 xml_set_element_handler($xml, 'tag_open', 'tag_close');
154 if (!xml_parse($xml, $data, true))
156 $this->error_code
= xml_get_error_code($xml);
157 $this->error_string
= xml_error_string($this->error_code
);
160 $this->current_line
= xml_get_current_line_number($xml);
161 $this->current_column
= xml_get_current_column_number($xml);
162 $this->current_byte
= xml_get_current_byte_index($xml);
163 xml_parser_free($xml);
168 libxml_clear_errors();
169 $xml = new XMLReader();
171 while (@$xml->read())
173 switch ($xml->nodeType
)
176 case constant('XMLReader::END_ELEMENT'):
177 if ($xml->namespaceURI
!== '')
179 $tagName = $xml->namespaceURI
. $this->separator
. $xml->localName
;
183 $tagName = $xml->localName
;
185 $this->tag_close(null, $tagName);
187 case constant('XMLReader::ELEMENT'):
188 $empty = $xml->isEmptyElement
;
189 if ($xml->namespaceURI
!== '')
191 $tagName = $xml->namespaceURI
. $this->separator
. $xml->localName
;
195 $tagName = $xml->localName
;
197 $attributes = array();
198 while ($xml->moveToNextAttribute())
200 if ($xml->namespaceURI
!== '')
202 $attrName = $xml->namespaceURI
. $this->separator
. $xml->localName
;
206 $attrName = $xml->localName
;
208 $attributes[$attrName] = $xml->value
;
210 $this->tag_open(null, $tagName, $attributes);
213 $this->tag_close(null, $tagName);
216 case constant('XMLReader::TEXT'):
218 case constant('XMLReader::CDATA'):
219 $this->cdata(null, $xml->value
);
223 if ($error = libxml_get_last_error())
225 $this->error_code
= $error->code
;
226 $this->error_string
= $error->message
;
227 $this->current_line
= $error->line
;
228 $this->current_column
= $error->column
;
238 public function get_error_code()
240 return $this->error_code
;
243 public function get_error_string()
245 return $this->error_string
;
248 public function get_current_line()
250 return $this->current_line
;
253 public function get_current_column()
255 return $this->current_column
;
258 public function get_current_byte()
260 return $this->current_byte
;
263 public function get_data()
268 public function tag_open($parser, $tag, $attributes)
270 list($this->namespace[], $this->element
[]) = $this->split_ns($tag);
273 foreach ($attributes as $name => $value)
275 list($attrib_namespace, $attribute) = $this->split_ns($name);
276 $attribs[$attrib_namespace][$attribute] = $value;
279 if (isset($attribs[SIMPLEPIE_NAMESPACE_XML
]['base']))
281 $base = $this->registry
->call('Misc', 'absolutize_url', array($attribs[SIMPLEPIE_NAMESPACE_XML
]['base'], end($this->xml_base
)));
284 $this->xml_base
[] = $base;
285 $this->xml_base_explicit
[] = true;
290 $this->xml_base
[] = end($this->xml_base
);
291 $this->xml_base_explicit
[] = end($this->xml_base_explicit
);
294 if (isset($attribs[SIMPLEPIE_NAMESPACE_XML
]['lang']))
296 $this->xml_lang
[] = $attribs[SIMPLEPIE_NAMESPACE_XML
]['lang'];
300 $this->xml_lang
[] = end($this->xml_lang
);
303 if ($this->current_xhtml_construct
>= 0)
305 $this->current_xhtml_construct++
;
306 if (end($this->namespace) === SIMPLEPIE_NAMESPACE_XHTML
)
308 $this->data
['data'] .= '<' . end($this->element
);
309 if (isset($attribs['']))
311 foreach ($attribs[''] as $name => $value)
313 $this->data
['data'] .= ' ' . $name . '="' . htmlspecialchars($value, ENT_COMPAT
, $this->encoding
) . '"';
316 $this->data
['data'] .= '>';
321 $this->datas
[] =& $this->data
;
322 $this->data
=& $this->data
['child'][end($this->namespace)][end($this->element
)][];
323 $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
));
324 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')
325 || (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')
326 || (end($this->namespace) === SIMPLEPIE_NAMESPACE_RSS_20
&& in_array(end($this->element
), array('title')))
327 || (end($this->namespace) === SIMPLEPIE_NAMESPACE_RSS_090
&& in_array(end($this->element
), array('title')))
328 || (end($this->namespace) === SIMPLEPIE_NAMESPACE_RSS_10
&& in_array(end($this->element
), array('title'))))
330 $this->current_xhtml_construct
= 0;
335 public function cdata($parser, $cdata)
337 if ($this->current_xhtml_construct
>= 0)
339 $this->data
['data'] .= htmlspecialchars($cdata, ENT_QUOTES
, $this->encoding
);
343 $this->data
['data'] .= $cdata;
347 public function tag_close($parser, $tag)
349 if ($this->current_xhtml_construct
>= 0)
351 $this->current_xhtml_construct
--;
352 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')))
354 $this->data
['data'] .= '</' . end($this->element
) . '>';
357 if ($this->current_xhtml_construct
=== -1)
359 $this->data
=& $this->datas
[count($this->datas
) - 1];
360 array_pop($this->datas
);
363 array_pop($this->element
);
364 array_pop($this->namespace);
365 array_pop($this->xml_base
);
366 array_pop($this->xml_base_explicit
);
367 array_pop($this->xml_lang
);
370 public function split_ns($string)
372 static $cache = array();
373 if (!isset($cache[$string]))
375 if ($pos = strpos($string, $this->separator
))
377 static $separator_length;
378 if (!$separator_length)
380 $separator_length = strlen($this->separator
);
382 $namespace = substr($string, 0, $pos);
383 $local_name = substr($string, $pos +
$separator_length);
384 if (strtolower($namespace) === SIMPLEPIE_NAMESPACE_ITUNES
)
386 $namespace = SIMPLEPIE_NAMESPACE_ITUNES
;
389 // Normalize the Media RSS namespaces
390 if ($namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG
||
391 $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG2
||
392 $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG3
||
393 $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG4
||
394 $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG5
)
396 $namespace = SIMPLEPIE_NAMESPACE_MEDIARSS
;
398 $cache[$string] = array($namespace, $local_name);
402 $cache[$string] = array('', $string);
405 return $cache[$string];