Atom.php 15 KB

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