rss.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: rss_8php-source.html 580 2008-07-01 14:45:49Z gwoo $ */
00003 /**
00004  * RSS Helper class file.
00005  *
00006  * Simplifies the output of RSS feeds.
00007  *
00008  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00009  * Copyright 2005-2008, Cake Software Foundation, Inc.
00010  *                              1785 E. Sahara Avenue, Suite 490-204
00011  *                              Las Vegas, Nevada 89104
00012  *
00013  * Licensed under The MIT License
00014  * Redistributions of files must retain the above copyright notice.
00015  *
00016  * @filesource
00017  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00018  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00019  * @package         cake
00020  * @subpackage      cake.cake.libs.view.helpers
00021  * @since           CakePHP(tm) v 1.2
00022  * @version         $Revision: 580 $
00023  * @modifiedby      $LastChangedBy: gwoo $
00024  * @lastmodified    $Date: 2008-07-01 09:45:49 -0500 (Tue, 01 Jul 2008) $
00025  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00026  */
00027 /**
00028  * XML Helper class for easy output of XML structures.
00029  *
00030  * XmlHelper encloses all methods needed while working with XML documents.
00031  *
00032  * @package     cake
00033  * @subpackage  cake.cake.libs.view.helpers
00034  */
00035 App::import('Helper', 'Xml');
00036 
00037 class RssHelper extends XmlHelper {
00038 
00039     var $Html = null;
00040 
00041     var $Time = null;
00042 
00043     var $helpers = array('Time');
00044 /**
00045  * Base URL
00046  *
00047  * @access public
00048  * @var string
00049  */
00050     var $base = null;
00051 /**
00052  * URL to current action.
00053  *
00054  * @access public
00055  * @var string
00056  */
00057     var $here = null;
00058 /**
00059  * Parameter array.
00060  *
00061  * @access public
00062  * @var array
00063  */
00064     var $params = array();
00065 /**
00066  * Current action.
00067  *
00068  * @access public
00069  * @var string
00070  */
00071     var $action = null;
00072 /**
00073  * POSTed model data
00074  *
00075  * @access public
00076  * @var array
00077  */
00078     var $data = null;
00079 /**
00080  * Name of the current model
00081  *
00082  * @access public
00083  * @var string
00084  */
00085     var $model = null;
00086 /**
00087  * Name of the current field
00088  *
00089  * @access public
00090  * @var string
00091  */
00092     var $field = null;
00093 /**
00094  * Default spec version of generated RSS
00095  *
00096  * @access public
00097  * @var string
00098  */
00099     var $version = '2.0';
00100 /**
00101  * Returns an RSS document wrapped in <rss /> tags
00102  *
00103  * @param  array  $attrib <rss /> tag attributes
00104  * @return string An RSS document
00105  */
00106     function document($attrib = array(), $content = null) {
00107         if ($content === null) {
00108             $content = $attrib;
00109             $attrib = array();
00110         }
00111         if (!isset($attrib['version']) || empty($attrib['version'])) {
00112             $attrib['version'] = $this->version;
00113         }
00114 
00115         return $this->elem('rss', $attrib, $content);
00116     }
00117 /**
00118  * Returns an RSS <channel /> element
00119  *
00120  * @param  array  $attrib   <channel /> tag attributes
00121  * @param  mixed  $elements Named array elements which are converted to tags
00122  * @param  mixed  $content  Content (<item />'s belonging to this channel
00123  * @return string An RSS <channel />
00124  */
00125     function channel($attrib = array(), $elements = array(), $content = null) {
00126         $view =& ClassRegistry::getObject('view');
00127 
00128         if (!isset($elements['title']) && !empty($view->pageTitle)) {
00129             $elements['title'] = $view->pageTitle;
00130         }
00131         if (!isset($elements['link'])) {
00132             $elements['link'] = '/';
00133         }
00134         if (!isset($elements['description'])) {
00135             $elements['description'] = '';
00136         }
00137         $elements['link'] = $this->url($elements['link'], true);
00138 
00139         $elems = '';
00140         foreach ($elements as $elem => $data) {
00141             $attributes = array();
00142             if (is_array($data) && isset($data['attrib']) && is_array($data['attrib'])) {
00143                 $attributes = $data['attrib'];
00144                 unset($data['attrib']);
00145             }
00146             $elems .= $this->elem($elem, $attributes, $data);
00147         }
00148         return $this->elem('channel', $attrib, $elems . $content, !($content === null));
00149     }
00150 /**
00151  * Transforms an array of data using an optional callback, and maps it to a set
00152  * of <item /> tags
00153  *
00154  * @param  array  $items    The list of items to be mapped
00155  * @param  mixed  $callback A string function name, or array containing an object
00156  *                          and a string method name
00157  * @return string A set of RSS <item /> elements
00158  */
00159     function items($items, $callback = null) {
00160         if ($callback != null) {
00161             $items = array_map($callback, $items);
00162         }
00163 
00164         $out = '';
00165         $c = count($items);
00166 
00167         for ($i = 0; $i < $c; $i++) {
00168             $out .= $this->item(array(), $items[$i]);
00169         }
00170         return $out;
00171     }
00172 /**
00173  * Converts an array into an <item /> element and its contents
00174  *
00175  * @param  array  $attrib      The attributes of the <item /> element
00176  * @param  array  $elements    The list of elements contained in this <item />
00177  * @return string An RSS <item /> element
00178  */
00179     function item($att = array(), $elements = array()) {
00180         $content = null;
00181 
00182         if (isset($elements['link']) && !isset($elements['guid'])) {
00183             $elements['guid'] = $elements['link'];
00184         }
00185 
00186         foreach ($elements as $key => $val) {
00187             $attrib = array();
00188             switch ($key) {
00189                 case 'pubDate' :
00190                     $val = $this->time($val);
00191                 break;
00192                 case 'link':
00193                 case 'guid':
00194                 case 'comments':
00195                     if (is_array($val) && isset($val['url'])) {
00196                         $attrib = $val;
00197                         unset($attrib['url']);
00198                         $val = $val['url'];
00199                     }
00200                     $val = $this->url($val, true);
00201                 break;
00202                 case 'source':
00203                     if (is_array($val) && isset($val['url'])) {
00204                         $attrib['url'] = $this->url($val['url'], true);
00205                         $val = $val['title'];
00206                     } elseif (is_array($val)) {
00207                         $attrib['url'] = $this->url($val[0], true);
00208                         $val = $val[1];
00209                     }
00210                 break;
00211                 case 'enclosure':
00212                     if (is_string($val['url']) && is_file(WWW_ROOT . $val['url']) && file_exists(WWW_ROOT . $val['url'])) {
00213                         if (!isset($val['length']) && strpos($val['url'], '://') === false) {
00214                             $val['length'] = sprintf("%u", filesize(WWW_ROOT . $val['url']));
00215                         }
00216                         if (!isset($val['type']) && function_exists('mime_content_type')) {
00217                             $val['type'] = mime_content_type(WWW_ROOT . $val['url']);
00218                         }
00219                     }
00220                     $val['url'] = $this->url($val['url'], true);
00221                     $attrib = $val;
00222                     $val = null;
00223                 break;
00224             }
00225             $escape = true;
00226             if (is_array($val) && isset($val['convertEntities'])) {
00227                 $escape = $val['convertEntities'];
00228                 unset($val['convertEntities']);
00229             }
00230             if (!is_null($val) && $escape) {
00231                 $val = h($val);
00232             }
00233             $elements[$key] = $this->elem($key, $attrib, $val);
00234         }
00235 
00236         if (!empty($elements)) {
00237             $content = join('', $elements);
00238         }
00239         return $this->output($this->elem('item', $att, $content, !($content === null)));
00240     }
00241 /**
00242  * Converts a time in any format to an RSS time
00243  *
00244  * @param  mixed  $time
00245  * @return string An RSS-formatted timestamp
00246  * @see TimeHelper::toRSS
00247  */
00248     function time($time) {
00249         return $this->Time->toRSS($time);
00250     }
00251 }
00252 ?>