| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* SVN FILE: $Id: rss.php 7945 2008-12-19 02:16:01Z gwoo $ */
|
|
|
3 |
/**
|
|
|
4 |
* RSS Helper class file.
|
|
|
5 |
*
|
|
|
6 |
* Simplifies the output of RSS feeds.
|
|
|
7 |
*
|
|
|
8 |
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
|
|
9 |
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
10 |
*
|
|
|
11 |
* Licensed under The MIT License
|
|
|
12 |
* Redistributions of files must retain the above copyright notice.
|
|
|
13 |
*
|
|
|
14 |
* @filesource
|
|
|
15 |
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
16 |
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
17 |
* @package cake
|
|
|
18 |
* @subpackage cake.cake.libs.view.helpers
|
|
|
19 |
* @since CakePHP(tm) v 1.2
|
|
|
20 |
* @version $Revision: 7945 $
|
|
|
21 |
* @modifiedby $LastChangedBy: gwoo $
|
|
|
22 |
* @lastmodified $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
|
|
|
23 |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
24 |
*/
|
|
|
25 |
App::import('Helper', 'Xml');
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* XML Helper class for easy output of XML structures.
|
|
|
29 |
*
|
|
|
30 |
* XmlHelper encloses all methods needed while working with XML documents.
|
|
|
31 |
*
|
|
|
32 |
* @package cake
|
|
|
33 |
* @subpackage cake.cake.libs.view.helpers
|
|
|
34 |
*/
|
|
|
35 |
class RssHelper extends XmlHelper {
|
|
|
36 |
/**
|
|
|
37 |
* Helpers used by RSS Helper
|
|
|
38 |
*
|
|
|
39 |
* @var array
|
|
|
40 |
* @access public
|
|
|
41 |
**/
|
|
|
42 |
var $helpers = array('Time');
|
|
|
43 |
/**
|
|
|
44 |
* Base URL
|
|
|
45 |
*
|
|
|
46 |
* @access public
|
|
|
47 |
* @var string
|
|
|
48 |
*/
|
|
|
49 |
var $base = null;
|
|
|
50 |
/**
|
|
|
51 |
* URL to current action.
|
|
|
52 |
*
|
|
|
53 |
* @access public
|
|
|
54 |
* @var string
|
|
|
55 |
*/
|
|
|
56 |
var $here = null;
|
|
|
57 |
/**
|
|
|
58 |
* Parameter array.
|
|
|
59 |
*
|
|
|
60 |
* @access public
|
|
|
61 |
* @var array
|
|
|
62 |
*/
|
|
|
63 |
var $params = array();
|
|
|
64 |
/**
|
|
|
65 |
* Current action.
|
|
|
66 |
*
|
|
|
67 |
* @access public
|
|
|
68 |
* @var string
|
|
|
69 |
*/
|
|
|
70 |
var $action = null;
|
|
|
71 |
/**
|
|
|
72 |
* POSTed model data
|
|
|
73 |
*
|
|
|
74 |
* @access public
|
|
|
75 |
* @var array
|
|
|
76 |
*/
|
|
|
77 |
var $data = null;
|
|
|
78 |
/**
|
|
|
79 |
* Name of the current model
|
|
|
80 |
*
|
|
|
81 |
* @access public
|
|
|
82 |
* @var string
|
|
|
83 |
*/
|
|
|
84 |
var $model = null;
|
|
|
85 |
/**
|
|
|
86 |
* Name of the current field
|
|
|
87 |
*
|
|
|
88 |
* @access public
|
|
|
89 |
* @var string
|
|
|
90 |
*/
|
|
|
91 |
var $field = null;
|
|
|
92 |
/**
|
|
|
93 |
* Default spec version of generated RSS
|
|
|
94 |
*
|
|
|
95 |
* @access public
|
|
|
96 |
* @var string
|
|
|
97 |
*/
|
|
|
98 |
var $version = '2.0';
|
|
|
99 |
/**
|
|
|
100 |
* Returns an RSS document wrapped in <rss /> tags
|
|
|
101 |
*
|
|
|
102 |
* @param array $attrib <rss /> tag attributes
|
|
|
103 |
* @return string An RSS document
|
|
|
104 |
*/
|
|
|
105 |
function document($attrib = array(), $content = null) {
|
|
|
106 |
if ($content === null) {
|
|
|
107 |
$content = $attrib;
|
|
|
108 |
$attrib = array();
|
|
|
109 |
}
|
|
|
110 |
if (!isset($attrib['version']) || empty($attrib['version'])) {
|
|
|
111 |
$attrib['version'] = $this->version;
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
return $this->elem('rss', $attrib, $content);
|
|
|
115 |
}
|
|
|
116 |
/**
|
|
|
117 |
* Returns an RSS <channel /> element
|
|
|
118 |
*
|
|
|
119 |
* @param array $attrib <channel /> tag attributes
|
|
|
120 |
* @param mixed $elements Named array elements which are converted to tags
|
|
|
121 |
* @param mixed $content Content (<item />'s belonging to this channel
|
|
|
122 |
* @return string An RSS <channel />
|
|
|
123 |
*/
|
|
|
124 |
function channel($attrib = array(), $elements = array(), $content = null) {
|
|
|
125 |
$view =& ClassRegistry::getObject('view');
|
|
|
126 |
|
|
|
127 |
if (!isset($elements['title']) && !empty($view->pageTitle)) {
|
|
|
128 |
$elements['title'] = $view->pageTitle;
|
|
|
129 |
}
|
|
|
130 |
if (!isset($elements['link'])) {
|
|
|
131 |
$elements['link'] = '/';
|
|
|
132 |
}
|
|
|
133 |
if (!isset($elements['description'])) {
|
|
|
134 |
$elements['description'] = '';
|
|
|
135 |
}
|
|
|
136 |
$elements['link'] = $this->url($elements['link'], true);
|
|
|
137 |
|
|
|
138 |
$elems = '';
|
|
|
139 |
foreach ($elements as $elem => $data) {
|
|
|
140 |
$attributes = array();
|
|
|
141 |
if (is_array($data)) {
|
|
|
142 |
if (strtolower($elem) == 'cloud') {
|
|
|
143 |
$attributes = $data;
|
|
|
144 |
$data = array();
|
|
|
145 |
} elseif (isset($data['attrib']) && is_array($data['attrib'])) {
|
|
|
146 |
$attributes = $data['attrib'];
|
|
|
147 |
unset($data['attrib']);
|
|
|
148 |
} else {
|
|
|
149 |
$innerElements = '';
|
|
|
150 |
foreach ($data as $subElement => $value) {
|
|
|
151 |
$innerElements .= $this->elem($subElement, array(), $value);
|
|
|
152 |
}
|
|
|
153 |
$data = $innerElements;
|
|
|
154 |
}
|
|
|
155 |
}
|
|
|
156 |
$elems .= $this->elem($elem, $attributes, $data);
|
|
|
157 |
}
|
|
|
158 |
return $this->elem('channel', $attrib, $elems . $content, !($content === null));
|
|
|
159 |
}
|
|
|
160 |
/**
|
|
|
161 |
* Transforms an array of data using an optional callback, and maps it to a set
|
|
|
162 |
* of <item /> tags
|
|
|
163 |
*
|
|
|
164 |
* @param array $items The list of items to be mapped
|
|
|
165 |
* @param mixed $callback A string function name, or array containing an object
|
|
|
166 |
* and a string method name
|
|
|
167 |
* @return string A set of RSS <item /> elements
|
|
|
168 |
*/
|
|
|
169 |
function items($items, $callback = null) {
|
|
|
170 |
if ($callback != null) {
|
|
|
171 |
$items = array_map($callback, $items);
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
$out = '';
|
|
|
175 |
$c = count($items);
|
|
|
176 |
|
|
|
177 |
for ($i = 0; $i < $c; $i++) {
|
|
|
178 |
$out .= $this->item(array(), $items[$i]);
|
|
|
179 |
}
|
|
|
180 |
return $out;
|
|
|
181 |
}
|
|
|
182 |
/**
|
|
|
183 |
* Converts an array into an <item /> element and its contents
|
|
|
184 |
*
|
|
|
185 |
* @param array $attrib The attributes of the <item /> element
|
|
|
186 |
* @param array $elements The list of elements contained in this <item />
|
|
|
187 |
* @return string An RSS <item /> element
|
|
|
188 |
*/
|
|
|
189 |
function item($att = array(), $elements = array()) {
|
|
|
190 |
$content = null;
|
|
|
191 |
|
|
|
192 |
if (isset($elements['link']) && !isset($elements['guid'])) {
|
|
|
193 |
$elements['guid'] = $elements['link'];
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
foreach ($elements as $key => $val) {
|
|
|
197 |
$attrib = array();
|
|
|
198 |
switch ($key) {
|
|
|
199 |
case 'pubDate' :
|
|
|
200 |
$val = $this->time($val);
|
|
|
201 |
break;
|
|
|
202 |
case 'category' :
|
|
|
203 |
if (is_array($val) && !empty($val[0])) {
|
|
|
204 |
foreach ($val as $category) {
|
|
|
205 |
$attrib = array();
|
|
|
206 |
if (isset($category['domain'])) {
|
|
|
207 |
$attrib['domain'] = $category['domain'];
|
|
|
208 |
unset($category['domain']);
|
|
|
209 |
}
|
|
|
210 |
$categories[] = $this->elem($key, $attrib, $category);
|
|
|
211 |
}
|
|
|
212 |
$elements[$key] = join('', $categories);
|
|
|
213 |
continue 2;
|
|
|
214 |
} else if (is_array($val) && isset($val['domain'])) {
|
|
|
215 |
$attrib['domain'] = $val['domain'];
|
|
|
216 |
}
|
|
|
217 |
break;
|
|
|
218 |
case 'link':
|
|
|
219 |
case 'guid':
|
|
|
220 |
case 'comments':
|
|
|
221 |
if (is_array($val) && isset($val['url'])) {
|
|
|
222 |
$attrib = $val;
|
|
|
223 |
unset($attrib['url']);
|
|
|
224 |
$val = $val['url'];
|
|
|
225 |
}
|
|
|
226 |
$val = $this->url($val, true);
|
|
|
227 |
break;
|
|
|
228 |
case 'source':
|
|
|
229 |
if (is_array($val) && isset($val['url'])) {
|
|
|
230 |
$attrib['url'] = $this->url($val['url'], true);
|
|
|
231 |
$val = $val['title'];
|
|
|
232 |
} elseif (is_array($val)) {
|
|
|
233 |
$attrib['url'] = $this->url($val[0], true);
|
|
|
234 |
$val = $val[1];
|
|
|
235 |
}
|
|
|
236 |
break;
|
|
|
237 |
case 'enclosure':
|
|
|
238 |
if (is_string($val['url']) && is_file(WWW_ROOT . $val['url']) && file_exists(WWW_ROOT . $val['url'])) {
|
|
|
239 |
if (!isset($val['length']) && strpos($val['url'], '://') === false) {
|
|
|
240 |
$val['length'] = sprintf("%u", filesize(WWW_ROOT . $val['url']));
|
|
|
241 |
}
|
|
|
242 |
if (!isset($val['type']) && function_exists('mime_content_type')) {
|
|
|
243 |
$val['type'] = mime_content_type(WWW_ROOT . $val['url']);
|
|
|
244 |
}
|
|
|
245 |
}
|
|
|
246 |
$val['url'] = $this->url($val['url'], true);
|
|
|
247 |
$attrib = $val;
|
|
|
248 |
$val = null;
|
|
|
249 |
break;
|
|
|
250 |
}
|
|
|
251 |
$escape = true;
|
|
|
252 |
if (is_array($val) && isset($val['convertEntities'])) {
|
|
|
253 |
$escape = $val['convertEntities'];
|
|
|
254 |
unset($val['convertEntities']);
|
|
|
255 |
}
|
|
|
256 |
if (!is_null($val) && $escape) {
|
|
|
257 |
$val = h($val);
|
|
|
258 |
}
|
|
|
259 |
$elements[$key] = $this->elem($key, $attrib, $val);
|
|
|
260 |
}
|
|
|
261 |
if (!empty($elements)) {
|
|
|
262 |
$content = join('', $elements);
|
|
|
263 |
}
|
|
|
264 |
return $this->output($this->elem('item', $att, $content, !($content === null)));
|
|
|
265 |
}
|
|
|
266 |
/**
|
|
|
267 |
* Converts a time in any format to an RSS time
|
|
|
268 |
*
|
|
|
269 |
* @param mixed $time
|
|
|
270 |
* @return string An RSS-formatted timestamp
|
|
|
271 |
* @see TimeHelper::toRSS
|
|
|
272 |
*/
|
|
|
273 |
function time($time) {
|
|
|
274 |
return $this->Time->toRSS($time);
|
|
|
275 |
}
|
|
|
276 |
}
|
|
|
277 |
?>
|