AtomFormat.php 3.8 KB

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