indexController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * This class handles main actions of FreshRSS.
  4. */
  5. class FreshRSS_index_Controller extends Minz_ActionController {
  6. public function indexAction() {
  7. // TODO: update the context with information from request.
  8. // TODO: then, in dedicated action, get corresponding entries
  9. $prefered_output = FreshRSS_Context::$conf->view_mode;
  10. Minz_Request::forward(array(
  11. 'c' => 'index',
  12. 'a' => $prefered_output
  13. ));
  14. return;
  15. // On récupère les différents éléments de filtrage
  16. $this->view->state = Minz_Request::param('state', FreshRSS_Context::$conf->default_view);
  17. $state_param = Minz_Request::param('state', null);
  18. $filter = Minz_Request::param('search', '');
  19. $this->view->order = $order = Minz_Request::param('order', FreshRSS_Context::$conf->sort_order);
  20. $nb = Minz_Request::param('nb', FreshRSS_Context::$conf->posts_per_page);
  21. $first = Minz_Request::param('next', '');
  22. $ajax_request = Minz_Request::param('ajax', false);
  23. if ($output === 'reader') {
  24. $nb = max(1, round($nb / 2));
  25. }
  26. if ($this->view->state === FreshRSS_Entry::STATE_NOT_READ) { //Any unread article in this category at all?
  27. switch ($getType) {
  28. case 'a':
  29. $hasUnread = $this->view->nb_not_read > 0;
  30. break;
  31. case 's':
  32. // This is deprecated. The favorite button does not exist anymore
  33. $hasUnread = $this->view->nb_favorites['unread'] > 0;
  34. break;
  35. case 'c':
  36. $hasUnread = (!isset($this->view->cat_aside[$getId]) ||
  37. $this->view->cat_aside[$getId]->nbNotRead() > 0);
  38. break;
  39. case 'f':
  40. $myFeed = FreshRSS_CategoryDAO::findFeed($this->view->cat_aside, $getId);
  41. $hasUnread = ($myFeed === null) || ($myFeed->nbNotRead() > 0);
  42. break;
  43. default:
  44. $hasUnread = true;
  45. break;
  46. }
  47. if (!$hasUnread && ($state_param === null)) {
  48. $this->view->state = FreshRSS_Entry::STATE_ALL;
  49. }
  50. }
  51. $this->view->today = @strtotime('today');
  52. try {
  53. $entries = $entryDAO->listWhere($getType, $getId, $this->view->state, $order, $nb + 1, $first, $filter);
  54. // Si on a récupéré aucun article "non lus"
  55. // on essaye de récupérer tous les articles
  56. if ($this->view->state === FreshRSS_Entry::STATE_NOT_READ && empty($entries) && ($state_param === null) && ($filter == '')) {
  57. Minz_Log::debug('Conflicting information about nbNotRead!');
  58. $feedDAO = FreshRSS_Factory::createFeedDao();
  59. try {
  60. $feedDAO->updateCachedValues();
  61. } catch (Exception $ex) {
  62. Minz_Log::notice('Failed to automatically correct nbNotRead! ' + $ex->getMessage());
  63. }
  64. $this->view->state = FreshRSS_Entry::STATE_ALL;
  65. $entries = $entryDAO->listWhere($getType, $getId, $this->view->state, $order, $nb, $first, $filter);
  66. }
  67. Minz_Request::_param('state', $this->view->state);
  68. if (count($entries) <= $nb) {
  69. $this->view->nextId = '';
  70. } else { //We have more elements for pagination
  71. $lastEntry = array_pop($entries);
  72. $this->view->nextId = $lastEntry->id();
  73. }
  74. $this->view->entries = $entries;
  75. } catch (FreshRSS_EntriesGetter_Exception $e) {
  76. Minz_Log::notice($e->getMessage());
  77. Minz_Error::error(404);
  78. }
  79. }
  80. /**
  81. * This action displays the normal view of FreshRSS.
  82. */
  83. public function normalAction() {
  84. if (!FreshRSS_Auth::hasAccess() && !Minz_Configuration::allowAnonymous()) {
  85. Minz_Request::forward(array('c' => 'auth', 'a' => 'login'));
  86. return;
  87. }
  88. try {
  89. $this->updateContext();
  90. } catch (Minz_Exception $e) {
  91. Minz_Error::error(404);
  92. }
  93. $this->view->categories = FreshRSS_Context::$categories;
  94. $title = FreshRSS_Context::$name;
  95. if (FreshRSS_Context::$get_unread > 0) {
  96. $title = '(' . FreshRSS_Context::$get_unread . ') · ' . $title;
  97. }
  98. Minz_View::prependTitle($title . ' · ');
  99. }
  100. /**
  101. * This action displays the global view of FreshRSS.
  102. */
  103. public function globalAction() {
  104. if (!FreshRSS_Auth::hasAccess() && !Minz_Configuration::allowAnonymous()) {
  105. Minz_Request::forward(array('c' => 'auth', 'a' => 'login'));
  106. return;
  107. }
  108. Minz_View::appendScript(Minz_Url::display('/scripts/global_view.js?' . @filemtime(PUBLIC_PATH . '/scripts/global_view.js')));
  109. try {
  110. $this->updateContext();
  111. } catch (Minz_Exception $e) {
  112. Minz_Error::error(404);
  113. }
  114. $this->view->categories = FreshRSS_Context::$categories;
  115. Minz_View::prependTitle(_t('gen.title.global_view') . ' · ');
  116. }
  117. /**
  118. * This action displays the RSS feed of FreshRSS.
  119. */
  120. public function rssAction() {
  121. $token = FreshRSS_Context::$conf->token;
  122. $token_param = Minz_Request::param('token', '');
  123. $token_is_ok = ($token != '' && $token === $token_param);
  124. // Check if user has access.
  125. if (!FreshRSS_Auth::hasAccess() &&
  126. !Minz_Configuration::allowAnonymous() &&
  127. !$token_is_ok) {
  128. Minz_Error::error(403);
  129. }
  130. try {
  131. $this->updateContext();
  132. } catch (Minz_Exception $e) {
  133. Minz_Error::error(404);
  134. }
  135. // No layout for RSS output.
  136. $this->view->rss_title = FreshRSS_Context::$name . ' | ' . Minz_View::title();
  137. $this->view->_useLayout(false);
  138. header('Content-Type: application/rss+xml; charset=utf-8');
  139. }
  140. /**
  141. * This action updates the Context object by using request parameters.
  142. */
  143. private function updateContext() {
  144. FreshRSS_Context::_get(Minz_Request::param('get', 'a'));
  145. }
  146. /**
  147. * This action displays the about page of FreshRSS.
  148. */
  149. public function aboutAction() {
  150. Minz_View::prependTitle(_t('about') . ' · ');
  151. }
  152. /**
  153. * This action displays logs of FreshRSS for the current user.
  154. */
  155. public function logsAction() {
  156. if (!FreshRSS_Auth::hasAccess()) {
  157. Minz_Error::error(403);
  158. }
  159. Minz_View::prependTitle(_t('logs') . ' · ');
  160. if (Minz_Request::isPost()) {
  161. FreshRSS_LogDAO::truncate();
  162. }
  163. $logs = FreshRSS_LogDAO::lines(); //TODO: ask only the necessary lines
  164. //gestion pagination
  165. $page = Minz_Request::param('page', 1);
  166. $this->view->logsPaginator = new Minz_Paginator($logs);
  167. $this->view->logsPaginator->_nbItemsPerPage(50);
  168. $this->view->logsPaginator->_currentPage($page);
  169. }
  170. }