1
0

AtomFormat.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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://g.etfv.co/'. $uri .'?icon.jpg');
  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. $entries .= <<<EOD
  33. <entry>
  34. <author>
  35. <name>{$entryName}</name>
  36. <uri>{$entryAuthor}</uri>
  37. </author>
  38. <title type="html"><![CDATA[{$entryTitle}]]></title>
  39. <link rel="alternate" type="text/html" href="{$entryUri}" />
  40. <id>{$entryUri}</id>
  41. <updated>{$entryTimestamp}</updated>
  42. <content type="html">{$entryContent}</content>
  43. </entry>
  44. EOD;
  45. }
  46. /*
  47. TODO :
  48. - Security: Disable Javascript ?
  49. - <updated> : Define new extra info ?
  50. - <content type="html"> : RFC look with xhtml, keep this in spite of ?
  51. */
  52. // #### TEMPORARY FIX ###
  53. $feedTimestamp = date(DATE_ATOM, time());
  54. // ################
  55. /* Data are prepared, now let's begin the "MAGIE !!!" */
  56. $toReturn = '<?xml version="1.0" encoding="UTF-8"?>';
  57. $toReturn .= <<<EOD
  58. <feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xml:lang="en-US">
  59. <title type="text">{$title}</title>
  60. <id>http{$https}://{$httpHost}{$httpInfo}/</id>
  61. <icon>{$icon}</icon>
  62. <logo>{$icon}</logo>
  63. <updated>{$feedTimestamp}</updated>
  64. <link rel="alternate" type="text/html" href="{$uri}" />
  65. <link rel="self" href="http{$https}://{$httpHost}{$serverRequestUri}" />
  66. {$entries}
  67. </feed>
  68. EOD;
  69. // Remove invalid non-UTF8 characters
  70. // We cannot use iconv because of a bug in some versions of iconv.
  71. // See http://www.php.net/manual/fr/function.iconv.php#108643
  72. //$toReturn = iconv("UTF-8", "UTF-8//IGNORE", $toReturn);
  73. // So we use mb_convert_encoding instead:
  74. ini_set('mbstring.substitute_character', 'none');
  75. $toReturn= mb_convert_encoding($toReturn, 'UTF-8', 'UTF-8');
  76. return $toReturn;
  77. }
  78. public function display(){
  79. $this
  80. ->setContentType('application/atom+xml; charset=UTF-8') // We force UTF-8 in ATOM output.
  81. ->callContentType();
  82. return parent::display();
  83. }
  84. }