2015-03-03 00:22:02 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2015-03-03 00:22:02 +03:00
|
|
|
/**
|
2019-02-06 19:18:33 +03:00
|
|
|
* MrssFormat - RSS 2.0 + Media RSS
|
|
|
|
* http://www.rssboard.org/rss-specification
|
|
|
|
* http://www.rssboard.org/media-rss
|
|
|
|
*
|
|
|
|
* Validators:
|
|
|
|
* https://validator.w3.org/feed/
|
|
|
|
* http://www.rssboard.org/rss-validator/
|
|
|
|
*
|
|
|
|
* Notes about the implementation:
|
|
|
|
*
|
|
|
|
* - The item author is not supported as it needs to be an e-mail address to be
|
|
|
|
* valid.
|
|
|
|
* - The RSS specification does not explicitly allow to have more than one
|
|
|
|
* enclosure as every item is meant to provide one "story", thus having
|
|
|
|
* multiple enclosures per item may lead to unexpected behavior.
|
|
|
|
* On top of that, it requires to have a length specified, which RSS-Bridge
|
|
|
|
* can't provide.
|
|
|
|
* - The Media RSS extension comes in handy, since it allows to have multiple
|
|
|
|
* enclosures, even though they recommend to have only one enclosure because
|
|
|
|
* of the one-story-per-item reason. It only requires to specify the URL,
|
|
|
|
* everything else is optional.
|
|
|
|
* - Since the Media RSS extension has its own namespace, the output is a valid
|
|
|
|
* RSS 2.0 feed that works with feed readers that don't support the extension.
|
|
|
|
*/
|
2016-09-10 21:41:11 +03:00
|
|
|
class MrssFormat extends FormatAbstract
|
|
|
|
{
|
2019-10-31 21:00:12 +03:00
|
|
|
const MIME_TYPE = 'application/rss+xml';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
protected const ATOM_NS = 'http://www.w3.org/2005/Atom';
|
|
|
|
protected const MRSS_NS = 'http://search.yahoo.com/mrss/';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
public function stringify()
|
|
|
|
{
|
2022-09-08 20:07:57 +03:00
|
|
|
$feedUrl = get_current_url();
|
2016-09-10 21:41:11 +03:00
|
|
|
$extraInfos = $this->getExtraInfos();
|
2022-08-06 23:46:28 +03:00
|
|
|
if (empty($extraInfos['uri'])) {
|
|
|
|
$uri = REPOSITORY;
|
|
|
|
} else {
|
|
|
|
$uri = $extraInfos['uri'];
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-08-06 23:46:28 +03:00
|
|
|
$document = new \DomDocument('1.0', $this->getCharset());
|
2022-06-08 00:22:03 +03:00
|
|
|
$document->formatOutput = true;
|
|
|
|
$feed = $document->createElement('rss');
|
2022-06-09 19:33:23 +03:00
|
|
|
$document->appendChild($feed);
|
2022-06-08 00:22:03 +03:00
|
|
|
$feed->setAttribute('version', '2.0');
|
|
|
|
$feed->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:atom', self::ATOM_NS);
|
|
|
|
$feed->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:media', self::MRSS_NS);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
$channel = $document->createElement('channel');
|
|
|
|
$feed->appendChild($channel);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
$title = $extraInfos['name'];
|
|
|
|
$channelTitle = $document->createElement('title');
|
|
|
|
$channel->appendChild($channelTitle);
|
2022-06-09 19:33:23 +03:00
|
|
|
$channelTitle->appendChild($document->createTextNode($title));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
$link = $document->createElement('link');
|
|
|
|
$channel->appendChild($link);
|
2022-06-09 19:33:23 +03:00
|
|
|
$link->appendChild($document->createTextNode($uri));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
$description = $document->createElement('description');
|
|
|
|
$channel->appendChild($description);
|
2022-06-09 19:33:23 +03:00
|
|
|
$description->appendChild($document->createTextNode($extraInfos['name']));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-09-25 23:32:15 +03:00
|
|
|
$allowedIconExtensions = [
|
|
|
|
'.gif',
|
|
|
|
'.jpg',
|
|
|
|
'.png',
|
|
|
|
];
|
2022-06-08 00:22:03 +03:00
|
|
|
$icon = $extraInfos['icon'];
|
2023-09-25 23:32:15 +03:00
|
|
|
if (!empty($icon) && in_array(substr($icon, -4), $allowedIconExtensions)) {
|
2022-06-08 00:22:03 +03:00
|
|
|
$feedImage = $document->createElement('image');
|
|
|
|
$channel->appendChild($feedImage);
|
|
|
|
$iconUrl = $document->createElement('url');
|
|
|
|
$iconUrl->appendChild($document->createTextNode($icon));
|
|
|
|
$feedImage->appendChild($iconUrl);
|
|
|
|
$iconTitle = $document->createElement('title');
|
|
|
|
$iconTitle->appendChild($document->createTextNode($title));
|
|
|
|
$feedImage->appendChild($iconTitle);
|
|
|
|
$iconLink = $document->createElement('link');
|
|
|
|
$iconLink->appendChild($document->createTextNode($uri));
|
|
|
|
$feedImage->appendChild($iconLink);
|
2016-09-10 22:01:02 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-09 19:33:23 +03:00
|
|
|
$linkAlternate = $document->createElementNS(self::ATOM_NS, 'link');
|
|
|
|
$channel->appendChild($linkAlternate);
|
2022-06-08 00:22:03 +03:00
|
|
|
$linkAlternate->setAttribute('rel', 'alternate');
|
|
|
|
$linkAlternate->setAttribute('type', 'text/html');
|
|
|
|
$linkAlternate->setAttribute('href', $uri);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-09 19:33:23 +03:00
|
|
|
$linkSelf = $document->createElementNS(self::ATOM_NS, 'link');
|
|
|
|
$channel->appendChild($linkSelf);
|
2022-06-08 00:22:03 +03:00
|
|
|
$linkSelf->setAttribute('rel', 'self');
|
|
|
|
$linkSelf->setAttribute('type', 'application/atom+xml');
|
|
|
|
$linkSelf->setAttribute('href', $feedUrl);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2017-07-29 20:28:00 +03:00
|
|
|
foreach ($this->getItems() as $item) {
|
2019-02-06 19:18:33 +03:00
|
|
|
$itemTimestamp = $item->getTimestamp();
|
2022-06-08 00:22:03 +03:00
|
|
|
$itemTitle = $item->getTitle();
|
|
|
|
$itemUri = $item->getURI();
|
2023-06-09 00:04:16 +03:00
|
|
|
$itemContent = $item->getContent() ? break_annoying_html_tags($item->getContent()) : '';
|
2019-02-06 19:18:33 +03:00
|
|
|
$entryID = $item->getUid();
|
|
|
|
$isPermaLink = 'false';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-08-06 23:46:28 +03:00
|
|
|
if (empty($entryID) && !empty($itemUri)) {
|
|
|
|
// Fallback to provided URI
|
2019-02-06 19:18:33 +03:00
|
|
|
$entryID = $itemUri;
|
|
|
|
$isPermaLink = 'true';
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-08-06 23:46:28 +03:00
|
|
|
if (empty($entryID)) {
|
|
|
|
// Fallback to title and content
|
2019-02-06 19:18:33 +03:00
|
|
|
$entryID = hash('sha1', $itemTitle . $itemContent);
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
$entry = $document->createElement('item');
|
2022-06-09 19:33:23 +03:00
|
|
|
$channel->appendChild($entry);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
if (!empty($itemTitle)) {
|
|
|
|
$entryTitle = $document->createElement('title');
|
|
|
|
$entry->appendChild($entryTitle);
|
2022-06-09 19:33:23 +03:00
|
|
|
$entryTitle->appendChild($document->createTextNode($itemTitle));
|
2022-06-08 00:22:03 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
if (!empty($itemUri)) {
|
|
|
|
$entryLink = $document->createElement('link');
|
|
|
|
$entry->appendChild($entryLink);
|
2022-06-09 19:33:23 +03:00
|
|
|
$entryLink->appendChild($document->createTextNode($itemUri));
|
2022-06-08 00:22:03 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
$entryGuid = $document->createElement('guid');
|
|
|
|
$entryGuid->setAttribute('isPermaLink', $isPermaLink);
|
|
|
|
$entry->appendChild($entryGuid);
|
2022-06-09 19:33:23 +03:00
|
|
|
$entryGuid->appendChild($document->createTextNode($entryID));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-02-06 19:18:33 +03:00
|
|
|
if (!empty($itemTimestamp)) {
|
2022-06-08 00:22:03 +03:00
|
|
|
$entryPublished = $document->createElement('pubDate');
|
|
|
|
$entry->appendChild($entryPublished);
|
2022-08-06 23:46:28 +03:00
|
|
|
$entryPublished->appendChild($document->createTextNode(gmdate(\DATE_RFC2822, $itemTimestamp)));
|
2019-02-06 19:18:33 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
if (!empty($itemContent)) {
|
|
|
|
$entryDescription = $document->createElement('description');
|
|
|
|
$entry->appendChild($entryDescription);
|
2022-06-09 19:33:23 +03:00
|
|
|
$entryDescription->appendChild($document->createTextNode($itemContent));
|
2022-06-08 00:22:03 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-02-06 19:18:33 +03:00
|
|
|
foreach ($item->getEnclosures() as $enclosure) {
|
2022-06-09 19:33:23 +03:00
|
|
|
$entryEnclosure = $document->createElementNS(self::MRSS_NS, 'content');
|
|
|
|
$entry->appendChild($entryEnclosure);
|
2022-06-08 00:22:03 +03:00
|
|
|
$entryEnclosure->setAttribute('url', $enclosure);
|
2022-08-06 23:46:28 +03:00
|
|
|
$entryEnclosure->setAttribute('type', parse_mime_type($enclosure));
|
2016-11-09 20:59:17 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-12-27 00:41:32 +03:00
|
|
|
foreach ($item->getCategories() as $category) {
|
2022-06-08 00:22:03 +03:00
|
|
|
$entryCategory = $document->createElement('category');
|
|
|
|
$entry->appendChild($entryCategory);
|
2022-06-09 19:33:23 +03:00
|
|
|
$entryCategory->appendChild($document->createTextNode($category));
|
2018-07-16 13:32:24 +03:00
|
|
|
}
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-09-25 23:32:15 +03:00
|
|
|
$xml = $document->saveXML();
|
2016-09-10 21:41:11 +03:00
|
|
|
// Remove invalid non-UTF8 characters
|
|
|
|
ini_set('mbstring.substitute_character', 'none');
|
2023-09-25 23:32:15 +03:00
|
|
|
$xml = mb_convert_encoding($xml, $this->getCharset(), 'UTF-8');
|
|
|
|
return $xml;
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2015-03-03 00:22:02 +03:00
|
|
|
}
|