AtomFormat.php 3.3 KB

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