PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Feed/Writer/Renderer/Feed/Rss.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 505 lines | 346 code | 27 blank | 132 comment | 56 complexity | f92910e51555caf464e94df552af1bde MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Feed_Writer
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Rss.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /** @see Zend_Feed_Writer_Feed */
  22. require_once 'Zend/Feed/Writer/Feed.php';
  23. /** @see Zend_Version */
  24. require_once 'Zend/Version.php';
  25. /** @see Zend_Feed_Writer_Renderer_RendererInterface */
  26. require_once 'Zend/Feed/Writer/Renderer/RendererInterface.php';
  27. /** @see Zend_Feed_Writer_Renderer_Entry_Rss */
  28. require_once 'Zend/Feed/Writer/Renderer/Entry/Rss.php';
  29. /** @see Zend_Feed_Writer_Renderer_RendererAbstract */
  30. require_once 'Zend/Feed/Writer/Renderer/RendererAbstract.php';
  31. /**
  32. * @category Zend
  33. * @package Zend_Feed_Writer
  34. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Feed_Writer_Renderer_Feed_Rss
  38. extends Zend_Feed_Writer_Renderer_RendererAbstract
  39. implements Zend_Feed_Writer_Renderer_RendererInterface
  40. {
  41. /**
  42. * Constructor
  43. *
  44. * @param Zend_Feed_Writer_Feed $container
  45. * @return void
  46. */
  47. public function __construct (Zend_Feed_Writer_Feed $container)
  48. {
  49. parent::__construct($container);
  50. }
  51. /**
  52. * Render RSS feed
  53. *
  54. * @return Zend_Feed_Writer_Renderer_Feed_Rss
  55. */
  56. public function render()
  57. {
  58. if (!$this->_container->getEncoding()) {
  59. $this->_container->setEncoding('UTF-8');
  60. }
  61. $this->_dom = new DOMDocument('1.0', $this->_container->getEncoding());
  62. $this->_dom->formatOutput = true;
  63. $this->_dom->substituteEntities = false;
  64. $rss = $this->_dom->createElement('rss');
  65. $this->setRootElement($rss);
  66. $rss->setAttribute('version', '2.0');
  67. $channel = $this->_dom->createElement('channel');
  68. $rss->appendChild($channel);
  69. $this->_dom->appendChild($rss);
  70. $this->_setLanguage($this->_dom, $channel);
  71. $this->_setBaseUrl($this->_dom, $channel);
  72. $this->_setTitle($this->_dom, $channel);
  73. $this->_setDescription($this->_dom, $channel);
  74. $this->_setImage($this->_dom, $channel);
  75. $this->_setDateCreated($this->_dom, $channel);
  76. $this->_setDateModified($this->_dom, $channel);
  77. $this->_setLastBuildDate($this->_dom, $channel);
  78. $this->_setGenerator($this->_dom, $channel);
  79. $this->_setLink($this->_dom, $channel);
  80. $this->_setAuthors($this->_dom, $channel);
  81. $this->_setCopyright($this->_dom, $channel);
  82. $this->_setCategories($this->_dom, $channel);
  83. foreach ($this->_extensions as $ext) {
  84. $ext->setType($this->getType());
  85. $ext->setRootElement($this->getRootElement());
  86. $ext->setDomDocument($this->getDomDocument(), $channel);
  87. $ext->render();
  88. }
  89. foreach ($this->_container as $entry) {
  90. if ($this->getDataContainer()->getEncoding()) {
  91. $entry->setEncoding($this->getDataContainer()->getEncoding());
  92. }
  93. if ($entry instanceof Zend_Feed_Writer_Entry) {
  94. $renderer = new Zend_Feed_Writer_Renderer_Entry_Rss($entry);
  95. } else {
  96. continue;
  97. }
  98. if ($this->_ignoreExceptions === true) {
  99. $renderer->ignoreExceptions();
  100. }
  101. $renderer->setType($this->getType());
  102. $renderer->setRootElement($this->_dom->documentElement);
  103. $renderer->render();
  104. $element = $renderer->getElement();
  105. $imported = $this->_dom->importNode($element, true);
  106. $channel->appendChild($imported);
  107. }
  108. return $this;
  109. }
  110. /**
  111. * Set feed language
  112. *
  113. * @param DOMDocument $dom
  114. * @param DOMElement $root
  115. * @return void
  116. */
  117. protected function _setLanguage(DOMDocument $dom, DOMElement $root)
  118. {
  119. $lang = $this->getDataContainer()->getLanguage();
  120. if (!$lang) {
  121. return;
  122. }
  123. $language = $dom->createElement('language');
  124. $root->appendChild($language);
  125. $language->nodeValue = $lang;
  126. }
  127. /**
  128. * Set feed title
  129. *
  130. * @param DOMDocument $dom
  131. * @param DOMElement $root
  132. * @return void
  133. */
  134. protected function _setTitle(DOMDocument $dom, DOMElement $root)
  135. {
  136. if(!$this->getDataContainer()->getTitle()) {
  137. require_once 'Zend/Feed/Exception.php';
  138. $message = 'RSS 2.0 feed elements MUST contain exactly one'
  139. . ' title element but a title has not been set';
  140. $exception = new Zend_Feed_Exception($message);
  141. if (!$this->_ignoreExceptions) {
  142. throw $exception;
  143. } else {
  144. $this->_exceptions[] = $exception;
  145. return;
  146. }
  147. }
  148. $title = $dom->createElement('title');
  149. $root->appendChild($title);
  150. $text = $dom->createTextNode($this->getDataContainer()->getTitle());
  151. $title->appendChild($text);
  152. }
  153. /**
  154. * Set feed description
  155. *
  156. * @param DOMDocument $dom
  157. * @param DOMElement $root
  158. * @return void
  159. */
  160. protected function _setDescription(DOMDocument $dom, DOMElement $root)
  161. {
  162. if(!$this->getDataContainer()->getDescription()) {
  163. require_once 'Zend/Feed/Exception.php';
  164. $message = 'RSS 2.0 feed elements MUST contain exactly one'
  165. . ' description element but one has not been set';
  166. $exception = new Zend_Feed_Exception($message);
  167. if (!$this->_ignoreExceptions) {
  168. throw $exception;
  169. } else {
  170. $this->_exceptions[] = $exception;
  171. return;
  172. }
  173. }
  174. $subtitle = $dom->createElement('description');
  175. $root->appendChild($subtitle);
  176. $text = $dom->createTextNode($this->getDataContainer()->getDescription());
  177. $subtitle->appendChild($text);
  178. }
  179. /**
  180. * Set date feed was last modified
  181. *
  182. * @param DOMDocument $dom
  183. * @param DOMElement $root
  184. * @return void
  185. */
  186. protected function _setDateModified(DOMDocument $dom, DOMElement $root)
  187. {
  188. if(!$this->getDataContainer()->getDateModified()) {
  189. return;
  190. }
  191. $updated = $dom->createElement('pubDate');
  192. $root->appendChild($updated);
  193. $text = $dom->createTextNode(
  194. $this->getDataContainer()->getDateModified()->get(Zend_Date::RSS)
  195. );
  196. $updated->appendChild($text);
  197. }
  198. /**
  199. * Set feed generator string
  200. *
  201. * @param DOMDocument $dom
  202. * @param DOMElement $root
  203. * @return void
  204. */
  205. protected function _setGenerator(DOMDocument $dom, DOMElement $root)
  206. {
  207. if(!$this->getDataContainer()->getGenerator()) {
  208. $this->getDataContainer()->setGenerator('Zend_Feed_Writer',
  209. Zend_Version::VERSION, 'http://framework.zend.com');
  210. }
  211. $gdata = $this->getDataContainer()->getGenerator();
  212. $generator = $dom->createElement('generator');
  213. $root->appendChild($generator);
  214. $name = $gdata['name'];
  215. if (array_key_exists('version', $gdata)) {
  216. $name .= ' ' . $gdata['version'];
  217. }
  218. if (array_key_exists('uri', $gdata)) {
  219. $name .= ' (' . $gdata['uri'] . ')';
  220. }
  221. $text = $dom->createTextNode($name);
  222. $generator->appendChild($text);
  223. }
  224. /**
  225. * Set link to feed
  226. *
  227. * @param DOMDocument $dom
  228. * @param DOMElement $root
  229. * @return void
  230. */
  231. protected function _setLink(DOMDocument $dom, DOMElement $root)
  232. {
  233. $value = $this->getDataContainer()->getLink();
  234. if(!$value) {
  235. require_once 'Zend/Feed/Exception.php';
  236. $message = 'RSS 2.0 feed elements MUST contain exactly one'
  237. . ' link element but one has not been set';
  238. $exception = new Zend_Feed_Exception($message);
  239. if (!$this->_ignoreExceptions) {
  240. throw $exception;
  241. } else {
  242. $this->_exceptions[] = $exception;
  243. return;
  244. }
  245. }
  246. $link = $dom->createElement('link');
  247. $root->appendChild($link);
  248. $text = $dom->createTextNode($value);
  249. $link->appendChild($text);
  250. if (!Zend_Uri::check($value)) {
  251. $link->setAttribute('isPermaLink', 'false');
  252. }
  253. }
  254. /**
  255. * Set feed authors
  256. *
  257. * @param DOMDocument $dom
  258. * @param DOMElement $root
  259. * @return void
  260. */
  261. protected function _setAuthors(DOMDocument $dom, DOMElement $root)
  262. {
  263. $authors = $this->getDataContainer()->getAuthors();
  264. if (!$authors || empty($authors)) {
  265. return;
  266. }
  267. foreach ($authors as $data) {
  268. $author = $this->_dom->createElement('author');
  269. $name = $data['name'];
  270. if (array_key_exists('email', $data)) {
  271. $name = $data['email'] . ' (' . $data['name'] . ')';
  272. }
  273. $text = $dom->createTextNode($name);
  274. $author->appendChild($text);
  275. $root->appendChild($author);
  276. }
  277. }
  278. /**
  279. * Set feed copyright
  280. *
  281. * @param DOMDocument $dom
  282. * @param DOMElement $root
  283. * @return void
  284. */
  285. protected function _setCopyright(DOMDocument $dom, DOMElement $root)
  286. {
  287. $copyright = $this->getDataContainer()->getCopyright();
  288. if (!$copyright) {
  289. return;
  290. }
  291. $copy = $dom->createElement('copyright');
  292. $root->appendChild($copy);
  293. $text = $dom->createTextNode($copyright);
  294. $copy->appendChild($text);
  295. }
  296. /**
  297. * Set feed channel image
  298. *
  299. * @param DOMDocument $dom
  300. * @param DOMElement $root
  301. * @return void
  302. */
  303. protected function _setImage(DOMDocument $dom, DOMElement $root)
  304. {
  305. $image = $this->getDataContainer()->getImage();
  306. if (!$image) {
  307. return;
  308. }
  309. if (!isset($image['title']) || empty($image['title'])
  310. || !is_string($image['title'])) {
  311. require_once 'Zend/Feed/Exception.php';
  312. $message = 'RSS 2.0 feed images must include a title';
  313. $exception = new Zend_Feed_Exception($message);
  314. if (!$this->_ignoreExceptions) {
  315. throw $exception;
  316. } else {
  317. $this->_exceptions[] = $exception;
  318. return;
  319. }
  320. }
  321. if (empty($image['link']) || !is_string($image['link'])
  322. || !Zend_Uri::check($image['link'])) {
  323. require_once 'Zend/Feed/Exception.php';
  324. $message = 'Invalid parameter: parameter \'link\''
  325. . ' must be a non-empty string and valid URI/IRI';
  326. $exception = new Zend_Feed_Exception($message);
  327. if (!$this->_ignoreExceptions) {
  328. throw $exception;
  329. } else {
  330. $this->_exceptions[] = $exception;
  331. return;
  332. }
  333. }
  334. $img = $dom->createElement('image');
  335. $root->appendChild($img);
  336. $url = $dom->createElement('url');
  337. $text = $dom->createTextNode($image['uri']);
  338. $url->appendChild($text);
  339. $title = $dom->createElement('title');
  340. $text = $dom->createTextNode($image['title']);
  341. $title->appendChild($text);
  342. $link = $dom->createElement('link');
  343. $text = $dom->createTextNode($image['link']);
  344. $link->appendChild($text);
  345. $img->appendChild($url);
  346. $img->appendChild($title);
  347. $img->appendChild($link);
  348. if (isset($image['height'])) {
  349. if (!ctype_digit((string) $image['height']) || $image['height'] > 400) {
  350. require_once 'Zend/Feed/Exception.php';
  351. $message = 'Invalid parameter: parameter \'height\''
  352. . ' must be an integer not exceeding 400';
  353. $exception = new Zend_Feed_Exception($message);
  354. if (!$this->_ignoreExceptions) {
  355. throw $exception;
  356. } else {
  357. $this->_exceptions[] = $exception;
  358. return;
  359. }
  360. }
  361. $height = $dom->createElement('height');
  362. $text = $dom->createTextNode($image['height']);
  363. $height->appendChild($text);
  364. $img->appendChild($height);
  365. }
  366. if (isset($image['width'])) {
  367. if (!ctype_digit((string) $image['width']) || $image['width'] > 144) {
  368. require_once 'Zend/Feed/Exception.php';
  369. $message = 'Invalid parameter: parameter \'width\''
  370. . ' must be an integer not exceeding 144';
  371. $exception = new Zend_Feed_Exception($message);
  372. if (!$this->_ignoreExceptions) {
  373. throw $exception;
  374. } else {
  375. $this->_exceptions[] = $exception;
  376. return;
  377. }
  378. }
  379. $width = $dom->createElement('width');
  380. $text = $dom->createTextNode($image['width']);
  381. $width->appendChild($text);
  382. $img->appendChild($width);
  383. }
  384. if (isset($image['description'])) {
  385. if (empty($image['description']) || !is_string($image['description'])) {
  386. require_once 'Zend/Feed/Exception.php';
  387. $message = 'Invalid parameter: parameter \'description\''
  388. . ' must be a non-empty string';
  389. $exception = new Zend_Feed_Exception($message);
  390. if (!$this->_ignoreExceptions) {
  391. throw $exception;
  392. } else {
  393. $this->_exceptions[] = $exception;
  394. return;
  395. }
  396. }
  397. $desc = $dom->createElement('description');
  398. $text = $dom->createTextNode($image['description']);
  399. $desc->appendChild($text);
  400. $img->appendChild($desc);
  401. }
  402. }
  403. /**
  404. * Set date feed was created
  405. *
  406. * @param DOMDocument $dom
  407. * @param DOMElement $root
  408. * @return void
  409. */
  410. protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
  411. {
  412. if(!$this->getDataContainer()->getDateCreated()) {
  413. return;
  414. }
  415. if(!$this->getDataContainer()->getDateModified()) {
  416. $this->getDataContainer()->setDateModified(
  417. $this->getDataContainer()->getDateCreated()
  418. );
  419. }
  420. }
  421. /**
  422. * Set date feed last build date
  423. *
  424. * @param DOMDocument $dom
  425. * @param DOMElement $root
  426. * @return void
  427. */
  428. protected function _setLastBuildDate(DOMDocument $dom, DOMElement $root)
  429. {
  430. if(!$this->getDataContainer()->getLastBuildDate()) {
  431. return;
  432. }
  433. $lastBuildDate = $dom->createElement('lastBuildDate');
  434. $root->appendChild($lastBuildDate);
  435. $text = $dom->createTextNode(
  436. $this->getDataContainer()->getLastBuildDate()->get(Zend_Date::RSS)
  437. );
  438. $lastBuildDate->appendChild($text);
  439. }
  440. /**
  441. * Set base URL to feed links
  442. *
  443. * @param DOMDocument $dom
  444. * @param DOMElement $root
  445. * @return void
  446. */
  447. protected function _setBaseUrl(DOMDocument $dom, DOMElement $root)
  448. {
  449. $baseUrl = $this->getDataContainer()->getBaseUrl();
  450. if (!$baseUrl) {
  451. return;
  452. }
  453. $root->setAttribute('xml:base', $baseUrl);
  454. }
  455. /**
  456. * Set feed categories
  457. *
  458. * @param DOMDocument $dom
  459. * @param DOMElement $root
  460. * @return void
  461. */
  462. protected function _setCategories(DOMDocument $dom, DOMElement $root)
  463. {
  464. $categories = $this->getDataContainer()->getCategories();
  465. if (!$categories) {
  466. return;
  467. }
  468. foreach ($categories as $cat) {
  469. $category = $dom->createElement('category');
  470. if (isset($cat['scheme'])) {
  471. $category->setAttribute('domain', $cat['scheme']);
  472. }
  473. $text = $dom->createTextNode($cat['term']);
  474. $category->appendChild($text);
  475. $root->appendChild($category);
  476. }
  477. }
  478. }