RssHelper.php 8.6 KB

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