AtomFormat.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. $icon = $this->xml_encode($uri .'/favicon.ico');
  17. $uri = $this->xml_encode($uri);
  18. $entries = '';
  19. foreach($this->getItems() as $item) {
  20. $entryAuthor = isset($item['author']) ? $this->xml_encode($item['author']) : '';
  21. $entryTitle = isset($item['title']) ? $this->xml_encode($item['title']) : '';
  22. $entryUri = isset($item['uri']) ? $this->xml_encode($item['uri']) : '';
  23. $entryTimestamp = isset($item['timestamp']) ? $this->xml_encode(date(DATE_ATOM, $item['timestamp'])) : '';
  24. $entryContent = isset($item['content']) ? $this->xml_encode($this->sanitizeHtml($item['content'])) : '';
  25. $entryEnclosures = '';
  26. if(isset($item['enclosures'])) {
  27. foreach($item['enclosures'] as $enclosure) {
  28. $entryEnclosures .= '<link rel="enclosure" href="'
  29. . $this->xml_encode($enclosure)
  30. . '"/>'
  31. . PHP_EOL;
  32. }
  33. }
  34. $entries .= <<<EOD
  35. <entry>
  36. <author>
  37. <name>{$entryAuthor}</name>
  38. </author>
  39. <title type="html">{$entryTitle}</title>
  40. <link rel="alternate" type="text/html" href="{$entryUri}" />
  41. <id>{$entryUri}</id>
  42. <updated>{$entryTimestamp}</updated>
  43. <content type="html">{$entryContent}</content>
  44. {$entryEnclosures}
  45. </entry>
  46. EOD;
  47. }
  48. $feedTimestamp = date(DATE_ATOM, time());
  49. $charset = $this->getCharset();
  50. /* Data are prepared, now let's begin the "MAGIE !!!" */
  51. $toReturn = <<<EOD
  52. <?xml version="1.0" encoding="{$charset}"?>
  53. <feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0">
  54. <title type="text">{$title}</title>
  55. <id>http{$https}://{$httpHost}{$httpInfo}/</id>
  56. <icon>{$icon}</icon>
  57. <logo>{$icon}</logo>
  58. <updated>{$feedTimestamp}</updated>
  59. <link rel="alternate" type="text/html" href="{$uri}" />
  60. <link rel="self" href="http{$https}://{$httpHost}{$serverRequestUri}" />
  61. {$entries}
  62. </feed>
  63. EOD;
  64. // Remove invalid characters
  65. ini_set('mbstring.substitute_character', 'none');
  66. $toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
  67. return $toReturn;
  68. }
  69. public function display(){
  70. $this
  71. ->setContentType('application/atom+xml; charset=' . $this->getCharset())
  72. ->callContentType();
  73. return parent::display();
  74. }
  75. private function xml_encode($text){
  76. return htmlspecialchars($text, ENT_XML1);
  77. }
  78. }