RssHelper.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. */
  90. public function document($attrib = array(), $content = null) {
  91. if ($content === null) {
  92. $content = $attrib;
  93. $attrib = array();
  94. }
  95. if (!isset($attrib['version']) || empty($attrib['version'])) {
  96. $attrib['version'] = $this->version;
  97. }
  98. return $this->elem('rss', $attrib, $content);
  99. }
  100. /**
  101. * Returns an RSS `<channel />` element
  102. *
  103. * @param array $attrib `<channel />` tag attributes
  104. * @param mixed $elements Named array elements which are converted to tags
  105. * @param mixed $content Content (`<item />`'s belonging to this channel
  106. * @return string An RSS `<channel />`
  107. */
  108. public function channel($attrib = array(), $elements = array(), $content = null) {
  109. if (!isset($elements['title']) && !empty($this->_View->pageTitle)) {
  110. $elements['title'] = $this->_View->pageTitle;
  111. }
  112. if (!isset($elements['link'])) {
  113. $elements['link'] = '/';
  114. }
  115. if (!isset($elements['description'])) {
  116. $elements['description'] = '';
  117. }
  118. $elements['link'] = $this->url($elements['link'], true);
  119. $elems = '';
  120. foreach ($elements as $elem => $data) {
  121. $attributes = array();
  122. if (is_array($data)) {
  123. if (strtolower($elem) == 'cloud') {
  124. $attributes = $data;
  125. $data = array();
  126. } elseif (isset($data['attrib']) && is_array($data['attrib'])) {
  127. $attributes = $data['attrib'];
  128. unset($data['attrib']);
  129. } else {
  130. $innerElements = '';
  131. foreach ($data as $subElement => $value) {
  132. $innerElements .= $this->elem($subElement, array(), $value);
  133. }
  134. $data = $innerElements;
  135. }
  136. }
  137. $elems .= $this->elem($elem, $attributes, $data);
  138. }
  139. return $this->elem('channel', $attrib, $elems . $content, !($content === null));
  140. }
  141. /**
  142. * Transforms an array of data using an optional callback, and maps it to a set
  143. * of `<item />` tags
  144. *
  145. * @param array $items The list of items to be mapped
  146. * @param mixed $callback A string function name, or array containing an object
  147. * and a string method name
  148. * @return string A set of RSS `<item />` elements
  149. */
  150. public function items($items, $callback = null) {
  151. if ($callback != null) {
  152. $items = array_map($callback, $items);
  153. }
  154. $out = '';
  155. $c = count($items);
  156. for ($i = 0; $i < $c; $i++) {
  157. $out .= $this->item(array(), $items[$i]);
  158. }
  159. return $out;
  160. }
  161. /**
  162. * Converts an array into an `<item />` element and its contents
  163. *
  164. * @param array $att The attributes of the `<item />` element
  165. * @param array $elements The list of elements contained in this `<item />`
  166. * @return string An RSS `<item />` element
  167. */
  168. public function item($att = array(), $elements = array()) {
  169. $content = null;
  170. if (isset($elements['link']) && !isset($elements['guid'])) {
  171. $elements['guid'] = $elements['link'];
  172. }
  173. foreach ($elements as $key => $val) {
  174. $attrib = array();
  175. $escape = true;
  176. if (is_array($val) && isset($val['convertEntities'])) {
  177. $escape = $val['convertEntities'];
  178. unset($val['convertEntities']);
  179. }
  180. switch ($key) {
  181. case 'pubDate' :
  182. $val = $this->time($val);
  183. break;
  184. case 'category' :
  185. if (is_array($val) && !empty($val[0])) {
  186. foreach ($val as $category) {
  187. $attrib = array();
  188. if (isset($category['domain'])) {
  189. $attrib['domain'] = $category['domain'];
  190. unset($category['domain']);
  191. }
  192. $categories[] = $this->elem($key, $attrib, $category);
  193. }
  194. $elements[$key] = implode('', $categories);
  195. continue 2;
  196. } else if (is_array($val) && isset($val['domain'])) {
  197. $attrib['domain'] = $val['domain'];
  198. }
  199. break;
  200. case 'link':
  201. case 'guid':
  202. case 'comments':
  203. if (is_array($val) && isset($val['url'])) {
  204. $attrib = $val;
  205. unset($attrib['url']);
  206. $val = $val['url'];
  207. }
  208. $val = $this->url($val, true);
  209. break;
  210. case 'source':
  211. if (is_array($val) && isset($val['url'])) {
  212. $attrib['url'] = $this->url($val['url'], true);
  213. $val = $val['title'];
  214. } elseif (is_array($val)) {
  215. $attrib['url'] = $this->url($val[0], true);
  216. $val = $val[1];
  217. }
  218. break;
  219. case 'enclosure':
  220. if (is_string($val['url']) && is_file(WWW_ROOT . $val['url']) && file_exists(WWW_ROOT . $val['url'])) {
  221. if (!isset($val['length']) && strpos($val['url'], '://') === false) {
  222. $val['length'] = sprintf("%u", filesize(WWW_ROOT . $val['url']));
  223. }
  224. if (!isset($val['type']) && function_exists('mime_content_type')) {
  225. $val['type'] = mime_content_type(WWW_ROOT . $val['url']);
  226. }
  227. }
  228. $val['url'] = $this->url($val['url'], true);
  229. $attrib = $val;
  230. $val = null;
  231. break;
  232. }
  233. if (!is_null($val) && $escape) {
  234. $val = h($val);
  235. }
  236. $elements[$key] = $this->elem($key, $attrib, $val);
  237. }
  238. if (!empty($elements)) {
  239. $content = implode('', $elements);
  240. }
  241. return $this->elem('item', (array)$att, $content, !($content === null));
  242. }
  243. /**
  244. * Converts a time in any format to an RSS time
  245. *
  246. * @param mixed $time
  247. * @return string An RSS-formatted timestamp
  248. * @see TimeHelper::toRSS
  249. */
  250. public function time($time) {
  251. return $this->Time->toRSS($time);
  252. }
  253. /**
  254. * Generates an XML element
  255. *
  256. * @param string $name The name of the XML element
  257. * @param array $attrib The attributes of the XML element
  258. * @param mixed $content XML element content
  259. * @param boolean $endTag Whether the end tag of the element should be printed
  260. * @return string XML
  261. */
  262. public function elem($name, $attrib = array(), $content = null, $endTag = true) {
  263. $namespace = null;
  264. if (isset($attrib['namespace'])) {
  265. $namespace = $attrib['namespace'];
  266. unset($attrib['namespace']);
  267. }
  268. $cdata = false;
  269. if (is_array($content) && isset($content['cdata'])) {
  270. $cdata = true;
  271. unset($content['cdata']);
  272. }
  273. if (is_array($content) && array_key_exists('value', $content)) {
  274. $content = $content['value'];
  275. }
  276. $children = array();
  277. if (is_array($content)) {
  278. $children = $content;
  279. $content = null;
  280. }
  281. $xml = '<' . $name;
  282. if (!empty($namespace)) {
  283. $xml .= ' xmlns:"' . $namespace . '"';
  284. }
  285. $bareName = $name;
  286. if (strpos($name, ':') !== false) {
  287. list($prefix, $bareName) = explode(':', $name, 2);
  288. switch ($prefix) {
  289. case 'atom':
  290. $xml .= ' xmlns:atom="http://www.w3.org/2005/Atom"';
  291. break;
  292. }
  293. }
  294. if ($cdata && !empty($content)) {
  295. $content = '<![CDATA[' . $content . ']]>';
  296. }
  297. $xml .= '>' . $content . '</' . $name. '>';
  298. $elem = Xml::build($xml, array('return' => 'domdocument'));
  299. $nodes = $elem->getElementsByTagName($bareName);
  300. foreach ($attrib as $key => $value) {
  301. $nodes->item(0)->setAttribute($key, $value);
  302. }
  303. foreach ($children as $k => $child) {
  304. $child = $elem->createElement($name, $child);
  305. $nodes->item(0)->appendChild($child);
  306. }
  307. $xml = $elem->saveXml();
  308. $xml = trim(substr($xml, strpos($xml, '?>') + 2));
  309. return $xml;
  310. }
  311. }