SimplePie_Parser

Parses XML into something sane.

Defined (1)

The class is defined in the following location(s).

/wp-includes/SimplePie/Parser.php  
  1. class SimplePie_Parser 
  2. var $error_code; 
  3. var $error_string; 
  4. var $current_line; 
  5. var $current_column; 
  6. var $current_byte; 
  7. var $separator = ' '; 
  8. var $namespace = array(''); 
  9. var $element = array(''); 
  10. var $xml_base = array(''); 
  11. var $xml_base_explicit = array(false); 
  12. var $xml_lang = array(''); 
  13. var $data = array(); 
  14. var $datas = array(array()); 
  15. var $current_xhtml_construct = -1; 
  16. var $encoding; 
  17. protected $registry; 
  18.  
  19. public function set_registry(SimplePie_Registry $registry) 
  20. $this->registry = $registry; 
  21.  
  22. public function parse(&$data, $encoding) 
  23. // Use UTF-8 if we get passed US-ASCII, as every US-ASCII character is a UTF-8 character 
  24. if (strtoupper($encoding) === 'US-ASCII') 
  25. $this->encoding = 'UTF-8'; 
  26. else 
  27. $this->encoding = $encoding; 
  28.  
  29. // Strip BOM: 
  30. // UTF-32 Big Endian BOM 
  31. if (substr($data, 0, 4) === "\x00\x00\xFE\xFF") 
  32. $data = substr($data, 4); 
  33. // UTF-32 Little Endian BOM 
  34. elseif (substr($data, 0, 4) === "\xFF\xFE\x00\x00") 
  35. $data = substr($data, 4); 
  36. // UTF-16 Big Endian BOM 
  37. elseif (substr($data, 0, 2) === "\xFE\xFF") 
  38. $data = substr($data, 2); 
  39. // UTF-16 Little Endian BOM 
  40. elseif (substr($data, 0, 2) === "\xFF\xFE") 
  41. $data = substr($data, 2); 
  42. // UTF-8 BOM 
  43. elseif (substr($data, 0, 3) === "\xEF\xBB\xBF") 
  44. $data = substr($data, 3); 
  45.  
  46. if (substr($data, 0, 5) === '<?xml' && strspn(substr($data, 5, 1), "\x09\x0A\x0D\x20") && ($pos = strpos($data, '?>')) !== false) 
  47. $declaration = $this->registry->create('XML_Declaration_Parser', array(substr($data, 5, $pos - 5))); 
  48. if ($declaration->parse()) 
  49. $data = substr($data, $pos + 2); 
  50. $data = '<?xml version="' . $declaration->version . '" encoding="' . $encoding . '" standalone="' . (($declaration->standalone) ? 'yes' : 'no') . '"?>' . $data; 
  51. else 
  52. $this->error_string = 'SimplePie bug! Please report this!'; 
  53. return false; 
  54.  
  55. $return = true; 
  56.  
  57. static $xml_is_sane = null; 
  58. if ($xml_is_sane === null) 
  59. $parser_check = xml_parser_create(); 
  60. xml_parse_into_struct($parser_check, '<foo>&</foo>', $values); 
  61. xml_parser_free($parser_check); 
  62. $xml_is_sane = isset($values[0]['value']); 
  63.  
  64. // Create the parser 
  65. if ($xml_is_sane) 
  66. $xml = xml_parser_create_ns($this->encoding, $this->separator); 
  67. xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE, 1); 
  68. xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, 0); 
  69. xml_set_object($xml, $this); 
  70. xml_set_character_data_handler($xml, 'cdata'); 
  71. xml_set_element_handler($xml, 'tag_open', 'tag_close'); 
  72.  
  73. // Parse! 
  74. if (!xml_parse($xml, $data, true)) 
  75. $this->error_code = xml_get_error_code($xml); 
  76. $this->error_string = xml_error_string($this->error_code); 
  77. $return = false; 
  78. $this->current_line = xml_get_current_line_number($xml); 
  79. $this->current_column = xml_get_current_column_number($xml); 
  80. $this->current_byte = xml_get_current_byte_index($xml); 
  81. xml_parser_free($xml); 
  82. return $return; 
  83. else 
  84. libxml_clear_errors(); 
  85. $xml = new XMLReader(); 
  86. $xml->xml($data); 
  87. while (@$xml->read()) 
  88. switch ($xml->nodeType) 
  89.  
  90. case constant('XMLReader::END_ELEMENT'): 
  91. if ($xml->namespaceURI !== '') 
  92. $tagName = $xml->namespaceURI . $this->separator . $xml->localName; 
  93. else 
  94. $tagName = $xml->localName; 
  95. $this->tag_close(null, $tagName); 
  96. break; 
  97. case constant('XMLReader::ELEMENT'): 
  98. $empty = $xml->isEmptyElement; 
  99. if ($xml->namespaceURI !== '') 
  100. $tagName = $xml->namespaceURI . $this->separator . $xml->localName; 
  101. else 
  102. $tagName = $xml->localName; 
  103. $attributes = array(); 
  104. while ($xml->moveToNextAttribute()) 
  105. if ($xml->namespaceURI !== '') 
  106. $attrName = $xml->namespaceURI . $this->separator . $xml->localName; 
  107. else 
  108. $attrName = $xml->localName; 
  109. $attributes[$attrName] = $xml->value; 
  110. $this->tag_open(null, $tagName, $attributes); 
  111. if ($empty) 
  112. $this->tag_close(null, $tagName); 
  113. break; 
  114. case constant('XMLReader::TEXT'): 
  115.  
  116. case constant('XMLReader::CDATA'): 
  117. $this->cdata(null, $xml->value); 
  118. break; 
  119. if ($error = libxml_get_last_error()) 
  120. $this->error_code = $error->code; 
  121. $this->error_string = $error->message; 
  122. $this->current_line = $error->line; 
  123. $this->current_column = $error->column; 
  124. return false; 
  125. else 
  126. return true; 
  127.  
  128. public function get_error_code() 
  129. return $this->error_code; 
  130.  
  131. public function get_error_string() 
  132. return $this->error_string; 
  133.  
  134. public function get_current_line() 
  135. return $this->current_line; 
  136.  
  137. public function get_current_column() 
  138. return $this->current_column; 
  139.  
  140. public function get_current_byte() 
  141. return $this->current_byte; 
  142.  
  143. public function get_data() 
  144. return $this->data; 
  145.  
  146. public function tag_open($parser, $tag, $attributes) 
  147. list($this->namespace[], $this->element[]) = $this->split_ns($tag); 
  148.  
  149. $attribs = array(); 
  150. foreach ($attributes as $name => $value) 
  151. list($attrib_namespace, $attribute) = $this->split_ns($name); 
  152. $attribs[$attrib_namespace][$attribute] = $value; 
  153.  
  154. if (isset($attribs[SIMPLEPIE_NAMESPACE_XML]['base'])) 
  155. $base = $this->registry->call('Misc', 'absolutize_url', array($attribs[SIMPLEPIE_NAMESPACE_XML]['base'], end($this->xml_base))); 
  156. if ($base !== false) 
  157. $this->xml_base[] = $base; 
  158. $this->xml_base_explicit[] = true; 
  159. else 
  160. $this->xml_base[] = end($this->xml_base); 
  161. $this->xml_base_explicit[] = end($this->xml_base_explicit); 
  162.  
  163. if (isset($attribs[SIMPLEPIE_NAMESPACE_XML]['lang'])) 
  164. $this->xml_lang[] = $attribs[SIMPLEPIE_NAMESPACE_XML]['lang']; 
  165. else 
  166. $this->xml_lang[] = end($this->xml_lang); 
  167.  
  168. if ($this->current_xhtml_construct >= 0) 
  169. $this->current_xhtml_construct++; 
  170. if (end($this->namespace) === SIMPLEPIE_NAMESPACE_XHTML) 
  171. $this->data['data'] .= '<' . end($this->element); 
  172. if (isset($attribs[''])) 
  173. foreach ($attribs[''] as $name => $value) 
  174. $this->data['data'] .= ' ' . $name . '="' . htmlspecialchars($value, ENT_COMPAT, $this->encoding) . '"'; 
  175. $this->data['data'] .= '>'; 
  176. else 
  177. $this->datas[] =& $this->data; 
  178. $this->data =& $this->data['child'][end($this->namespace)][end($this->element)][]; 
  179. $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)); 
  180. 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') 
  181. || (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') 
  182. || (end($this->namespace) === SIMPLEPIE_NAMESPACE_RSS_20 && in_array(end($this->element), array('title'))) 
  183. || (end($this->namespace) === SIMPLEPIE_NAMESPACE_RSS_090 && in_array(end($this->element), array('title'))) 
  184. || (end($this->namespace) === SIMPLEPIE_NAMESPACE_RSS_10 && in_array(end($this->element), array('title')))) 
  185. $this->current_xhtml_construct = 0; 
  186.  
  187. public function cdata($parser, $cdata) 
  188. if ($this->current_xhtml_construct >= 0) 
  189. $this->data['data'] .= htmlspecialchars($cdata, ENT_QUOTES, $this->encoding); 
  190. else 
  191. $this->data['data'] .= $cdata; 
  192.  
  193. public function tag_close($parser, $tag) 
  194. if ($this->current_xhtml_construct >= 0) 
  195. $this->current_xhtml_construct--; 
  196. 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'))) 
  197. $this->data['data'] .= '</' . end($this->element) . '>'; 
  198. if ($this->current_xhtml_construct === -1) 
  199. $this->data =& $this->datas[count($this->datas) - 1]; 
  200. array_pop($this->datas); 
  201.  
  202. array_pop($this->element); 
  203. array_pop($this->namespace); 
  204. array_pop($this->xml_base); 
  205. array_pop($this->xml_base_explicit); 
  206. array_pop($this->xml_lang); 
  207.  
  208. public function split_ns($string) 
  209. static $cache = array(); 
  210. if (!isset($cache[$string])) 
  211. if ($pos = strpos($string, $this->separator)) 
  212. static $separator_length; 
  213. if (!$separator_length) 
  214. $separator_length = strlen($this->separator); 
  215. $namespace = substr($string, 0, $pos); 
  216. $local_name = substr($string, $pos + $separator_length); 
  217. if (strtolower($namespace) === SIMPLEPIE_NAMESPACE_ITUNES) 
  218. $namespace = SIMPLEPIE_NAMESPACE_ITUNES; 
  219.  
  220. // Normalize the Media RSS namespaces 
  221. if ($namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG || 
  222. $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG2 || 
  223. $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG3 || 
  224. $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG4 || 
  225. $namespace === SIMPLEPIE_NAMESPACE_MEDIARSS_WRONG5 ) 
  226. $namespace = SIMPLEPIE_NAMESPACE_MEDIARSS; 
  227. $cache[$string] = array($namespace, $local_name); 
  228. else 
  229. $cache[$string] = array('', $string); 
  230. return $cache[$string];