PageRenderTime 65ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/application/libraries/Zend/Feed/Writer/Renderer/Entry/Atom.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 444 lines | 299 code | 23 blank | 122 comment | 47 complexity | b704515315fa03ae2abf05d40d7974ee 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: Atom.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * @see Zend_Feed_Writer_Renderer_RendererAbstract
  23. */
  24. require_once 'Zend/Feed/Writer/Renderer/RendererAbstract.php';
  25. require_once 'Zend/Feed/Writer/Renderer/Feed/Atom/Source.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Feed_Writer
  29. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Feed_Writer_Renderer_Entry_Atom
  33. extends Zend_Feed_Writer_Renderer_RendererAbstract
  34. implements Zend_Feed_Writer_Renderer_RendererInterface
  35. {
  36. /**
  37. * Constructor
  38. *
  39. * @param Zend_Feed_Writer_Entry $container
  40. * @return void
  41. */
  42. public function __construct (Zend_Feed_Writer_Entry $container)
  43. {
  44. parent::__construct($container);
  45. }
  46. /**
  47. * Render atom entry
  48. *
  49. * @return Zend_Feed_Writer_Renderer_Entry_Atom
  50. */
  51. public function render()
  52. {
  53. $this->_dom = new DOMDocument('1.0', $this->_container->getEncoding());
  54. $this->_dom->formatOutput = true;
  55. $entry = $this->_dom->createElementNS(Zend_Feed_Writer::NAMESPACE_ATOM_10, 'entry');
  56. $this->_dom->appendChild($entry);
  57. $this->_setSource($this->_dom, $entry);
  58. $this->_setTitle($this->_dom, $entry);
  59. $this->_setDescription($this->_dom, $entry);
  60. $this->_setDateCreated($this->_dom, $entry);
  61. $this->_setDateModified($this->_dom, $entry);
  62. $this->_setLink($this->_dom, $entry);
  63. $this->_setId($this->_dom, $entry);
  64. $this->_setAuthors($this->_dom, $entry);
  65. $this->_setEnclosure($this->_dom, $entry);
  66. $this->_setContent($this->_dom, $entry);
  67. $this->_setCategories($this->_dom, $entry);
  68. foreach ($this->_extensions as $ext) {
  69. $ext->setType($this->getType());
  70. $ext->setRootElement($this->getRootElement());
  71. $ext->setDomDocument($this->getDomDocument(), $entry);
  72. $ext->render();
  73. }
  74. return $this;
  75. }
  76. /**
  77. * Set entry title
  78. *
  79. * @param DOMDocument $dom
  80. * @param DOMElement $root
  81. * @return void
  82. */
  83. protected function _setTitle(DOMDocument $dom, DOMElement $root)
  84. {
  85. if(!$this->getDataContainer()->getTitle()) {
  86. require_once 'Zend/Feed/Exception.php';
  87. $message = 'Atom 1.0 entry elements MUST contain exactly one'
  88. . ' atom:title element but a title has not been set';
  89. $exception = new Zend_Feed_Exception($message);
  90. if (!$this->_ignoreExceptions) {
  91. throw $exception;
  92. } else {
  93. $this->_exceptions[] = $exception;
  94. return;
  95. }
  96. }
  97. $title = $dom->createElement('title');
  98. $root->appendChild($title);
  99. $title->setAttribute('type', 'html');
  100. $cdata = $dom->createCDATASection($this->getDataContainer()->getTitle());
  101. $title->appendChild($cdata);
  102. }
  103. /**
  104. * Set entry description
  105. *
  106. * @param DOMDocument $dom
  107. * @param DOMElement $root
  108. * @return void
  109. */
  110. protected function _setDescription(DOMDocument $dom, DOMElement $root)
  111. {
  112. if(!$this->getDataContainer()->getDescription()) {
  113. return; // unless src content or base64
  114. }
  115. $subtitle = $dom->createElement('summary');
  116. $root->appendChild($subtitle);
  117. $subtitle->setAttribute('type', 'html');
  118. $cdata = $dom->createCDATASection(
  119. $this->getDataContainer()->getDescription()
  120. );
  121. $subtitle->appendChild($cdata);
  122. }
  123. /**
  124. * Set date entry was modified
  125. *
  126. * @param DOMDocument $dom
  127. * @param DOMElement $root
  128. * @return void
  129. */
  130. protected function _setDateModified(DOMDocument $dom, DOMElement $root)
  131. {
  132. if(!$this->getDataContainer()->getDateModified()) {
  133. require_once 'Zend/Feed/Exception.php';
  134. $message = 'Atom 1.0 entry elements MUST contain exactly one'
  135. . ' atom:updated element but a modification date has not been set';
  136. $exception = new Zend_Feed_Exception($message);
  137. if (!$this->_ignoreExceptions) {
  138. throw $exception;
  139. } else {
  140. $this->_exceptions[] = $exception;
  141. return;
  142. }
  143. }
  144. $updated = $dom->createElement('updated');
  145. $root->appendChild($updated);
  146. $text = $dom->createTextNode(
  147. $this->getDataContainer()->getDateModified()->get(Zend_Date::ISO_8601)
  148. );
  149. $updated->appendChild($text);
  150. }
  151. /**
  152. * Set date entry was created
  153. *
  154. * @param DOMDocument $dom
  155. * @param DOMElement $root
  156. * @return void
  157. */
  158. protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
  159. {
  160. if (!$this->getDataContainer()->getDateCreated()) {
  161. return;
  162. }
  163. $el = $dom->createElement('published');
  164. $root->appendChild($el);
  165. $text = $dom->createTextNode(
  166. $this->getDataContainer()->getDateCreated()->get(Zend_Date::ISO_8601)
  167. );
  168. $el->appendChild($text);
  169. }
  170. /**
  171. * Set entry authors
  172. *
  173. * @param DOMDocument $dom
  174. * @param DOMElement $root
  175. * @return void
  176. */
  177. protected function _setAuthors(DOMDocument $dom, DOMElement $root)
  178. {
  179. $authors = $this->_container->getAuthors();
  180. if ((!$authors || empty($authors))) {
  181. /**
  182. * This will actually trigger an Exception at the feed level if
  183. * a feed level author is not set.
  184. */
  185. return;
  186. }
  187. foreach ($authors as $data) {
  188. $author = $this->_dom->createElement('author');
  189. $name = $this->_dom->createElement('name');
  190. $author->appendChild($name);
  191. $root->appendChild($author);
  192. $text = $dom->createTextNode($data['name']);
  193. $name->appendChild($text);
  194. if (array_key_exists('email', $data)) {
  195. $email = $this->_dom->createElement('email');
  196. $author->appendChild($email);
  197. $text = $dom->createTextNode($data['email']);
  198. $email->appendChild($text);
  199. }
  200. if (array_key_exists('uri', $data)) {
  201. $uri = $this->_dom->createElement('uri');
  202. $author->appendChild($uri);
  203. $text = $dom->createTextNode($data['uri']);
  204. $uri->appendChild($text);
  205. }
  206. }
  207. }
  208. /**
  209. * Set entry enclosure
  210. *
  211. * @param DOMDocument $dom
  212. * @param DOMElement $root
  213. * @return void
  214. */
  215. protected function _setEnclosure(DOMDocument $dom, DOMElement $root)
  216. {
  217. $data = $this->_container->getEnclosure();
  218. if ((!$data || empty($data))) {
  219. return;
  220. }
  221. $enclosure = $this->_dom->createElement('link');
  222. $enclosure->setAttribute('rel', 'enclosure');
  223. if (isset($data['type'])) {
  224. $enclosure->setAttribute('type', $data['type']);
  225. }
  226. if (isset($data['length'])) {
  227. $enclosure->setAttribute('length', $data['length']);
  228. }
  229. $enclosure->setAttribute('href', $data['uri']);
  230. $root->appendChild($enclosure);
  231. }
  232. protected function _setLink(DOMDocument $dom, DOMElement $root)
  233. {
  234. if(!$this->getDataContainer()->getLink()) {
  235. return;
  236. }
  237. $link = $dom->createElement('link');
  238. $root->appendChild($link);
  239. $link->setAttribute('rel', 'alternate');
  240. $link->setAttribute('type', 'text/html');
  241. $link->setAttribute('href', $this->getDataContainer()->getLink());
  242. }
  243. /**
  244. * Set entry identifier
  245. *
  246. * @param DOMDocument $dom
  247. * @param DOMElement $root
  248. * @return void
  249. */
  250. protected function _setId(DOMDocument $dom, DOMElement $root)
  251. {
  252. if(!$this->getDataContainer()->getId()
  253. && !$this->getDataContainer()->getLink()) {
  254. require_once 'Zend/Feed/Exception.php';
  255. $message = 'Atom 1.0 entry elements MUST contain exactly one '
  256. . 'atom:id element, or as an alternative, we can use the same '
  257. . 'value as atom:link however neither a suitable link nor an '
  258. . 'id have been set';
  259. $exception = new Zend_Feed_Exception($message);
  260. if (!$this->_ignoreExceptions) {
  261. throw $exception;
  262. } else {
  263. $this->_exceptions[] = $exception;
  264. return;
  265. }
  266. }
  267. if (!$this->getDataContainer()->getId()) {
  268. $this->getDataContainer()->setId(
  269. $this->getDataContainer()->getLink());
  270. }
  271. if (!Zend_Uri::check($this->getDataContainer()->getId()) &&
  272. !preg_match("#^urn:[a-zA-Z0-9][a-zA-Z0-9\-]{1,31}:([a-zA-Z0-9\(\)\+\,\.\:\=\@\;\$\_\!\*\-]|%[0-9a-fA-F]{2})*#",
  273. $this->getDataContainer()->getId()
  274. ) && !$this->_validateTagUri($this->getDataContainer()->getId())) {
  275. require_once 'Zend/Feed/Exception.php';
  276. throw new Zend_Feed_Exception('Atom 1.0 IDs must be a valid URI/IRI');
  277. }
  278. $id = $dom->createElement('id');
  279. $root->appendChild($id);
  280. $text = $dom->createTextNode($this->getDataContainer()->getId());
  281. $id->appendChild($text);
  282. }
  283. /**
  284. * Validate a URI using the tag scheme (RFC 4151)
  285. *
  286. * @param string $id
  287. * @return bool
  288. */
  289. protected function _validateTagUri($id)
  290. {
  291. if (preg_match('/^tag:(?<name>.*),(?<date>\d{4}-?\d{0,2}-?\d{0,2}):(?<specific>.*)(.*:)*$/', $id, $matches)) {
  292. $dvalid = false;
  293. $nvalid = false;
  294. $date = $matches['date'];
  295. $d6 = strtotime($date);
  296. if ((strlen($date) == 4) && $date <= date('Y')) {
  297. $dvalid = true;
  298. } elseif ((strlen($date) == 7) && ($d6 < strtotime("now"))) {
  299. $dvalid = true;
  300. } elseif ((strlen($date) == 10) && ($d6 < strtotime("now"))) {
  301. $dvalid = true;
  302. }
  303. $validator = new Zend_Validate_EmailAddress;
  304. if ($validator->isValid($matches['name'])) {
  305. $nvalid = true;
  306. } else {
  307. $nvalid = $validator->isValid('info@' . $matches['name']);
  308. }
  309. return $dvalid && $nvalid;
  310. }
  311. return false;
  312. }
  313. /**
  314. * Set entry content
  315. *
  316. * @param DOMDocument $dom
  317. * @param DOMElement $root
  318. * @return void
  319. */
  320. protected function _setContent(DOMDocument $dom, DOMElement $root)
  321. {
  322. $content = $this->getDataContainer()->getContent();
  323. if (!$content && !$this->getDataContainer()->getLink()) {
  324. require_once 'Zend/Feed/Exception.php';
  325. $message = 'Atom 1.0 entry elements MUST contain exactly one '
  326. . 'atom:content element, or as an alternative, at least one link '
  327. . 'with a rel attribute of "alternate" to indicate an alternate '
  328. . 'method to consume the content.';
  329. $exception = new Zend_Feed_Exception($message);
  330. if (!$this->_ignoreExceptions) {
  331. throw $exception;
  332. } else {
  333. $this->_exceptions[] = $exception;
  334. return;
  335. }
  336. }
  337. if (!$content) {
  338. return;
  339. }
  340. $element = $dom->createElement('content');
  341. $element->setAttribute('type', 'xhtml');
  342. $xhtmlElement = $this->_loadXhtml($content);
  343. $xhtml = $dom->importNode($xhtmlElement, true);
  344. $element->appendChild($xhtml);
  345. $root->appendChild($element);
  346. }
  347. /**
  348. * Load a HTML string and attempt to normalise to XML
  349. */
  350. protected function _loadXhtml($content)
  351. {
  352. $xhtml = '';
  353. if (class_exists('tidy', false)) {
  354. $tidy = new tidy;
  355. $config = array(
  356. 'output-xhtml' => true,
  357. 'show-body-only' => true,
  358. 'quote-nbsp' => false
  359. );
  360. $encoding = str_replace('-', '', $this->getEncoding());
  361. $tidy->parseString($content, $config, $encoding);
  362. $tidy->cleanRepair();
  363. $xhtml = (string) $tidy;
  364. } else {
  365. $xhtml = $content;
  366. }
  367. $xhtml = preg_replace(array(
  368. "/(<[\/]?)([a-zA-Z]+)/"
  369. ), '$1xhtml:$2', $xhtml);
  370. $dom = new DOMDocument('1.0', $this->getEncoding());
  371. $dom->loadXML('<xhtml:div xmlns:xhtml="http://www.w3.org/1999/xhtml">'
  372. . $xhtml . '</xhtml:div>');
  373. return $dom->documentElement;
  374. }
  375. /**
  376. * Set entry cateories
  377. *
  378. * @param DOMDocument $dom
  379. * @param DOMElement $root
  380. * @return void
  381. */
  382. protected function _setCategories(DOMDocument $dom, DOMElement $root)
  383. {
  384. $categories = $this->getDataContainer()->getCategories();
  385. if (!$categories) {
  386. return;
  387. }
  388. foreach ($categories as $cat) {
  389. $category = $dom->createElement('category');
  390. $category->setAttribute('term', $cat['term']);
  391. if (isset($cat['label'])) {
  392. $category->setAttribute('label', $cat['label']);
  393. } else {
  394. $category->setAttribute('label', $cat['term']);
  395. }
  396. if (isset($cat['scheme'])) {
  397. $category->setAttribute('scheme', $cat['scheme']);
  398. }
  399. $root->appendChild($category);
  400. }
  401. }
  402. /**
  403. * Append Source element (Atom 1.0 Feed Metadata)
  404. *
  405. * @param DOMDocument $dom
  406. * @param DOMElement $root
  407. * @return void
  408. */
  409. protected function _setSource(DOMDocument $dom, DOMElement $root)
  410. {
  411. $source = $this->getDataContainer()->getSource();
  412. if (!$source) {
  413. return;
  414. }
  415. $renderer = new Zend_Feed_Writer_Renderer_Feed_Atom_Source($source);
  416. $renderer->setType($this->getType());
  417. $element = $renderer->render()->getElement();
  418. $imported = $dom->importNode($element, true);
  419. $root->appendChild($imported);
  420. }
  421. }