AtomFormat.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Atom
  4. * Documentation Source http://en.wikipedia.org/wiki/Atom_%28standard%29 and http://tools.ietf.org/html/rfc4287
  5. */
  6. class AtomFormat extends FormatAbstract{
  7. public function stringify(){
  8. /* Datas preparation */
  9. $https = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 's' : '' );
  10. $httpHost = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
  11. $httpInfo = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
  12. $serverRequestUri = $this->xml_encode($_SERVER['REQUEST_URI']);
  13. $extraInfos = $this->getExtraInfos();
  14. $title = $this->xml_encode($extraInfos['name']);
  15. $uri = !empty($extraInfos['uri']) ? $extraInfos['uri'] : 'https://github.com/sebsauvage/rss-bridge';
  16. $icon = $this->xml_encode('http://icons.better-idea.org/icon?url='. $uri .'&size=64');
  17. $uri = $this->xml_encode($uri);
  18. $entries = '';
  19. foreach($this->getDatas() as $data){
  20. $entryAuthor = is_null($data['author']) ? '' : $this->xml_encode($data['author']);
  21. $entryTitle = is_null($data['title']) ? '' : $this->xml_encode($data['title']);
  22. $entryUri = is_null($data['uri']) ? '' : $this->xml_encode($data['uri']);
  23. $entryTimestamp = is_null($data['timestamp']) ? '' : $this->xml_encode(date(DATE_ATOM, $data['timestamp']));
  24. $entryContent = is_null($data['content']) ? '' : $this->xml_encode($this->sanitizeHtml($data['content']));
  25. $entries .= <<<EOD
  26. <entry>
  27. <author>
  28. <name>{$entryAuthor}</name>
  29. </author>
  30. <title type="html"><![CDATA[{$entryTitle}]]></title>
  31. <link rel="alternate" type="text/html" href="{$entryUri}" />
  32. <id>{$entryUri}</id>
  33. <updated>{$entryTimestamp}</updated>
  34. <content type="html">{$entryContent}</content>
  35. </entry>
  36. EOD;
  37. }
  38. /*
  39. TODO :
  40. - Security: Disable Javascript ?
  41. - <updated> : Define new extra info ?
  42. - <content type="html"> : RFC look with xhtml, keep this in spite of ?
  43. */
  44. // #### TEMPORARY FIX ###
  45. $feedTimestamp = date(DATE_ATOM, time());
  46. // ################
  47. /* Data are prepared, now let's begin the "MAGIE !!!" */
  48. $toReturn = '<?xml version="1.0" encoding="UTF-8"?>';
  49. $toReturn .= <<<EOD
  50. <feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xml:lang="en-US">
  51. <title type="text">{$title}</title>
  52. <id>http{$https}://{$httpHost}{$httpInfo}/</id>
  53. <icon>{$icon}</icon>
  54. <logo>{$icon}</logo>
  55. <updated>{$feedTimestamp}</updated>
  56. <link rel="alternate" type="text/html" href="{$uri}" />
  57. <link rel="self" href="http{$https}://{$httpHost}{$serverRequestUri}" />
  58. {$entries}
  59. </feed>
  60. EOD;
  61. // Remove invalid non-UTF8 characters
  62. // We cannot use iconv because of a bug in some versions of iconv.
  63. // See http://www.php.net/manual/fr/function.iconv.php#108643
  64. //$toReturn = iconv("UTF-8", "UTF-8//IGNORE", $toReturn);
  65. // So we use mb_convert_encoding instead:
  66. ini_set('mbstring.substitute_character', 'none');
  67. $toReturn= mb_convert_encoding($toReturn, 'UTF-8', 'UTF-8');
  68. return $toReturn;
  69. }
  70. public function display(){
  71. $this
  72. ->setContentType('application/atom+xml; charset=UTF-8') // We force UTF-8 in ATOM output.
  73. ->callContentType();
  74. return parent::display();
  75. }
  76. private function xml_encode($text) {
  77. return htmlspecialchars($text, ENT_XML1);
  78. }
  79. }