From 9c661f27c3a9dcbc19a5d787831e7eabc68fd0ac Mon Sep 17 00:00:00 2001 From: Alexander Walther Date: Sun, 31 Dec 2023 16:35:33 +0100 Subject: [PATCH] RSS-Erstellung finalisieren MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Grundsätzlich werden damit RSS-Feeds erfolgreich erstellt und zurückgegeben. --- lib/rex_api_neues_rss.php | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/lib/rex_api_neues_rss.php b/lib/rex_api_neues_rss.php index 427e956..9102b42 100644 --- a/lib/rex_api_neues_rss.php +++ b/lib/rex_api_neues_rss.php @@ -10,8 +10,16 @@ public function execute(): void $lang_id = rex_request('lang_id', 'int', null); $category_id = rex_request('category_id', 'int', null); + if($category_id && $category = neues_category::get($category_id)) { + /** @var neues_category $category */ + $collection = neues_entry::findOnline($category_id); + $filename = 'rss.neues.' . rex_string::normalize($category->getName()) . '.xml'; + } else { + $collection = neues_entry::findOnline(); + $filename = 'rss.neues.xml'; + } header('Content-Type: application/rss+xml; charset=utf-8'); - exit(self::getRssFeed(neues_entry::findOnline($category_id), $domain_id, $lang_id, 'rss.neues.xml')); + exit(self::getRssFeed($collection, $domain_id, $lang_id, $filename)); } public static function getRssFeed(rex_yform_manager_collection $collection, $domain, $lang, $filename) @@ -50,26 +58,42 @@ public static function createRssFeed(?rex_yform_manager_collection $collection = $channel->appendChild($head_link); foreach ($collection as $entry) { + /** @var neues_entry $entry */ $item = $xml->createElement('item'); $channel->appendChild($item); $item_title = $xml->createElement('title', htmlspecialchars($entry->getName())); $item->appendChild($item_title); - $entry->setTeasertext($entry->getTeaser()); + $item_description = $xml->createElement('description', htmlspecialchars(strip_tags($entry->getDescription()))); + $item->appendChild($item_description); - $entry->setDescription($entry->getDescription()); - - $item_link = $xml->createElement('link', $entry->getUrl()); + $item_link = $xml->createElement('link', rex::getServer() . $entry->getUrl()); $item->appendChild($item_link); $item_pubDate = $xml->createElement('pubDate', date('r', strtotime($entry->getPublishDate()))); $item->appendChild($item_pubDate); - $item_guid = $xml->createElement('guid', $entry->getUuid()); + $item_guid = $xml->createElement('guid', self::guidv4(str_pad($entry->getId(), 16, '0', STR_PAD_LEFT))); $item->appendChild($item_guid); } - return $xml->save(rex_path::base($filename)); + $return = $xml->saveXML(); + $xml->save(rex_path::base($filename)); + return $return; + } + + public static function guidv4($data = null) + { + // Generate 16 bytes (128 bits) of random data or use the data passed into the function. + $data ??= random_bytes(16); + + // Set version to 0100 + $data[6] = chr(ord($data[6]) & 0x0F | 0x40); + // Set bits 6-7 to 10 + $data[8] = chr(ord($data[8]) & 0x3F | 0x80); + + // Output the 36 character UUID. + return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); } }