1
0

MrssFormat.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Mrss
  4. * Documentation Source http://www.rssboard.org/media-rss
  5. *
  6. * @name Media RSS
  7. */
  8. class MrssFormat extends FormatAbstract{
  9. public function stringify(){
  10. /* Datas preparation */
  11. $https = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 's' : '' );
  12. $httpHost = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
  13. $httpInfo = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
  14. $serverRequestUri = $this->xml_encode($_SERVER['REQUEST_URI']);
  15. $extraInfos = $this->getExtraInfos();
  16. $title = $this->xml_encode($extraInfos['name']);
  17. $uri = $this->xml_encode(!empty($extraInfos['uri']) ? $extraInfos['uri'] : 'https://github.com/sebsauvage/rss-bridge');
  18. $items = '';
  19. foreach($this->getDatas() as $data){
  20. $itemAuthor = is_null($data->author) ? '' : $this->xml_encode($data->author);
  21. $itemTitle = strip_tags(is_null($data->title) ? '' : $this->xml_encode($data->title));
  22. $itemUri = is_null($data->uri) ? '' : $this->xml_encode($data->uri);
  23. $itemTimestamp = is_null($data->timestamp) ? '' : $this->xml_encode(date(DATE_RFC2822, $data->timestamp));
  24. $itemContent = is_null($data->content) ? '' : $this->xml_encode($this->sanitizeHtml($data->content));
  25. $items .= <<<EOD
  26. <item>
  27. <title>{$itemTitle}</title>
  28. <link>{$itemUri}</link>
  29. <guid isPermaLink="true">{$itemUri}</guid>
  30. <pubDate>{$itemTimestamp}</pubDate>
  31. <description>{$itemContent}</description>
  32. <author>{$itemAuthor}</author>
  33. </item>
  34. EOD;
  35. }
  36. /*
  37. TODO :
  38. - Security: Disable Javascript ?
  39. - <updated> : Define new extra info ?
  40. - <content type="html"> : RFC look with xhtml, keep this in spite of ?
  41. */
  42. /* Data are prepared, now let's begin the "MAGIE !!!" */
  43. $toReturn = '<?xml version="1.0" encoding="UTF-8"?>';
  44. $toReturn .= <<<EOD
  45. <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom">
  46. <channel>
  47. <title>{$title}</title>
  48. <link>http{$https}://{$httpHost}{$httpInfo}/</link>
  49. <description>{$title}</description>
  50. <atom:link rel="alternate" type="text/html" href="{$uri}" />
  51. <atom:link rel="self" href="http{$https}://{$httpHost}{$serverRequestUri}" />
  52. {$items}
  53. </channel>
  54. </rss>
  55. EOD;
  56. // Remove invalid non-UTF8 characters
  57. // We cannot use iconv because of a bug in some versions of iconv.
  58. // See http://www.php.net/manual/fr/function.iconv.php#108643
  59. //$toReturn = iconv("UTF-8", "UTF-8//IGNORE", $toReturn);
  60. // So we use mb_convert_encoding instead:
  61. ini_set('mbstring.substitute_character', 'none');
  62. $toReturn= mb_convert_encoding($toReturn, 'UTF-8', 'UTF-8');
  63. return $toReturn;
  64. }
  65. public function display(){
  66. $this
  67. ->setContentType('application/rss+xml; charset=UTF-8') // We force UTF-8 in RSS output.
  68. ->callContentType();
  69. return parent::display();
  70. }
  71. private function xml_encode($text) {
  72. return htmlspecialchars($text, ENT_XML1);
  73. }
  74. }