2013-08-11 15:30:41 +04:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2013-08-11 15:30:41 +04:00
|
|
|
/**
|
2019-01-21 19:22:30 +03:00
|
|
|
* AtomFormat - RFC 4287: The Atom Syndication Format
|
|
|
|
* https://tools.ietf.org/html/rfc4287
|
|
|
|
*
|
|
|
|
* Validator:
|
|
|
|
* https://validator.w3.org/feed/
|
|
|
|
*/
|
2013-08-11 15:30:41 +04:00
|
|
|
class AtomFormat extends FormatAbstract
|
|
|
|
{
|
2019-10-31 21:00:12 +03:00
|
|
|
const MIME_TYPE = 'application/atom+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
|
|
|
|
2024-08-23 18:34:06 +03:00
|
|
|
public function render(): string
|
2016-09-10 21:41:11 +03:00
|
|
|
{
|
2024-08-23 18:34:06 +03:00
|
|
|
$document = new \DomDocument('1.0', 'UTF-8');
|
2024-01-09 22:18:33 +03:00
|
|
|
$document->formatOutput = true;
|
2023-10-16 03:58:03 +03:00
|
|
|
|
2022-09-08 20:07:57 +03:00
|
|
|
$feedUrl = get_current_url();
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
$feed = $document->createElementNS(self::ATOM_NS, 'feed');
|
|
|
|
$document->appendChild($feed);
|
2022-06-09 19:33:23 +03:00
|
|
|
$feed->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:media', self::MRSS_NS);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2024-01-09 22:18:33 +03:00
|
|
|
$feedArray = $this->getFeed();
|
|
|
|
foreach ($feedArray as $feedKey => $feedValue) {
|
|
|
|
if (in_array($feedKey, ['donationUri'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($feedKey === 'name') {
|
|
|
|
$title = $document->createElement('title');
|
|
|
|
$feed->appendChild($title);
|
|
|
|
$title->setAttribute('type', 'text');
|
|
|
|
$title->appendChild($document->createTextNode($feedValue));
|
|
|
|
} elseif ($feedKey === 'icon') {
|
|
|
|
if ($feedValue) {
|
|
|
|
$icon = $document->createElement('icon');
|
|
|
|
$feed->appendChild($icon);
|
|
|
|
$icon->appendChild($document->createTextNode($feedValue));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2024-01-09 22:18:33 +03:00
|
|
|
$logo = $document->createElement('logo');
|
|
|
|
$feed->appendChild($logo);
|
|
|
|
$logo->appendChild($document->createTextNode($feedValue));
|
|
|
|
}
|
|
|
|
} elseif ($feedKey === 'uri') {
|
|
|
|
if ($feedValue) {
|
|
|
|
$linkAlternate = $document->createElement('link');
|
|
|
|
$feed->appendChild($linkAlternate);
|
|
|
|
$linkAlternate->setAttribute('rel', 'alternate');
|
|
|
|
$linkAlternate->setAttribute('type', 'text/html');
|
|
|
|
$linkAlternate->setAttribute('href', $feedValue);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2024-01-09 22:18:33 +03:00
|
|
|
$linkSelf = $document->createElement('link');
|
|
|
|
$feed->appendChild($linkSelf);
|
|
|
|
$linkSelf->setAttribute('rel', 'self');
|
|
|
|
$linkSelf->setAttribute('type', 'application/atom+xml');
|
|
|
|
$linkSelf->setAttribute('href', $feedUrl);
|
|
|
|
}
|
|
|
|
} elseif ($feedKey === 'itunes') {
|
|
|
|
// todo: skip?
|
|
|
|
} else {
|
|
|
|
$element = $document->createElement($feedKey);
|
|
|
|
$feed->appendChild($element);
|
|
|
|
$element->appendChild($document->createTextNode($feedValue));
|
|
|
|
}
|
2018-08-21 18:46:47 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2024-01-09 22:18:33 +03:00
|
|
|
$id = $document->createElement('id');
|
|
|
|
$feed->appendChild($id);
|
|
|
|
$id->appendChild($document->createTextNode($feedUrl));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
$feedTimestamp = gmdate(DATE_ATOM, $this->lastModified);
|
|
|
|
$updated = $document->createElement('updated');
|
|
|
|
$feed->appendChild($updated);
|
2022-06-09 19:33:23 +03:00
|
|
|
$updated->appendChild($document->createTextNode($feedTimestamp));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
// since we can't guarantee that all items have an author,
|
|
|
|
// a global feed author is mandatory
|
|
|
|
$feedAuthor = 'RSS-Bridge';
|
|
|
|
$author = $document->createElement('author');
|
2022-06-09 19:33:23 +03:00
|
|
|
$feed->appendChild($author);
|
2022-06-08 00:22:03 +03:00
|
|
|
$authorName = $document->createElement('name');
|
|
|
|
$author->appendChild($authorName);
|
2022-06-09 19:33:23 +03:00
|
|
|
$authorName->appendChild($document->createTextNode($feedAuthor));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2017-07-29 20:28:00 +03:00
|
|
|
foreach ($this->getItems() as $item) {
|
2023-10-16 03:58:03 +03:00
|
|
|
$itemArray = $item->toArray();
|
2019-01-21 19:22:30 +03:00
|
|
|
$entryTimestamp = $item->getTimestamp();
|
2019-08-28 17:29:13 +03:00
|
|
|
$entryTitle = $item->getTitle();
|
2019-01-21 19:22:30 +03:00
|
|
|
$entryContent = $item->getContent();
|
|
|
|
$entryUri = $item->getURI();
|
2019-02-03 22:56:41 +03:00
|
|
|
$entryID = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-02-03 22:56:41 +03:00
|
|
|
if (!empty($item->getUid())) {
|
|
|
|
$entryID = 'urn:sha1:' . $item->getUid();
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-08-06 23:46:28 +03:00
|
|
|
if (empty($entryID)) {
|
|
|
|
// Fallback to provided URI
|
2022-06-08 00:22:03 +03:00
|
|
|
$entryID = $entryUri;
|
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-01-21 19:22:30 +03:00
|
|
|
$entryID = 'urn:sha1:' . hash('sha1', $entryTitle . $entryContent);
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2019-01-21 19:22:30 +03:00
|
|
|
if (empty($entryTimestamp)) {
|
|
|
|
$entryTimestamp = $this->lastModified;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2019-01-21 19:22:30 +03:00
|
|
|
if (empty($entryTitle)) {
|
|
|
|
$entryTitle = str_replace("\n", ' ', strip_tags($entryContent));
|
2023-09-25 23:32:15 +03:00
|
|
|
if (strlen($entryTitle) > 140) {
|
|
|
|
$wrapPos = strpos(wordwrap($entryTitle, 140), "\n");
|
2019-01-21 19:22:30 +03:00
|
|
|
$entryTitle = substr($entryTitle, 0, $wrapPos) . '...';
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2019-01-21 19:22:30 +03:00
|
|
|
if (empty($entryContent)) {
|
2022-05-08 05:21:32 +03:00
|
|
|
$entryContent = ' ';
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
$entry = $document->createElement('entry');
|
2022-06-09 19:33:23 +03:00
|
|
|
$feed->appendChild($entry);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
$title = $document->createElement('title');
|
2022-06-09 19:33:23 +03:00
|
|
|
$entry->appendChild($title);
|
2022-06-08 00:22:03 +03:00
|
|
|
$title->setAttribute('type', 'html');
|
|
|
|
$title->appendChild($document->createTextNode($entryTitle));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-08-06 23:46:28 +03:00
|
|
|
$entryTimestamp = gmdate(\DATE_ATOM, $entryTimestamp);
|
2022-06-08 00:22:03 +03:00
|
|
|
$published = $document->createElement('published');
|
|
|
|
$entry->appendChild($published);
|
2022-06-09 19:33:23 +03:00
|
|
|
$published->appendChild($document->createTextNode($entryTimestamp));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
$updated = $document->createElement('updated');
|
|
|
|
$entry->appendChild($updated);
|
2022-06-09 19:33:23 +03:00
|
|
|
$updated->appendChild($document->createTextNode($entryTimestamp));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
$id = $document->createElement('id');
|
|
|
|
$entry->appendChild($id);
|
2022-06-09 19:33:23 +03:00
|
|
|
$id->appendChild($document->createTextNode($entryID));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-10-16 03:58:03 +03:00
|
|
|
if (isset($itemArray['itunes'])) {
|
|
|
|
$feed->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:itunes', self::ITUNES_NS);
|
|
|
|
foreach ($itemArray['itunes'] as $itunesKey => $itunesValue) {
|
|
|
|
$itunesProperty = $document->createElementNS(self::ITUNES_NS, $itunesKey);
|
|
|
|
$entry->appendChild($itunesProperty);
|
|
|
|
$itunesProperty->appendChild($document->createTextNode($itunesValue));
|
|
|
|
}
|
2023-10-19 18:02:53 +03:00
|
|
|
if (isset($itemArray['enclosure'])) {
|
|
|
|
$itunesEnclosure = $document->createElement('enclosure');
|
|
|
|
$entry->appendChild($itunesEnclosure);
|
|
|
|
$itunesEnclosure->setAttribute('url', $itemArray['enclosure']['url']);
|
|
|
|
$itunesEnclosure->setAttribute('length', $itemArray['enclosure']['length']);
|
|
|
|
$itunesEnclosure->setAttribute('type', $itemArray['enclosure']['type']);
|
|
|
|
}
|
2023-10-16 03:58:03 +03:00
|
|
|
} elseif (!empty($entryUri)) {
|
2022-06-08 00:22:03 +03:00
|
|
|
$entryLinkAlternate = $document->createElement('link');
|
2022-06-09 19:33:23 +03:00
|
|
|
$entry->appendChild($entryLinkAlternate);
|
2022-06-08 00:22:03 +03:00
|
|
|
$entryLinkAlternate->setAttribute('rel', 'alternate');
|
|
|
|
$entryLinkAlternate->setAttribute('type', 'text/html');
|
|
|
|
$entryLinkAlternate->setAttribute('href', $entryUri);
|
2016-11-09 20:59:17 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
if (!empty($item->getAuthor())) {
|
|
|
|
$author = $document->createElement('author');
|
2022-06-09 19:33:23 +03:00
|
|
|
$entry->appendChild($author);
|
2022-06-08 00:22:03 +03:00
|
|
|
$authorName = $document->createElement('name');
|
|
|
|
$author->appendChild($authorName);
|
2022-06-09 19:33:23 +03:00
|
|
|
$authorName->appendChild($document->createTextNode($item->getAuthor()));
|
2018-07-16 13:32:24 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
$content = $document->createElement('content');
|
|
|
|
$content->setAttribute('type', 'html');
|
2024-01-09 23:36:42 +03:00
|
|
|
$content->appendChild($document->createTextNode($entryContent));
|
2022-06-08 00:22:03 +03:00
|
|
|
$entry->appendChild($content);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
foreach ($item->getEnclosures() as $enclosure) {
|
|
|
|
$entryEnclosure = $document->createElement('link');
|
2022-06-09 19:33:23 +03:00
|
|
|
$entry->appendChild($entryEnclosure);
|
2022-06-08 00:22:03 +03:00
|
|
|
$entryEnclosure->setAttribute('rel', 'enclosure');
|
2022-08-06 23:46:28 +03:00
|
|
|
$entryEnclosure->setAttribute('type', parse_mime_type($enclosure));
|
2022-06-08 00:22:03 +03:00
|
|
|
$entryEnclosure->setAttribute('href', $enclosure);
|
2019-01-21 19:22:30 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
foreach ($item->getCategories() as $category) {
|
|
|
|
$entryCategory = $document->createElement('category');
|
|
|
|
$entry->appendChild($entryCategory);
|
2022-06-09 19:33:23 +03:00
|
|
|
$entryCategory->setAttribute('term', $category);
|
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($item->thumbnail)) {
|
2022-06-09 19:33:23 +03:00
|
|
|
$thumbnail = $document->createElementNS(self::MRSS_NS, 'thumbnail');
|
2022-06-08 00:22:03 +03:00
|
|
|
$entry->appendChild($thumbnail);
|
2022-06-09 19:33:23 +03:00
|
|
|
$thumbnail->setAttribute('url', $item->thumbnail);
|
2019-01-21 19:22:30 +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();
|
|
|
|
return $xml;
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2014-10-21 22:55:18 +04:00
|
|
|
}
|