RssView.php 10 KB

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