PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/view/helpers/rss.php

https://github.com/mariuz/firetube
PHP | 278 lines | 147 code | 10 blank | 121 comment | 40 complexity | 1e11c0300511d121eb1eb401f56bde54 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * RSS Helper class file.
  5. *
  6. * Simplifies the output of RSS feeds.
  7. *
  8. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  9. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. *
  11. * Licensed under The MIT License
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package cake
  17. * @subpackage cake.cake.libs.view.helpers
  18. * @since CakePHP(tm) v 1.2
  19. * @version $Revision$
  20. * @modifiedby $LastChangedBy$
  21. * @lastmodified $Date$
  22. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  23. */
  24. App::import('Helper', 'Xml');
  25. /**
  26. * XML Helper class for easy output of XML structures.
  27. *
  28. * XmlHelper encloses all methods needed while working with XML documents.
  29. *
  30. * @package cake
  31. * @subpackage cake.cake.libs.view.helpers
  32. */
  33. class RssHelper extends XmlHelper {
  34. /**
  35. * Helpers used by RSS Helper
  36. *
  37. * @var array
  38. * @access public
  39. **/
  40. var $helpers = array('Time');
  41. /**
  42. * Base URL
  43. *
  44. * @access public
  45. * @var string
  46. */
  47. var $base = null;
  48. /**
  49. * URL to current action.
  50. *
  51. * @access public
  52. * @var string
  53. */
  54. var $here = null;
  55. /**
  56. * Parameter array.
  57. *
  58. * @access public
  59. * @var array
  60. */
  61. var $params = array();
  62. /**
  63. * Current action.
  64. *
  65. * @access public
  66. * @var string
  67. */
  68. var $action = null;
  69. /**
  70. * POSTed model data
  71. *
  72. * @access public
  73. * @var array
  74. */
  75. var $data = null;
  76. /**
  77. * Name of the current model
  78. *
  79. * @access public
  80. * @var string
  81. */
  82. var $model = null;
  83. /**
  84. * Name of the current field
  85. *
  86. * @access public
  87. * @var string
  88. */
  89. var $field = null;
  90. /**
  91. * Default spec version of generated RSS
  92. *
  93. * @access public
  94. * @var string
  95. */
  96. var $version = '2.0';
  97. /**
  98. * Returns an RSS document wrapped in <rss /> tags
  99. *
  100. * @param array $attrib <rss /> tag attributes
  101. * @return string An RSS document
  102. */
  103. 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. return $this->elem('rss', $attrib, $content);
  112. }
  113. /**
  114. * Returns an RSS <channel /> element
  115. *
  116. * @param array $attrib <channel /> tag attributes
  117. * @param mixed $elements Named array elements which are converted to tags
  118. * @param mixed $content Content (<item />'s belonging to this channel
  119. * @return string An RSS <channel />
  120. */
  121. function channel($attrib = array(), $elements = array(), $content = null) {
  122. $view =& ClassRegistry::getObject('view');
  123. if (!isset($elements['title']) && !empty($view->pageTitle)) {
  124. $elements['title'] = $view->pageTitle;
  125. }
  126. if (!isset($elements['link'])) {
  127. $elements['link'] = '/';
  128. }
  129. if (!isset($elements['description'])) {
  130. $elements['description'] = '';
  131. }
  132. $elements['link'] = $this->url($elements['link'], true);
  133. $elems = '';
  134. foreach ($elements as $elem => $data) {
  135. $attributes = array();
  136. if (is_array($data)) {
  137. if (strtolower($elem) == 'cloud') {
  138. $attributes = $data;
  139. $data = array();
  140. } elseif (isset($data['attrib']) && is_array($data['attrib'])) {
  141. $attributes = $data['attrib'];
  142. unset($data['attrib']);
  143. } else {
  144. $innerElements = '';
  145. foreach ($data as $subElement => $value) {
  146. $innerElements .= $this->elem($subElement, array(), $value);
  147. }
  148. $data = $innerElements;
  149. }
  150. }
  151. $elems .= $this->elem($elem, $attributes, $data);
  152. }
  153. return $this->elem('channel', $attrib, $elems . $content, !($content === null));
  154. }
  155. /**
  156. * Transforms an array of data using an optional callback, and maps it to a set
  157. * of <item /> tags
  158. *
  159. * @param array $items The list of items to be mapped
  160. * @param mixed $callback A string function name, or array containing an object
  161. * and a string method name
  162. * @return string A set of RSS <item /> elements
  163. */
  164. function items($items, $callback = null) {
  165. if ($callback != null) {
  166. $items = array_map($callback, $items);
  167. }
  168. $out = '';
  169. $c = count($items);
  170. for ($i = 0; $i < $c; $i++) {
  171. $out .= $this->item(array(), $items[$i]);
  172. }
  173. return $out;
  174. }
  175. /**
  176. * Converts an array into an <item /> element and its contents
  177. *
  178. * @param array $attrib The attributes of the <item /> element
  179. * @param array $elements The list of elements contained in this <item />
  180. * @return string An RSS <item /> element
  181. */
  182. function item($att = array(), $elements = array()) {
  183. $content = null;
  184. if (isset($elements['link']) && !isset($elements['guid'])) {
  185. $elements['guid'] = $elements['link'];
  186. }
  187. foreach ($elements as $key => $val) {
  188. $attrib = array();
  189. $escape = true;
  190. if (is_array($val) && isset($val['convertEntities'])) {
  191. $escape = $val['convertEntities'];
  192. unset($val['convertEntities']);
  193. }
  194. switch ($key) {
  195. case 'pubDate' :
  196. $val = $this->time($val);
  197. break;
  198. case 'category' :
  199. if (is_array($val) && !empty($val[0])) {
  200. foreach ($val as $category) {
  201. $attrib = array();
  202. if (isset($category['domain'])) {
  203. $attrib['domain'] = $category['domain'];
  204. unset($category['domain']);
  205. }
  206. $categories[] = $this->elem($key, $attrib, $category);
  207. }
  208. $elements[$key] = implode('', $categories);
  209. continue 2;
  210. } else if (is_array($val) && isset($val['domain'])) {
  211. $attrib['domain'] = $val['domain'];
  212. }
  213. break;
  214. case 'link':
  215. case 'guid':
  216. case 'comments':
  217. if (is_array($val) && isset($val['url'])) {
  218. $attrib = $val;
  219. unset($attrib['url']);
  220. $val = $val['url'];
  221. }
  222. $val = $this->url($val, true);
  223. break;
  224. case 'source':
  225. if (is_array($val) && isset($val['url'])) {
  226. $attrib['url'] = $this->url($val['url'], true);
  227. $val = $val['title'];
  228. } elseif (is_array($val)) {
  229. $attrib['url'] = $this->url($val[0], true);
  230. $val = $val[1];
  231. }
  232. break;
  233. case 'enclosure':
  234. if (is_string($val['url']) && is_file(WWW_ROOT . $val['url']) && file_exists(WWW_ROOT . $val['url'])) {
  235. if (!isset($val['length']) && strpos($val['url'], '://') === false) {
  236. $val['length'] = sprintf("%u", filesize(WWW_ROOT . $val['url']));
  237. }
  238. if (!isset($val['type']) && function_exists('mime_content_type')) {
  239. $val['type'] = mime_content_type(WWW_ROOT . $val['url']);
  240. }
  241. }
  242. $val['url'] = $this->url($val['url'], true);
  243. $attrib = $val;
  244. $val = null;
  245. break;
  246. }
  247. if (!is_null($val) && $escape) {
  248. $val = h($val);
  249. }
  250. $elements[$key] = $this->elem($key, $attrib, $val);
  251. }
  252. if (!empty($elements)) {
  253. $content = implode('', $elements);
  254. }
  255. return $this->output($this->elem('item', $att, $content, !($content === null)));
  256. }
  257. /**
  258. * Converts a time in any format to an RSS time
  259. *
  260. * @param mixed $time
  261. * @return string An RSS-formatted timestamp
  262. * @see TimeHelper::toRSS
  263. */
  264. function time($time) {
  265. return $this->Time->toRSS($time);
  266. }
  267. }
  268. ?>