Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
atom.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_Atom is a feed that implements the atom specification
14  *
15  * Please note that just by using this class you won't automatically
16  * produce valid atom files. For example, you have to specify either an editor
17  * for the feed or an author for every single feed item.
18  *
19  * @package Joomla.Platform
20  * @subpackage Document
21  * @see http://www.atomenabled.org/developers/syndication/atom-format-spec.php
22  * @since 11.1
23  */
25 {
26  /**
27  * Document mime type
28  *
29  * @var string
30  * @since 11.1
31  */
32  protected $_mime = "application/atom+xml";
33 
34  /**
35  * Render the feed.
36  *
37  * @param string $name The name of the element to render
38  * @param array $params Array of values
39  * @param string $content Override the output of the renderer
40  *
41  * @return string The output of the script
42  *
43  * @see JDocumentRenderer::render()
44  * @since 11.1
45  */
46  public function render($name = '', $params = null, $content = null)
47  {
48  $app = JFactory::getApplication();
49 
50  // Gets and sets timezone offset from site configuration
51  $tz = new DateTimeZone($app->getCfg('offset'));
52  $now = JFactory::getDate();
53  $now->setTimeZone($tz);
54 
55  $data = $this->_doc;
56 
57  $uri = JUri::getInstance();
58  $url = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port'));
59  $syndicationURL = JRoute::_('&format=feed&type=atom');
60 
61  if ($app->getCfg('sitename_pagetitles', 0) == 1)
62  {
63  $title = JText::sprintf('JPAGETITLE', $app->getCfg('sitename'), $data->title);
64  }
65  elseif ($app->getCfg('sitename_pagetitles', 0) == 2)
66  {
67  $title = JText::sprintf('JPAGETITLE', $data->title, $app->getCfg('sitename'));
68  }
69  else
70  {
71  $title = $data->title;
72  }
73 
74  $feed_title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8');
75 
76  $feed = "<feed xmlns=\"http://www.w3.org/2005/Atom\" ";
77  if ($data->language != "")
78  {
79  $feed .= " xml:lang=\"" . $data->language . "\"";
80  }
81  $feed .= ">\n";
82  $feed .= " <title type=\"text\">" . $feed_title . "</title>\n";
83  $feed .= " <subtitle type=\"text\">" . htmlspecialchars($data->description, ENT_COMPAT, 'UTF-8') . "</subtitle>\n";
84  if (empty($data->category) === false)
85  {
86  if (is_array($data->category))
87  {
88  foreach ($data->category as $cat)
89  {
90  $feed .= " <category term=\"" . htmlspecialchars($cat, ENT_COMPAT, 'UTF-8') . "\" />\n";
91  }
92  }
93  else
94  {
95  $feed .= " <category term=\"" . htmlspecialchars($data->category, ENT_COMPAT, 'UTF-8') . "\" />\n";
96  }
97  }
98  $feed .= " <link rel=\"alternate\" type=\"text/html\" href=\"" . $url . "\"/>\n";
99  $feed .= " <id>" . str_replace(' ', '%20', $data->getBase()) . "</id>\n";
100  $feed .= " <updated>" . htmlspecialchars($now->toISO8601(true), ENT_COMPAT, 'UTF-8') . "</updated>\n";
101  if ($data->editor != "")
102  {
103  $feed .= " <author>\n";
104  $feed .= " <name>" . $data->editor . "</name>\n";
105  if ($data->editorEmail != "")
106  {
107  $feed .= " <email>" . htmlspecialchars($data->editorEmail, ENT_COMPAT, 'UTF-8') . "</email>\n";
108  }
109  $feed .= " </author>\n";
110  }
111  $feed .= " <generator uri=\"http://joomla.org\" version=\"1.6\">" . $data->getGenerator() . "</generator>\n";
112  $feed .= ' <link rel="self" type="application/atom+xml" href="' . str_replace(' ', '%20', $url . $syndicationURL) . "\"/>\n";
113 
114  for ($i = 0, $count = count($data->items); $i < $count; $i++)
115  {
116  $feed .= " <entry>\n";
117  $feed .= " <title>" . htmlspecialchars(strip_tags($data->items[$i]->title), ENT_COMPAT, 'UTF-8') . "</title>\n";
118  $feed .= ' <link rel="alternate" type="text/html" href="' . $url . $data->items[$i]->link . "\"/>\n";
119 
120  if ($data->items[$i]->date == "")
121  {
122  $data->items[$i]->date = $now->toUnix();
123  }
124  $itemDate = JFactory::getDate($data->items[$i]->date);
125  $itemDate->setTimeZone($tz);
126  $feed .= " <published>" . htmlspecialchars($itemDate->toISO8601(true), ENT_COMPAT, 'UTF-8') . "</published>\n";
127  $feed .= " <updated>" . htmlspecialchars($itemDate->toISO8601(true), ENT_COMPAT, 'UTF-8') . "</updated>\n";
128  if (empty($data->items[$i]->guid) === true)
129  {
130  $feed .= " <id>" . str_replace(' ', '%20', $url . $data->items[$i]->link) . "</id>\n";
131  }
132  else
133  {
134  $feed .= " <id>" . htmlspecialchars($data->items[$i]->guid, ENT_COMPAT, 'UTF-8') . "</id>\n";
135  }
136 
137  if ($data->items[$i]->author != "")
138  {
139  $feed .= " <author>\n";
140  $feed .= " <name>" . htmlspecialchars($data->items[$i]->author, ENT_COMPAT, 'UTF-8') . "</name>\n";
141  if ($data->items[$i]->authorEmail != "")
142  {
143  $feed .= " <email>" . htmlspecialchars($data->items[$i]->authorEmail, ENT_COMPAT, 'UTF-8') . "</email>\n";
144  }
145  $feed .= " </author>\n";
146  }
147  if ($data->items[$i]->description != "")
148  {
149  $feed .= " <summary type=\"html\">" . htmlspecialchars($data->items[$i]->description, ENT_COMPAT, 'UTF-8') . "</summary>\n";
150  $feed .= " <content type=\"html\">" . htmlspecialchars($data->items[$i]->description, ENT_COMPAT, 'UTF-8') . "</content>\n";
151  }
152  if (empty($data->items[$i]->category) === false)
153  {
154  if (is_array($data->items[$i]->category))
155  {
156  foreach ($data->items[$i]->category as $cat)
157  {
158  $feed .= " <category term=\"" . htmlspecialchars($cat, ENT_COMPAT, 'UTF-8') . "\" />\n";
159  }
160  }
161  else
162  {
163  $feed .= " <category term=\"" . htmlspecialchars($data->items[$i]->category, ENT_COMPAT, 'UTF-8') . "\" />\n";
164  }
165  }
166  if ($data->items[$i]->enclosure != null)
167  {
168  $feed .= " <link rel=\"enclosure\" href=\"" . $data->items[$i]->enclosure->url . "\" type=\""
169  . $data->items[$i]->enclosure->type . "\" length=\"" . $data->items[$i]->enclosure->length . "\" />\n";
170  }
171  $feed .= " </entry>\n";
172  }
173  $feed .= "</feed>\n";
174  return $feed;
175  }
176 }