rss.php

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

Generated on Sun Nov 22 00:30:54 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7