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