// SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue
// SPDX-License-Identifier: BSD-3-Clause
use SimplePie\XML\Declaration\Parser as DeclarationParser;
* Parses XML into something sane
* This class can be overloaded with {@see \SimplePie\SimplePie::set_parser_class()}
class Parser implements RegistryAware
public $namespace = [''];
public $xml_base_explicit = [false];
/** @var array<string, mixed> */
/** @var array<array<string, mixed>> */
public $current_xhtml_construct = -1;
public function set_registry(\SimplePie\Registry $registry)
$this->registry = $registry;
public function parse(string &$data, string $encoding, string $url = '')
if (class_exists('DOMXpath') && function_exists('Mf2\parse')) {
$doc = new \DOMDocument();
$xpath = new \DOMXpath($doc);
// Check for both h-feed and h-entry, as both a feed with no entries
// and a list of entries without an h-feed wrapper are both valid.
$query = '//*[contains(concat(" ", @class, " "), " h-feed ") or '.
'contains(concat(" ", @class, " "), " h-entry ")]';
/** @var \DOMNodeList<\DOMElement> $result */
$result = $xpath->query($query);
if ($result->length !== 0) {
return $this->parse_microformats($data, $url);
// Use UTF-8 if we get passed US-ASCII, as every US-ASCII character is a UTF-8 character
if (strtoupper($encoding) === 'US-ASCII') {
$this->encoding = 'UTF-8';
$this->encoding = $encoding;
if (substr($data, 0, 4) === "\x00\x00\xFE\xFF") {
$data = substr($data, 4);
// UTF-32 Little Endian BOM
elseif (substr($data, 0, 4) === "\xFF\xFE\x00\x00") {
$data = substr($data, 4);
elseif (substr($data, 0, 2) === "\xFE\xFF") {
$data = substr($data, 2);
// UTF-16 Little Endian BOM
elseif (substr($data, 0, 2) === "\xFF\xFE") {
$data = substr($data, 2);
elseif (substr($data, 0, 3) === "\xEF\xBB\xBF") {
$data = substr($data, 3);
if (substr($data, 0, 5) === '<?xml' && strspn(substr($data, 5, 1), "\x09\x0A\x0D\x20") && ($pos = strpos($data, '?>')) !== false) {
$declaration = $this->registry->create(DeclarationParser::class, [substr($data, 5, $pos - 5)]);
if ($declaration->parse()) {
$data = substr($data, $pos + 2);
$data = '<?xml version="' . $declaration->version . '" encoding="' . $encoding . '" standalone="' . (($declaration->standalone) ? 'yes' : 'no') . '"?>' . "\n" .
self::set_doctype($data);
$this->error_string = 'SimplePie bug! Please report this!';
$data = self::set_doctype($data);
static $xml_is_sane = null;
if ($xml_is_sane === null) {
$parser_check = xml_parser_create();
xml_parse_into_struct($parser_check, '<foo>&</foo>', $values);
if (\PHP_VERSION_ID < 80000) {
xml_parser_free($parser_check);
$xml_is_sane = isset($values[0]['value']);
$xml = xml_parser_create_ns($this->encoding, $this->separator);
xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, 0);
xml_set_character_data_handler($xml, [$this, 'cdata']);
xml_set_element_handler($xml, [$this, 'tag_open'], [$this, 'tag_close']);
$wrapper = @is_writable(sys_get_temp_dir()) ? 'php://temp' : 'php://memory';
if (($stream = fopen($wrapper, 'r+')) &&
fwrite($stream, $data) &&
//Parse by chunks not to use too much memory
$stream_data = (string) fread($stream, 1048576);
if (!xml_parse($xml, $stream_data, feof($stream))) {
$this->error_code = xml_get_error_code($xml);
$this->error_string = xml_error_string($this->error_code) ?: "Unknown";
} while (!feof($stream));
$this->current_line = xml_get_current_line_number($xml);
$this->current_column = xml_get_current_column_number($xml);
$this->current_byte = xml_get_current_byte_index($xml);
if (\PHP_VERSION_ID < 80000) {
switch ($xml->nodeType) {
case \XMLReader::END_ELEMENT:
if ($xml->namespaceURI !== '') {
$tagName = $xml->namespaceURI . $this->separator . $xml->localName;
$tagName = $xml->localName;
$this->tag_close(null, $tagName);
case \XMLReader::ELEMENT:
$empty = $xml->isEmptyElement;
if ($xml->namespaceURI !== '') {
$tagName = $xml->namespaceURI . $this->separator . $xml->localName;
$tagName = $xml->localName;
while ($xml->moveToNextAttribute()) {
if ($xml->namespaceURI !== '') {
$attrName = $xml->namespaceURI . $this->separator . $xml->localName;
$attrName = $xml->localName;
$attributes[$attrName] = $xml->value;
$this->tag_open(null, $tagName, $attributes);
$this->tag_close(null, $tagName);
$this->cdata(null, $xml->value);
if ($error = libxml_get_last_error()) {
$this->error_code = $error->code;
$this->error_string = $error->message;
$this->current_line = $error->line;
$this->current_column = $error->column;
public function get_error_code()
return $this->error_code;
public function get_error_string()
return $this->error_string;
public function get_current_line()
return $this->current_line;
public function get_current_column()
return $this->current_column;
public function get_current_byte()
return $this->current_byte;
* @return array<string, mixed>
public function get_data()
* @param XMLParser|resource|null $parser
* @param array<string, string> $attributes
public function tag_open($parser, string $tag, array $attributes)
[$this->namespace[], $this->element[]] = $this->split_ns($tag);
foreach ($attributes as $name => $value) {
[$attrib_namespace, $attribute] = $this->split_ns($name);
$attribs[$attrib_namespace][$attribute] = $value;
if (isset($attribs[\SimplePie\SimplePie::NAMESPACE_XML]['base'])) {
$base = $this->registry->call(Misc::class, 'absolutize_url', [$attribs[\SimplePie\SimplePie::NAMESPACE_XML]['base'], end($this->xml_base)]);
$this->xml_base[] = $base;
$this->xml_base_explicit[] = true;
$this->xml_base[] = end($this->xml_base) ?: '';
$this->xml_base_explicit[] = end($this->xml_base_explicit);
if (isset($attribs[\SimplePie\SimplePie::NAMESPACE_XML]['lang'])) {
$this->xml_lang[] = $attribs[\SimplePie\SimplePie::NAMESPACE_XML]['lang'];
$this->xml_lang[] = end($this->xml_lang) ?: '';
if ($this->current_xhtml_construct >= 0) {
$this->current_xhtml_construct++;
if (end($this->namespace) === \SimplePie\SimplePie::NAMESPACE_XHTML) {
$this->data['data'] .= '<' . end($this->element);
if (isset($attribs[''])) {
foreach ($attribs[''] as $name => $value) {
$this->data['data'] .= ' ' . $name . '="' . htmlspecialchars($value, ENT_COMPAT, $this->encoding) . '"';
$this->data['data'] .= '>';
$this->datas[] = &$this->data;
$this->data = &$this->data['child'][end($this->namespace)][end($this->element)][];
$this->data = ['data' => '', 'attribs' => $attribs, 'xml_base' => end($this->xml_base), 'xml_base_explicit' => end($this->xml_base_explicit), 'xml_lang' => end($this->xml_lang)];
if ((end($this->namespace) === \SimplePie\SimplePie::NAMESPACE_ATOM_03 && in_array(end($this->element), ['title', 'tagline', 'copyright', 'info', 'summary', 'content']) && isset($attribs['']['mode']) && $attribs['']['mode'] === 'xml')
|| (end($this->namespace) === \SimplePie\SimplePie::NAMESPACE_ATOM_10 && in_array(end($this->element), ['rights', 'subtitle', 'summary', 'info', 'title', 'content']) && isset($attribs['']['type']) && $attribs['']['type'] === 'xhtml')
|| (end($this->namespace) === \SimplePie\SimplePie::NAMESPACE_RSS_20 && in_array(end($this->element), ['title']))
|| (end($this->namespace) === \SimplePie\SimplePie::NAMESPACE_RSS_090 && in_array(end($this->element), ['title']))
|| (end($this->namespace) === \SimplePie\SimplePie::NAMESPACE_RSS_10 && in_array(end($this->element), ['title']))) {
$this->current_xhtml_construct = 0;
* @param XMLParser|resource|null $parser
public function cdata($parser, string $cdata)
if ($this->current_xhtml_construct >= 0) {
$this->data['data'] .= htmlspecialchars($cdata, ENT_QUOTES, $this->encoding);
$this->data['data'] .= $cdata;
* @param XMLParser|resource|null $parser
public function tag_close($parser, string $tag)
if ($this->current_xhtml_construct >= 0) {
$this->current_xhtml_construct--;
if (end($this->namespace) === \SimplePie\SimplePie::NAMESPACE_XHTML && !in_array(end($this->element), ['area', 'base', 'basefont', 'br', 'col', 'frame', 'hr', 'img', 'input', 'isindex', 'link', 'meta', 'param'])) {
$this->data['data'] .= '</' . end($this->element) . '>';
if ($this->current_xhtml_construct === -1) {
$this->data = &$this->datas[count($this->datas) - 1];
array_pop($this->element);
array_pop($this->namespace);
array_pop($this->xml_base);
array_pop($this->xml_base_explicit);
array_pop($this->xml_lang);
* @return array{string, string}
public function split_ns(string $string)
if (!isset($cache[$string])) {
if ($pos = strpos($string, $this->separator)) {
static $separator_length;
if (!$separator_length) {
$separator_length = strlen($this->separator);
$namespace = substr($string, 0, $pos);
$local_name = substr($string, $pos + $separator_length);
if (strtolower($namespace) === \SimplePie\SimplePie::NAMESPACE_ITUNES) {
$namespace = \SimplePie\SimplePie::NAMESPACE_ITUNES;
// Normalize the Media RSS namespaces
if ($namespace === \SimplePie\SimplePie::NAMESPACE_MEDIARSS_WRONG ||
$namespace === \SimplePie\SimplePie::NAMESPACE_MEDIARSS_WRONG2 ||
$namespace === \SimplePie\SimplePie::NAMESPACE_MEDIARSS_WRONG3 ||
$namespace === \SimplePie\SimplePie::NAMESPACE_MEDIARSS_WRONG4 ||
$namespace === \SimplePie\SimplePie::NAMESPACE_MEDIARSS_WRONG5) {
$namespace = \SimplePie\SimplePie::NAMESPACE_MEDIARSS;
$cache[$string] = [$namespace, $local_name];
$cache[$string] = ['', $string];
* @param array<string, mixed> $data
private function parse_hcard(array $data, bool $category = false): string
// Check if h-card is set and pass that information on in the link.
if (isset($data['type']) && in_array('h-card', $data['type'])) {
if (isset($data['properties']['name'][0])) {
$name = $data['properties']['name'][0];
if (isset($data['properties']['url'][0])) {
$link = $data['properties']['url'][0];
// can't have commas in categories.
$name = str_replace(',', '', $name);
$person_tag = $category ? '<span class="person-tag"></span>' : '';
return '<a class="h-card" href="'.$link.'">'.$person_tag.$name.'</a>';
return $data['value'] ?? '';
private function parse_microformats(string &$data, string $url): bool
// For PHPStan, we already check that in call site.
\assert(function_exists('Mf2\parse'));
\assert(function_exists('Mf2\fetch'));
$mf = \Mf2\parse($data, $url);
// First look for an h-feed.
foreach ($mf['items'] as $mf_item) {
if (in_array('h-feed', $mf_item['type'])) {
// Also look for h-feed or h-entry in the children of each top level item.
if (!isset($mf_item['children'][0]['type'])) {
if (in_array('h-feed', $mf_item['children'][0]['type'])) {
$h_feed = $mf_item['children'][0];
// In this case the parent of the h-feed may be an h-card, so use it as
if (in_array('h-card', $mf_item['type'])) {
} elseif (in_array('h-entry', $mf_item['children'][0]['type'])) {
$entries = $mf_item['children'];
// In this case the parent of the h-entry list may be an h-card, so use
// it as the feed_author.
if (in_array('h-card', $mf_item['type'])) {
if (isset($h_feed['children'])) {
$entries = $h_feed['children'];
// Also set the feed title and store author from the h-feed if available.
if (isset($mf['items'][0]['properties']['name'][0])) {
$feed_title = $mf['items'][0]['properties']['name'][0];
if (isset($mf['items'][0]['properties']['author'][0])) {
$feed_author = $mf['items'][0]['properties']['author'][0];
} elseif (count($entries) === 0) {
for ($i = 0; $i < count($entries); $i++) {
if (in_array('h-entry', $entry['type'])) {
if (isset($entry['properties']['url'][0])) {
$link = $entry['properties']['url'][0];
if (isset($link['value'])) {
$item['link'] = [['data' => $link]];
if (isset($entry['properties']['uid'][0])) {
$guid = $entry['properties']['uid'][0];
if (isset($guid['value'])) {