2013-08-11 15:30:41 +04:00
|
|
|
<?php
|
|
|
|
/**
|
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';
|
|
|
|
|
2019-01-21 19:22:30 +03:00
|
|
|
const LIMIT_TITLE = 140;
|
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
public function stringify(){
|
2019-01-21 19:22:30 +03:00
|
|
|
$urlPrefix = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';
|
|
|
|
$urlHost = (isset($_SERVER['HTTP_HOST'])) ? $_SERVER['HTTP_HOST'] : '';
|
|
|
|
$urlPath = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : '';
|
|
|
|
$urlRequest = (isset($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : '';
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2019-01-21 19:22:30 +03:00
|
|
|
$feedUrl = $this->xml_encode($urlPrefix . $urlHost . $urlRequest);
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
$extraInfos = $this->getExtraInfos();
|
|
|
|
$title = $this->xml_encode($extraInfos['name']);
|
2018-11-05 21:05:59 +03:00
|
|
|
$uri = !empty($extraInfos['uri']) ? $extraInfos['uri'] : REPOSITORY;
|
2018-06-30 11:16:16 +03:00
|
|
|
|
2019-01-21 19:22:30 +03:00
|
|
|
// since we can't guarantee that all items have an author,
|
|
|
|
// a global feed author is mandatory
|
|
|
|
$feedAuthor = 'RSS-Bridge';
|
|
|
|
|
2018-06-30 11:16:16 +03:00
|
|
|
$uriparts = parse_url($uri);
|
2018-08-21 18:46:47 +03:00
|
|
|
if(!empty($extraInfos['icon'])) {
|
|
|
|
$icon = $extraInfos['icon'];
|
|
|
|
} else {
|
2018-11-05 14:55:58 +03:00
|
|
|
$icon = $this->xml_encode($uriparts['scheme'] . '://' . $uriparts['host'] . '/favicon.ico');
|
2018-08-21 18:46:47 +03:00
|
|
|
}
|
2018-06-30 11:16:16 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
$uri = $this->xml_encode($uri);
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
$entries = '';
|
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 = '';
|
2019-01-21 19:22:30 +03:00
|
|
|
|
2019-02-03 22:56:41 +03:00
|
|
|
if (!empty($item->getUid()))
|
|
|
|
$entryID = 'urn:sha1:' . $item->getUid();
|
|
|
|
|
|
|
|
if (empty($entryID)) // Fallback to provided URI
|
|
|
|
$entryID = $this->xml_encode($entryUri);
|
|
|
|
|
|
|
|
if (empty($entryID)) // Fallback to title and content
|
2019-01-21 19:22: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));
|
|
|
|
if (strlen($entryTitle) > self::LIMIT_TITLE) {
|
|
|
|
$wrapPos = strpos(wordwrap($entryTitle, self::LIMIT_TITLE), "\n");
|
|
|
|
$entryTitle = substr($entryTitle, 0, $wrapPos) . '...';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($entryContent))
|
|
|
|
$entryContent = $entryTitle;
|
|
|
|
|
2022-04-13 22:35:54 +03:00
|
|
|
$entryAuthor = '';
|
|
|
|
if ($item->getAuthor()) {
|
|
|
|
$entryAuthor = $this->xml_encode($item->getAuthor());
|
|
|
|
}
|
|
|
|
|
2019-01-21 19:22:30 +03:00
|
|
|
$entryTitle = $this->xml_encode($entryTitle);
|
|
|
|
$entryUri = $this->xml_encode($entryUri);
|
|
|
|
$entryTimestamp = $this->xml_encode(gmdate(DATE_ATOM, $entryTimestamp));
|
|
|
|
$entryContent = $this->xml_encode($this->sanitizeHtml($entryContent));
|
2016-11-09 20:59:17 +03:00
|
|
|
|
2016-11-13 00:04:42 +03:00
|
|
|
$entryEnclosures = '';
|
2018-12-27 00:41:32 +03:00
|
|
|
foreach($item->getEnclosures() as $enclosure) {
|
|
|
|
$entryEnclosures .= '<link rel="enclosure" href="'
|
|
|
|
. $this->xml_encode($enclosure)
|
|
|
|
. '" type="' . getMimeType($enclosure) . '" />'
|
|
|
|
. PHP_EOL;
|
2016-11-09 20:59:17 +03:00
|
|
|
}
|
|
|
|
|
2018-07-16 13:32:24 +03:00
|
|
|
$entryCategories = '';
|
2018-12-27 00:41:32 +03:00
|
|
|
foreach($item->getCategories() as $category) {
|
|
|
|
$entryCategories .= '<category term="'
|
|
|
|
. $this->xml_encode($category)
|
|
|
|
. '"/>'
|
|
|
|
. PHP_EOL;
|
2018-07-16 13:32:24 +03:00
|
|
|
}
|
|
|
|
|
2020-02-27 00:13:40 +03:00
|
|
|
$entryThumbnail = $item->thumbnail;
|
|
|
|
if (!empty($entryThumbnail))
|
|
|
|
$entryThumbnail = '<media:thumbnail url="' . $this->xml_encode($entryThumbnail) . '"/>';
|
|
|
|
|
2019-01-21 19:22:30 +03:00
|
|
|
$entryLinkAlternate = '';
|
|
|
|
if (!empty($entryUri)) {
|
|
|
|
$entryLinkAlternate = '<link rel="alternate" type="text/html" href="'
|
|
|
|
. $entryUri
|
|
|
|
. '"/>';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($entryAuthor)) {
|
|
|
|
$entryAuthor = '<author><name>'
|
|
|
|
. $entryAuthor
|
|
|
|
. '</name></author>';
|
|
|
|
}
|
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
$entries .= <<<EOD
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
<entry>
|
2016-11-09 00:15:53 +03:00
|
|
|
<title type="html">{$entryTitle}</title>
|
2019-01-21 19:22:30 +03:00
|
|
|
<published>{$entryTimestamp}</published>
|
2016-09-10 21:41:11 +03:00
|
|
|
<updated>{$entryTimestamp}</updated>
|
2019-01-21 19:22:30 +03:00
|
|
|
<id>{$entryID}</id>
|
|
|
|
{$entryLinkAlternate}
|
|
|
|
{$entryAuthor}
|
2016-09-10 21:41:11 +03:00
|
|
|
<content type="html">{$entryContent}</content>
|
2016-11-13 00:04:42 +03:00
|
|
|
{$entryEnclosures}
|
2018-07-16 13:32:24 +03:00
|
|
|
{$entryCategories}
|
2020-02-27 00:13:40 +03:00
|
|
|
{$entryThumbnail}
|
2016-09-10 21:41:11 +03:00
|
|
|
</entry>
|
2013-08-11 15:30:41 +04:00
|
|
|
|
|
|
|
EOD;
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2019-01-21 19:22:30 +03:00
|
|
|
$feedTimestamp = gmdate(DATE_ATOM, $this->lastModified);
|
|
|
|
$charset = $this->getCharset();
|
2014-05-14 16:27:57 +04:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
/* Data are prepared, now let's begin the "MAGIE !!!" */
|
2016-11-07 22:49:44 +03:00
|
|
|
$toReturn = <<<EOD
|
|
|
|
<?xml version="1.0" encoding="{$charset}"?>
|
2020-05-19 11:00:12 +03:00
|
|
|
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
<title type="text">{$title}</title>
|
2019-01-21 19:22:30 +03:00
|
|
|
<id>{$feedUrl}</id>
|
2016-09-10 21:41:11 +03:00
|
|
|
<icon>{$icon}</icon>
|
|
|
|
<logo>{$icon}</logo>
|
|
|
|
<updated>{$feedTimestamp}</updated>
|
2019-01-21 19:22:30 +03:00
|
|
|
<author>
|
|
|
|
<name>{$feedAuthor}</name>
|
|
|
|
</author>
|
2016-09-10 21:41:11 +03:00
|
|
|
<link rel="alternate" type="text/html" href="{$uri}" />
|
2019-01-21 19:22:30 +03:00
|
|
|
<link rel="self" type="application/atom+xml" href="{$feedUrl}" />
|
2013-08-11 15:30:41 +04:00
|
|
|
{$entries}
|
|
|
|
</feed>
|
|
|
|
EOD;
|
|
|
|
|
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');
|
2016-11-07 22:49:44 +03:00
|
|
|
$toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
|
2016-09-10 21:41:11 +03:00
|
|
|
return $toReturn;
|
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
public function display(){
|
|
|
|
$this
|
2019-10-31 21:00:12 +03:00
|
|
|
->setContentType(self::MIME_TYPE . '; charset=' . $this->getCharset())
|
2016-09-10 21:41:11 +03:00
|
|
|
->callContentType();
|
2016-08-09 17:36:31 +03:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
return parent::display();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function xml_encode($text){
|
|
|
|
return htmlspecialchars($text, ENT_XML1);
|
|
|
|
}
|
2014-10-21 22:55:18 +04:00
|
|
|
}
|