1<?php
2
3// SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue
4// SPDX-License-Identifier: BSD-3-Clause
5
6declare(strict_types=1);
7
8namespace SimplePie;
9
10use SimplePie\XML\Declaration\Parser as DeclarationParser;
11use XMLParser;
12
13/**
14 * Parses XML into something sane
15 *
16 *
17 * This class can be overloaded with {@see \SimplePie\SimplePie::set_parser_class()}
18 */
19class Parser implements RegistryAware
20{
21 /** @var int */
22 public $error_code;
23 /** @var string */
24 public $error_string;
25 /** @var int */
26 public $current_line;
27 /** @var int */
28 public $current_column;
29 /** @var int */
30 public $current_byte;
31 /** @var string */
32 public $separator = ' ';
33 /** @var string[] */
34 public $namespace = [''];
35 /** @var string[] */
36 public $element = [''];
37 /** @var string[] */
38 public $xml_base = [''];
39 /** @var bool[] */
40 public $xml_base_explicit = [false];
41 /** @var string[] */
42 public $xml_lang = [''];
43 /** @var array<string, mixed> */
44 public $data = [];
45 /** @var array<array<string, mixed>> */
46 public $datas = [[]];
47 /** @var int */
48 public $current_xhtml_construct = -1;
49 /** @var string */
50 public $encoding;
51 /** @var Registry */
52 protected $registry;
53
54 /**
55 * @return void
56 */
57 public function set_registry(\SimplePie\Registry $registry)
58 {
59 $this->registry = $registry;
60 }
61
62 /**
63 * @return bool
64 */
65 public function parse(string &$data, string $encoding, string $url = '')
66 {
67 if (class_exists('DOMXpath') && function_exists('Mf2\parse')) {
68 $doc = new \DOMDocument();
69 @$doc->loadHTML($data);
70 $xpath = new \DOMXpath($doc);
71 // Check for both h-feed and h-entry, as both a feed with no entries
72 // and a list of entries without an h-feed wrapper are both valid.
73 $query = '//*[contains(concat(" ", @class, " "), " h-feed ") or '.
74 'contains(concat(" ", @class, " "), " h-entry ")]';
75 /** @var \DOMNodeList<\DOMElement> $result */
76 $result = $xpath->query($query);
77 if ($result->length !== 0) {
78 return $this->parse_microformats($data, $url);
79 }
80 }
81
82 // Use UTF-8 if we get passed US-ASCII, as every US-ASCII character is a UTF-8 character
83 if (strtoupper($encoding) === 'US-ASCII') {
84 $this->encoding = 'UTF-8';
85 } else {
86 $this->encoding = $encoding;
87 }
88
89 // Strip BOM:
90 // UTF-32 Big Endian BOM
91 if (substr($data, 0, 4) === "\x00\x00\xFE\xFF") {
92 $data = substr($data, 4);
93 }
94 // UTF-32 Little Endian BOM
95 elseif (substr($data, 0, 4) === "\xFF\xFE\x00\x00") {
96 $data = substr($data, 4);
97 }
98 // UTF-16 Big Endian BOM
99 elseif (substr($data, 0, 2) === "\xFE\xFF") {
100 $data = substr($data, 2);
101 }
102 // UTF-16 Little Endian BOM
103 elseif (substr($data, 0, 2) === "\xFF\xFE") {
104 $data = substr($data, 2);
105 }
106 // UTF-8 BOM
107 elseif (substr($data, 0, 3) === "\xEF\xBB\xBF") {
108 $data = substr($data, 3);
109 }
110
111 if (substr($data, 0, 5) === '<?xml' && strspn(substr($data, 5, 1), "\x09\x0A\x0D\x20") && ($pos = strpos($data, '?>')) !== false) {
112 $declaration = $this->registry->create(DeclarationParser::class, [substr($data, 5, $pos - 5)]);
113 if ($declaration->parse()) {
114 $data = substr($data, $pos + 2);
115 $data = '<?xml version="' . $declaration->version . '" encoding="' . $encoding . '" standalone="' . (($declaration->standalone) ? 'yes' : 'no') . '"?>' . "\n" .
116 self::set_doctype($data);
117 } else {
118 $this->error_string = 'SimplePie bug! Please report this!';
119 return false;
120 }
121 } else {
122 $data = self::set_doctype($data);
123 }
124
125 $return = true;
126
127 static $xml_is_sane = null;
128 if ($xml_is_sane === null) {
129 $parser_check = xml_parser_create();
130 xml_parse_into_struct($parser_check, '<foo>&</foo>', $values);
131 if (\PHP_VERSION_ID < 80000) {
132 xml_parser_free($parser_check);
133 }
134 $xml_is_sane = isset($values[0]['value']);
135 }
136
137 // Create the parser
138 if ($xml_is_sane) {
139 $xml = xml_parser_create_ns($this->encoding, $this->separator);
140 xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE, 1);
141 xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, 0);
142 xml_set_character_data_handler($xml, [$this, 'cdata']);
143 xml_set_element_handler($xml, [$this, 'tag_open'], [$this, 'tag_close']);
144
145 // Parse!
146 $wrapper = @is_writable(sys_get_temp_dir()) ? 'php://temp' : 'php://memory';
147 if (($stream = fopen($wrapper, 'r+')) &&
148 fwrite($stream, $data) &&
149 rewind($stream)) {
150 //Parse by chunks not to use too much memory
151 do {
152 $stream_data = (string) fread($stream, 1048576);
153
154 if (!xml_parse($xml, $stream_data, feof($stream))) {
155 $this->error_code = xml_get_error_code($xml);
156 $this->error_string = xml_error_string($this->error_code) ?: "Unknown";
157 $return = false;
158 break;
159 }
160 } while (!feof($stream));
161 fclose($stream);
162 } else {
163 $return = false;
164 }
165
166 $this->current_line = xml_get_current_line_number($xml);
167 $this->current_column = xml_get_current_column_number($xml);
168 $this->current_byte = xml_get_current_byte_index($xml);
169 if (\PHP_VERSION_ID < 80000) {
170 xml_parser_free($xml);
171 }
172 return $return;
173 }
174
175 libxml_clear_errors();
176 $xml = new \XMLReader();
177 $xml->xml($data);
178 while (@$xml->read()) {
179 switch ($xml->nodeType) {
180 case \XMLReader::END_ELEMENT:
181 if ($xml->namespaceURI !== '') {
182 $tagName = $xml->namespaceURI . $this->separator . $xml->localName;
183 } else {
184 $tagName = $xml->localName;
185 }
186 $this->tag_close(null, $tagName);
187 break;
188 case \XMLReader::ELEMENT:
189 $empty = $xml->isEmptyElement;
190 if ($xml->namespaceURI !== '') {
191 $tagName = $xml->namespaceURI . $this->separator . $xml->localName;
192 } else {
193 $tagName = $xml->localName;
194 }
195 $attributes = [];
196 while ($xml->moveToNextAttribute()) {
197 if ($xml->namespaceURI !== '') {
198 $attrName = $xml->namespaceURI . $this->separator . $xml->localName;
199 } else {
200 $attrName = $xml->localName;
201 }
202 $attributes[$attrName] = $xml->value;
203 }
204 $this->tag_open(null, $tagName, $attributes);
205 if ($empty) {
206 $this->tag_close(null, $tagName);
207 }
208 break;
209 case \XMLReader::TEXT:
210
211 case \XMLReader::CDATA:
212 $this->cdata(null, $xml->value);
213 break;
214 }
215 }
216 if ($error = libxml_get_last_error()) {
217 $this->error_code = $error->code;
218 $this->error_string = $error->message;
219 $this->current_line = $error->line;
220 $this->current_column = $error->column;
221 return false;
222 }
223
224 return true;
225 }
226
227 /**
228 * @return int
229 */
230 public function get_error_code()
231 {
232 return $this->error_code;
233 }
234
235 /**
236 * @return string
237 */
238 public function get_error_string()
239 {
240 return $this->error_string;
241 }
242
243 /**
244 * @return int
245 */
246 public function get_current_line()
247 {
248 return $this->current_line;
249 }
250
251 /**
252 * @return int
253 */
254 public function get_current_column()
255 {
256 return $this->current_column;
257 }
258
259 /**
260 * @return int
261 */
262 public function get_current_byte()
263 {
264 return $this->current_byte;
265 }
266
267 /**
268 * @return array<string, mixed>
269 */
270 public function get_data()
271 {
272 return $this->data;
273 }
274
275 /**
276 * @param XMLParser|resource|null $parser
277 * @param array<string, string> $attributes
278 * @return void
279 */
280 public function tag_open($parser, string $tag, array $attributes)
281 {
282 [$this->namespace[], $this->element[]] = $this->split_ns($tag);
283
284 $attribs = [];
285 foreach ($attributes as $name => $value) {
286 [$attrib_namespace, $attribute] = $this->split_ns($name);
287 $attribs[$attrib_namespace][$attribute] = $value;
288 }
289
290 if (isset($attribs[\SimplePie\SimplePie::NAMESPACE_XML]['base'])) {
291 $base = $this->registry->call(Misc::class, 'absolutize_url', [$attribs[\SimplePie\SimplePie::NAMESPACE_XML]['base'], end($this->xml_base)]);
292 if ($base !== false) {
293 $this->xml_base[] = $base;
294 $this->xml_base_explicit[] = true;
295 }
296 } else {
297 $this->xml_base[] = end($this->xml_base) ?: '';
298 $this->xml_base_explicit[] = end($this->xml_base_explicit);
299 }
300
301 if (isset($attribs[\SimplePie\SimplePie::NAMESPACE_XML]['lang'])) {
302 $this->xml_lang[] = $attribs[\SimplePie\SimplePie::NAMESPACE_XML]['lang'];
303 } else {
304 $this->xml_lang[] = end($this->xml_lang) ?: '';
305 }
306
307 if ($this->current_xhtml_construct >= 0) {
308 $this->current_xhtml_construct++;
309 if (end($this->namespace) === \SimplePie\SimplePie::NAMESPACE_XHTML) {
310 $this->data['data'] .= '<' . end($this->element);
311 if (isset($attribs[''])) {
312 foreach ($attribs[''] as $name => $value) {
313 $this->data['data'] .= ' ' . $name . '="' . htmlspecialchars($value, ENT_COMPAT, $this->encoding) . '"';
314 }
315 }
316 $this->data['data'] .= '>';
317 }
318 } else {
319 $this->datas[] = &$this->data;
320 $this->data = &$this->data['child'][end($this->namespace)][end($this->element)][];
321 $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)];
322 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')
323 || (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')
324 || (end($this->namespace) === \SimplePie\SimplePie::NAMESPACE_RSS_20 && in_array(end($this->element), ['title']))
325 || (end($this->namespace) === \SimplePie\SimplePie::NAMESPACE_RSS_090 && in_array(end($this->element), ['title']))
326 || (end($this->namespace) === \SimplePie\SimplePie::NAMESPACE_RSS_10 && in_array(end($this->element), ['title']))) {
327 $this->current_xhtml_construct = 0;
328 }
329 }
330 }
331
332 /**
333 * @param XMLParser|resource|null $parser
334 * @return void
335 */
336 public function cdata($parser, string $cdata)
337 {
338 if ($this->current_xhtml_construct >= 0) {
339 $this->data['data'] .= htmlspecialchars($cdata, ENT_QUOTES, $this->encoding);
340 } else {
341 $this->data['data'] .= $cdata;
342 }
343 }
344
345 /**
346 * @param XMLParser|resource|null $parser
347 * @return void
348 */
349 public function tag_close($parser, string $tag)
350 {
351 if ($this->current_xhtml_construct >= 0) {
352 $this->current_xhtml_construct--;
353 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'])) {
354 $this->data['data'] .= '</' . end($this->element) . '>';
355 }
356 }
357 if ($this->current_xhtml_construct === -1) {
358 $this->data = &$this->datas[count($this->datas) - 1];
359 array_pop($this->datas);
360 }
361
362 array_pop($this->element);
363 array_pop($this->namespace);
364 array_pop($this->xml_base);
365 array_pop($this->xml_base_explicit);
366 array_pop($this->xml_lang);
367 }
368
369 /**
370 * @return array{string, string}
371 */
372 public function split_ns(string $string)
373 {
374 static $cache = [];
375 if (!isset($cache[$string])) {
376 if ($pos = strpos($string, $this->separator)) {
377 static $separator_length;
378 if (!$separator_length) {
379 $separator_length = strlen($this->separator);
380 }
381 $namespace = substr($string, 0, $pos);
382 $local_name = substr($string, $pos + $separator_length);
383 if (strtolower($namespace) === \SimplePie\SimplePie::NAMESPACE_ITUNES) {
384 $namespace = \SimplePie\SimplePie::NAMESPACE_ITUNES;
385 }
386
387 // Normalize the Media RSS namespaces
388 if ($namespace === \SimplePie\SimplePie::NAMESPACE_MEDIARSS_WRONG ||
389 $namespace === \SimplePie\SimplePie::NAMESPACE_MEDIARSS_WRONG2 ||
390 $namespace === \SimplePie\SimplePie::NAMESPACE_MEDIARSS_WRONG3 ||
391 $namespace === \SimplePie\SimplePie::NAMESPACE_MEDIARSS_WRONG4 ||
392 $namespace === \SimplePie\SimplePie::NAMESPACE_MEDIARSS_WRONG5) {
393 $namespace = \SimplePie\SimplePie::NAMESPACE_MEDIARSS;
394 }
395 $cache[$string] = [$namespace, $local_name];
396 } else {
397 $cache[$string] = ['', $string];
398 }
399 }
400 return $cache[$string];
401 }
402
403 /**
404 * @param array<string, mixed> $data
405 */
406 private function parse_hcard(array $data, bool $category = false): string
407 {
408 $name = '';
409 $link = '';
410 // Check if h-card is set and pass that information on in the link.
411 if (isset($data['type']) && in_array('h-card', $data['type'])) {
412 if (isset($data['properties']['name'][0])) {
413 $name = $data['properties']['name'][0];
414 }
415 if (isset($data['properties']['url'][0])) {
416 $link = $data['properties']['url'][0];
417 if ($name === '') {
418 $name = $link;
419 } else {
420 // can't have commas in categories.
421 $name = str_replace(',', '', $name);
422 }
423 $person_tag = $category ? '<span class="person-tag"></span>' : '';
424 return '<a class="h-card" href="'.$link.'">'.$person_tag.$name.'</a>';
425 }
426 }
427 return $data['value'] ?? '';
428 }
429
430 /**
431 * @return true
432 */
433 private function parse_microformats(string &$data, string $url): bool
434 {
435 // For PHPStan, we already check that in call site.
436 \assert(function_exists('Mf2\parse'));
437 \assert(function_exists('Mf2\fetch'));
438 $feed_title = '';
439 $feed_author = null;
440 $author_cache = [];
441 $items = [];
442 $entries = [];
443 $mf = \Mf2\parse($data, $url);
444 // First look for an h-feed.
445 $h_feed = [];
446 foreach ($mf['items'] as $mf_item) {
447 if (in_array('h-feed', $mf_item['type'])) {
448 $h_feed = $mf_item;
449 break;
450 }
451 // Also look for h-feed or h-entry in the children of each top level item.
452 if (!isset($mf_item['children'][0]['type'])) {
453 continue;
454 }
455 if (in_array('h-feed', $mf_item['children'][0]['type'])) {
456 $h_feed = $mf_item['children'][0];
457 // In this case the parent of the h-feed may be an h-card, so use it as
458 // the feed_author.
459 if (in_array('h-card', $mf_item['type'])) {
460 $feed_author = $mf_item;
461 }
462 break;
463 } elseif (in_array('h-entry', $mf_item['children'][0]['type'])) {
464 $entries = $mf_item['children'];
465 // In this case the parent of the h-entry list may be an h-card, so use
466 // it as the feed_author.
467 if (in_array('h-card', $mf_item['type'])) {
468 $feed_author = $mf_item;
469 }
470 break;
471 }
472 }
473 if (isset($h_feed['children'])) {
474 $entries = $h_feed['children'];
475 // Also set the feed title and store author from the h-feed if available.
476 if (isset($mf['items'][0]['properties']['name'][0])) {
477 $feed_title = $mf['items'][0]['properties']['name'][0];
478 }
479 if (isset($mf['items'][0]['properties']['author'][0])) {
480 $feed_author = $mf['items'][0]['properties']['author'][0];
481 }
482 } elseif (count($entries) === 0) {
483 $entries = $mf['items'];
484 }
485 for ($i = 0; $i < count($entries); $i++) {
486 $entry = $entries[$i];
487 if (in_array('h-entry', $entry['type'])) {
488 $item = [];
489 $title = '';
490 $description = '';
491 if (isset($entry['properties']['url'][0])) {
492 $link = $entry['properties']['url'][0];
493 if (isset($link['value'])) {
494 $link = $link['value'];
495 }
496 $item['link'] = [['data' => $link]];
497 }
498 if (isset($entry['properties']['uid'][0])) {
499 $guid = $entry['properties']['uid'][0];
500 if (isset($guid['value'])) {
501 $guid = $guid['value'];
502 }
503 $item['guid'] = [['data' => $guid]];
504 }
505 if (isset($entry['properties']['name'][0])) {
506 $title = $entry['properties']['name'][0];
507 if (isset($title['value'])) {
508 $title = $title['value'];
509 }
510 $item['title'] = [['data' => $title]];
511 }
512 if (isset($entry['properties']['author'][0]) || isset($feed_author)) {
513 // author is a special case, it can be plain text or an h-card array.
514 // If it's plain text it can also be a url that should be followed to
515 // get the actual h-card.
516 $author = $entry['properties']['author'][0] ?? $feed_author;
517 if (!is_string($author)) {
518 $author = $this->parse_hcard($author);
519 } elseif (strpos($author, 'http') === 0) {
520 if (isset($author_cache[$author])) {
521 $author = $author_cache[$author];
522 } else {
523 if ($mf = \Mf2\fetch($author)) {
524 foreach ($mf['items'] as $hcard) {
525 // Only interested in an h-card by itself in this case.
526 if (!in_array('h-card', $hcard['type'])) {
527 continue;
528 }
529 // It must have a url property matching what we fetched.
530 if (!isset($hcard['properties']['url']) ||
531 !(in_array($author, $hcard['properties']['url']))) {
532 continue;
533 }
534 // Save parse_hcard the trouble of finding the correct url.
535 $hcard['properties']['url'][0] = $author;
536 // Cache this h-card for the next h-entry to check.
537 $author_cache[$author] = $this->parse_hcard($hcard);
538 $author = $author_cache[$author];
539 break;
540 }
541 }
542 }
543 }
544 $item['author'] = [['data' => $author]];
545 }
546 if (isset($entry['properties']['photo'][0])) {
547 // If a photo is also in content, don't need to add it again here.
548 $content = '';
549 if (isset($entry['properties']['content'][0]['html'])) {
550 $content = $entry['properties']['content'][0]['html'];
551 }
552 $photo_list = [];
553 for ($j = 0; $j < count($entry['properties']['photo']); $j++) {
554 $photo = $entry['properties']['photo'][$j];
555 if (!empty($photo) && strpos($content, $photo) === false) {
556 $photo_list[] = $photo;
557 }
558 }
559 // When there's more than one photo show the first and use a lightbox.
560 // Need a permanent, unique name for the image set, but don't have
561 // anything unique except for the content itself, so use that.
562 $count = count($photo_list);
563 if ($count > 1) {
564 $image_set_id = preg_replace('/[[:^alnum:]]/', '', $photo_list[0]);
565 $description = '<p>';
566 for ($j = 0; $j < $count; $j++) {
567 $hidden = $j === 0 ? '' : 'class="hidden" ';
568 $description .= '<a href="'.$photo_list[$j].'" '.$hidden.
569 'data-lightbox="image-set-'.$image_set_id.'">'.
570 '<img src="'.$photo_list[$j].'"></a>';
571 }
572 $description .= '<br><b>'.$count.' photos</b></p>';
573 } elseif ($count == 1) {
574 $description = '<p><img src="'.$photo_list[0].'"></p>';
575 }
576 }
577 if (isset($entry['properties']['content'][0]['html'])) {
578 // e-content['value'] is the same as p-name when they are on the same
579 // element. Use this to replace title with a strip_tags version so
580 // that alt text from images is not included in the title.
581 if ($entry['properties']['content'][0]['value'] === $title) {
582 $title = strip_tags($entry['properties']['content'][0]['html']);
583 $item['title'] = [['data' => $title]];
584 }
585 $description .= $entry['properties']['content'][0]['html'];
586 if (isset($entry['properties']['in-reply-to'][0])) {
587 $in_reply_to = '';
588 if (is_string($entry['properties']['in-reply-to'][0])) {
589 $in_reply_to = $entry['properties']['in-reply-to'][0];
590 } elseif (isset($entry['properties']['in-reply-to'][0]['value'])) {
591 $in_reply_to = $entry['properties']['in-reply-to'][0]['value'];
592 }
593 if ($in_reply_to !== '') {
594 $description .= '<p><span class="in-reply-to"></span> '.
595 '<a href="'.$in_reply_to.'">'.$in_reply_to.'</a><p>';
596 }
597 }
598 $item['description'] = [['data' => $description]];
599 }
600 if (isset($entry['properties']['category'])) {
601 $category_csv = '';
602 // Categories can also contain h-cards.
603 foreach ($entry['properties']['category'] as $category) {
604 if ($category_csv !== '') {
605 $category_csv .= ', ';
606 }
607 if (is_string($category)) {
608 // Can't have commas in categories.
609 $category_csv .= str_replace(',', '', $category);
610 } else {
611 $category_csv .= $this->parse_hcard($category, true);
612 }
613 }
614 $item['category'] = [['data' => $category_csv]];
615 }
616 if (isset($entry['properties']['published'][0])) {
617 $timestamp = strtotime($entry['properties']['published'][0]);
618 $pub_date = date('F j Y g:ia', $timestamp).' GMT';
619 $item['pubDate'] = [['data' => $pub_date]];
620 }
621 // The title and description are set to the empty string to represent
622 // a deleted item (which also makes it an invalid rss item).
623 if (isset($entry['properties']['deleted'][0])) {
624 $item['title'] = [['data' => '']];
625 $item['description'] = [['data' => '']];
626 }
627 $items[] = ['child' => ['' => $item]];
628 }
629 }
630 // Mimic RSS data format when storing microformats.
631 $link = [['data' => $url]];
632 $image = '';
633 if (!is_string($feed_author) &&
634 isset($feed_author['properties']['photo'][0])) {
635 $image = [['child' => ['' => ['url' =>
636 [['data' => $feed_author['properties']['photo'][0]]]]]]];
637 }
638 // Use the name given for the h-feed, or get the title from the html.
639 if ($feed_title !== '') {
640 $feed_title = [['data' => htmlspecialchars($feed_title)]];
641 } elseif ($position = strpos($data, '<title>')) {
642 $start = $position < 200 ? 0 : $position - 200;
643 $check = substr($data, $start, 400);
644 $matches = [];
645 if (preg_match('/<title>(.+)<\/title>/', $check, $matches)) {
646 $feed_title = [['data' => htmlspecialchars($matches[1])]];
647 }
648 }
649 $channel = ['channel' => [['child' => ['' =>
650 ['link' => $link, 'image' => $image, 'title' => $feed_title,
651 'item' => $items]]]]];
652 $rss = [['attribs' => ['' => ['version' => '2.0']],
653 'child' => ['' => $channel]]];
654 $this->data = ['child' => ['' => ['rss' => $rss]]];
655 return true;
656 }
657
658 private static function set_doctype(string $data): string
659 {
660 // Strip DOCTYPE except if containing an [internal subset]
661 $data = preg_replace('/^\\s*<!DOCTYPE\\s[^>\\[\\]]*>\s*/', '', $data) ?? $data;
662 // Declare HTML entities only if no remaining DOCTYPE
663 $doctype = preg_match('/^\\s*<!DOCTYPE\\s/', $data) ? '' : self::declare_html_entities();
664 return $doctype . $data;
665 }
666
667 private static function declare_html_entities(): string
668 {
669 // This is required because the RSS specification says that entity-encoded
670 // html is allowed, but the xml specification says they must be declared.
671 return '<!DOCTYPE rss [ <!ENTITY nbsp " "> <!ENTITY iexcl "¡"> <!ENTITY cent "¢"> <!ENTITY pound "£"> <!ENTITY curren "¤"> <!ENTITY yen "¥"> <!ENTITY brvbar "¦"> <!ENTITY sect "§"> <!ENTITY uml "¨"> <!ENTITY copy "©"> <!ENTITY ordf "ª"> <!ENTITY laquo "«"> <!ENTITY not "¬"> <!ENTITY shy "­"> <!ENTITY reg "®"> <!ENTITY macr "¯"> <!ENTITY deg "°"> <!ENTITY plusmn "±"> <!ENTITY sup2 "²"> <!ENTITY sup3 "³"> <!ENTITY acute "´"> <!ENTITY micro "µ"> <!ENTITY para "¶"> <!ENTITY middot "·"> <!ENTITY cedil "¸"> <!ENTITY sup1 "¹"> <!ENTITY ordm "º"> <!ENTITY raquo "»"> <!ENTITY frac14 "¼"> <!ENTITY frac12 "½"> <!ENTITY frac34 "¾"> <!ENTITY iquest "¿"> <!ENTITY Agrave "À"> <!ENTITY Aacute "Á"> <!ENTITY Acirc "Â"> <!ENTITY Atilde "Ã"> <!ENTITY Auml "Ä"> <!ENTITY Aring "Å"> <!ENTITY AElig "Æ"> <!ENTITY Ccedil "Ç"> <!ENTITY Egrave "È"> <!ENTITY Eacute "É"> <!ENTITY Ecirc "Ê"> <!ENTITY Euml "Ë"> <!ENTITY Igrave "Ì"> <!ENTITY Iacute "Í"> <!ENTITY Icirc "Î"> <!ENTITY Iuml "Ï"> <!ENTITY ETH "Ð"> <!ENTITY Ntilde "Ñ"> <!ENTITY Ograve "Ò"> <!ENTITY Oacute "Ó"> <!ENTITY Ocirc "Ô"> <!ENTITY Otilde "Õ"> <!ENTITY Ouml "Ö"> <!ENTITY times "×"> <!ENTITY Oslash "Ø"> <!ENTITY Ugrave "Ù"> <!ENTITY Uacute "Ú"> <!ENTITY Ucirc "Û"> <!ENTITY Uuml "Ü"> <!ENTITY Yacute "Ý"> <!ENTITY THORN "Þ"> <!ENTITY szlig "ß"> <!ENTITY agrave "à"> <!ENTITY aacute "á"> <!ENTITY acirc "â"> <!ENTITY atilde "ã"> <!ENTITY auml "ä"> <!ENTITY aring "å"> <!ENTITY aelig "æ"> <!ENTITY ccedil "ç"> <!ENTITY egrave "è"> <!ENTITY eacute "é"> <!ENTITY ecirc "ê"> <!ENTITY euml "ë"> <!ENTITY igrave "ì"> <!ENTITY iacute "í"> <!ENTITY icirc "î"> <!ENTITY iuml "ï"> <!ENTITY eth "ð"> <!ENTITY ntilde "ñ"> <!ENTITY ograve "ò"> <!ENTITY oacute "ó"> <!ENTITY ocirc "ô"> <!ENTITY otilde "õ"> <!ENTITY ouml "ö"> <!ENTITY divide "÷"> <!ENTITY oslash "ø"> <!ENTITY ugrave "ù"> <!ENTITY uacute "ú"> <!ENTITY ucirc "û"> <!ENTITY uuml "ü"> <!ENTITY yacute "ý"> <!ENTITY thorn "þ"> <!ENTITY yuml "ÿ"> <!ENTITY OElig "Œ"> <!ENTITY oelig "œ"> <!ENTITY Scaron "Š"> <!ENTITY scaron "š"> <!ENTITY Yuml "Ÿ"> <!ENTITY fnof "ƒ"> <!ENTITY circ "ˆ"> <!ENTITY tilde "˜"> <!ENTITY Alpha "Α"> <!ENTITY Beta "Β"> <!ENTITY Gamma "Γ"> <!ENTITY Epsilon "Ε"> <!ENTITY Zeta "Ζ"> <!ENTITY Eta "Η"> <!ENTITY Theta "Θ"> <!ENTITY Iota "Ι"> <!ENTITY Kappa "Κ"> <!ENTITY Lambda "Λ"> <!ENTITY Mu "Μ"> <!ENTITY Nu "Ν"> <!ENTITY Xi "Ξ"> <!ENTITY Omicron "Ο"> <!ENTITY Pi "Π"> <!ENTITY Rho "Ρ"> <!ENTITY Sigma "Σ"> <!ENTITY Tau "Τ"> <!ENTITY Upsilon "Υ"> <!ENTITY Phi "Φ"> <!ENTITY Chi "Χ"> <!ENTITY Psi "Ψ"> <!ENTITY Omega "Ω"> <!ENTITY alpha "α"> <!ENTITY beta "β"> <!ENTITY gamma "γ"> <!ENTITY delta "δ"> <!ENTITY epsilon "ε"> <!ENTITY zeta "ζ"> <!ENTITY eta "η"> <!ENTITY theta "θ"> <!ENTITY iota "ι"> <!ENTITY kappa "κ"> <!ENTITY lambda "λ"> <!ENTITY mu "μ"> <!ENTITY nu "ν"> <!ENTITY xi "ξ"> <!ENTITY omicron "ο"> <!ENTITY pi "π"> <!ENTITY rho "ρ"> <!ENTITY sigmaf "ς"> <!ENTITY sigma "σ"> <!ENTITY tau "τ"> <!ENTITY upsilon "υ"> <!ENTITY phi "φ"> <!ENTITY chi "χ"> <!ENTITY psi "ψ"> <!ENTITY omega "ω"> <!ENTITY thetasym "ϑ"> <!ENTITY upsih "ϒ"> <!ENTITY piv "ϖ"> <!ENTITY ensp " "> <!ENTITY emsp " "> <!ENTITY thinsp " "> <!ENTITY zwnj "‌"> <!ENTITY zwj "‍"> <!ENTITY lrm "‎"> <!ENTITY rlm "‏"> <!ENTITY ndash "–"> <!ENTITY mdash "—"> <!ENTITY lsquo "‘"> <!ENTITY rsquo "’"> <!ENTITY sbquo "‚"> <!ENTITY ldquo "“"> <!ENTITY rdquo "”"> <!ENTITY bdquo "„"> <!ENTITY dagger "†"> <!ENTITY Dagger "‡"> <!ENTITY bull "•"> <!ENTITY hellip "…"> <!ENTITY permil "‰"> <!ENTITY prime "′"> <!ENTITY Prime "″"> <!ENTITY lsaquo "‹"> <!ENTITY rsaquo "›"> <!ENTITY oline "‾"> <!ENTITY frasl "⁄"> <!ENTITY euro "€"> <!ENTITY image "ℑ"> <!ENTITY weierp "℘"> <!ENTITY real "ℜ"> <!ENTITY trade "™"> <!ENTITY alefsym "ℵ"> <!ENTITY larr "←"> <!ENTITY uarr "↑"> <!ENTITY rarr "→"> <!ENTITY darr "↓"> <!ENTITY harr "↔"> <!ENTITY crarr "↵"> <!ENTITY lArr "⇐"> <!ENTITY uArr "⇑"> <!ENTITY rArr "⇒"> <!ENTITY dArr "⇓"> <!ENTITY hArr "⇔"> <!ENTITY forall "∀"> <!ENTITY part "∂"> <!ENTITY exist "∃"> <!ENTITY empty "∅"> <!ENTITY nabla "∇"> <!ENTITY isin "∈"> <!ENTITY notin "∉"> <!ENTITY ni "∋"> <!ENTITY prod "∏"> <!ENTITY sum "∑"> <!ENTITY minus "−"> <!ENTITY lowast "∗"> <!ENTITY radic "√"> <!ENTITY prop "∝"> <!ENTITY infin "∞"> <!ENTITY ang "∠"> <!ENTITY and "∧"> <!ENTITY or "∨"> <!ENTITY cap "∩"> <!ENTITY cup "∪"> <!ENTITY int "∫"> <!ENTITY there4 "∴"> <!ENTITY sim "∼"> <!ENTITY cong "≅"> <!ENTITY asymp "≈"> <!ENTITY ne "≠"> <!ENTITY equiv "≡"> <!ENTITY le "≤"> <!ENTITY ge "≥"> <!ENTITY sub "⊂"> <!ENTITY sup "⊃"> <!ENTITY nsub "⊄"> <!ENTITY sube "⊆"> <!ENTITY supe "⊇"> <!ENTITY oplus "⊕"> <!ENTITY otimes "⊗"> <!ENTITY perp "⊥"> <!ENTITY sdot "⋅"> <!ENTITY lceil "⌈"> <!ENTITY rceil "⌉"> <!ENTITY lfloor "⌊"> <!ENTITY rfloor "⌋"> <!ENTITY lang "〈"> <!ENTITY rang "〉"> <!ENTITY loz "◊"> <!ENTITY spades "♠"> <!ENTITY clubs "♣"> <!ENTITY hearts "♥"> <!ENTITY diams "♦"> ]>';
672 }
673}
674
675class_alias('SimplePie\Parser', 'SimplePie_Parser');
676