PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/View/Helper/RssHelper.php

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