PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/SimplePie/Parser.php

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