AtomFormat.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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/sebsauvage/rss-bridge';
  16. $icon = $this->xml_encode('http://icons.better-idea.org/icon?url='. $uri .'&size=64');
  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. foreach($item['enclosures'] as $enclosure)
  27. $entryEnclosures .= "<link rel=\"enclosure\" href=\"".$this->xml_encode($enclosure)."\"/>";
  28. $entries .= <<<EOD
  29. <entry>
  30. <author>
  31. <name>{$entryAuthor}</name>
  32. </author>
  33. <title type="html">{$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. {$entryEnclosures}
  39. </entry>
  40. EOD;
  41. }
  42. $feedTimestamp = date(DATE_ATOM, time());
  43. $charset = $this->getCharset();
  44. /* Data are prepared, now let's begin the "MAGIE !!!" */
  45. $toReturn = <<<EOD
  46. <?xml version="1.0" encoding="{$charset}"?>
  47. <feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0">
  48. <title type="text">{$title}</title>
  49. <id>http{$https}://{$httpHost}{$httpInfo}/</id>
  50. <icon>{$icon}</icon>
  51. <logo>{$icon}</logo>
  52. <updated>{$feedTimestamp}</updated>
  53. <link rel="alternate" type="text/html" href="{$uri}" />
  54. <link rel="self" href="http{$https}://{$httpHost}{$serverRequestUri}" />
  55. {$entries}
  56. </feed>
  57. EOD;
  58. // Remove invalid characters
  59. ini_set('mbstring.substitute_character', 'none');
  60. $toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
  61. return $toReturn;
  62. }
  63. public function display(){
  64. $this
  65. ->setContentType('application/atom+xml; charset=' . $this->getCharset())
  66. ->callContentType();
  67. return parent::display();
  68. }
  69. private function xml_encode($text){
  70. return htmlspecialchars($text, ENT_XML1);
  71. }
  72. }