MrssFormat.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/RSS-Bridge/rss-bridge';
  18. }
  19. $icon = $this->xml_encode($uri .'/favicon.ico');
  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. $entryEnclosuresWarning = '';
  28. $entryEnclosures = '';
  29. if(isset($item['enclosures'])) {
  30. $entryEnclosures .= '<enclosure url="'
  31. . $this->xml_encode($item['enclosures'][0])
  32. . '"/>';
  33. if(count($item['enclosures']) > 1) {
  34. $entryEnclosures .= PHP_EOL;
  35. $entryEnclosuresWarning = '&lt;br&gt;Warning:
  36. Some media files might not be shown to you. Consider using the ATOM format instead!';
  37. foreach($item['enclosures'] as $enclosure) {
  38. $entryEnclosures .= '<atom:link rel="enclosure" href="'
  39. . $enclosure . '" />'
  40. . PHP_EOL;
  41. }
  42. }
  43. }
  44. $items .= <<<EOD
  45. <item>
  46. <title>{$itemTitle}</title>
  47. <link>{$itemUri}</link>
  48. <guid isPermaLink="true">{$itemUri}</guid>
  49. <pubDate>{$itemTimestamp}</pubDate>
  50. <description>{$itemContent}{$entryEnclosuresWarning}</description>
  51. <author>{$itemAuthor}</author>
  52. {$entryEnclosures}
  53. </item>
  54. EOD;
  55. }
  56. $charset = $this->getCharset();
  57. /* Data are prepared, now let's begin the "MAGIE !!!" */
  58. $toReturn = <<<EOD
  59. <?xml version="1.0" encoding="{$charset}"?>
  60. <rss version="2.0"
  61. xmlns:dc="http://purl.org/dc/elements/1.1/"
  62. xmlns:media="http://search.yahoo.com/mrss/"
  63. xmlns:atom="http://www.w3.org/2005/Atom">
  64. <channel>
  65. <title>{$title}</title>
  66. <link>http{$https}://{$httpHost}{$httpInfo}/</link>
  67. <description>{$title}</description>
  68. <image url="{$icon}" title="{$title}" link="{$uri}"/>
  69. <atom:link rel="alternate" type="text/html" href="{$uri}" />
  70. <atom:link rel="self" href="http{$https}://{$httpHost}{$serverRequestUri}" />
  71. {$items}
  72. </channel>
  73. </rss>
  74. EOD;
  75. // Remove invalid non-UTF8 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/rss+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. }