MrssFormat.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. $uriparts = parse_url($uri);
  20. $icon = $this->xml_encode($uriparts['scheme'] . '://' . $uriparts['host'] .'/favicon.ico');
  21. $items = '';
  22. foreach($this->getItems() as $item) {
  23. $itemAuthor = isset($item['author']) ? $this->xml_encode($item['author']) : '';
  24. $itemTitle = strip_tags(isset($item['title']) ? $this->xml_encode($item['title']) : '');
  25. $itemUri = isset($item['uri']) ? $this->xml_encode($item['uri']) : '';
  26. $itemTimestamp = isset($item['timestamp']) ? $this->xml_encode(date(DATE_RFC2822, $item['timestamp'])) : '';
  27. $itemContent = isset($item['content']) ? $this->xml_encode($this->sanitizeHtml($item['content'])) : '';
  28. $entryEnclosuresWarning = '';
  29. $entryEnclosures = '';
  30. if(isset($item['enclosures'])) {
  31. $entryEnclosures .= '<enclosure url="'
  32. . $this->xml_encode($item['enclosures'][0])
  33. . '"/>';
  34. if(count($item['enclosures']) > 1) {
  35. $entryEnclosures .= PHP_EOL;
  36. $entryEnclosuresWarning = '&lt;br&gt;Warning:
  37. Some media files might not be shown to you. Consider using the ATOM format instead!';
  38. foreach($item['enclosures'] as $enclosure) {
  39. $entryEnclosures .= '<atom:link rel="enclosure" href="'
  40. . $enclosure . '" />'
  41. . PHP_EOL;
  42. }
  43. }
  44. }
  45. $entryCategories = '';
  46. if(isset($item['categories'])) {
  47. foreach($item['categories'] as $category) {
  48. $entryCategories .= '<category>'
  49. . $category . '</category>'
  50. . PHP_EOL;
  51. }
  52. }
  53. $items .= <<<EOD
  54. <item>
  55. <title>{$itemTitle}</title>
  56. <link>{$itemUri}</link>
  57. <guid isPermaLink="true">{$itemUri}</guid>
  58. <pubDate>{$itemTimestamp}</pubDate>
  59. <description>{$itemContent}{$entryEnclosuresWarning}</description>
  60. <author>{$itemAuthor}</author>
  61. {$entryEnclosures}
  62. {$entryCategories}
  63. </item>
  64. EOD;
  65. }
  66. $charset = $this->getCharset();
  67. /* Data are prepared, now let's begin the "MAGIE !!!" */
  68. $toReturn = <<<EOD
  69. <?xml version="1.0" encoding="{$charset}"?>
  70. <rss version="2.0"
  71. xmlns:dc="http://purl.org/dc/elements/1.1/"
  72. xmlns:media="http://search.yahoo.com/mrss/"
  73. xmlns:atom="http://www.w3.org/2005/Atom">
  74. <channel>
  75. <title>{$title}</title>
  76. <link>http{$https}://{$httpHost}{$httpInfo}/</link>
  77. <description>{$title}</description>
  78. <image url="{$icon}" title="{$title}" link="{$uri}"/>
  79. <atom:link rel="alternate" type="text/html" href="{$uri}" />
  80. <atom:link rel="self" href="http{$https}://{$httpHost}{$serverRequestUri}" />
  81. {$items}
  82. </channel>
  83. </rss>
  84. EOD;
  85. // Remove invalid non-UTF8 characters
  86. ini_set('mbstring.substitute_character', 'none');
  87. $toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
  88. return $toReturn;
  89. }
  90. public function display(){
  91. $this
  92. ->setContentType('application/rss+xml; charset=' . $this->getCharset())
  93. ->callContentType();
  94. return parent::display();
  95. }
  96. private function xml_encode($text){
  97. return htmlspecialchars($text, ENT_XML1);
  98. }
  99. }