*/ private array $authors; private string $content; private string $link; private int $date; private int $lastSeen = 0; private ?int $lastModified = null; private ?int $lastUserModified = null; /** In microseconds */ private string $date_added = '0'; private string $hash = ''; private ?int $sqlContentLength = null; private ?bool $is_read; private ?bool $is_favorite; private bool $is_updated = false; private int $feedId; private ?FreshRSS_Feed $feed; /** @var array */ private array $tags = []; /** * @param string|array $tags * @param int|numeric-string $pubdate */ public function __construct(int $feedId = 0, string $guid = '', string $title = '', string $authors = '', string $content = '', string $link = '', int|string $pubdate = 0, bool|int|null $is_read = false, bool|int|null $is_favorite = false, $tags = '') { $this->_title($title); $this->_authors($authors); $this->_content($content); $this->_link($link); $this->_date($pubdate); $this->_isRead($is_read); $this->_isFavorite($is_favorite); $this->_feedId($feedId); $this->_tags($tags); $this->_guid($guid); } /** @param array{id?:string,guid?:string,title?:string,author?:string,content?:string,link?:string, * date?:int|string,lastSeen?:int,lastModified?:int,lastUserModified?:int, * hash?:string,is_read?:bool|int,is_favorite?:bool|int,id_feed?:int, * tags?:string|array,attributes?:?string, * thumbnail?:string,timestamp?:string, * content_length?:int} $dao */ public static function fromArray(array $dao): FreshRSS_Entry { if (empty($dao['content']) || !is_string($dao['content'])) { $dao['content'] = ''; } if (!is_numeric($dao['date'] ?? null)) { $dao['date'] = 0; } $dao['attributes'] = empty($dao['attributes']) ? [] : json_decode($dao['attributes'], true); if (is_array($dao['attributes'])) { $dao['attributes'] = array_filter($dao['attributes'], 'is_string', ARRAY_FILTER_USE_KEY); } else { $dao['attributes'] = []; } if (!empty($dao['thumbnail'])) { $dao['attributes']['thumbnail'] = [ 'url' => $dao['thumbnail'], ]; } $entry = new FreshRSS_Entry( $dao['id_feed'] ?? 0, $dao['guid'] ?? '', $dao['title'] ?? '', $dao['author'] ?? '', $dao['content'], $dao['link'] ?? '', $dao['date'] ?? 0, $dao['is_read'] ?? false, $dao['is_favorite'] ?? false, $dao['tags'] ?? '' ); if (!empty($dao['id']) && is_numeric($dao['id'])) { $entry->_id($dao['id']); } if (!empty($dao['timestamp'])) { $entry->_date(strtotime($dao['timestamp']) ?: 0); } if (empty($dao['lastSeen'])) { $entry->_lastSeen($entry->id() == '0' ? 0 : (int)substr($entry->id(), 0, -6)); // Microseconds to seconds } else { $entry->_lastSeen($dao['lastSeen']); } if (!empty($dao['lastModified'])) { $entry->_lastModified($dao['lastModified']); } if (!empty($dao['lastUserModified'])) { $entry->_lastUserModified($dao['lastUserModified']); } if (!empty($dao['attributes'])) { $entry->_attributes($dao['attributes']); } if (!empty($dao['hash'])) { $entry->_hash($dao['hash']); } if (isset($dao['content_length']) && is_numeric($dao['content_length'])) { $entry->_sqlContentLength((int)$dao['content_length']); } return $entry; } /** * @param Traversable,attributes?:?string, * thumbnail?:string,timestamp?:string}> $daos * @return Traversable */ public static function fromTraversable(Traversable $daos): Traversable { foreach ($daos as $dao) { yield FreshRSS_Entry::fromArray($dao); } } /** @return numeric-string */ public function id(): string { return $this->id; } public function guid(): string { return $this->guid; } public function title(): string { $title = ''; if ($this->title === '') { // used while fetching the article from feed and store it in the database $title = $this->guid(); } else { // used while fetching from the database if ($this->title !== $this->guid) { $title = $this->title; } else { $content = trim(strip_tags($this->content(false))); $title = trim(mb_substr($content, 0, MAX_CHARS_EMPTY_FEED_TITLE, 'UTF-8')); if ($title === '') { $title = $this->guid(); } elseif (strlen($content) > strlen($title)) { $title .= '…'; } } } return $title; } #[Deprecated('Use authors() instead')] public function author(): string { return $this->authors(true); } /** * @phpstan-return ($asString is true ? string : array) * @return string|array */ public function authors(bool $asString = false): string|array { if ($asString) { return $this->authors == null ? '' : ';' . implode('; ', $this->authors); } else { return $this->authors; } } /** * Basic test without ambition to catch all cases such as unquoted addresses, variants of entities, HTML comments, etc. */ private static function containsLink(string $html, string $link): bool { return preg_match('/(?P[\'"])' . preg_quote($link, '/') . '(?P=delim)/', $html) == 1; } /** @param array{url?:string,length?:int,medium?:string,type?:string} $enclosure */ private static function enclosureIsImage(array $enclosure): bool { $elink = $enclosure['url'] ?? ''; $length = $enclosure['length'] ?? 0; $medium = $enclosure['medium'] ?? ''; $mime = $enclosure['type'] ?? ''; return ($elink != '' && $medium === 'image') || str_starts_with($mime, 'image') || ($mime == '' && $length == 0 && preg_match('/[.](avif|gif|jpe?g|png|svg|webp)([?#]|$)/i', $elink)); } /** * Provides the original content without additional content potentially added by loadCompleteContent(). */ public function originalContent(): string { return $this->attributeString('original_content') ?? preg_replace('#.*#s', '', $this->content) ?? ''; } /** * @param bool $withEnclosures Set to true to include the enclosures in the returned HTML, false otherwise. * @param bool $allowDuplicateEnclosures Set to false to remove obvious enclosure duplicates (based on simple string comparison), true otherwise. * @return string HTML content */ public function content(bool $withEnclosures = true, bool $allowDuplicateEnclosures = false): string { if (!$withEnclosures) { return $this->content; } $content = $this->content; $thumbnailAttribute = $this->attributeArray('thumbnail') ?? []; if (!empty($thumbnailAttribute['url'])) { $elink = $thumbnailAttribute['url']; if (is_string($elink) && \SimplePie\Misc::is_remote_uri($elink) && ($allowDuplicateEnclosures || !self::containsLink($content, $elink))) { $content .= <<

HTML; } } $attributeEnclosures = $this->attributeArray('enclosures'); if (empty($attributeEnclosures)) { return $content; } foreach ($attributeEnclosures as $enclosure) { if (!is_array($enclosure)) { continue; } $elink = $enclosure['url'] ?? ''; if ($elink == '' || !is_string($elink) || !\SimplePie\Misc::is_remote_uri($elink)) { continue; } if (!$allowDuplicateEnclosures && self::containsLink($content, $elink)) { continue; } $credits = $enclosure['credit'] ?? ''; $description = is_string($enclosure['description'] ?? null) ? nl2br($enclosure['description'], true) : ''; $length = is_numeric($enclosure['length'] ?? null) ? (int)$enclosure['length'] : 0; $medium = is_string($enclosure['medium'] ?? null) ? $enclosure['medium'] : ''; $mime = is_string($enclosure['type'] ?? null) ? $enclosure['type'] : ''; $thumbnails = $enclosure['thumbnails'] ?? null; if (!is_array($thumbnails)) { $thumbnails = []; } $etitle = is_string($enclosure['title'] ?? null) ? $enclosure['title'] : ''; $content .= "\n"; $content .= '
'; foreach ($thumbnails as $thumbnail) { if (is_string($thumbnail) && \SimplePie\Misc::is_remote_uri($thumbnail)) { $content .= '

'; } } if (self::enclosureIsImage(['url' => $elink, 'length' => $length, 'medium' => $medium, 'type' => $mime])) { $content .= '

'; } elseif ($medium === 'audio' || str_starts_with($mime, 'audio')) { $content .= '

💾

'; } elseif ($medium === 'video' || str_starts_with($mime, 'video')) { $content .= '

💾

'; } else { //e.g. application, text, unknown $content .= '

💾

'; } if ($credits != '') { if (!is_array($credits)) { $credits = [$credits]; } foreach ($credits as $credit) { if (is_string($credit)) { $content .= '

© ' . $credit . '

'; } } } if ($description != '') { $content .= '
' . $description . '
'; } $content .= "
\n"; } return $content; } /** @return Traversable,'height'?:int,'width'?:int,'thumbnails'?:array}> */ public function enclosures(bool $searchBodyImages = false): Traversable { $attributeEnclosures = $this->attributeArray('enclosures'); if (is_array($attributeEnclosures)) { // FreshRSS 1.20.1+: The enclosures are saved as attributes /** @var list,'height'?:int,'width'?:int,'thumbnails'?:array}> $attributeEnclosures */ yield from $attributeEnclosures; } try { $searchEnclosures = !is_iterable($attributeEnclosures) && (str_contains($this->content, '