AtomFormat.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. /* 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. <updated></updated>
  54. <link rel="alternate" type="text/html" href="{$uri}" />
  55. <link rel="self" href="http{$https}://{$httpHost}{$serverRequestUri}" />
  56. {$entries}
  57. </feed>
  58. EOD;
  59. // Remove invalid non-UTF8 characters
  60. // We cannot use iconv because of a bug in some versions of iconv.
  61. // See http://www.php.net/manual/fr/function.iconv.php#108643
  62. //$toReturn = iconv("UTF-8", "UTF-8//IGNORE", $toReturn);
  63. // So we use mb_convert_encoding instead:
  64. ini_set('mbstring.substitute_character', 'none');
  65. $toReturn= mb_convert_encoding($toReturn, 'UTF-8', 'UTF-8');
  66. return $toReturn;
  67. }
  68. public function display(){
  69. $this
  70. ->setContentType('application/atom+xml; charset=utf8') // We force UTF-8 in ATOM output.
  71. ->callContentType();
  72. return parent::display();
  73. }
  74. }