1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19:
20: App::uses('AppHelper', 'View/Helper');
21: App::uses('Xml', 'Utility');
22:
23: 24: 25: 26: 27: 28: 29:
30: class RssHelper extends AppHelper {
31:
32: 33: 34: 35: 36:
37: public $helpers = array('Time');
38:
39: 40: 41: 42: 43:
44: public $base = null;
45:
46: 47: 48: 49: 50:
51: public $here = null;
52:
53: 54: 55: 56: 57:
58: public $params = array();
59:
60: 61: 62: 63: 64:
65: public $action = null;
66:
67: 68: 69: 70: 71:
72: public $data = null;
73:
74: 75: 76: 77: 78:
79: public $model = null;
80:
81: 82: 83: 84: 85:
86: public $field = null;
87:
88: 89: 90: 91: 92:
93: public $version = '2.0';
94:
95: 96: 97: 98: 99: 100: 101: 102:
103: public function document($attrib = array(), $content = null) {
104: if ($content === null) {
105: $content = $attrib;
106: $attrib = array();
107: }
108: if (!isset($attrib['version']) || empty($attrib['version'])) {
109: $attrib['version'] = $this->version;
110: }
111:
112: return $this->elem('rss', $attrib, $content);
113: }
114:
115: 116: 117: 118: 119: 120: 121: 122: 123:
124: public function channel($attrib = array(), $elements = array(), $content = null) {
125: if (!isset($elements['title']) && !empty($this->_View->pageTitle)) {
126: $elements['title'] = $this->_View->pageTitle;
127: }
128: if (!isset($elements['link'])) {
129: $elements['link'] = '/';
130: }
131: if (!isset($elements['description'])) {
132: $elements['description'] = '';
133: }
134: $elements['link'] = $this->url($elements['link'], true);
135:
136: $elems = '';
137: foreach ($elements as $elem => $data) {
138: $attributes = array();
139: if (is_array($data)) {
140: if (strtolower($elem) == 'cloud') {
141: $attributes = $data;
142: $data = array();
143: } elseif (isset($data['attrib']) && is_array($data['attrib'])) {
144: $attributes = $data['attrib'];
145: unset($data['attrib']);
146: } else {
147: $innerElements = '';
148: foreach ($data as $subElement => $value) {
149: $innerElements .= $this->elem($subElement, array(), $value);
150: }
151: $data = $innerElements;
152: }
153: }
154: $elems .= $this->elem($elem, $attributes, $data);
155: }
156: return $this->elem('channel', $attrib, $elems . $content, !($content === null));
157: }
158:
159: 160: 161: 162: 163: 164: 165: 166: 167: 168:
169: public 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: 184: 185: 186: 187: 188: 189: 190:
191: public function item($att = array(), $elements = array()) {
192: $content = null;
193:
194: if (isset($elements['link']) && !isset($elements['guid'])) {
195: $elements['guid'] = $elements['link'];
196: }
197:
198: foreach ($elements as $key => $val) {
199: $attrib = array();
200:
201: $escape = true;
202: if (is_array($val) && isset($val['convertEntities'])) {
203: $escape = $val['convertEntities'];
204: unset($val['convertEntities']);
205: }
206:
207: switch ($key) {
208: case 'pubDate' :
209: $val = $this->time($val);
210: break;
211: case 'category' :
212: if (is_array($val) && !empty($val[0])) {
213: foreach ($val as $category) {
214: $attrib = array();
215: if (is_array($category) && isset($category['domain'])) {
216: $attrib['domain'] = $category['domain'];
217: unset($category['domain']);
218: }
219: $categories[] = $this->elem($key, $attrib, $category);
220: }
221: $elements[$key] = implode('', $categories);
222: continue 2;
223: } elseif (is_array($val) && isset($val['domain'])) {
224: $attrib['domain'] = $val['domain'];
225: }
226: break;
227: case 'link':
228: case 'guid':
229: case 'comments':
230: if (is_array($val) && isset($val['url'])) {
231: $attrib = $val;
232: unset($attrib['url']);
233: $val = $val['url'];
234: }
235: $val = $this->url($val, true);
236: break;
237: case 'source':
238: if (is_array($val) && isset($val['url'])) {
239: $attrib['url'] = $this->url($val['url'], true);
240: $val = $val['title'];
241: } elseif (is_array($val)) {
242: $attrib['url'] = $this->url($val[0], true);
243: $val = $val[1];
244: }
245: break;
246: case 'enclosure':
247: if (is_string($val['url']) && is_file(WWW_ROOT . $val['url']) && file_exists(WWW_ROOT . $val['url'])) {
248: if (!isset($val['length']) && strpos($val['url'], '://') === false) {
249: $val['length'] = sprintf("%u", filesize(WWW_ROOT . $val['url']));
250: }
251: if (!isset($val['type']) && function_exists('mime_content_type')) {
252: $val['type'] = mime_content_type(WWW_ROOT . $val['url']);
253: }
254: }
255: $val['url'] = $this->url($val['url'], true);
256: $attrib = $val;
257: $val = null;
258: break;
259: default:
260: $attrib = $att;
261: }
262: if (!is_null($val) && $escape) {
263: $val = h($val);
264: }
265: $elements[$key] = $this->elem($key, $attrib, $val);
266: }
267: if (!empty($elements)) {
268: $content = implode('', $elements);
269: }
270: return $this->elem('item', (array)$att, $content, !($content === null));
271: }
272:
273: 274: 275: 276: 277: 278: 279: 280:
281: public function time($time) {
282: return $this->Time->toRSS($time);
283: }
284:
285: 286: 287: 288: 289: 290: 291: 292: 293: 294:
295: public function elem($name, $attrib = array(), $content = null, $endTag = true) {
296: $namespace = null;
297: if (isset($attrib['namespace'])) {
298: $namespace = $attrib['namespace'];
299: unset($attrib['namespace']);
300: }
301: $cdata = false;
302: if (is_array($content) && isset($content['cdata'])) {
303: $cdata = true;
304: unset($content['cdata']);
305: }
306: if (is_array($content) && array_key_exists('value', $content)) {
307: $content = $content['value'];
308: }
309: $children = array();
310: if (is_array($content)) {
311: $children = $content;
312: $content = null;
313: }
314:
315: $xml = '<' . $name;
316: if (!empty($namespace)) {
317: $xml .= ' xmlns';
318: if (is_array($namespace)) {
319: $xml .= ':' . $namespace['prefix'];
320: $namespace = $namespace['url'];
321: }
322: $xml .= '="' . $namespace . '"';
323: }
324: $bareName = $name;
325: if (strpos($name, ':') !== false) {
326: list($prefix, $bareName) = explode(':', $name, 2);
327: switch ($prefix) {
328: case 'atom':
329: $xml .= ' xmlns:atom="http://www.w3.org/2005/Atom"';
330: break;
331: }
332: }
333: if ($cdata && !empty($content)) {
334: $content = '<![CDATA[' . $content . ']]>';
335: }
336: $xml .= '>' . $content . '</' . $name . '>';
337: $elem = Xml::build($xml, array('return' => 'domdocument'));
338: $nodes = $elem->getElementsByTagName($bareName);
339: if ($attrib) {
340: foreach ($attrib as $key => $value) {
341: $nodes->item(0)->setAttribute($key, $value);
342: }
343: }
344: foreach ($children as $child) {
345: $child = $elem->createElement($name, $child);
346: $nodes->item(0)->appendChild($child);
347: }
348:
349: $xml = $elem->saveXml();
350: $xml = trim(substr($xml, strpos($xml, '?>') + 2));
351: return $xml;
352: }
353:
354: }
355: