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
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
public function stringify()
|
|
|
|
{
|
2022-09-08 20:07:57 +03:00
|
|
|
$feedUrl = get_current_url();
|
2022-07-01 16:10:30 +03:00
|
|
|
|
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->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
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
$title = $document->createElement('title');
|
2022-06-09 19:33:23 +03:00
|
|
|
$feed->appendChild($title);
|
2022-06-08 00:22:03 +03:00
|
|
|
$title->setAttribute('type', 'text');
|
|
|
|
$title->appendChild($document->createTextNode($extraInfos['name']));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
$id = $document->createElement('id');
|
|
|
|
$feed->appendChild($id);
|
2022-06-09 19:33:23 +03:00
|
|
|
$id->appendChild($document->createTextNode($feedUrl));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-06-30 11:16:16 +03:00
|
|
|
$uriparts = parse_url($uri);
|
2022-08-06 23:46:28 +03:00
|
|
|
if (empty($extraInfos['icon'])) {
|
2022-06-08 00:22:03 +03:00
|
|
|
$iconUrl = $uriparts['scheme'] . '://' . $uriparts['host'] . '/favicon.ico';
|
2022-08-06 23:46:28 +03:00
|
|
|
} else {
|
|
|
|
$iconUrl = $extraInfos['icon'];
|
2018-08-21 18:46:47 +03:00
|
|
|
}
|
2022-06-08 00:22:03 +03:00
|
|
|
$icon = $document->createElement('icon');
|
|
|
|
$feed->appendChild($icon);
|
2022-06-09 19:33:23 +03:00
|
|
|
$icon->appendChild($document->createTextNode($iconUrl));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
$logo = $document->createElement('logo');
|
|
|
|
$feed->appendChild($logo);
|
2022-06-09 19:33:23 +03:00
|
|
|
$logo->appendChild($document->createTextNode($iconUrl));
|
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
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
$linkAlternate = $document->createElement('link');
|
2022-06-09 19:33:23 +03:00
|
|
|
$feed->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-08 00:22:03 +03:00
|
|
|
$linkSelf = $document->createElement('link');
|
2022-06-09 19:33:23 +03:00
|
|
|
$feed->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-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
|
|
|
|
2022-06-08 00:22:03 +03:00
|
|
|
if (!empty($entryUri)) {
|
|
|
|
$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');
|
2023-06-09 00:04:16 +03:00
|
|
|
$content->appendChild($document->createTextNode(break_annoying_html_tags($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();
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2016-11-07 22:49:44 +03:00
|
|
|
// Remove invalid characters
|
2016-09-10 21:41:11 +03:00
|
|
|
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
|
|
|
}
|
2014-10-21 22:55:18 +04:00
|
|
|
}
|