PageRenderTime 171ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/Rss.php

https://github.com/lamarios/selfoss
PHP | 135 lines | 95 code | 21 blank | 19 comment | 22 complexity | eeb9b37049b9cc9ab300f81567a1f885 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?PHP
  2. namespace controllers;
  3. /**
  4. * Controller for rss access
  5. *
  6. * @package controllers
  7. * @copyright Copyright (c) Tobias Zeising (http://www.aditu.de)
  8. * @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
  9. * @author Tobias Zeising <tobias.zeising@aditu.de>
  10. */
  11. class Rss extends BaseController {
  12. /**
  13. * rss feed
  14. *
  15. * @return void
  16. */
  17. public function rss() {
  18. $feedWriter = new \FeedWriter(\RSS2);
  19. $feedWriter->setTitle(\F3::get('rss_title'));
  20. $feedWriter->setLink($this->view->base);
  21. // get sources
  22. $sourceDao = new \daos\Sources();
  23. $lastSourceId = 0;
  24. $lastSourceName = "";
  25. // set options
  26. $options = array();
  27. if(count($_GET)>0)
  28. $options = $_GET;
  29. $options['items'] = \F3::get('rss_max_items');
  30. if(\F3::get('PARAMS["tag"]')!=null)
  31. $options['tag'] = \F3::get('PARAMS["tag"]');
  32. if(\F3::get('PARAMS["type"]')!=null)
  33. $options['type'] = \F3::get('PARAMS["type"]');
  34. // get items
  35. $newestEntryDate = false;
  36. $lastid = -1;
  37. $itemDao = new \daos\Items();
  38. foreach($itemDao->get($options) as $item) {
  39. if($newestEntryDate===false)
  40. $newestEntryDate = $item['datetime'];
  41. $newItem = $feedWriter->createNewItem();
  42. // get Source Name
  43. if ($item['source'] != $lastSourceId){
  44. foreach($sourceDao->get() as $source) {
  45. if ($source['id'] == $item['source']) {
  46. $lastSourceId = $source['id'];
  47. $lastSourceName = $source['title'];
  48. break;
  49. }
  50. }
  51. }
  52. $newItem->setTitle(str_replace('&', '&amp;', $this->UTF8entities($item['title'] . " (" . $lastSourceName . ")")));
  53. @$newItem->setLink($item['link']);
  54. $newItem->setDate($item['datetime']);
  55. $newItem->setDescription(str_replace('&#34;', '"', $item['content']));
  56. // add tags in category node
  57. $itemsTags = explode(",",$item['tags']);
  58. foreach($itemsTags as $tag) {
  59. $tag = trim($tag);
  60. if(strlen($tag)>0)
  61. $newItem->addElement('category', $tag);
  62. }
  63. $feedWriter->addItem($newItem);
  64. $lastid = $item['id'];
  65. // mark as read
  66. if(\F3::get('rss_mark_as_read')==1 && $lastid!=-1)
  67. $itemDao->mark($lastid);
  68. }
  69. if($newestEntryDate===false)
  70. $newestEntryDate = date(\DATE_ATOM , time());
  71. $feedWriter->setChannelElement('updated', $newestEntryDate);
  72. $feedWriter->genarateFeed();
  73. }
  74. private function UTF8entities($content="") {
  75. $contents = $this->unicode_string_to_array($content);
  76. $swap = "";
  77. $iCount = count($contents);
  78. for ($o=0;$o<$iCount;$o++) {
  79. $contents[$o] = $this->unicode_entity_replace($contents[$o]);
  80. $swap .= $contents[$o];
  81. }
  82. return mb_convert_encoding($swap,"UTF-8"); //not really necessary, but why not.
  83. }
  84. private function unicode_string_to_array( $string ) { //adjwilli
  85. $strlen = mb_strlen($string);
  86. while ($strlen) {
  87. $array[] = mb_substr( $string, 0, 1, "UTF-8" );
  88. $string = mb_substr( $string, 1, $strlen, "UTF-8" );
  89. $strlen = mb_strlen( $string );
  90. }
  91. return $array;
  92. }
  93. private function unicode_entity_replace($c) { //m. perez
  94. $h = ord($c{0});
  95. if ($h <= 0x7F) {
  96. return $c;
  97. } else if ($h < 0xC2) {
  98. return $c;
  99. }
  100. if ($h <= 0xDF) {
  101. $h = ($h & 0x1F) << 6 | (ord($c{1}) & 0x3F);
  102. $h = "&#" . $h . ";";
  103. return $h;
  104. } else if ($h <= 0xEF) {
  105. $h = ($h & 0x0F) << 12 | (ord($c{1}) & 0x3F) << 6 | (ord($c{2}) & 0x3F);
  106. $h = "&#" . $h . ";";
  107. return $h;
  108. } else if ($h <= 0xF4) {
  109. $h = ($h & 0x0F) << 18 | (ord($c{1}) & 0x3F) << 12 | (ord($c{2}) & 0x3F) << 6 | (ord($c{3}) & 0x3F);
  110. $h = "&#" . $h . ";";
  111. return $h;
  112. }
  113. }
  114. }