Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
rss.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Document
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * JDocumentRenderer_RSS is a feed that implements RSS 2.0 Specification
14  *
15  * @package Joomla.Platform
16  * @subpackage Document
17  * @see http://www.rssboard.org/rss-specification
18  * @since 11.1
19  */
21 {
22  /**
23  * Renderer mime type
24  *
25  * @var string
26  * @since 11.1
27  */
28  protected $_mime = "application/rss+xml";
29 
30  /**
31  * Render the feed.
32  *
33  * @param string $name The name of the element to render
34  * @param array $params Array of values
35  * @param string $content Override the output of the renderer
36  *
37  * @return string The output of the script
38  *
39  * @see JDocumentRenderer::render()
40  * @since 11.1
41  */
42  public function render($name = '', $params = null, $content = null)
43  {
44  $app = JFactory::getApplication();
45 
46  // Gets and sets timezone offset from site configuration
47  $tz = new DateTimeZone($app->getCfg('offset'));
48  $now = JFactory::getDate();
49  $now->setTimeZone($tz);
50 
51  $data = $this->_doc;
52 
53  $uri = JUri::getInstance();
54  $url = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port'));
55  $syndicationURL = JRoute::_('&format=feed&type=rss');
56 
57  if ($app->getCfg('sitename_pagetitles', 0) == 1)
58  {
59  $title = JText::sprintf('JPAGETITLE', $app->getCfg('sitename'), $data->title);
60  }
61  elseif ($app->getCfg('sitename_pagetitles', 0) == 2)
62  {
63  $title = JText::sprintf('JPAGETITLE', $data->title, $app->getCfg('sitename'));
64  }
65  else
66  {
67  $title = $data->title;
68  }
69 
70  $feed_title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8');
71 
72  $feed = "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n";
73  $feed .= " <channel>\n";
74  $feed .= " <title>" . $feed_title . "</title>\n";
75  $feed .= " <description><![CDATA[" . $data->description . "]]></description>\n";
76  $feed .= " <link>" . str_replace(' ', '%20', $url . $data->link) . "</link>\n";
77  $feed .= " <lastBuildDate>" . htmlspecialchars($now->toRFC822(true), ENT_COMPAT, 'UTF-8') . "</lastBuildDate>\n";
78  $feed .= " <generator>" . $data->getGenerator() . "</generator>\n";
79  $feed .= ' <atom:link rel="self" type="application/rss+xml" href="' . str_replace(' ', '%20', $url . $syndicationURL) . "\"/>\n";
80 
81  if ($data->image != null)
82  {
83  $feed .= " <image>\n";
84  $feed .= " <url>" . $data->image->url . "</url>\n";
85  $feed .= " <title>" . htmlspecialchars($data->image->title, ENT_COMPAT, 'UTF-8') . "</title>\n";
86  $feed .= " <link>" . str_replace(' ', '%20', $data->image->link) . "</link>\n";
87  if ($data->image->width != "")
88  {
89  $feed .= " <width>" . $data->image->width . "</width>\n";
90  }
91  if ($data->image->height != "")
92  {
93  $feed .= " <height>" . $data->image->height . "</height>\n";
94  }
95  if ($data->image->description != "")
96  {
97  $feed .= " <description><![CDATA[" . $data->image->description . "]]></description>\n";
98  }
99  $feed .= " </image>\n";
100  }
101  if ($data->language != "")
102  {
103  $feed .= " <language>" . $data->language . "</language>\n";
104  }
105  if ($data->copyright != "")
106  {
107  $feed .= " <copyright>" . htmlspecialchars($data->copyright, ENT_COMPAT, 'UTF-8') . "</copyright>\n";
108  }
109  if ($data->editorEmail != "")
110  {
111  $feed .= " <managingEditor>" . htmlspecialchars($data->editorEmail, ENT_COMPAT, 'UTF-8') . ' ('
112  . htmlspecialchars($data->editor, ENT_COMPAT, 'UTF-8') . ")</managingEditor>\n";
113  }
114  if ($data->webmaster != "")
115  {
116  $feed .= " <webMaster>" . htmlspecialchars($data->webmaster, ENT_COMPAT, 'UTF-8') . "</webMaster>\n";
117  }
118  if ($data->pubDate != "")
119  {
120  $pubDate = JFactory::getDate($data->pubDate);
121  $pubDate->setTimeZone($tz);
122  $feed .= " <pubDate>" . htmlspecialchars($pubDate->toRFC822(true), ENT_COMPAT, 'UTF-8') . "</pubDate>\n";
123  }
124  if (empty($data->category) === false)
125  {
126  if (is_array($data->category))
127  {
128  foreach ($data->category as $cat)
129  {
130  $feed .= " <category>" . htmlspecialchars($cat, ENT_COMPAT, 'UTF-8') . "</category>\n";
131  }
132  }
133  else
134  {
135  $feed .= " <category>" . htmlspecialchars($data->category, ENT_COMPAT, 'UTF-8') . "</category>\n";
136  }
137  }
138  if ($data->docs != "")
139  {
140  $feed .= " <docs>" . htmlspecialchars($data->docs, ENT_COMPAT, 'UTF-8') . "</docs>\n";
141  }
142  if ($data->ttl != "")
143  {
144  $feed .= " <ttl>" . htmlspecialchars($data->ttl, ENT_COMPAT, 'UTF-8') . "</ttl>\n";
145  }
146  if ($data->rating != "")
147  {
148  $feed .= " <rating>" . htmlspecialchars($data->rating, ENT_COMPAT, 'UTF-8') . "</rating>\n";
149  }
150  if ($data->skipHours != "")
151  {
152  $feed .= " <skipHours>" . htmlspecialchars($data->skipHours, ENT_COMPAT, 'UTF-8') . "</skipHours>\n";
153  }
154  if ($data->skipDays != "")
155  {
156  $feed .= " <skipDays>" . htmlspecialchars($data->skipDays, ENT_COMPAT, 'UTF-8') . "</skipDays>\n";
157  }
158 
159  for ($i = 0, $count = count($data->items); $i < $count; $i++)
160  {
161  if ((strpos($data->items[$i]->link, 'http://') === false) && (strpos($data->items[$i]->link, 'https://') === false))
162  {
163  $data->items[$i]->link = str_replace(' ', '%20', $url . $data->items[$i]->link);
164  }
165  $feed .= " <item>\n";
166  $feed .= " <title>" . htmlspecialchars(strip_tags($data->items[$i]->title), ENT_COMPAT, 'UTF-8') . "</title>\n";
167  $feed .= " <link>" . str_replace(' ', '%20', $data->items[$i]->link) . "</link>\n";
168 
169  if (empty($data->items[$i]->guid) === true)
170  {
171  $feed .= " <guid isPermaLink=\"true\">" . str_replace(' ', '%20', $data->items[$i]->link) . "</guid>\n";
172  }
173  else
174  {
175  $feed .= " <guid isPermaLink=\"false\">" . htmlspecialchars($data->items[$i]->guid, ENT_COMPAT, 'UTF-8') . "</guid>\n";
176  }
177 
178  $feed .= " <description><![CDATA[" . $this->_relToAbs($data->items[$i]->description) . "]]></description>\n";
179 
180  if ($data->items[$i]->authorEmail != "")
181  {
182  $feed .= " <author>"
183  . htmlspecialchars($data->items[$i]->authorEmail . ' (' . $data->items[$i]->author . ')', ENT_COMPAT, 'UTF-8') . "</author>\n";
184  }
185 
186  /*
187  * @todo: On hold
188  * if ($data->items[$i]->source!="") {
189  * $data.= " <source>".htmlspecialchars($data->items[$i]->source, ENT_COMPAT, 'UTF-8')."</source>\n";
190  * }
191  */
192 
193  if (empty($data->items[$i]->category) === false)
194  {
195  if (is_array($data->items[$i]->category))
196  {
197  foreach ($data->items[$i]->category as $cat)
198  {
199  $feed .= " <category>" . htmlspecialchars($cat, ENT_COMPAT, 'UTF-8') . "</category>\n";
200  }
201  }
202  else
203  {
204  $feed .= " <category>" . htmlspecialchars($data->items[$i]->category, ENT_COMPAT, 'UTF-8') . "</category>\n";
205  }
206  }
207  if ($data->items[$i]->comments != "")
208  {
209  $feed .= " <comments>" . htmlspecialchars($data->items[$i]->comments, ENT_COMPAT, 'UTF-8') . "</comments>\n";
210  }
211  if ($data->items[$i]->date != "")
212  {
213  $itemDate = JFactory::getDate($data->items[$i]->date);
214  $itemDate->setTimeZone($tz);
215  $feed .= " <pubDate>" . htmlspecialchars($itemDate->toRFC822(true), ENT_COMPAT, 'UTF-8') . "</pubDate>\n";
216  }
217  if ($data->items[$i]->enclosure != null)
218  {
219  $feed .= " <enclosure url=\"";
220  $feed .= $data->items[$i]->enclosure->url;
221  $feed .= "\" length=\"";
222  $feed .= $data->items[$i]->enclosure->length;
223  $feed .= "\" type=\"";
224  $feed .= $data->items[$i]->enclosure->type;
225  $feed .= "\"/>\n";
226  }
227 
228  $feed .= " </item>\n";
229  }
230  $feed .= " </channel>\n";
231  $feed .= "</rss>\n";
232  return $feed;
233  }
234 
235  /**
236  * Convert links in a text from relative to absolute
237  *
238  * @param string $text The text processed
239  *
240  * @return string Text with converted links
241  *
242  * @since 11.1
243  */
244  public function _relToAbs($text)
245  {
246  $base = JUri::base();
247  $text = preg_replace("/(href|src)=\"(?!http|ftp|https|mailto|data)([^\"]*)\"/", "$1=\"$base\$2\"", $text);
248 
249  return $text;
250  }
251 }