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/
|
|
|
|
*/
|
2022-07-01 16:10:30 +03:00
|
|
|
class AtomFormat extends FormatAbstract
|
|
|
|
{
|
|
|
|
const MIME_TYPE = 'application/atom+xml';
|
|
|
|
|
|
|
|
protected const ATOM_NS = 'http://www.w3.org/2005/Atom';
|
|
|
|
protected const MRSS_NS = 'http://search.yahoo.com/mrss/';
|
|
|
|
|
2024-08-23 18:34:06 +03:00
|
|
|
public function render(): string
|
2022-07-01 16:10:30 +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
|
|
|
|
|
|
|
$feed = $document->createElementNS(self::ATOM_NS, 'feed');
|
|
|
|
$document->appendChild($feed);
|
|
|
|
$feed->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:media', self::MRSS_NS);
|
|
|
|
|
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));
|
|
|
|
}
|
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
|
|
|
|
|
|
|
$feedTimestamp = gmdate(DATE_ATOM, $this->lastModified);
|
|
|
|
$updated = $document->createElement('updated');
|
|
|
|
$feed->appendChild($updated);
|
|
|
|
$updated->appendChild($document->createTextNode($feedTimestamp));
|
|
|
|
|
|
|
|
// since we can't guarantee that all items have an author,
|
|
|
|
// a global feed author is mandatory
|
|
|
|
$feedAuthor = 'RSS-Bridge';
|
|
|
|
$author = $document->createElement('author');
|
|
|
|
$feed->appendChild($author);
|
|
|
|
$authorName = $document->createElement('name');
|
|
|
|
$author->appendChild($authorName);
|
|
|
|
$authorName->appendChild($document->createTextNode($feedAuthor));
|
|
|
|
|
|
|
|
foreach ($this->getItems() as $item) {
|
2023-10-16 03:58:03 +03:00
|
|
|
$itemArray = $item->toArray();
|
2022-07-01 16:10:30 +03:00
|
|
|
$entryTimestamp = $item->getTimestamp();
|
|
|
|
$entryTitle = $item->getTitle();
|
|
|
|
$entryContent = $item->getContent();
|
|
|
|
$entryUri = $item->getURI();
|
|
|
|
$entryID = '';
|
|
|
|
|
|
|
|
if (!empty($item->getUid())) {
|
|
|
|
$entryID = 'urn:sha1:' . $item->getUid();
|
|
|
|
}
|
|
|
|
|
2022-08-06 23:46:28 +03:00
|
|
|
if (empty($entryID)) {
|
|
|
|
// Fallback to provided URI
|
2022-07-01 16:10:30 +03:00
|
|
|
$entryID = $entryUri;
|
|
|
|
}
|
|
|
|
|
2022-08-06 23:46:28 +03:00
|
|
|
if (empty($entryID)) {
|
|
|
|
// Fallback to title and content
|
2022-07-01 16:10:30 +03:00
|
|
|
$entryID = 'urn:sha1:' . hash('sha1', $entryTitle . $entryContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($entryTimestamp)) {
|
|
|
|
$entryTimestamp = $this->lastModified;
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2022-07-01 16:10:30 +03:00
|
|
|
$entryTitle = substr($entryTitle, 0, $wrapPos) . '...';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($entryContent)) {
|
|
|
|
$entryContent = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
$entry = $document->createElement('entry');
|
|
|
|
$feed->appendChild($entry);
|
|
|
|
|
|
|
|
$title = $document->createElement('title');
|
|
|
|
$entry->appendChild($title);
|
|
|
|
$title->setAttribute('type', 'html');
|
|
|
|
$title->appendChild($document->createTextNode($entryTitle));
|
|
|
|
|
2022-08-06 23:46:28 +03:00
|
|
|
$entryTimestamp = gmdate(\DATE_ATOM, $entryTimestamp);
|
2022-07-01 16:10:30 +03:00
|
|
|
$published = $document->createElement('published');
|
|
|
|
$entry->appendChild($published);
|
|
|
|
$published->appendChild($document->createTextNode($entryTimestamp));
|
|
|
|
|
|
|
|
$updated = $document->createElement('updated');
|
|
|
|
$entry->appendChild($updated);
|
|
|
|
$updated->appendChild($document->createTextNode($entryTimestamp));
|
|
|
|
|
|
|
|
$id = $document->createElement('id');
|
|
|
|
$entry->appendChild($id);
|
|
|
|
$id->appendChild($document->createTextNode($entryID));
|
|
|
|
|
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-07-01 16:10:30 +03:00
|
|
|
$entryLinkAlternate = $document->createElement('link');
|
|
|
|
$entry->appendChild($entryLinkAlternate);
|
|
|
|
$entryLinkAlternate->setAttribute('rel', 'alternate');
|
|
|
|
$entryLinkAlternate->setAttribute('type', 'text/html');
|
|
|
|
$entryLinkAlternate->setAttribute('href', $entryUri);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($item->getAuthor())) {
|
|
|
|
$author = $document->createElement('author');
|
|
|
|
$entry->appendChild($author);
|
|
|
|
$authorName = $document->createElement('name');
|
|
|
|
$author->appendChild($authorName);
|
|
|
|
$authorName->appendChild($document->createTextNode($item->getAuthor()));
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = $document->createElement('content');
|
|
|
|
$content->setAttribute('type', 'html');
|
2024-01-09 23:36:42 +03:00
|
|
|
$content->appendChild($document->createTextNode($entryContent));
|
2022-07-01 16:10:30 +03:00
|
|
|
$entry->appendChild($content);
|
|
|
|
|
|
|
|
foreach ($item->getEnclosures() as $enclosure) {
|
|
|
|
$entryEnclosure = $document->createElement('link');
|
|
|
|
$entry->appendChild($entryEnclosure);
|
|
|
|
$entryEnclosure->setAttribute('rel', 'enclosure');
|
2022-08-06 23:46:28 +03:00
|
|
|
$entryEnclosure->setAttribute('type', parse_mime_type($enclosure));
|
2022-07-01 16:10:30 +03:00
|
|
|
$entryEnclosure->setAttribute('href', $enclosure);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($item->getCategories() as $category) {
|
|
|
|
$entryCategory = $document->createElement('category');
|
|
|
|
$entry->appendChild($entryCategory);
|
|
|
|
$entryCategory->setAttribute('term', $category);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($item->thumbnail)) {
|
|
|
|
$thumbnail = $document->createElementNS(self::MRSS_NS, 'thumbnail');
|
|
|
|
$entry->appendChild($thumbnail);
|
|
|
|
$thumbnail->setAttribute('url', $item->thumbnail);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 23:32:15 +03:00
|
|
|
$xml = $document->saveXML();
|
|
|
|
return $xml;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2014-10-21 22:55:18 +04:00
|
|
|
}
|