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
47 class SimplePie_Parser
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('');
61 var $datas = array(array());
62 var $current_xhtml_construct = -1;
65 public function parse(&$data, $encoding)
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')
70 $this->encoding
= 'UTF-8';
74 $this->encoding
= $encoding;
78 // UTF-32 Big Endian BOM
79 if (substr($data, 0, 4) === "\x00\x00\xFE\xFF")
81 $data = substr($data, 4);
83 // UTF-32 Little Endian BOM
84 elseif (substr($data, 0, 4) === "\xFF\xFE\x00\x00")
86 $data = substr($data, 4);
88 // UTF-16 Big Endian BOM
89 elseif (substr($data, 0, 2) === "\xFE\xFF")
91 $data = substr($data, 2);
93 // UTF-16 Little Endian BOM
94 elseif (substr($data, 0, 2) === "\xFF\xFE")
96 $data = substr($data, 2);
99 elseif (substr($data, 0, 3) === "\xEF\xBB\xBF")
101 $data = substr($data, 3);
104 if (substr($data, 0, 5) === '<?xml' && strspn(substr($data, 5, 1), "\x09\x0A\x0D\x20") && ($pos = strpos($data, '?>')) !== false)
106 $declaration = new SimplePie_XML_Declaration_Parser(substr($data, 5, $pos - 5));
107 if ($declaration->parse())
109 $data = substr($data, $pos +
2);
110 $data = '<?xml version="' . $declaration->version
. '" encoding="' . $encoding . '" standalone="' . (($declaration->standalone
) ? 'yes' : 'no') . '"?>' . $data;
114 $this->error_string
= 'SimplePie bug! Please report this!';
121 static $xml_is_sane = null;
122 if ($xml_is_sane === null)
124 $parser_check = xml_parser_create();
125 xml_parse_into_struct($parser_check, '<foo>&</foo>', $values);
126 xml_parser_free($parser_check);
127 $xml_is_sane = isset($values[0]['value']);
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');
141 if (!xml_parse($xml, $data, true))
143 $this->error_code
= xml_get_error_code($xml);
144 $this->error_string
= xml_error_string($this->error_code
);
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);
155 libxml_clear_errors();
156 $xml = new XMLReader();
158 while (@$xml->read())
160 switch ($xml->nodeType
)
163 case constant('XMLReader::END_ELEMENT'):
164 if ($xml->namespaceURI
!== '')
166 $tagName = $xml->namespaceURI
. $this->separator
. $xml->localName
;
170 $tagName = $xml->localName
;
172 $this->tag_close(null, $tagName);
174 case constant('XMLReader::ELEMENT'):
175 $empty = $xml->isEmptyElement
;
176 if ($xml->namespaceURI
!== '')
178 $tagName = $xml->namespaceURI
. $this->separator
. $xml->localName
;
182 $tagName = $xml->localName
;
184 $attributes = array();
185 while ($xml->moveToNextAttribute())
187 if ($xml->namespaceURI
!== '')
189 $attrName = $xml->namespaceURI
. $this->separator
. $xml->localName
;
193 $attrName = $xml->localName
;
195 $attributes[$attrName] = $xml->value
;
197 $this->tag_open(null, $tagName, $attributes);
200 $this->tag_close(null, $tagName);
203 case constant('XMLReader::TEXT'):
205 case constant('XMLReader::CDATA'):
206 $this->cdata(null, $xml->value
);
210 if ($error = libxml_get_last_error())
212 $this->error_code
= $error->code
;
213 $this->error_string
= $error->message
;
214 $this->current_line
= $error->line
;
215 $this->current_column
= $error->column
;
225 public function get_error_code()
227 return $this->error_code
;
230 public function get_error_string()
232 return $this->error_string
;
235 public function get_current_line()
237 return $this->current_line
;
240 public function get_current_column()
242 return $this->current_column
;
245 public function get_current_byte()
247 return $this->current_byte
;
250 public function get_data()
255 public function tag_open($parser, $tag, $attributes)
257 list($this->namespace[], $this->element
[]) = $this->split_ns($tag);
260 foreach ($attributes as $name => $value)
262 list($attrib_namespace, $attribute) = $this->split_ns($name);
263 $attribs[$attrib_namespace][$attribute] = $value;
266 if (isset($attribs[SIMPLEPIE_NAMESPACE_XML
]['base']))
268 $this->xml_base
[] = SimplePie_Misc
::absolutize_url($attribs[SIMPLEPIE_NAMESPACE_XML
]['base'], end($this->xml_base
));
269 $this->xml_base_explicit
[] = true;
273 $this->xml_base
[] = end($this->xml_base
);
274 $this->xml_base_explicit
[] = end($this->xml_base_explicit
);
277 if (isset($attribs[SIMPLEPIE_NAMESPACE_XML
]['lang']))
279 $this->xml_lang
[] = $attribs[SIMPLEPIE_NAMESPACE_XML
]['lang'];
283 $this->xml_lang
[] = end($this->xml_lang
);
286 if ($this->current_xhtml_construct
>= 0)
288 $this->current_xhtml_construct++
;
289 if (end($this->namespace) === SIMPLEPIE_NAMESPACE_XHTML
)
291 $this->data
['data'] .= '<' . end($this->element
);
292 if (isset($attribs['']))
294 foreach ($attribs[''] as $name => $value)
296 $this->data
['data'] .= ' ' . $name . '="' . htmlspecialchars($value, ENT_COMPAT
, $this->encoding
) . '"';
299 $this->data
['data'] .= '>';
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'))
310 $this->current_xhtml_construct
= 0;
315 public function cdata($parser, $cdata)
317 if ($this->current_xhtml_construct
>= 0)
319 $this->data
['data'] .= htmlspecialchars($cdata, ENT_QUOTES
, $this->encoding
);
323 $this->data
['data'] .= $cdata;
327 public function tag_close($parser, $tag)
329 if ($this->current_xhtml_construct
>= 0)
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')))
334 $this->data
['data'] .= '</' . end($this->element
) . '>';
337 if ($this->current_xhtml_construct
=== -1)
339 $this->data
=& $this->datas
[count($this->datas
) - 1];
340 array_pop($this->datas
);
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
);
350 public function split_ns($string)
352 static $cache = array();
353 if (!isset($cache[$string]))
355 if ($pos = strpos($string, $this->separator
))
357 static $separator_length;
358 if (!$separator_length)
360 $separator_length = strlen($this->separator
);
362 $namespace = substr($string, 0, $pos);
363 $local_name = substr($string, $pos +
$separator_length);
364 if (strtolower($namespace) === SIMPLEPIE_NAMESPACE_ITUNES
)
366 $namespace = SIMPLEPIE_NAMESPACE_ITUNES
;
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
)
376 $namespace = SIMPLEPIE_NAMESPACE_MEDIARSS
;
378 $cache[$string] = array($namespace, $local_name);
382 $cache[$string] = array('', $string);
385 return $cache[$string];