From 325aba63f0cc34046d20dac03d31582516d6226a Mon Sep 17 00:00:00 2001 From: Thomas Skerbis Date: Sun, 31 Dec 2023 18:02:23 +0100 Subject: [PATCH 01/10] RSS mit SimpleXML und mit rex_response sowie clear outputbuffer --- lib/rex_api_neues_rss.php | 93 ++++++++++++--------------------------- 1 file changed, 27 insertions(+), 66 deletions(-) diff --git a/lib/rex_api_neues_rss.php b/lib/rex_api_neues_rss.php index 9102b42..e89945c 100644 --- a/lib/rex_api_neues_rss.php +++ b/lib/rex_api_neues_rss.php @@ -2,98 +2,59 @@ class rex_api_neues_rss extends rex_api_function { - protected $published = true; // Aufruf aus dem Frontend erlaubt + protected $published = true; // Erlaubt den Aufruf aus dem Frontend - public function execute(): void + public function execute() { $domain_id = rex_request('domain_id', 'int', null); $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 */ + if ($category_id && $category = neues_category::get($category_id)) { $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($collection, $domain_id, $lang_id, $filename)); + + rex_response::cleanOutputBuffers(); + rex_response::sendContentType('application/xml; charset=utf-8'); + + // RSS-Feed generieren und ausgeben + echo self::getRssFeed($collection, $domain_id, $lang_id, $filename); + exit(); } - public static function getRssFeed(rex_yform_manager_collection $collection, $domain, $lang, $filename) + public static function getRssFeed($collection, $domain, $lang, $filename) { return self::createRssFeed($collection, $domain, $lang, $filename); } - public static function createRssFeed(?rex_yform_manager_collection $collection = null, $domain = null, $lang = null, $filename = 'rss.neues.xml') + public static function createRssFeed($collection, $domain, $lang, $filename = 'rss.neues.xml') { - if (!$collection) { - $collection = neues_entry::findOnline(); - } - - $xml = new DOMDocument('1.0', 'utf-8'); - $xml->formatOutput = true; + $xml = new SimpleXMLElement(''); - $rss = $xml->createElement('rss'); - $rss->setAttribute('version', '2.0'); - $xml->appendChild($rss); - - $channel = $xml->createElement('channel'); - $rss->appendChild($channel); - - $head_title = $xml->createElement('title', rex::getServerName()); - $channel->appendChild($head_title); - - $head_description = $xml->createElement('description', ''); - $channel->appendChild($head_description); + $channel = $xml->addChild('channel'); + $channel->addChild('title', rex::getServerName()); + # $channel->addChild('description', $description); + $channel->addChild('link', rex::getServer()); if ($lang) { - $head_language = $xml->createElement('language', $lang->getCode()); - $channel->appendChild($head_language); + $channel->addChild('language', $lang); } - $head_link = $xml->createElement('link', rex::getServer()); - $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); - - $item_description = $xml->createElement('description', htmlspecialchars(strip_tags($entry->getDescription()))); - $item->appendChild($item_description); - - $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', self::guidv4(str_pad($entry->getId(), 16, '0', STR_PAD_LEFT))); - $item->appendChild($item_guid); + $item = $channel->addChild('item'); + $item->addChild('title', htmlspecialchars($entry->getName())); + $item->addChild('description', htmlspecialchars(strip_tags($entry->getDescription()))); + $item->addChild('link', rex::getServer() . $entry->getUrl()); + $item->addChild('pubDate', date('r', strtotime($entry->getPublishDate()))); + $item->addChild('guid', rex::getServer() . $entry->getUrl()); } - $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)); + // Speichern und ausgeben des XML + $xml->asXML(rex_path::base($filename)); + return $xml->asXML(); } } From 7015ab007698aba2eb99eca6a6ebd69a0f1e2710 Mon Sep 17 00:00:00 2001 From: skerbis Date: Sun, 31 Dec 2023 17:02:51 +0000 Subject: [PATCH 02/10] Apply php-cs-fixer changes --- lib/rex_api_neues_rss.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rex_api_neues_rss.php b/lib/rex_api_neues_rss.php index e89945c..89f07bf 100644 --- a/lib/rex_api_neues_rss.php +++ b/lib/rex_api_neues_rss.php @@ -23,7 +23,7 @@ public function execute() // RSS-Feed generieren und ausgeben echo self::getRssFeed($collection, $domain_id, $lang_id, $filename); - exit(); + exit; } public static function getRssFeed($collection, $domain, $lang, $filename) @@ -37,7 +37,7 @@ public static function createRssFeed($collection, $domain, $lang, $filename = 'r $channel = $xml->addChild('channel'); $channel->addChild('title', rex::getServerName()); - # $channel->addChild('description', $description); + // $channel->addChild('description', $description); $channel->addChild('link', rex::getServer()); if ($lang) { From b729cee1fa1b0c58422d7edea151d5bd9d8800b6 Mon Sep 17 00:00:00 2001 From: Thomas Skerbis Date: Sun, 31 Dec 2023 18:42:41 +0100 Subject: [PATCH 03/10] generate channel description --- lib/rex_api_neues_rss.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/rex_api_neues_rss.php b/lib/rex_api_neues_rss.php index 89f07bf..2711bf2 100644 --- a/lib/rex_api_neues_rss.php +++ b/lib/rex_api_neues_rss.php @@ -12,9 +12,12 @@ public function execute() if ($category_id && $category = neues_category::get($category_id)) { $collection = neues_entry::findOnline($category_id); - $filename = 'rss.neues.' . rex_string::normalize($category->getName()) . '.xml'; + $categoryname = rex_string::normalize($category->getName()); + $filename = 'rss.neues.' . $categoryname . '.xml'; + $description = 'RSS-FEED: ' . rex::getServerName() . ' | ' . $categoryname; } else { $collection = neues_entry::findOnline(); + $description = 'RSS-FEED: ' . rex::getServerName(); $filename = 'rss.neues.xml'; } @@ -22,7 +25,7 @@ public function execute() rex_response::sendContentType('application/xml; charset=utf-8'); // RSS-Feed generieren und ausgeben - echo self::getRssFeed($collection, $domain_id, $lang_id, $filename); + echo self::getRssFeed($collection, $domain_id, $lang_id, $description, $filename); exit; } @@ -31,13 +34,13 @@ public static function getRssFeed($collection, $domain, $lang, $filename) return self::createRssFeed($collection, $domain, $lang, $filename); } - public static function createRssFeed($collection, $domain, $lang, $filename = 'rss.neues.xml') + public static function createRssFeed($collection, $domain, $lang, $description, $filename = 'rss.neues.xml') { $xml = new SimpleXMLElement(''); $channel = $xml->addChild('channel'); $channel->addChild('title', rex::getServerName()); - // $channel->addChild('description', $description); + $channel->addChild('description', $description); $channel->addChild('link', rex::getServer()); if ($lang) { From 0acbf7376b0e9eca8f62aef0cbc25ad4b6a3a6c0 Mon Sep 17 00:00:00 2001 From: Thomas Skerbis Date: Sun, 31 Dec 2023 19:25:38 +0100 Subject: [PATCH 04/10] =?UTF-8?q?Sprachcode=20erg=C3=A4nzt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/rex_api_neues_rss.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/rex_api_neues_rss.php b/lib/rex_api_neues_rss.php index 2711bf2..f009553 100644 --- a/lib/rex_api_neues_rss.php +++ b/lib/rex_api_neues_rss.php @@ -29,9 +29,9 @@ public function execute() exit; } - public static function getRssFeed($collection, $domain, $lang, $filename) + public static function getRssFeed($collection, $domain, $lang, $description, $filename) { - return self::createRssFeed($collection, $domain, $lang, $filename); + return self::createRssFeed($collection, $domain, $lang, $description, $filename); } public static function createRssFeed($collection, $domain, $lang, $description, $filename = 'rss.neues.xml') @@ -43,8 +43,8 @@ public static function createRssFeed($collection, $domain, $lang, $description, $channel->addChild('description', $description); $channel->addChild('link', rex::getServer()); - if ($lang) { - $channel->addChild('language', $lang); + if ($lang && $lang > 0) { + $channel->addChild('language', rex_clang::get($lang)->getCode()); } foreach ($collection as $entry) { From 9c2e4a6bd94621cc9b1433de5844e5e8ebb6d01c Mon Sep 17 00:00:00 2001 From: Thomas Skerbis Date: Sun, 31 Dec 2023 23:24:05 +0100 Subject: [PATCH 05/10] doppel // verhindern in urls --- lib/rex_api_neues_rss.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/rex_api_neues_rss.php b/lib/rex_api_neues_rss.php index f009553..4f49e71 100644 --- a/lib/rex_api_neues_rss.php +++ b/lib/rex_api_neues_rss.php @@ -26,13 +26,17 @@ public function execute() // RSS-Feed generieren und ausgeben echo self::getRssFeed($collection, $domain_id, $lang_id, $description, $filename); - exit; + die(); } public static function getRssFeed($collection, $domain, $lang, $description, $filename) { return self::createRssFeed($collection, $domain, $lang, $description, $filename); } + + public static function joinUrls($url1, $url2) { + return rtrim($url1, '/') . '/' . ltrim($url2, '/'); + } public static function createRssFeed($collection, $domain, $lang, $description, $filename = 'rss.neues.xml') { @@ -51,9 +55,9 @@ public static function createRssFeed($collection, $domain, $lang, $description, $item = $channel->addChild('item'); $item->addChild('title', htmlspecialchars($entry->getName())); $item->addChild('description', htmlspecialchars(strip_tags($entry->getDescription()))); - $item->addChild('link', rex::getServer() . $entry->getUrl()); + $item->addChild('link', self::joinUrls(rex::getServer(), $entry->getUrl())); $item->addChild('pubDate', date('r', strtotime($entry->getPublishDate()))); - $item->addChild('guid', rex::getServer() . $entry->getUrl()); + $item->addChild('guid', self::joinUrls(rex::getServer(), $entry->getUrl())); } // Speichern und ausgeben des XML From 5f154fc059c9aa07a53fb51c997dad4e3e77b7ba Mon Sep 17 00:00:00 2001 From: skerbis Date: Sun, 31 Dec 2023 22:24:28 +0000 Subject: [PATCH 06/10] Apply php-cs-fixer changes --- lib/rex_api_neues_rss.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/rex_api_neues_rss.php b/lib/rex_api_neues_rss.php index 4f49e71..1eceb4e 100644 --- a/lib/rex_api_neues_rss.php +++ b/lib/rex_api_neues_rss.php @@ -26,15 +26,16 @@ public function execute() // RSS-Feed generieren und ausgeben echo self::getRssFeed($collection, $domain_id, $lang_id, $description, $filename); - die(); + exit; } public static function getRssFeed($collection, $domain, $lang, $description, $filename) { return self::createRssFeed($collection, $domain, $lang, $description, $filename); } - - public static function joinUrls($url1, $url2) { + + public static function joinUrls($url1, $url2) + { return rtrim($url1, '/') . '/' . ltrim($url2, '/'); } From c7c81f7b1688fe37f8821e915d9ccaa297e9d86e Mon Sep 17 00:00:00 2001 From: Thomas Skerbis Date: Mon, 1 Jan 2024 18:47:32 +0100 Subject: [PATCH 07/10] small code fixes --- lib/rex_api_neues_rss.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/rex_api_neues_rss.php b/lib/rex_api_neues_rss.php index 1eceb4e..dd80b67 100644 --- a/lib/rex_api_neues_rss.php +++ b/lib/rex_api_neues_rss.php @@ -4,7 +4,7 @@ class rex_api_neues_rss extends rex_api_function { protected $published = true; // Erlaubt den Aufruf aus dem Frontend - public function execute() + public function execute():void { $domain_id = rex_request('domain_id', 'int', null); $lang_id = rex_request('lang_id', 'int', null); @@ -39,7 +39,7 @@ public static function joinUrls($url1, $url2) return rtrim($url1, '/') . '/' . ltrim($url2, '/'); } - public static function createRssFeed($collection, $domain, $lang, $description, $filename = 'rss.neues.xml') + public static function createRssFeed(rex_yform_manager_collection $collection, $domain, $lang, $description, $filename = 'rss.neues.xml') { $xml = new SimpleXMLElement(''); @@ -53,6 +53,7 @@ public static function createRssFeed($collection, $domain, $lang, $description, } foreach ($collection as $entry) { + /** @var neues_entry $entry */ $item = $channel->addChild('item'); $item->addChild('title', htmlspecialchars($entry->getName())); $item->addChild('description', htmlspecialchars(strip_tags($entry->getDescription()))); From 704ab722ff42bc27e64ccdb2f8393163aa6492e1 Mon Sep 17 00:00:00 2001 From: skerbis Date: Mon, 1 Jan 2024 17:47:52 +0000 Subject: [PATCH 08/10] Apply php-cs-fixer changes --- lib/rex_api_neues_rss.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rex_api_neues_rss.php b/lib/rex_api_neues_rss.php index dd80b67..4421b22 100644 --- a/lib/rex_api_neues_rss.php +++ b/lib/rex_api_neues_rss.php @@ -4,7 +4,7 @@ class rex_api_neues_rss extends rex_api_function { protected $published = true; // Erlaubt den Aufruf aus dem Frontend - public function execute():void + public function execute(): void { $domain_id = rex_request('domain_id', 'int', null); $lang_id = rex_request('lang_id', 'int', null); From b7e1d86409dfaa476725843a10fbda28dddaa670 Mon Sep 17 00:00:00 2001 From: Thomas Skerbis Date: Mon, 1 Jan 2024 18:55:06 +0100 Subject: [PATCH 09/10] =?UTF-8?q?guid=20isPermalink=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/rex_api_neues_rss.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rex_api_neues_rss.php b/lib/rex_api_neues_rss.php index 4421b22..d4362e7 100644 --- a/lib/rex_api_neues_rss.php +++ b/lib/rex_api_neues_rss.php @@ -59,7 +59,7 @@ public static function createRssFeed(rex_yform_manager_collection $collection, $ $item->addChild('description', htmlspecialchars(strip_tags($entry->getDescription()))); $item->addChild('link', self::joinUrls(rex::getServer(), $entry->getUrl())); $item->addChild('pubDate', date('r', strtotime($entry->getPublishDate()))); - $item->addChild('guid', self::joinUrls(rex::getServer(), $entry->getUrl())); + $item->addChild('guid', self::joinUrls(rex::getServer(), $entry->getUrl()))->addAttribute('isPermaLink', 'true'); } // Speichern und ausgeben des XML From 01e828e7f92374ec1fc923d0d382de8776837e99 Mon Sep 17 00:00:00 2001 From: Thomas Skerbis Date: Mon, 1 Jan 2024 19:11:51 +0100 Subject: [PATCH 10/10] rex_escape bei description --- lib/rex_api_neues_rss.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/rex_api_neues_rss.php b/lib/rex_api_neues_rss.php index d4362e7..0659cb8 100644 --- a/lib/rex_api_neues_rss.php +++ b/lib/rex_api_neues_rss.php @@ -12,9 +12,8 @@ public function execute(): void if ($category_id && $category = neues_category::get($category_id)) { $collection = neues_entry::findOnline($category_id); - $categoryname = rex_string::normalize($category->getName()); - $filename = 'rss.neues.' . $categoryname . '.xml'; - $description = 'RSS-FEED: ' . rex::getServerName() . ' | ' . $categoryname; + $filename = 'rss.neues.' . rex_string::normalize($category->getName()) . '.xml'; + $description = 'RSS-FEED: ' . rex::getServerName() . ' | ' . rex_escape($category->getName()); } else { $collection = neues_entry::findOnline(); $description = 'RSS-FEED: ' . rex::getServerName();