1
0

MrssFormat.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. $entryEnclosure = '';
  28. if(isset($item['enclosure'])){
  29. $entryEnclosure = '<enclosure url="' . $this->xml_encode($item['enclosure']) . '"/>';
  30. }
  31. $items .= <<<EOD
  32. <item>
  33. <title>{$itemTitle}</title>
  34. <link>{$itemUri}</link>
  35. <guid isPermaLink="true">{$itemUri}</guid>
  36. <pubDate>{$itemTimestamp}</pubDate>
  37. <description>{$itemContent}</description>
  38. <author>{$itemAuthor}</author>
  39. {$entryEnclosure}
  40. </item>
  41. EOD;
  42. }
  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. <rss version="2.0"
  48. xmlns:dc="http://purl.org/dc/elements/1.1/"
  49. xmlns:media="http://search.yahoo.com/mrss/"
  50. xmlns:atom="http://www.w3.org/2005/Atom">
  51. <channel>
  52. <title>{$title}</title>
  53. <link>http{$https}://{$httpHost}{$httpInfo}/</link>
  54. <description>{$title}</description>
  55. <image url="{$icon}" title="{$title}" link="{$uri}"/>
  56. <atom:link rel="alternate" type="text/html" href="{$uri}" />
  57. <atom:link rel="self" href="http{$https}://{$httpHost}{$serverRequestUri}" />
  58. {$items}
  59. </channel>
  60. </rss>
  61. EOD;
  62. // Remove invalid non-UTF8 characters
  63. ini_set('mbstring.substitute_character', 'none');
  64. $toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
  65. return $toReturn;
  66. }
  67. public function display(){
  68. $this
  69. ->setContentType('application/rss+xml; charset=' . $this->getCharset())
  70. ->callContentType();
  71. return parent::display();
  72. }
  73. private function xml_encode($text){
  74. return htmlspecialchars($text, ENT_XML1);
  75. }
  76. }