AtomRenderer.php
6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Document\Renderer\Feed;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Document\DocumentRenderer;
use Joomla\CMS\Uri\Uri;
/**
* AtomRenderer is a feed that implements the atom specification
*
* Please note that just by using this class you won't automatically
* produce valid atom files. For example, you have to specify either an editor
* for the feed or an author for every single feed item.
*
* @link http://www.atomenabled.org/developers/syndication/atom-format-spec.php
* @since 3.5
*
* @property-read \Joomla\CMS\Document\FeedDocument $_doc Reference to the Document object that instantiated the renderer
*/
class AtomRenderer extends DocumentRenderer
{
/**
* Document mime type
*
* @var string
* @since 3.5
*/
protected $_mime = 'application/atom+xml';
/**
* Render the feed.
*
* @param string $name The name of the element to render
* @param array $params Array of values
* @param string $content Override the output of the renderer
*
* @return string The output of the script
*
* @see DocumentRenderer::render()
* @since 3.5
*/
public function render($name = '', $params = null, $content = null)
{
$app = \JFactory::getApplication();
// Gets and sets timezone offset from site configuration
$tz = new \DateTimeZone($app->get('offset'));
$now = \JFactory::getDate();
$now->setTimeZone($tz);
$data = $this->_doc;
$url = Uri::getInstance()->toString(array('scheme', 'user', 'pass', 'host', 'port'));
$syndicationURL = \JRoute::_('&format=feed&type=atom');
$title = $data->getTitle();
if ($app->get('sitename_pagetitles', 0) == 1)
{
$title = \JText::sprintf('JPAGETITLE', $app->get('sitename'), $data->getTitle());
}
elseif ($app->get('sitename_pagetitles', 0) == 2)
{
$title = \JText::sprintf('JPAGETITLE', $data->getTitle(), $app->get('sitename'));
}
$feed_title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8');
$feed = "<feed xmlns=\"http://www.w3.org/2005/Atom\" ";
if ($data->getLanguage() != '')
{
$feed .= " xml:lang=\"" . $data->getLanguage() . "\"";
}
$feed .= ">\n";
$feed .= " <title type=\"text\">" . $feed_title . "</title>\n";
$feed .= " <subtitle type=\"text\">" . htmlspecialchars($data->getDescription(), ENT_COMPAT, 'UTF-8') . "</subtitle>\n";
if (!empty($data->category))
{
if (is_array($data->category))
{
foreach ($data->category as $cat)
{
$feed .= " <category term=\"" . htmlspecialchars($cat, ENT_COMPAT, 'UTF-8') . "\" />\n";
}
}
else
{
$feed .= " <category term=\"" . htmlspecialchars($data->category, ENT_COMPAT, 'UTF-8') . "\" />\n";
}
}
$feed .= " <link rel=\"alternate\" type=\"text/html\" href=\"" . $url . "\"/>\n";
$feed .= " <id>" . str_replace(' ', '%20', $data->getBase()) . "</id>\n";
$feed .= " <updated>" . htmlspecialchars($now->toISO8601(true), ENT_COMPAT, 'UTF-8') . "</updated>\n";
if ($data->editor != '')
{
$feed .= " <author>\n";
$feed .= " <name>" . $data->editor . "</name>\n";
if ($data->editorEmail != '')
{
$feed .= " <email>" . htmlspecialchars($data->editorEmail, ENT_COMPAT, 'UTF-8') . "</email>\n";
}
$feed .= " </author>\n";
}
$versionHtmlEscaped = '';
if ($app->get('MetaVersion', 0))
{
$minorVersion = \JVersion::MAJOR_VERSION . '.' . \JVersion::MINOR_VERSION;
$versionHtmlEscaped = ' version="' . htmlspecialchars($minorVersion, ENT_COMPAT, 'UTF-8') . '"';
}
$feed .= " <generator uri=\"https://www.joomla.org\"" . $versionHtmlEscaped . ">" . $data->getGenerator() . "</generator>\n";
$feed .= " <link rel=\"self\" type=\"application/atom+xml\" href=\"" . str_replace(' ', '%20', $url . $syndicationURL) . "\"/>\n";
for ($i = 0, $count = count($data->items); $i < $count; $i++)
{
$itemlink = $data->items[$i]->link;
if (preg_match('/[\x80-\xFF]/', $itemlink))
{
$itemlink = implode('/', array_map('rawurlencode', explode('/', $itemlink)));
}
$feed .= " <entry>\n";
$feed .= " <title>" . htmlspecialchars(strip_tags($data->items[$i]->title), ENT_COMPAT, 'UTF-8') . "</title>\n";
$feed .= " <link rel=\"alternate\" type=\"text/html\" href=\"" . $url . $itemlink . "\"/>\n";
if ($data->items[$i]->date == '')
{
$data->items[$i]->date = $now->toUnix();
}
$itemDate = \JFactory::getDate($data->items[$i]->date);
$itemDate->setTimeZone($tz);
$feed .= " <published>" . htmlspecialchars($itemDate->toISO8601(true), ENT_COMPAT, 'UTF-8') . "</published>\n";
$feed .= " <updated>" . htmlspecialchars($itemDate->toISO8601(true), ENT_COMPAT, 'UTF-8') . "</updated>\n";
if (empty($data->items[$i]->guid))
{
$itemGuid = str_replace(' ', '%20', $url . $itemlink);
}
else
{
$itemGuid = htmlspecialchars($data->items[$i]->guid, ENT_COMPAT, 'UTF-8');
}
$feed .= " <id>" . $itemGuid . "</id>\n";
if ($data->items[$i]->author != '')
{
$feed .= " <author>\n";
$feed .= " <name>" . htmlspecialchars($data->items[$i]->author, ENT_COMPAT, 'UTF-8') . "</name>\n";
if (!empty($data->items[$i]->authorEmail))
{
$feed .= " <email>" . htmlspecialchars($data->items[$i]->authorEmail, ENT_COMPAT, 'UTF-8') . "</email>\n";
}
$feed .= " </author>\n";
}
if (!empty($data->items[$i]->description))
{
$feed .= " <summary type=\"html\">" . htmlspecialchars($this->_relToAbs($data->items[$i]->description), ENT_COMPAT, 'UTF-8') . "</summary>\n";
$feed .= " <content type=\"html\">" . htmlspecialchars($this->_relToAbs($data->items[$i]->description), ENT_COMPAT, 'UTF-8') . "</content>\n";
}
if (!empty($data->items[$i]->category))
{
if (is_array($data->items[$i]->category))
{
foreach ($data->items[$i]->category as $cat)
{
$feed .= " <category term=\"" . htmlspecialchars($cat, ENT_COMPAT, 'UTF-8') . "\" />\n";
}
}
else
{
$feed .= " <category term=\"" . htmlspecialchars($data->items[$i]->category, ENT_COMPAT, 'UTF-8') . "\" />\n";
}
}
if ($data->items[$i]->enclosure != null)
{
$feed .= " <link rel=\"enclosure\" href=\"" . $data->items[$i]->enclosure->url . "\" type=\""
. $data->items[$i]->enclosure->type . "\" length=\"" . $data->items[$i]->enclosure->length . "\" />\n";
}
$feed .= " </entry>\n";
}
$feed .= "</feed>\n";
return $feed;
}
}