Skip to content

Commit

Permalink
Merge pull request #54 from FriendsOfREDAXO/neues-rss-feed
Browse files Browse the repository at this point in the history
RSS-Erstellung finalisieren
  • Loading branch information
alxndr-w authored Dec 31, 2023
2 parents 69b6da9 + 9c661f2 commit 61a6945
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions lib/rex_api_neues_rss.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 61a6945

Please sign in to comment.