RssView.php 9.8 KB

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