Edit File by line
/home/zeestwma/redstone.../wp-inclu.../SimplePi.../src
File: Parser.php
<?php
[0] Fix | Delete
[1] Fix | Delete
// SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue
[2] Fix | Delete
// SPDX-License-Identifier: BSD-3-Clause
[3] Fix | Delete
[4] Fix | Delete
declare(strict_types=1);
[5] Fix | Delete
[6] Fix | Delete
namespace SimplePie;
[7] Fix | Delete
[8] Fix | Delete
use SimplePie\XML\Declaration\Parser as DeclarationParser;
[9] Fix | Delete
use XMLParser;
[10] Fix | Delete
[11] Fix | Delete
/**
[12] Fix | Delete
* Parses XML into something sane
[13] Fix | Delete
*
[14] Fix | Delete
*
[15] Fix | Delete
* This class can be overloaded with {@see \SimplePie\SimplePie::set_parser_class()}
[16] Fix | Delete
*/
[17] Fix | Delete
class Parser implements RegistryAware
[18] Fix | Delete
{
[19] Fix | Delete
/** @var int */
[20] Fix | Delete
public $error_code;
[21] Fix | Delete
/** @var string */
[22] Fix | Delete
public $error_string;
[23] Fix | Delete
/** @var int */
[24] Fix | Delete
public $current_line;
[25] Fix | Delete
/** @var int */
[26] Fix | Delete
public $current_column;
[27] Fix | Delete
/** @var int */
[28] Fix | Delete
public $current_byte;
[29] Fix | Delete
/** @var string */
[30] Fix | Delete
public $separator = ' ';
[31] Fix | Delete
/** @var string[] */
[32] Fix | Delete
public $namespace = [''];
[33] Fix | Delete
/** @var string[] */
[34] Fix | Delete
public $element = [''];
[35] Fix | Delete
/** @var string[] */
[36] Fix | Delete
public $xml_base = [''];
[37] Fix | Delete
/** @var bool[] */
[38] Fix | Delete
public $xml_base_explicit = [false];
[39] Fix | Delete
/** @var string[] */
[40] Fix | Delete
public $xml_lang = [''];
[41] Fix | Delete
/** @var array<string, mixed> */
[42] Fix | Delete
public $data = [];
[43] Fix | Delete
/** @var array<array<string, mixed>> */
[44] Fix | Delete
public $datas = [[]];
[45] Fix | Delete
/** @var int */
[46] Fix | Delete
public $current_xhtml_construct = -1;
[47] Fix | Delete
/** @var string */
[48] Fix | Delete
public $encoding;
[49] Fix | Delete
/** @var Registry */
[50] Fix | Delete
protected $registry;
[51] Fix | Delete
[52] Fix | Delete
/**
[53] Fix | Delete
* @return void
[54] Fix | Delete
*/
[55] Fix | Delete
public function set_registry(\SimplePie\Registry $registry)
[56] Fix | Delete
{
[57] Fix | Delete
$this->registry = $registry;
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
/**
[61] Fix | Delete
* @return bool
[62] Fix | Delete
*/
[63] Fix | Delete
public function parse(string &$data, string $encoding, string $url = '')
[64] Fix | Delete
{
[65] Fix | Delete
if (class_exists('DOMXpath') && function_exists('Mf2\parse')) {
[66] Fix | Delete
$doc = new \DOMDocument();
[67] Fix | Delete
@$doc->loadHTML($data);
[68] Fix | Delete
$xpath = new \DOMXpath($doc);
[69] Fix | Delete
// Check for both h-feed and h-entry, as both a feed with no entries
[70] Fix | Delete
// and a list of entries without an h-feed wrapper are both valid.
[71] Fix | Delete
$query = '//*[contains(concat(" ", @class, " "), " h-feed ") or '.
[72] Fix | Delete
'contains(concat(" ", @class, " "), " h-entry ")]';
[73] Fix | Delete
/** @var \DOMNodeList<\DOMElement> $result */
[74] Fix | Delete
$result = $xpath->query($query);
[75] Fix | Delete
if ($result->length !== 0) {
[76] Fix | Delete
return $this->parse_microformats($data, $url);
[77] Fix | Delete
}
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
// Use UTF-8 if we get passed US-ASCII, as every US-ASCII character is a UTF-8 character
[81] Fix | Delete
if (strtoupper($encoding) === 'US-ASCII') {
[82] Fix | Delete
$this->encoding = 'UTF-8';
[83] Fix | Delete
} else {
[84] Fix | Delete
$this->encoding = $encoding;
[85] Fix | Delete
}
[86] Fix | Delete
[87] Fix | Delete
// Strip BOM:
[88] Fix | Delete
// UTF-32 Big Endian BOM
[89] Fix | Delete
if (substr($data, 0, 4) === "\x00\x00\xFE\xFF") {
[90] Fix | Delete
$data = substr($data, 4);
[91] Fix | Delete
}
[92] Fix | Delete
// UTF-32 Little Endian BOM
[93] Fix | Delete
elseif (substr($data, 0, 4) === "\xFF\xFE\x00\x00") {
[94] Fix | Delete
$data = substr($data, 4);
[95] Fix | Delete
}
[96] Fix | Delete
// UTF-16 Big Endian BOM
[97] Fix | Delete
elseif (substr($data, 0, 2) === "\xFE\xFF") {
[98] Fix | Delete
$data = substr($data, 2);
[99] Fix | Delete
}
[100] Fix | Delete
// UTF-16 Little Endian BOM
[101] Fix | Delete
elseif (substr($data, 0, 2) === "\xFF\xFE") {
[102] Fix | Delete
$data = substr($data, 2);
[103] Fix | Delete
}
[104] Fix | Delete
// UTF-8 BOM
[105] Fix | Delete
elseif (substr($data, 0, 3) === "\xEF\xBB\xBF") {
[106] Fix | Delete
$data = substr($data, 3);
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
if (substr($data, 0, 5) === '<?xml' && strspn(substr($data, 5, 1), "\x09\x0A\x0D\x20") && ($pos = strpos($data, '?>')) !== false) {
[110] Fix | Delete
$declaration = $this->registry->create(DeclarationParser::class, [substr($data, 5, $pos - 5)]);
[111] Fix | Delete
if ($declaration->parse()) {
[112] Fix | Delete
$data = substr($data, $pos + 2);
[113] Fix | Delete
$data = '<?xml version="' . $declaration->version . '" encoding="' . $encoding . '" standalone="' . (($declaration->standalone) ? 'yes' : 'no') . '"?>' . "\n" .
[114] Fix | Delete
self::set_doctype($data);
[115] Fix | Delete
} else {
[116] Fix | Delete
$this->error_string = 'SimplePie bug! Please report this!';
[117] Fix | Delete
return false;
[118] Fix | Delete
}
[119] Fix | Delete
} else {
[120] Fix | Delete
$data = self::set_doctype($data);
[121] Fix | Delete
}
[122] Fix | Delete
[123] Fix | Delete
$return = true;
[124] Fix | Delete
[125] Fix | Delete
static $xml_is_sane = null;
[126] Fix | Delete
if ($xml_is_sane === null) {
[127] Fix | Delete
$parser_check = xml_parser_create();
[128] Fix | Delete
xml_parse_into_struct($parser_check, '<foo>&amp;</foo>', $values);
[129] Fix | Delete
if (\PHP_VERSION_ID < 80000) {
[130] Fix | Delete
xml_parser_free($parser_check);
[131] Fix | Delete
}
[132] Fix | Delete
$xml_is_sane = isset($values[0]['value']);
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
// Create the parser
[136] Fix | Delete
if ($xml_is_sane) {
[137] Fix | Delete
$xml = xml_parser_create_ns($this->encoding, $this->separator);
[138] Fix | Delete
xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE, 1);
[139] Fix | Delete
xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, 0);
[140] Fix | Delete
xml_set_character_data_handler($xml, [$this, 'cdata']);
[141] Fix | Delete
xml_set_element_handler($xml, [$this, 'tag_open'], [$this, 'tag_close']);
[142] Fix | Delete
[143] Fix | Delete
// Parse!
[144] Fix | Delete
$wrapper = @is_writable(sys_get_temp_dir()) ? 'php://temp' : 'php://memory';
[145] Fix | Delete
if (($stream = fopen($wrapper, 'r+')) &&
[146] Fix | Delete
fwrite($stream, $data) &&
[147] Fix | Delete
rewind($stream)) {
[148] Fix | Delete
//Parse by chunks not to use too much memory
[149] Fix | Delete
do {
[150] Fix | Delete
$stream_data = (string) fread($stream, 1048576);
[151] Fix | Delete
[152] Fix | Delete
if (!xml_parse($xml, $stream_data, feof($stream))) {
[153] Fix | Delete
$this->error_code = xml_get_error_code($xml);
[154] Fix | Delete
$this->error_string = xml_error_string($this->error_code) ?: "Unknown";
[155] Fix | Delete
$return = false;
[156] Fix | Delete
break;
[157] Fix | Delete
}
[158] Fix | Delete
} while (!feof($stream));
[159] Fix | Delete
fclose($stream);
[160] Fix | Delete
} else {
[161] Fix | Delete
$return = false;
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
$this->current_line = xml_get_current_line_number($xml);
[165] Fix | Delete
$this->current_column = xml_get_current_column_number($xml);
[166] Fix | Delete
$this->current_byte = xml_get_current_byte_index($xml);
[167] Fix | Delete
if (\PHP_VERSION_ID < 80000) {
[168] Fix | Delete
xml_parser_free($xml);
[169] Fix | Delete
}
[170] Fix | Delete
return $return;
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
libxml_clear_errors();
[174] Fix | Delete
$xml = new \XMLReader();
[175] Fix | Delete
$xml->xml($data);
[176] Fix | Delete
while (@$xml->read()) {
[177] Fix | Delete
switch ($xml->nodeType) {
[178] Fix | Delete
case \XMLReader::END_ELEMENT:
[179] Fix | Delete
if ($xml->namespaceURI !== '') {
[180] Fix | Delete
$tagName = $xml->namespaceURI . $this->separator . $xml->localName;
[181] Fix | Delete
} else {
[182] Fix | Delete
$tagName = $xml->localName;
[183] Fix | Delete
}
[184] Fix | Delete
$this->tag_close(null, $tagName);
[185] Fix | Delete
break;
[186] Fix | Delete
case \XMLReader::ELEMENT:
[187] Fix | Delete
$empty = $xml->isEmptyElement;
[188] Fix | Delete
if ($xml->namespaceURI !== '') {
[189] Fix | Delete
$tagName = $xml->namespaceURI . $this->separator . $xml->localName;
[190] Fix | Delete
} else {
[191] Fix | Delete
$tagName = $xml->localName;
[192] Fix | Delete
}
[193] Fix | Delete
$attributes = [];
[194] Fix | Delete
while ($xml->moveToNextAttribute()) {
[195] Fix | Delete
if ($xml->namespaceURI !== '') {
[196] Fix | Delete
$attrName = $xml->namespaceURI . $this->separator . $xml->localName;
[197] Fix | Delete
} else {
[198] Fix | Delete
$attrName = $xml->localName;
[199] Fix | Delete
}
[200] Fix | Delete
$attributes[$attrName] = $xml->value;
[201] Fix | Delete
}
[202] Fix | Delete
$this->tag_open(null, $tagName, $attributes);
[203] Fix | Delete
if ($empty) {
[204] Fix | Delete
$this->tag_close(null, $tagName);
[205] Fix | Delete
}
[206] Fix | Delete
break;
[207] Fix | Delete
case \XMLReader::TEXT:
[208] Fix | Delete
[209] Fix | Delete
case \XMLReader::CDATA:
[210] Fix | Delete
$this->cdata(null, $xml->value);
[211] Fix | Delete
break;
[212] Fix | Delete
}
[213] Fix | Delete
}
[214] Fix | Delete
if ($error = libxml_get_last_error()) {
[215] Fix | Delete
$this->error_code = $error->code;
[216] Fix | Delete
$this->error_string = $error->message;
[217] Fix | Delete
$this->current_line = $error->line;
[218] Fix | Delete
$this->current_column = $error->column;
[219] Fix | Delete
return false;
[220] Fix | Delete
}
[221] Fix | Delete
[222] Fix | Delete
return true;
[223] Fix | Delete
}
[224] Fix | Delete
[225] Fix | Delete
/**
[226] Fix | Delete
* @return int
[227] Fix | Delete
*/
[228] Fix | Delete
public function get_error_code()
[229] Fix | Delete
{
[230] Fix | Delete
return $this->error_code;
[231] Fix | Delete
}
[232] Fix | Delete
[233] Fix | Delete
/**
[234] Fix | Delete
* @return string
[235] Fix | Delete
*/
[236] Fix | Delete
public function get_error_string()
[237] Fix | Delete
{
[238] Fix | Delete
return $this->error_string;
[239] Fix | Delete
}
[240] Fix | Delete
[241] Fix | Delete
/**
[242] Fix | Delete
* @return int
[243] Fix | Delete
*/
[244] Fix | Delete
public function get_current_line()
[245] Fix | Delete
{
[246] Fix | Delete
return $this->current_line;
[247] Fix | Delete
}
[248] Fix | Delete
[249] Fix | Delete
/**
[250] Fix | Delete
* @return int
[251] Fix | Delete
*/
[252] Fix | Delete
public function get_current_column()
[253] Fix | Delete
{
[254] Fix | Delete
return $this->current_column;
[255] Fix | Delete
}
[256] Fix | Delete
[257] Fix | Delete
/**
[258] Fix | Delete
* @return int
[259] Fix | Delete
*/
[260] Fix | Delete
public function get_current_byte()
[261] Fix | Delete
{
[262] Fix | Delete
return $this->current_byte;
[263] Fix | Delete
}
[264] Fix | Delete
[265] Fix | Delete
/**
[266] Fix | Delete
* @return array<string, mixed>
[267] Fix | Delete
*/
[268] Fix | Delete
public function get_data()
[269] Fix | Delete
{
[270] Fix | Delete
return $this->data;
[271] Fix | Delete
}
[272] Fix | Delete
[273] Fix | Delete
/**
[274] Fix | Delete
* @param XMLParser|resource|null $parser
[275] Fix | Delete
* @param array<string, string> $attributes
[276] Fix | Delete
* @return void
[277] Fix | Delete
*/
[278] Fix | Delete
public function tag_open($parser, string $tag, array $attributes)
[279] Fix | Delete
{
[280] Fix | Delete
[$this->namespace[], $this->element[]] = $this->split_ns($tag);
[281] Fix | Delete
[282] Fix | Delete
$attribs = [];
[283] Fix | Delete
foreach ($attributes as $name => $value) {
[284] Fix | Delete
[$attrib_namespace, $attribute] = $this->split_ns($name);
[285] Fix | Delete
$attribs[$attrib_namespace][$attribute] = $value;
[286] Fix | Delete
}
[287] Fix | Delete
[288] Fix | Delete
if (isset($attribs[\SimplePie\SimplePie::NAMESPACE_XML]['base'])) {
[289] Fix | Delete
$base = $this->registry->call(Misc::class, 'absolutize_url', [$attribs[\SimplePie\SimplePie::NAMESPACE_XML]['base'], end($this->xml_base)]);
[290] Fix | Delete
if ($base !== false) {
[291] Fix | Delete
$this->xml_base[] = $base;
[292] Fix | Delete
$this->xml_base_explicit[] = true;
[293] Fix | Delete
}
[294] Fix | Delete
} else {
[295] Fix | Delete
$this->xml_base[] = end($this->xml_base) ?: '';
[296] Fix | Delete
$this->xml_base_explicit[] = end($this->xml_base_explicit);
[297] Fix | Delete
}
[298] Fix | Delete
[299] Fix | Delete
if (isset($attribs[\SimplePie\SimplePie::NAMESPACE_XML]['lang'])) {
[300] Fix | Delete
$this->xml_lang[] = $attribs[\SimplePie\SimplePie::NAMESPACE_XML]['lang'];
[301] Fix | Delete
} else {
[302] Fix | Delete
$this->xml_lang[] = end($this->xml_lang) ?: '';
[303] Fix | Delete
}
[304] Fix | Delete
[305] Fix | Delete
if ($this->current_xhtml_construct >= 0) {
[306] Fix | Delete
$this->current_xhtml_construct++;
[307] Fix | Delete
if (end($this->namespace) === \SimplePie\SimplePie::NAMESPACE_XHTML) {
[308] Fix | Delete
$this->data['data'] .= '<' . end($this->element);
[309] Fix | Delete
if (isset($attribs[''])) {
[310] Fix | Delete
foreach ($attribs[''] as $name => $value) {
[311] Fix | Delete
$this->data['data'] .= ' ' . $name . '="' . htmlspecialchars($value, ENT_COMPAT, $this->encoding) . '"';
[312] Fix | Delete
}
[313] Fix | Delete
}
[314] Fix | Delete
$this->data['data'] .= '>';
[315] Fix | Delete
}
[316] Fix | Delete
} else {
[317] Fix | Delete
$this->datas[] = &$this->data;
[318] Fix | Delete
$this->data = &$this->data['child'][end($this->namespace)][end($this->element)][];
[319] Fix | Delete
$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)];
[320] Fix | Delete
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')
[321] Fix | Delete
|| (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')
[322] Fix | Delete
|| (end($this->namespace) === \SimplePie\SimplePie::NAMESPACE_RSS_20 && in_array(end($this->element), ['title']))
[323] Fix | Delete
|| (end($this->namespace) === \SimplePie\SimplePie::NAMESPACE_RSS_090 && in_array(end($this->element), ['title']))
[324] Fix | Delete
|| (end($this->namespace) === \SimplePie\SimplePie::NAMESPACE_RSS_10 && in_array(end($this->element), ['title']))) {
[325] Fix | Delete
$this->current_xhtml_construct = 0;
[326] Fix | Delete
}
[327] Fix | Delete
}
[328] Fix | Delete
}
[329] Fix | Delete
[330] Fix | Delete
/**
[331] Fix | Delete
* @param XMLParser|resource|null $parser
[332] Fix | Delete
* @return void
[333] Fix | Delete
*/
[334] Fix | Delete
public function cdata($parser, string $cdata)
[335] Fix | Delete
{
[336] Fix | Delete
if ($this->current_xhtml_construct >= 0) {
[337] Fix | Delete
$this->data['data'] .= htmlspecialchars($cdata, ENT_QUOTES, $this->encoding);
[338] Fix | Delete
} else {
[339] Fix | Delete
$this->data['data'] .= $cdata;
[340] Fix | Delete
}
[341] Fix | Delete
}
[342] Fix | Delete
[343] Fix | Delete
/**
[344] Fix | Delete
* @param XMLParser|resource|null $parser
[345] Fix | Delete
* @return void
[346] Fix | Delete
*/
[347] Fix | Delete
public function tag_close($parser, string $tag)
[348] Fix | Delete
{
[349] Fix | Delete
if ($this->current_xhtml_construct >= 0) {
[350] Fix | Delete
$this->current_xhtml_construct--;
[351] Fix | Delete
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'])) {
[352] Fix | Delete
$this->data['data'] .= '</' . end($this->element) . '>';
[353] Fix | Delete
}
[354] Fix | Delete
}
[355] Fix | Delete
if ($this->current_xhtml_construct === -1) {
[356] Fix | Delete
$this->data = &$this->datas[count($this->datas) - 1];
[357] Fix | Delete
array_pop($this->datas);
[358] Fix | Delete
}
[359] Fix | Delete
[360] Fix | Delete
array_pop($this->element);
[361] Fix | Delete
array_pop($this->namespace);
[362] Fix | Delete
array_pop($this->xml_base);
[363] Fix | Delete
array_pop($this->xml_base_explicit);
[364] Fix | Delete
array_pop($this->xml_lang);
[365] Fix | Delete
}
[366] Fix | Delete
[367] Fix | Delete
/**
[368] Fix | Delete
* @return array{string, string}
[369] Fix | Delete
*/
[370] Fix | Delete
public function split_ns(string $string)
[371] Fix | Delete
{
[372] Fix | Delete
static $cache = [];
[373] Fix | Delete
if (!isset($cache[$string])) {
[374] Fix | Delete
if ($pos = strpos($string, $this->separator)) {
[375] Fix | Delete
static $separator_length;
[376] Fix | Delete
if (!$separator_length) {
[377] Fix | Delete
$separator_length = strlen($this->separator);
[378] Fix | Delete
}
[379] Fix | Delete
$namespace = substr($string, 0, $pos);
[380] Fix | Delete
$local_name = substr($string, $pos + $separator_length);
[381] Fix | Delete
if (strtolower($namespace) === \SimplePie\SimplePie::NAMESPACE_ITUNES) {
[382] Fix | Delete
$namespace = \SimplePie\SimplePie::NAMESPACE_ITUNES;
[383] Fix | Delete
}
[384] Fix | Delete
[385] Fix | Delete
// Normalize the Media RSS namespaces
[386] Fix | Delete
if ($namespace === \SimplePie\SimplePie::NAMESPACE_MEDIARSS_WRONG ||
[387] Fix | Delete
$namespace === \SimplePie\SimplePie::NAMESPACE_MEDIARSS_WRONG2 ||
[388] Fix | Delete
$namespace === \SimplePie\SimplePie::NAMESPACE_MEDIARSS_WRONG3 ||
[389] Fix | Delete
$namespace === \SimplePie\SimplePie::NAMESPACE_MEDIARSS_WRONG4 ||
[390] Fix | Delete
$namespace === \SimplePie\SimplePie::NAMESPACE_MEDIARSS_WRONG5) {
[391] Fix | Delete
$namespace = \SimplePie\SimplePie::NAMESPACE_MEDIARSS;
[392] Fix | Delete
}
[393] Fix | Delete
$cache[$string] = [$namespace, $local_name];
[394] Fix | Delete
} else {
[395] Fix | Delete
$cache[$string] = ['', $string];
[396] Fix | Delete
}
[397] Fix | Delete
}
[398] Fix | Delete
return $cache[$string];
[399] Fix | Delete
}
[400] Fix | Delete
[401] Fix | Delete
/**
[402] Fix | Delete
* @param array<string, mixed> $data
[403] Fix | Delete
*/
[404] Fix | Delete
private function parse_hcard(array $data, bool $category = false): string
[405] Fix | Delete
{
[406] Fix | Delete
$name = '';
[407] Fix | Delete
$link = '';
[408] Fix | Delete
// Check if h-card is set and pass that information on in the link.
[409] Fix | Delete
if (isset($data['type']) && in_array('h-card', $data['type'])) {
[410] Fix | Delete
if (isset($data['properties']['name'][0])) {
[411] Fix | Delete
$name = $data['properties']['name'][0];
[412] Fix | Delete
}
[413] Fix | Delete
if (isset($data['properties']['url'][0])) {
[414] Fix | Delete
$link = $data['properties']['url'][0];
[415] Fix | Delete
if ($name === '') {
[416] Fix | Delete
$name = $link;
[417] Fix | Delete
} else {
[418] Fix | Delete
// can't have commas in categories.
[419] Fix | Delete
$name = str_replace(',', '', $name);
[420] Fix | Delete
}
[421] Fix | Delete
$person_tag = $category ? '<span class="person-tag"></span>' : '';
[422] Fix | Delete
return '<a class="h-card" href="'.$link.'">'.$person_tag.$name.'</a>';
[423] Fix | Delete
}
[424] Fix | Delete
}
[425] Fix | Delete
return $data['value'] ?? '';
[426] Fix | Delete
}
[427] Fix | Delete
[428] Fix | Delete
/**
[429] Fix | Delete
* @return true
[430] Fix | Delete
*/
[431] Fix | Delete
private function parse_microformats(string &$data, string $url): bool
[432] Fix | Delete
{
[433] Fix | Delete
// For PHPStan, we already check that in call site.
[434] Fix | Delete
\assert(function_exists('Mf2\parse'));
[435] Fix | Delete
\assert(function_exists('Mf2\fetch'));
[436] Fix | Delete
$feed_title = '';
[437] Fix | Delete
$feed_author = null;
[438] Fix | Delete
$author_cache = [];
[439] Fix | Delete
$items = [];
[440] Fix | Delete
$entries = [];
[441] Fix | Delete
$mf = \Mf2\parse($data, $url);
[442] Fix | Delete
// First look for an h-feed.
[443] Fix | Delete
$h_feed = [];
[444] Fix | Delete
foreach ($mf['items'] as $mf_item) {
[445] Fix | Delete
if (in_array('h-feed', $mf_item['type'])) {
[446] Fix | Delete
$h_feed = $mf_item;
[447] Fix | Delete
break;
[448] Fix | Delete
}
[449] Fix | Delete
// Also look for h-feed or h-entry in the children of each top level item.
[450] Fix | Delete
if (!isset($mf_item['children'][0]['type'])) {
[451] Fix | Delete
continue;
[452] Fix | Delete
}
[453] Fix | Delete
if (in_array('h-feed', $mf_item['children'][0]['type'])) {
[454] Fix | Delete
$h_feed = $mf_item['children'][0];
[455] Fix | Delete
// In this case the parent of the h-feed may be an h-card, so use it as
[456] Fix | Delete
// the feed_author.
[457] Fix | Delete
if (in_array('h-card', $mf_item['type'])) {
[458] Fix | Delete
$feed_author = $mf_item;
[459] Fix | Delete
}
[460] Fix | Delete
break;
[461] Fix | Delete
} elseif (in_array('h-entry', $mf_item['children'][0]['type'])) {
[462] Fix | Delete
$entries = $mf_item['children'];
[463] Fix | Delete
// In this case the parent of the h-entry list may be an h-card, so use
[464] Fix | Delete
// it as the feed_author.
[465] Fix | Delete
if (in_array('h-card', $mf_item['type'])) {
[466] Fix | Delete
$feed_author = $mf_item;
[467] Fix | Delete
}
[468] Fix | Delete
break;
[469] Fix | Delete
}
[470] Fix | Delete
}
[471] Fix | Delete
if (isset($h_feed['children'])) {
[472] Fix | Delete
$entries = $h_feed['children'];
[473] Fix | Delete
// Also set the feed title and store author from the h-feed if available.
[474] Fix | Delete
if (isset($mf['items'][0]['properties']['name'][0])) {
[475] Fix | Delete
$feed_title = $mf['items'][0]['properties']['name'][0];
[476] Fix | Delete
}
[477] Fix | Delete
if (isset($mf['items'][0]['properties']['author'][0])) {
[478] Fix | Delete
$feed_author = $mf['items'][0]['properties']['author'][0];
[479] Fix | Delete
}
[480] Fix | Delete
} elseif (count($entries) === 0) {
[481] Fix | Delete
$entries = $mf['items'];
[482] Fix | Delete
}
[483] Fix | Delete
for ($i = 0; $i < count($entries); $i++) {
[484] Fix | Delete
$entry = $entries[$i];
[485] Fix | Delete
if (in_array('h-entry', $entry['type'])) {
[486] Fix | Delete
$item = [];
[487] Fix | Delete
$title = '';
[488] Fix | Delete
$description = '';
[489] Fix | Delete
if (isset($entry['properties']['url'][0])) {
[490] Fix | Delete
$link = $entry['properties']['url'][0];
[491] Fix | Delete
if (isset($link['value'])) {
[492] Fix | Delete
$link = $link['value'];
[493] Fix | Delete
}
[494] Fix | Delete
$item['link'] = [['data' => $link]];
[495] Fix | Delete
}
[496] Fix | Delete
if (isset($entry['properties']['uid'][0])) {
[497] Fix | Delete
$guid = $entry['properties']['uid'][0];
[498] Fix | Delete
if (isset($guid['value'])) {
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function