1: <?php
  2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19: 
 20: 
 21: App::uses('AppHelper', 'View/Helper');
 22: App::uses('Xml', 'Utility');
 23: 
 24:  25:  26:  27:  28:  29:  30: 
 31: class RssHelper extends AppHelper {
 32: 
 33:  34:  35:  36:  37: 
 38:     public $helpers = array('Time');
 39: 
 40:  41:  42:  43:  44: 
 45:     public $base = null;
 46: 
 47:  48:  49:  50:  51: 
 52:     public $here = null;
 53: 
 54:  55:  56:  57:  58: 
 59:     public $params = array();
 60: 
 61:  62:  63:  64:  65: 
 66:     public $action = null;
 67: 
 68:  69:  70:  71:  72: 
 73:     public $data = null;
 74: 
 75:  76:  77:  78:  79: 
 80:     public $model = null;
 81: 
 82:  83:  84:  85:  86: 
 87:     public $field = null;
 88: 
 89:  90:  91:  92:  93: 
 94:     public $version = '2.0';
 95: 
 96:  97:  98:  99: 100: 101: 102: 103: 
104:     public 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: 124: 
125:     public function channel($attrib = array(), $elements = array(), $content = null) {
126:         if (!isset($elements['link'])) {
127:             $elements['link'] = '/';
128:         }
129:         if (!isset($elements['title'])) {
130:             $elements['title'] = '';
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: 169: 
170:     public function items($items, $callback = null) {
171:         if ($callback) {
172:             $items = array_map($callback, $items);
173:         }
174: 
175:         $out = '';
176:         $c = count($items);
177: 
178:         for ($i = 0; $i < $c; $i++) {
179:             $out .= $this->item(array(), $items[$i]);
180:         }
181:         return $out;
182:     }
183: 
184: 185: 186: 187: 188: 189: 190: 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 (is_array($category) && 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:                     } elseif (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']) && file_exists(WWW_ROOT . $val['url'])) {
249:                         if (!isset($val['length']) && strpos($val['url'], '://') === false) {
250:                             $val['length'] = sprintf("%u", filesize(WWW_ROOT . $val['url']));
251:                         }
252:                         if (!isset($val['type']) && function_exists('mime_content_type')) {
253:                             $val['type'] = mime_content_type(WWW_ROOT . $val['url']);
254:                         }
255:                     }
256:                     $val['url'] = $this->url($val['url'], true);
257:                     $attrib = $val;
258:                     $val = null;
259:                     break;
260:                 default:
261:                     $attrib = $att;
262:             }
263:             if ($val !== null && $escape) {
264:                 $val = h($val);
265:             }
266:             $elements[$key] = $this->elem($key, $attrib, $val);
267:         }
268:         if (!empty($elements)) {
269:             $content = implode('', $elements);
270:         }
271:         return $this->elem('item', (array)$att, $content, !($content === null));
272:     }
273: 
274: 275: 276: 277: 278: 279: 280: 281: 
282:     public function time($time) {
283:         return $this->Time->toRSS($time);
284:     }
285: 
286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 
296:     public function elem($name, $attrib = array(), $content = null, $endTag = true) {
297:         $namespace = null;
298:         if (isset($attrib['namespace'])) {
299:             $namespace = $attrib['namespace'];
300:             unset($attrib['namespace']);
301:         }
302:         $cdata = false;
303:         if (is_array($content) && isset($content['cdata'])) {
304:             $cdata = true;
305:             unset($content['cdata']);
306:         }
307:         if (is_array($content) && array_key_exists('value', $content)) {
308:             $content = $content['value'];
309:         }
310:         $children = array();
311:         if (is_array($content)) {
312:             $children = $content;
313:             $content = null;
314:         }
315: 
316:         $xml = '<' . $name;
317:         if (!empty($namespace)) {
318:             $xml .= ' xmlns';
319:             if (is_array($namespace)) {
320:                 $xml .= ':' . $namespace['prefix'];
321:                 $namespace = $namespace['url'];
322:             }
323:             $xml .= '="' . $namespace . '"';
324:         }
325:         $bareName = $name;
326:         if (strpos($name, ':') !== false) {
327:             list($prefix, $bareName) = explode(':', $name, 2);
328:             switch ($prefix) {
329:                 case 'atom':
330:                     $xml .= ' xmlns:atom="http://www.w3.org/2005/Atom"';
331:                     break;
332:             }
333:         }
334:         if ($cdata && !empty($content)) {
335:             $content = '<![CDATA[' . $content . ']]>';
336:         }
337:         $xml .= '>' . $content . '</' . $name . '>';
338:         $elem = Xml::build($xml, array('return' => 'domdocument'));
339:         $nodes = $elem->getElementsByTagName($bareName);
340:         if ($attrib) {
341:             foreach ($attrib as $key => $value) {
342:                 $nodes->item(0)->setAttribute($key, $value);
343:             }
344:         }
345:         foreach ($children as $child) {
346:             $child = $elem->createElement($name, $child);
347:             $nodes->item(0)->appendChild($child);
348:         }
349: 
350:         $xml = $elem->saveXml();
351:         $xml = trim(substr($xml, strpos($xml, '?>') + 2));
352:         return $xml;
353:     }
354: 
355: }
356: