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 (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:                     } else if (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:             }
260:             if (!is_null($val) && $escape) {
261:                 $val = h($val);
262:             }
263:             $elements[$key] = $this->elem($key, $attrib, $val);
264:         }
265:         if (!empty($elements)) {
266:             $content = implode('', $elements);
267:         }
268:         return $this->elem('item', (array)$att, $content, !($content === null));
269:     }
270: 
271: 272: 273: 274: 275: 276: 277: 278: 
279:     public function time($time) {
280:         return $this->Time->toRSS($time);
281:     }
282: 
283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 
293:     public function elem($name, $attrib = array(), $content = null, $endTag = true) {
294:         $namespace = null;
295:         if (isset($attrib['namespace'])) {
296:             $namespace = $attrib['namespace'];
297:             unset($attrib['namespace']);
298:         }
299:         $cdata = false;
300:         if (is_array($content) && isset($content['cdata'])) {
301:             $cdata = true;
302:             unset($content['cdata']);
303:         }
304:         if (is_array($content) && array_key_exists('value', $content)) {
305:             $content = $content['value'];
306:         }
307:         $children = array();
308:         if (is_array($content)) {
309:             $children = $content;
310:             $content = null;
311:         }
312: 
313:         $xml = '<' . $name;
314:         if (!empty($namespace)) {
315:             $xml .= ' xmlns:"' . $namespace . '"';
316:         }
317:         $bareName = $name;
318:         if (strpos($name, ':') !== false) {
319:             list($prefix, $bareName) = explode(':', $name, 2);
320:             switch ($prefix) {
321:                 case 'atom':
322:                     $xml .= ' xmlns:atom="http://www.w3.org/2005/Atom"';
323:                     break;
324:             }
325:         }
326:         if ($cdata && !empty($content)) {
327:             $content = '<![CDATA[' . $content . ']]>';
328:         }
329:         $xml .= '>' . $content . '</' . $name. '>';
330:         $elem = Xml::build($xml, array('return' => 'domdocument'));
331:         $nodes = $elem->getElementsByTagName($bareName);
332:         foreach ($attrib as $key => $value) {
333:             $nodes->item(0)->setAttribute($key, $value);
334:         }
335:         foreach ($children as $k => $child) {
336:             $child = $elem->createElement($name, $child);
337:             $nodes->item(0)->appendChild($child);
338:         }
339: 
340:         $xml = $elem->saveXml();
341:         $xml = trim(substr($xml, strpos($xml, '?>') + 2));
342:         return $xml;
343:     }
344: }
345: