AtomFormat.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Atom
  4. * Documentation Source http://en.wikipedia.org/wiki/Atom_%28standard%29 and
  5. * http://tools.ietf.org/html/rfc4287
  6. */
  7. class AtomFormat extends FormatAbstract{
  8. public function stringify(){
  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/RSS-Bridge/rss-bridge';
  16. $uriparts = parse_url($uri);
  17. $icon = $this->xml_encode($uriparts['scheme'] . '://' . $uriparts['host'] .'/favicon.ico');
  18. $uri = $this->xml_encode($uri);
  19. $entries = '';
  20. foreach($this->getItems() as $item) {
  21. $entryAuthor = isset($item['author']) ? $this->xml_encode($item['author']) : '';
  22. $entryTitle = isset($item['title']) ? $this->xml_encode($item['title']) : '';
  23. $entryUri = isset($item['uri']) ? $this->xml_encode($item['uri']) : '';
  24. $entryTimestamp = isset($item['timestamp']) ? $this->xml_encode(date(DATE_ATOM, $item['timestamp'])) : '';
  25. $entryContent = isset($item['content']) ? $this->xml_encode($this->sanitizeHtml($item['content'])) : '';
  26. $entryEnclosures = '';
  27. if(isset($item['enclosures'])) {
  28. foreach($item['enclosures'] as $enclosure) {
  29. $entryEnclosures .= '<link rel="enclosure" href="'
  30. . $this->xml_encode($enclosure)
  31. . '"/>'
  32. . PHP_EOL;
  33. }
  34. }
  35. $entryCategories = '';
  36. if(isset($item['categories'])) {
  37. foreach($item['categories'] as $category) {
  38. $entryCategories .= '<category term="'
  39. . $this->xml_encode($category)
  40. . '"/>'
  41. . PHP_EOL;
  42. }
  43. }
  44. $entries .= <<<EOD
  45. <entry>
  46. <author>
  47. <name>{$entryAuthor}</name>
  48. </author>
  49. <title type="html">{$entryTitle}</title>
  50. <link rel="alternate" type="text/html" href="{$entryUri}" />
  51. <id>{$entryUri}</id>
  52. <updated>{$entryTimestamp}</updated>
  53. <content type="html">{$entryContent}</content>
  54. {$entryEnclosures}
  55. {$entryCategories}
  56. </entry>
  57. EOD;
  58. }
  59. $feedTimestamp = date(DATE_ATOM, time());
  60. $charset = $this->getCharset();
  61. /* Data are prepared, now let's begin the "MAGIE !!!" */
  62. $toReturn = <<<EOD
  63. <?xml version="1.0" encoding="{$charset}"?>
  64. <feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0">
  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 characters
  76. ini_set('mbstring.substitute_character', 'none');
  77. $toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
  78. return $toReturn;
  79. }
  80. public function display(){
  81. $this
  82. ->setContentType('application/atom+xml; charset=' . $this->getCharset())
  83. ->callContentType();
  84. return parent::display();
  85. }
  86. private function xml_encode($text){
  87. return htmlspecialchars($text, ENT_XML1);
  88. }
  89. }