PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/View/Helper/RssHelper.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 344 lines | 195 code | 27 blank | 122 comment | 51 complexity | fc294859d0a4a70a2c6d96af3441ed74 MD5 | raw file
  1. <?php
  2. /**
  3. * RSS Helper class file.
  4. *
  5. * Simplifies the output of RSS feeds.
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.View.Helper
  16. * @since CakePHP(tm) v 1.2
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('AppHelper', 'View/Helper');
  20. App::uses('Xml', 'Utility');
  21. /**
  22. * RSS Helper class for easy output RSS structures.
  23. *
  24. * @package Cake.View.Helper
  25. * @property TimeHelper $Time
  26. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/rss.html
  27. */
  28. class RssHelper extends AppHelper {
  29. /**
  30. * Helpers used by RSS Helper
  31. *
  32. * @var array
  33. */
  34. public $helpers = array('Time');
  35. /**
  36. * Base URL
  37. *
  38. * @var string
  39. */
  40. public $base = null;
  41. /**
  42. * URL to current action.
  43. *
  44. * @var string
  45. */
  46. public $here = null;
  47. /**
  48. * Parameter array.
  49. *
  50. * @var array
  51. */
  52. public $params = array();
  53. /**
  54. * Current action.
  55. *
  56. * @var string
  57. */
  58. public $action = null;
  59. /**
  60. * POSTed model data
  61. *
  62. * @var array
  63. */
  64. public $data = null;
  65. /**
  66. * Name of the current model
  67. *
  68. * @var string
  69. */
  70. public $model = null;
  71. /**
  72. * Name of the current field
  73. *
  74. * @var string
  75. */
  76. public $field = null;
  77. /**
  78. * Default spec version of generated RSS
  79. *
  80. * @var string
  81. */
  82. public $version = '2.0';
  83. /**
  84. * Returns an RSS document wrapped in `<rss />` tags
  85. *
  86. * @param array $attrib `<rss />` tag attributes
  87. * @param string $content
  88. * @return string An RSS document
  89. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/rss.html#RssHelper::document
  90. */
  91. public function document($attrib = array(), $content = null) {
  92. if ($content === null) {
  93. $content = $attrib;
  94. $attrib = array();
  95. }
  96. if (!isset($attrib['version']) || empty($attrib['version'])) {
  97. $attrib['version'] = $this->version;
  98. }
  99. return $this->elem('rss', $attrib, $content);
  100. }
  101. /**
  102. * Returns an RSS `<channel />` element
  103. *
  104. * @param array $attrib `<channel />` tag attributes
  105. * @param mixed $elements Named array elements which are converted to tags
  106. * @param mixed $content Content (`<item />`'s belonging to this channel
  107. * @return string An RSS `<channel />`
  108. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/rss.html#RssHelper::channel
  109. */
  110. public function channel($attrib = array(), $elements = array(), $content = null) {
  111. if (!isset($elements['title']) && !empty($this->_View->pageTitle)) {
  112. $elements['title'] = $this->_View->pageTitle;
  113. }
  114. if (!isset($elements['link'])) {
  115. $elements['link'] = '/';
  116. }
  117. if (!isset($elements['description'])) {
  118. $elements['description'] = '';
  119. }
  120. $elements['link'] = $this->url($elements['link'], true);
  121. $elems = '';
  122. foreach ($elements as $elem => $data) {
  123. $attributes = array();
  124. if (is_array($data)) {
  125. if (strtolower($elem) == 'cloud') {
  126. $attributes = $data;
  127. $data = array();
  128. } elseif (isset($data['attrib']) && is_array($data['attrib'])) {
  129. $attributes = $data['attrib'];
  130. unset($data['attrib']);
  131. } else {
  132. $innerElements = '';
  133. foreach ($data as $subElement => $value) {
  134. $innerElements .= $this->elem($subElement, array(), $value);
  135. }
  136. $data = $innerElements;
  137. }
  138. }
  139. $elems .= $this->elem($elem, $attributes, $data);
  140. }
  141. return $this->elem('channel', $attrib, $elems . $content, !($content === null));
  142. }
  143. /**
  144. * Transforms an array of data using an optional callback, and maps it to a set
  145. * of `<item />` tags
  146. *
  147. * @param array $items The list of items to be mapped
  148. * @param mixed $callback A string function name, or array containing an object
  149. * and a string method name
  150. * @return string A set of RSS `<item />` elements
  151. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/rss.html#RssHelper::items
  152. */
  153. public function items($items, $callback = null) {
  154. if ($callback != null) {
  155. $items = array_map($callback, $items);
  156. }
  157. $out = '';
  158. $c = count($items);
  159. for ($i = 0; $i < $c; $i++) {
  160. $out .= $this->item(array(), $items[$i]);
  161. }
  162. return $out;
  163. }
  164. /**
  165. * Converts an array into an `<item />` element and its contents
  166. *
  167. * @param array $att The attributes of the `<item />` element
  168. * @param array $elements The list of elements contained in this `<item />`
  169. * @return string An RSS `<item />` element
  170. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/rss.html#RssHelper::item
  171. */
  172. public function item($att = array(), $elements = array()) {
  173. $content = null;
  174. if (isset($elements['link']) && !isset($elements['guid'])) {
  175. $elements['guid'] = $elements['link'];
  176. }
  177. foreach ($elements as $key => $val) {
  178. $attrib = array();
  179. $escape = true;
  180. if (is_array($val) && isset($val['convertEntities'])) {
  181. $escape = $val['convertEntities'];
  182. unset($val['convertEntities']);
  183. }
  184. switch ($key) {
  185. case 'pubDate' :
  186. $val = $this->time($val);
  187. break;
  188. case 'category' :
  189. if (is_array($val) && !empty($val[0])) {
  190. foreach ($val as $category) {
  191. $attrib = array();
  192. if (isset($category['domain'])) {
  193. $attrib['domain'] = $category['domain'];
  194. unset($category['domain']);
  195. }
  196. $categories[] = $this->elem($key, $attrib, $category);
  197. }
  198. $elements[$key] = implode('', $categories);
  199. continue 2;
  200. } else if (is_array($val) && isset($val['domain'])) {
  201. $attrib['domain'] = $val['domain'];
  202. }
  203. break;
  204. case 'link':
  205. case 'guid':
  206. case 'comments':
  207. if (is_array($val) && isset($val['url'])) {
  208. $attrib = $val;
  209. unset($attrib['url']);
  210. $val = $val['url'];
  211. }
  212. $val = $this->url($val, true);
  213. break;
  214. case 'source':
  215. if (is_array($val) && isset($val['url'])) {
  216. $attrib['url'] = $this->url($val['url'], true);
  217. $val = $val['title'];
  218. } elseif (is_array($val)) {
  219. $attrib['url'] = $this->url($val[0], true);
  220. $val = $val[1];
  221. }
  222. break;
  223. case 'enclosure':
  224. if (is_string($val['url']) && is_file(WWW_ROOT . $val['url']) && file_exists(WWW_ROOT . $val['url'])) {
  225. if (!isset($val['length']) && strpos($val['url'], '://') === false) {
  226. $val['length'] = sprintf("%u", filesize(WWW_ROOT . $val['url']));
  227. }
  228. if (!isset($val['type']) && function_exists('mime_content_type')) {
  229. $val['type'] = mime_content_type(WWW_ROOT . $val['url']);
  230. }
  231. }
  232. $val['url'] = $this->url($val['url'], true);
  233. $attrib = $val;
  234. $val = null;
  235. break;
  236. }
  237. if (!is_null($val) && $escape) {
  238. $val = h($val);
  239. }
  240. $elements[$key] = $this->elem($key, $attrib, $val);
  241. }
  242. if (!empty($elements)) {
  243. $content = implode('', $elements);
  244. }
  245. return $this->elem('item', (array)$att, $content, !($content === null));
  246. }
  247. /**
  248. * Converts a time in any format to an RSS time
  249. *
  250. * @param mixed $time
  251. * @return string An RSS-formatted timestamp
  252. * @see TimeHelper::toRSS
  253. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/rss.html#RssHelper::time
  254. */
  255. public function time($time) {
  256. return $this->Time->toRSS($time);
  257. }
  258. /**
  259. * Generates an XML element
  260. *
  261. * @param string $name The name of the XML element
  262. * @param array $attrib The attributes of the XML element
  263. * @param mixed $content XML element content
  264. * @param boolean $endTag Whether the end tag of the element should be printed
  265. * @return string XML
  266. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/rss.html#RssHelper::elem
  267. */
  268. public function elem($name, $attrib = array(), $content = null, $endTag = true) {
  269. $namespace = null;
  270. if (isset($attrib['namespace'])) {
  271. $namespace = $attrib['namespace'];
  272. unset($attrib['namespace']);
  273. }
  274. $cdata = false;
  275. if (is_array($content) && isset($content['cdata'])) {
  276. $cdata = true;
  277. unset($content['cdata']);
  278. }
  279. if (is_array($content) && array_key_exists('value', $content)) {
  280. $content = $content['value'];
  281. }
  282. $children = array();
  283. if (is_array($content)) {
  284. $children = $content;
  285. $content = null;
  286. }
  287. $xml = '<' . $name;
  288. if (!empty($namespace)) {
  289. $xml .= ' xmlns:"' . $namespace . '"';
  290. }
  291. $bareName = $name;
  292. if (strpos($name, ':') !== false) {
  293. list($prefix, $bareName) = explode(':', $name, 2);
  294. switch ($prefix) {
  295. case 'atom':
  296. $xml .= ' xmlns:atom="http://www.w3.org/2005/Atom"';
  297. break;
  298. }
  299. }
  300. if ($cdata && !empty($content)) {
  301. $content = '<![CDATA[' . $content . ']]>';
  302. }
  303. $xml .= '>' . $content . '</' . $name. '>';
  304. $elem = Xml::build($xml, array('return' => 'domdocument'));
  305. $nodes = $elem->getElementsByTagName($bareName);
  306. foreach ($attrib as $key => $value) {
  307. $nodes->item(0)->setAttribute($key, $value);
  308. }
  309. foreach ($children as $k => $child) {
  310. $child = $elem->createElement($name, $child);
  311. $nodes->item(0)->appendChild($child);
  312. }
  313. $xml = $elem->saveXml();
  314. $xml = trim(substr($xml, strpos($xml, '?>') + 2));
  315. return $xml;
  316. }
  317. }