MrssFormat.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Mrss
  4. * Documentation Source http://www.rssboard.org/media-rss
  5. */
  6. class MrssFormat extends FormatAbstract {
  7. public function stringify(){
  8. $https = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 's' : '';
  9. $httpHost = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
  10. $httpInfo = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
  11. $serverRequestUri = $this->xml_encode($_SERVER['REQUEST_URI']);
  12. $extraInfos = $this->getExtraInfos();
  13. $title = $this->xml_encode($extraInfos['name']);
  14. if(!empty($extraInfos['uri'])){
  15. $uri = $this->xml_encode($extraInfos['uri']);
  16. } else {
  17. $uri = 'https://github.com/sebsauvage/rss-bridge';
  18. }
  19. $icon = $this->xml_encode('http://icons.better-idea.org/icon?url='. $uri .'&size=64');
  20. $items = '';
  21. foreach($this->getItems() as $item){
  22. $itemAuthor = isset($item['author']) ? $this->xml_encode($item['author']) : '';
  23. $itemTitle = strip_tags(isset($item['title']) ? $this->xml_encode($item['title']) : '');
  24. $itemUri = isset($item['uri']) ? $this->xml_encode($item['uri']) : '';
  25. $itemTimestamp = isset($item['timestamp']) ? $this->xml_encode(date(DATE_RFC2822, $item['timestamp'])) : '';
  26. $itemContent = isset($item['content']) ? $this->xml_encode($this->sanitizeHtml($item['content'])) : '';
  27. $entryEnclosures = "";
  28. foreach($item['enclosures'] as $enclosure)
  29. $entryEnclosures .= "<enclosure url=\"".$this->xml_encode($enclosure)."\"/>";
  30. $items .= <<<EOD
  31. <item>
  32. <title>{$itemTitle}</title>
  33. <link>{$itemUri}</link>
  34. <guid isPermaLink="true">{$itemUri}</guid>
  35. <pubDate>{$itemTimestamp}</pubDate>
  36. <description>{$itemContent}</description>
  37. <author>{$itemAuthor}</author>
  38. {$entryEnclosures}
  39. </item>
  40. EOD;
  41. }
  42. $charset = $this->getCharset();
  43. /* Data are prepared, now let's begin the "MAGIE !!!" */
  44. $toReturn = <<<EOD
  45. <?xml version="1.0" encoding="{$charset}"?>
  46. <rss version="2.0"
  47. xmlns:dc="http://purl.org/dc/elements/1.1/"
  48. xmlns:media="http://search.yahoo.com/mrss/"
  49. xmlns:atom="http://www.w3.org/2005/Atom">
  50. <channel>
  51. <title>{$title}</title>
  52. <link>http{$https}://{$httpHost}{$httpInfo}/</link>
  53. <description>{$title}</description>
  54. <image url="{$icon}" title="{$title}" link="{$uri}"/>
  55. <atom:link rel="alternate" type="text/html" href="{$uri}" />
  56. <atom:link rel="self" href="http{$https}://{$httpHost}{$serverRequestUri}" />
  57. {$items}
  58. </channel>
  59. </rss>
  60. EOD;
  61. // Remove invalid non-UTF8 characters
  62. ini_set('mbstring.substitute_character', 'none');
  63. $toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
  64. return $toReturn;
  65. }
  66. public function display(){
  67. $this
  68. ->setContentType('application/rss+xml; charset=' . $this->getCharset())
  69. ->callContentType();
  70. return parent::display();
  71. }
  72. private function xml_encode($text){
  73. return htmlspecialchars($text, ENT_XML1);
  74. }
  75. }