1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20: App::import('Helper', 'Xml');
21:
22: 23: 24: 25: 26: 27: 28: 29: 30:
31: class RssHelper extends XmlHelper {
32:
33: 34: 35: 36: 37: 38:
39: var $helpers = array('Time');
40:
41: 42: 43: 44: 45: 46:
47: var $base = null;
48:
49: 50: 51: 52: 53: 54:
55: var $here = null;
56:
57: 58: 59: 60: 61: 62:
63: var $params = array();
64:
65: 66: 67: 68: 69: 70:
71: var $action = null;
72:
73: 74: 75: 76: 77: 78:
79: var $data = null;
80:
81: 82: 83: 84: 85: 86:
87: var $model = null;
88:
89: 90: 91: 92: 93: 94:
95: var $field = null;
96:
97: 98: 99: 100: 101: 102:
103: var $version = '2.0';
104:
105: 106: 107: 108: 109: 110: 111:
112: function document($attrib = array(), $content = null) {
113: if ($content === null) {
114: $content = $attrib;
115: $attrib = array();
116: }
117: if (!isset($attrib['version']) || empty($attrib['version'])) {
118: $attrib['version'] = $this->version;
119: }
120:
121: return $this->elem('rss', $attrib, $content);
122: }
123:
124: 125: 126: 127: 128: 129: 130: 131: 132:
133: function channel($attrib = array(), $elements = array(), $content = null) {
134: $view =& ClassRegistry::getObject('view');
135:
136: if (!isset($elements['title']) && !empty($view->pageTitle)) {
137: $elements['title'] = $view->pageTitle;
138: }
139: if (!isset($elements['link'])) {
140: $elements['link'] = '/';
141: }
142: if (!isset($elements['description'])) {
143: $elements['description'] = '';
144: }
145: $elements['link'] = $this->url($elements['link'], true);
146:
147: $elems = '';
148: foreach ($elements as $elem => $data) {
149: $attributes = array();
150: if (is_array($data)) {
151: if (strtolower($elem) == 'cloud') {
152: $attributes = $data;
153: $data = array();
154: } elseif (isset($data['attrib']) && is_array($data['attrib'])) {
155: $attributes = $data['attrib'];
156: unset($data['attrib']);
157: } else {
158: $innerElements = '';
159: foreach ($data as $subElement => $value) {
160: $innerElements .= $this->elem($subElement, array(), $value);
161: }
162: $data = $innerElements;
163: }
164: }
165: $elems .= $this->elem($elem, $attributes, $data);
166: }
167: return $this->elem('channel', $attrib, $elems . $content, !($content === null));
168: }
169:
170: 171: 172: 173: 174: 175: 176: 177: 178: 179:
180: function items($items, $callback = null) {
181: if ($callback != null) {
182: $items = array_map($callback, $items);
183: }
184:
185: $out = '';
186: $c = count($items);
187:
188: for ($i = 0; $i < $c; $i++) {
189: $out .= $this->item(array(), $items[$i]);
190: }
191: return $out;
192: }
193:
194: 195: 196: 197: 198: 199: 200: 201:
202: function item($att = array(), $elements = array()) {
203: $content = null;
204:
205: if (isset($elements['link']) && !isset($elements['guid'])) {
206: $elements['guid'] = $elements['link'];
207: }
208:
209: foreach ($elements as $key => $val) {
210: $attrib = array();
211:
212: $escape = true;
213: if (is_array($val) && isset($val['convertEntities'])) {
214: $escape = $val['convertEntities'];
215: unset($val['convertEntities']);
216: }
217:
218: switch ($key) {
219: case 'pubDate' :
220: $val = $this->time($val);
221: break;
222: case 'category' :
223: if (is_array($val) && !empty($val[0])) {
224: foreach ($val as $category) {
225: $attrib = array();
226: if (isset($category['domain'])) {
227: $attrib['domain'] = $category['domain'];
228: unset($category['domain']);
229: }
230: $categories[] = $this->elem($key, $attrib, $category);
231: }
232: $elements[$key] = implode('', $categories);
233: continue 2;
234: } else if (is_array($val) && isset($val['domain'])) {
235: $attrib['domain'] = $val['domain'];
236: }
237: break;
238: case 'link':
239: case 'guid':
240: case 'comments':
241: if (is_array($val) && isset($val['url'])) {
242: $attrib = $val;
243: unset($attrib['url']);
244: $val = $val['url'];
245: }
246: $val = $this->url($val, true);
247: break;
248: case 'source':
249: if (is_array($val) && isset($val['url'])) {
250: $attrib['url'] = $this->url($val['url'], true);
251: $val = $val['title'];
252: } elseif (is_array($val)) {
253: $attrib['url'] = $this->url($val[0], true);
254: $val = $val[1];
255: }
256: break;
257: case 'enclosure':
258: if (is_string($val['url']) && is_file(WWW_ROOT . $val['url']) && file_exists(WWW_ROOT . $val['url'])) {
259: if (!isset($val['length']) && strpos($val['url'], '://') === false) {
260: $val['length'] = sprintf("%u", filesize(WWW_ROOT . $val['url']));
261: }
262: if (!isset($val['type']) && function_exists('mime_content_type')) {
263: $val['type'] = mime_content_type(WWW_ROOT . $val['url']);
264: }
265: }
266: $val['url'] = $this->url($val['url'], true);
267: $attrib = $val;
268: $val = null;
269: break;
270: }
271: if (!is_null($val) && $escape) {
272: $val = h($val);
273: }
274: $elements[$key] = $this->elem($key, $attrib, $val);
275: }
276: if (!empty($elements)) {
277: $content = implode('', $elements);
278: }
279: return $this->elem('item', $att, $content, !($content === null));
280: }
281:
282: 283: 284: 285: 286: 287: 288:
289: function time($time) {
290: return $this->Time->toRSS($time);
291: }
292: }
293: