RssView.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. /**
  3. * Licensed under The MIT License
  4. * For full copyright and license information, please see the LICENSE.txt
  5. * Redistributions of files must retain the above copyright notice.
  6. *
  7. * @author Mark Scherer
  8. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  9. * @link http://www.dereuromark.de/2013/10/03/rss-feeds-in-cakephp
  10. */
  11. App::uses('View', 'View');
  12. App::uses('Xml', 'Utility');
  13. App::uses('CakeTime', 'Utility');
  14. App::uses('Routing', 'Router');
  15. /**
  16. * A view class that is used for creating RSS feeds.
  17. *
  18. * By setting the '_serialize' key in your controller, you can specify a view variable
  19. * that should be serialized to XML and used as the response for the request.
  20. * This allows you to omit views + layouts, if your just need to emit a single view
  21. * variable as the XML response.
  22. *
  23. * In your controller, you could do the following:
  24. *
  25. * `$this->set(array('posts' => $posts, '_serialize' => 'posts'));`
  26. *
  27. * When the view is rendered, the `$posts` view variable will be serialized
  28. * into the RSS XML.
  29. *
  30. * **Note** The view variable you specify must be compatible with Xml::fromArray().
  31. *
  32. * If you don't use the `_serialize` key, you will need a view. You can use extended
  33. * views to provide layout like functionality. This is currently not yet tested/supported.
  34. */
  35. class RssView extends View {
  36. /**
  37. * Default spec version of generated RSS.
  38. *
  39. * @var string
  40. */
  41. public $version = '2.0';
  42. /**
  43. * The subdirectory. RSS views are always in rss. Currently not in use.
  44. *
  45. * @var string
  46. */
  47. public $subDir = 'rss';
  48. /**
  49. * Holds usable namespaces.
  50. *
  51. * @var array
  52. * @link http://validator.w3.org/feed/docs/howto/declare_namespaces.html
  53. */
  54. protected $_namespaces = array(
  55. 'atom' => 'http://www.w3.org/2005/Atom',
  56. 'content' => 'http://purl.org/rss/1.0/modules/content/',
  57. 'dc' => 'http://purl.org/dc/elements/1.1/',
  58. 'sy' => 'http://purl.org/rss/1.0/modules/syndication/'
  59. );
  60. /**
  61. * Holds the namespace keys in use.
  62. *
  63. * @var array
  64. */
  65. protected $_usedNamespaces = array();
  66. /**
  67. * Holds CDATA placeholders.
  68. *
  69. * @var array
  70. */
  71. protected $_cdata = array();
  72. /**
  73. * Constructor
  74. *
  75. * @param Controller $controller
  76. */
  77. public function __construct(Controller $controller = null) {
  78. parent::__construct($controller);
  79. if (isset($controller->response) && $controller->response instanceof CakeResponse) {
  80. $controller->response->type('rss');
  81. }
  82. }
  83. /**
  84. * If you are using namespaces that are not yet known to the class, you need to globablly
  85. * add them with this method. Namespaces will only be added for actually used prefixes.
  86. *
  87. * @param string $prefix
  88. * @param string $url
  89. * @return void
  90. */
  91. public function setNamespace($prefix, $url) {
  92. $this->_namespaces[$prefix] = $url;
  93. }
  94. /**
  95. * Prepares the channel and sets default values.
  96. *
  97. * @param array $channel
  98. * @return array Channel
  99. */
  100. public function channel($channel) {
  101. if (!isset($channel['link'])) {
  102. $channel['link'] = '/';
  103. }
  104. if (!isset($channel['title'])) {
  105. $channel['title'] = '';
  106. }
  107. if (!isset($channel['description'])) {
  108. $channel['description'] = '';
  109. }
  110. $channel = $this->_prepareOutput($channel);
  111. return $channel;
  112. }
  113. /**
  114. * Converts a time in any format to an RSS time
  115. *
  116. * @param integer|string|DateTime $time
  117. * @return string An RSS-formatted timestamp
  118. * @see CakeTime::toRSS
  119. */
  120. public function time($time) {
  121. return CakeTime::toRSS($time);
  122. }
  123. /**
  124. * Skip loading helpers if this is a _serialize based view.
  125. *
  126. * @return void
  127. */
  128. public function loadHelpers() {
  129. if (isset($this->viewVars['_serialize'])) {
  130. return;
  131. }
  132. parent::loadHelpers();
  133. }
  134. /**
  135. * Render a RSS view.
  136. *
  137. * Uses the special '_serialize' parameter to convert a set of
  138. * view variables into a XML response. Makes generating simple
  139. * XML responses very easy. You can omit the '_serialize' parameter,
  140. * and use a normal view + layout as well.
  141. *
  142. * @param string $view The view being rendered.
  143. * @param string $layout The layout being rendered.
  144. * @return string The rendered view.
  145. */
  146. public function render($view = null, $layout = null) {
  147. if (isset($this->viewVars['_serialize'])) {
  148. return $this->_serialize($this->viewVars['_serialize']);
  149. }
  150. if ($view !== false && $this->_getViewFileName($view)) {
  151. return parent::render($view, false);
  152. }
  153. }
  154. /**
  155. * Serialize view vars.
  156. *
  157. * @param array $serialize The viewVars that need to be serialized.
  158. * @return string The serialized data
  159. */
  160. protected function _serialize($serialize) {
  161. $rootNode = isset($this->viewVars['_rootNode']) ? $this->viewVars['_rootNode'] : 'channel';
  162. if (is_array($serialize)) {
  163. $data = array($rootNode => array());
  164. foreach ($serialize as $alias => $key) {
  165. if (is_numeric($alias)) {
  166. $alias = $key;
  167. }
  168. $data[$rootNode][$alias] = $this->viewVars[$key];
  169. }
  170. } else {
  171. $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
  172. if (is_array($data) && Set::numeric(array_keys($data))) {
  173. $data = array($rootNode => array($serialize => $data));
  174. }
  175. }
  176. $defaults = array('document' => array(), 'channel' => array(), 'items' => array());
  177. $data += $defaults;
  178. if (!empty($data['document']['namespace'])) {
  179. foreach ($data['document']['namespace'] as $prefix => $url) {
  180. $this->setNamespace($prefix, $url);
  181. }
  182. }
  183. $channel = $this->channel($data['channel']);
  184. if (!empty($channel['image']) && empty($channel['image']['title'])) {
  185. $channel['image']['title'] = $channel['title'];
  186. }
  187. foreach ($data['items'] as $item) {
  188. $channel['item'][] = $this->_prepareOutput($item);
  189. }
  190. $array = array(
  191. 'rss' => array(
  192. '@version' => $this->version,
  193. 'channel' => $channel,
  194. )
  195. );
  196. $namespaces = array();
  197. foreach ($this->_usedNamespaces as $usedNamespacePrefix) {
  198. if (!isset($this->_namespaces[$usedNamespacePrefix])) {
  199. throw new RuntimeException(__('The prefix %s is not specified.', $usedNamespacePrefix));
  200. }
  201. $namespaces['xmlns:' . $usedNamespacePrefix] = $this->_namespaces[$usedNamespacePrefix];
  202. }
  203. $array['rss'] += $namespaces;
  204. $options = array();
  205. if (Configure::read('debug')) {
  206. $options['pretty'] = true;
  207. }
  208. $output = Xml::fromArray($array, $options)->asXML();
  209. $output = $this->_replaceCdata($output);
  210. return $output;
  211. }
  212. /**
  213. * RssView::_prepareOutput()
  214. *
  215. * @param aray $item
  216. * @return void
  217. */
  218. protected function _prepareOutput($item) {
  219. foreach ($item as $key => $val) {
  220. // Detect namespaces
  221. $prefix = null;
  222. $bareKey = $key;
  223. if (strpos($key, ':') !== false) {
  224. list($prefix, $bareKey) = explode(':', $key, 2);
  225. if (strpos($prefix, '@') !== false) {
  226. $prefix = substr($prefix, 1);
  227. }
  228. if (!in_array($prefix, $this->_usedNamespaces)) {
  229. $this->_usedNamespaces[] = $prefix;
  230. }
  231. }
  232. $attrib = null;
  233. switch ($bareKey) {
  234. case 'encoded':
  235. $val = $this->_newCdata($val);
  236. break;
  237. case 'pubDate':
  238. $val = $this->time($val);
  239. break;
  240. /*
  241. case 'category' :
  242. if (is_array($val) && !empty($val[0])) {
  243. foreach ($val as $category) {
  244. $attrib = array();
  245. if (is_array($category) && isset($category['domain'])) {
  246. $attrib['domain'] = $category['domain'];
  247. unset($category['domain']);
  248. }
  249. $categories[] = $this->elem($key, $attrib, $category);
  250. }
  251. $elements[$key] = implode('', $categories);
  252. continue 2;
  253. } elseif (is_array($val) && isset($val['domain'])) {
  254. $attrib['domain'] = $val['domain'];
  255. }
  256. break;
  257. */
  258. case 'link':
  259. case 'url':
  260. case 'guid':
  261. case 'comments':
  262. if (is_array($val) && isset($val['@href'])) {
  263. $attrib = $val;
  264. $attrib['@href'] = Router::url($val['@href'], true);
  265. if ($prefix === 'atom') {
  266. $attrib['@rel'] = 'self';
  267. $attrib['@type'] = 'application/rss+xml';
  268. }
  269. $val = $attrib;
  270. } elseif (is_array($val) && isset($val['url'])) {
  271. $val['url'] = Router::url($val['url'], true);
  272. if ($bareKey === 'guid') {
  273. $val['@'] = $val['url'];
  274. unset($val['url']);
  275. }
  276. } else {
  277. $val = Router::url($val, true);
  278. }
  279. break;
  280. case 'source':
  281. if (is_array($val) && isset($val['url'])) {
  282. $attrib['url'] = Router::url($val['url'], true);
  283. $val = $val['title'];
  284. } elseif (is_array($val)) {
  285. $attrib['url'] = Router::url($val[0], true);
  286. $val = $val[1];
  287. }
  288. break;
  289. case 'enclosure':
  290. if (is_string($val['url']) && is_file(WWW_ROOT . $val['url']) && file_exists(WWW_ROOT . $val['url'])) {
  291. if (!isset($val['length']) && strpos($val['url'], '://') === false) {
  292. $val['length'] = sprintf("%u", filesize(WWW_ROOT . $val['url']));
  293. }
  294. if (!isset($val['type']) && function_exists('mime_content_type')) {
  295. $val['type'] = mime_content_type(WWW_ROOT . $val['url']);
  296. }
  297. }
  298. $val['url'] = Router::url($val['url'], true);
  299. $attrib = $val;
  300. $val = null;
  301. break;
  302. default:
  303. //$attrib = $att;
  304. }
  305. if (is_array($val)) {
  306. $val = $this->_prepareOutput($val);
  307. }
  308. $item[$key] = $val;
  309. }
  310. return $item;
  311. }
  312. /**
  313. * RssView::_newCdata()
  314. *
  315. * @param string $content
  316. * @return string
  317. */
  318. protected function _newCdata($content) {
  319. $i = count($this->_cdata);
  320. $this->_cdata[$i] = $content;
  321. return '###CDATA-' . $i . '###';
  322. }
  323. /**
  324. * RssView::_replaceCdata()
  325. *
  326. * @param string $content
  327. * @return string
  328. */
  329. protected function _replaceCdata($content) {
  330. foreach ($this->_cdata as $n => $data) {
  331. $data = '<![CDATA[' . $data . ']]>';
  332. $content = str_replace('###CDATA-' . $n . '###', $data, $content);
  333. }
  334. return $content;
  335. }
  336. }