/mvc/helpers/rss
[return to app]1
<?php
2 /**
3 * RSS Helper class file.
4 *
5 * This is CakePHP 2.0 RssHelper class updated to PHP5 syntax for Vork
6 *
7 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
9 *
10 * Licensed under The MIT License
11 * Redistributions of files must retain the above copyright notice.
12 *
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14 * @link http://cakephp.org CakePHP(tm) Project
15 * @package cake.libs.view.helpers
16 * @since CakePHP(tm) v 1.2
17 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
18 */
19
20 /**
21 * RSS Helper class for easy output RSS structures.
22 *
23 * @package cake.libs.view.helpers
24 * @link http://book.cakephp.org/view/1460/RSS
25 */
26 class rssHelper{
27
28 /**
29 * Helpers used by RSS Helper
30 *
31 * @var array
32 * @access public
33 */
34 public $helpers = array('Time');
35
36 /**
37 * Base URL
38 *
39 * @access public
40 * @var string
41 */
42 public $base = null;
43
44 /**
45 * URL to current action.
46 *
47 * @access public
48 * @var string
49 */
50 public $here = null;
51
52 /**
53 * Parameter array.
54 *
55 * @access public
56 * @var array
57 */
58 public $params = array();
59
60 /**
61 * Current action.
62 *
63 * @access public
64 * @var string
65 */
66 public $action = null;
67
68 /**
69 * POSTed model data
70 *
71 * @access public
72 * @var array
73 */
74 public $data = null;
75
76 /**
77 * Name of the current model
78 *
79 * @access public
80 * @var string
81 */
82 public $model = null;
83
84 /**
85 * Name of the current field
86 *
87 * @access public
88 * @var string
89 */
90 public $field = null;
91
92 /**
93 * Default spec version of generated RSS
94 *
95 * @access public
96 * @var string
97 */
98 public $version = '2.0';
99
100 /**
101 * Returns an RSS document wrapped in `<rss />` tags
102 *
103 * @param array $attrib `<rss />` tag attributes
104 * @return string An RSS document
105 */
106 public function document($attrib = array(), $content = null) {
107 if ($content === null) {
108 $content = $attrib;
109 $attrib = array();
110 }
111 if (!isset($attrib['version']) || empty($attrib['version'])) {
112 $attrib['version'] = $this->version;
113 }
114
115 return $this->elem('rss', $attrib, $content);
116 }
117
118 /**
119 * Returns an RSS `<channel />` element
120 *
121 * @param array $attrib `<channel />` tag attributes
122 * @param mixed $elements Named array elements which are converted to tags
123 * @param mixed $content Content (`<item />`'s belonging to this channel
124 * @return string An RSS `<channel />`
125 */
126 public function channel($attrib = array(), $elements = array(), $content = null) {
127 if (!isset($elements['title']) && !empty($this->_View->pageTitle)) {
128 $elements['title'] = $this->_View->pageTitle;
129 }
130 if (!isset($elements['link'])) {
131 $elements['link'] = '/';
132 }
133 if (!isset($elements['description'])) {
134 $elements['description'] = '';
135 }
136 //Commented by Ajay
137 //$elements['link'] = $this->url($elements['link'], true);
138
139 $elems = '';
140 foreach ($elements as $elem => $data) {
141 $attributes = array();
142 if (is_array($data)) {
143 if (strtolower($elem) == 'cloud') {
144 $attributes = $data;
145 $data = array();
146 } elseif (isset($data['attrib']) && is_array($data['attrib'])) {
147 $attributes = $data['attrib'];
148 unset($data['attrib']);
149 } else {
150 $innerElements = '';
151 foreach ($data as $subElement => $value) {
152 $innerElements .= $this->elem($subElement, array(), $value);
153 }
154 $data = $innerElements;
155 }
156 }
157 $elems .= $this->elem($elem, $attributes, $data);
158 }
159 return $this->elem('channel', $attrib, $elems . $content, !($content === null));
160 }
161
162 /**
163 * Transforms an array of data using an optional callback, and maps it to a set
164 * of `<item />` tags
165 *
166 * @param array $items The list of items to be mapped
167 * @param mixed $callback A string function name, or array containing an object
168 * and a string method name
169 * @return string A set of RSS `<item />` elements
170 */
171 public function items($items, $callback = null) {
172 if ($callback != null) {
173 $items = array_map($callback, $items);
174 }
175
176 $out = '';
177 $c = count($items);
178
179 for ($i = 0; $i < $c; $i++) {
180 $out .= $this->item(array(), $items[$i]);
181 }
182 return $out;
183 }
184
185 /**
186 * Converts an array into an `<item />` element and its contents
187 *
188 * @param array $attrib The attributes of the `<item />` element
189 * @param array $elements The list of elements contained in this `<item />`
190 * @return string An RSS `<item />` element
191 */
192 public function item($att = array(), $elements = array()) {
193 $content = null;
194
195 if (isset($elements['link']) && !isset($elements['guid'])) {
196 $elements['guid'] = $elements['link'];
197 }
198
199 foreach ($elements as $key => $val) {
200 $attrib = array();
201
202 $escape = true;
203 if (is_array($val) && isset($val['convertEntities'])) {
204 $escape = $val['convertEntities'];
205 unset($val['convertEntities']);
206 }
207
208 switch ($key) {
209 case 'pubDate' :
210 $val = $this->time($val);
211 break;
212 case 'category' :
213 if (is_array($val) && !empty($val[0])) {
214 foreach ($val as $category) {
215 $attrib = array();
216 if (isset($category['domain'])) {
217 $attrib['domain'] = $category['domain'];
218 unset($category['domain']);
219 }
220 $categories[] = $this->elem($key, $attrib, $category);
221 }
222 $elements[$key] = implode('', $categories);
223 continue 2;
224 } else if (is_array($val) && isset($val['domain'])) {
225 $attrib['domain'] = $val['domain'];
226 }
227 break;
228 case 'link':
229 case 'guid':
230 case 'comments':
231 if (is_array($val) && isset($val['url'])) {
232 $attrib = $val;
233 unset($attrib['url']);
234 $val = $val['url'];
235 }
236 //$val = $this->url($val, true);
237 break;
238 case 'source':
239 if (is_array($val) && isset($val['url'])) {
240 $attrib['url'] = $this->url($val['url'], true);
241 $val = $val['title'];
242 } elseif (is_array($val)) {
243 $attrib['url'] = $this->url($val[0], true);
244 $val = $val[1];
245 }
246 break;
247 case 'enclosure':
248 if (is_string($val['url']) && is_file(WWW_ROOT . $val['url'])
249 && file_exists(WWW_ROOT . $val['url'])) {
250 if (!isset($val['length']) && strpos($val['url'], '://') === false) {
251 $val['length']
252 = sprintf("%u", filesize(WWW_ROOT . $val['url']));
253 }
254 if (!isset($val['type']) && function_exists('mime_content_type')) {
255 $val['type'] = mime_content_type(WWW_ROOT . $val['url']);
256 }
257 }
258 $val['url'] = $this->url($val['url'], true);
259 $attrib = $val;
260 $val = null;
261 break;
262 }
263 if (!is_null($val) && $escape) {
264 $basics = get::helper('cakeBasics');
265 $val = $basics->h($val);
266 }
267 $elements[$key] = $this->elem($key, $attrib, $val);
268 }
269 if (!empty($elements)) {
270 $content = implode('', $elements);
271 }
272 return $this->elem('item', (array)$att, $content, !($content === null));
273 }
274
275 /**
276 * Converts a time in any format to an RSS time
277 *
278 * @param mixed $time
279 * @return string An RSS-formatted timestamp
280 * @see TimeHelper::toRSS
281 */
282 function time($time) {
283 $timehelper = get::helper('time');
284 return $timehelper->toRSS($time);
285 //return $time;
286 }
287
288 /**
289 * Generates an XML element
290 *
291 * @param string $name The name of the XML element
292 * @param array $attrib The attributes of the XML element
293 * @param mixed $content XML element content
294 * @param boolean $endTag Whether the end tag of the element should be printed
295 * @return string XML
296 */
297 function elem($name, $attrib = array(), $content = null, $endTag = true) {
298 $namespace = null;
299 if (isset($attrib['namespace'])) {
300 $namespace = $attrib['namespace'];
301 unset($attrib['namespace']);
302 }
303 $cdata = false;
304 if (is_array($content) && isset($content['cdata'])) {
305 $cdata = true;
306 unset($content['cdata']);
307 }
308 if (is_array($content) && array_key_exists('value', $content)) {
309 $content = $content['value'];
310 }
311 $children = array();
312 if (is_array($content)) {
313 $children = $content;
314 $content = null;
315 }
316
317 $xml = '<' . $name;
318 if (!empty($namespace)) {
319 $xml .= ' xmlns:"' . $namespace . '"';
320 }
321 $bareName = $name;
322 if (strpos($name, ':') !== false) {
323 list($prefix, $bareName) = explode(':', $name, 2);
324 switch ($prefix) {
325 case 'atom':
326 $xml .= ' xmlns:atom="http://www.w3.org/2005/Atom"';
327 break;
328 }
329 }
330 if ($cdata && !empty($content)) {
331 $content = '<![CDATA[' . $content . ']]>';
332 }
333 $xml .= '>' . $content . '</' . $name. '>';
334 $xmlhelper = get::helper('xml');
335 $elem = $xmlhelper->build($xml, array('return' => 'domdocument'));
336 $nodes = $elem->getElementsByTagName($bareName);
337 foreach ($attrib as $key => $value) {
338
339
340
341 $nodes->item(0)->setAttribute($key, $value);
342 }
343 foreach ($children as $k => $child) {
344 $child = $elem->createElement($name, $child);
345 $nodes->item(0)->appendChild($child);
346 }
347
348 $xml = $elem->saveXml();
349 $xml = trim(substr($xml, strpos($xml, '?>') + 2));
350 return $xml;
351 }
352 }
353