AtomFormat.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. $entries .= <<<EOD
  26. <entry>
  27. <author>
  28. <name>{$entryAuthor}</name>
  29. </author>
  30. <title type="html"><![CDATA[{$entryTitle}]]></title>
  31. <link rel="alternate" type="text/html" href="{$entryUri}" />
  32. <id>{$entryUri}</id>
  33. <updated>{$entryTimestamp}</updated>
  34. <content type="html">{$entryContent}</content>
  35. </entry>
  36. EOD;
  37. }
  38. $feedTimestamp = date(DATE_ATOM, time());
  39. /* Data are prepared, now let's begin the "MAGIE !!!" */
  40. $toReturn = '<?xml version="1.0" encoding="UTF-8"?>';
  41. $toReturn .= <<<EOD
  42. <feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xml:lang="en-US">
  43. <title type="text">{$title}</title>
  44. <id>http{$https}://{$httpHost}{$httpInfo}/</id>
  45. <icon>{$icon}</icon>
  46. <logo>{$icon}</logo>
  47. <updated>{$feedTimestamp}</updated>
  48. <link rel="alternate" type="text/html" href="{$uri}" />
  49. <link rel="self" href="http{$https}://{$httpHost}{$serverRequestUri}" />
  50. {$entries}
  51. </feed>
  52. EOD;
  53. // Remove invalid non-UTF8 characters
  54. ini_set('mbstring.substitute_character', 'none');
  55. $toReturn = mb_convert_encoding($toReturn, 'UTF-8', 'UTF-8');
  56. return $toReturn;
  57. }
  58. public function display(){
  59. $this
  60. ->setContentType('application/atom+xml; charset=UTF-8')
  61. ->callContentType();
  62. return parent::display();
  63. }
  64. private function xml_encode($text){
  65. return htmlspecialchars($text, ENT_XML1);
  66. }
  67. }