MrssFormat.php 3.2 KB

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