Parser.php 12 KB

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