indexController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * This class handles main actions of FreshRSS.
  4. */
  5. class FreshRSS_index_Controller extends Minz_ActionController {
  6. /**
  7. * This action only redirect on the default view mode (normal or global)
  8. */
  9. public function indexAction() {
  10. $prefered_output = FreshRSS_Context::$conf->view_mode;
  11. Minz_Request::forward(array(
  12. 'c' => 'index',
  13. 'a' => $prefered_output
  14. ));
  15. }
  16. /**
  17. * This action displays the normal view of FreshRSS.
  18. */
  19. public function normalAction() {
  20. if (!FreshRSS_Auth::hasAccess() && !Minz_Configuration::allowAnonymous()) {
  21. Minz_Request::forward(array('c' => 'auth', 'a' => 'login'));
  22. return;
  23. }
  24. try {
  25. $this->updateContext();
  26. } catch (FreshRSS_Context_Exception $e) {
  27. Minz_Error::error(404);
  28. }
  29. try {
  30. $entries = $this->listEntriesByContext();
  31. if (count($entries) > FreshRSS_Context::$number) {
  32. // We have more elements for pagination
  33. $last_entry = array_pop($entries);
  34. FreshRSS_Context::$next_id = $last_entry->id();
  35. }
  36. $this->view->entries = $entries;
  37. } catch (FreshRSS_EntriesGetter_Exception $e) {
  38. Minz_Log::notice($e->getMessage());
  39. Minz_Error::error(404);
  40. }
  41. $this->view->categories = FreshRSS_Context::$categories;
  42. $this->view->rss_title = FreshRSS_Context::$name . ' | ' . Minz_View::title();
  43. $title = FreshRSS_Context::$name;
  44. if (FreshRSS_Context::$get_unread > 0) {
  45. $title = '(' . FreshRSS_Context::$get_unread . ') · ' . $title;
  46. }
  47. Minz_View::prependTitle($title . ' · ');
  48. }
  49. /**
  50. * This action displays the global view of FreshRSS.
  51. */
  52. public function globalAction() {
  53. if (!FreshRSS_Auth::hasAccess() && !Minz_Configuration::allowAnonymous()) {
  54. Minz_Request::forward(array('c' => 'auth', 'a' => 'login'));
  55. return;
  56. }
  57. Minz_View::appendScript(Minz_Url::display('/scripts/global_view.js?' . @filemtime(PUBLIC_PATH . '/scripts/global_view.js')));
  58. try {
  59. $this->updateContext();
  60. } catch (FreshRSS_Context_Exception $e) {
  61. Minz_Error::error(404);
  62. }
  63. $this->view->categories = FreshRSS_Context::$categories;
  64. $this->view->rss_title = FreshRSS_Context::$name . ' | ' . Minz_View::title();
  65. Minz_View::prependTitle(_t('gen.title.global_view') . ' · ');
  66. }
  67. /**
  68. * This action displays the RSS feed of FreshRSS.
  69. */
  70. public function rssAction() {
  71. $token = FreshRSS_Context::$conf->token;
  72. $token_param = Minz_Request::param('token', '');
  73. $token_is_ok = ($token != '' && $token === $token_param);
  74. // Check if user has access.
  75. if (!FreshRSS_Auth::hasAccess() &&
  76. !Minz_Configuration::allowAnonymous() &&
  77. !$token_is_ok) {
  78. Minz_Error::error(403);
  79. }
  80. try {
  81. $this->updateContext();
  82. } catch (FreshRSS_Context_Exception $e) {
  83. Minz_Error::error(404);
  84. }
  85. try {
  86. $this->view->entries = $this->listEntriesByContext();
  87. } catch (FreshRSS_EntriesGetter_Exception $e) {
  88. Minz_Log::notice($e->getMessage());
  89. Minz_Error::error(404);
  90. }
  91. // No layout for RSS output.
  92. $this->view->rss_title = FreshRSS_Context::$name . ' | ' . Minz_View::title();
  93. $this->view->_useLayout(false);
  94. header('Content-Type: application/rss+xml; charset=utf-8');
  95. }
  96. /**
  97. * This action updates the Context object by using request parameters.
  98. *
  99. * Parameters are:
  100. * - state (default: conf->default_view)
  101. * - search (default: empty string)
  102. * - order (default: conf->sort_order)
  103. * - nb (default: conf->posts_per_page)
  104. * - next (default: empty string)
  105. */
  106. private function updateContext() {
  107. FreshRSS_Context::_get(Minz_Request::param('get', 'a'));
  108. FreshRSS_Context::$state = Minz_Request::param(
  109. 'state', FreshRSS_Context::$conf->default_state
  110. );
  111. $state_forced_by_user = Minz_Request::param('state', false) !== false;
  112. if (FreshRSS_Context::$conf->default_view === 'adaptive' &&
  113. FreshRSS_Context::$get_unread <= 0 &&
  114. !FreshRSS_Context::isStateEnabled(FreshRSS_Entry::STATE_READ) &&
  115. !$state_forced_by_user) {
  116. FreshRSS_Context::$state |= FreshRSS_Entry::STATE_READ;
  117. }
  118. FreshRSS_Context::$search = Minz_Request::param('search', '');
  119. FreshRSS_Context::$order = Minz_Request::param(
  120. 'order', FreshRSS_Context::$conf->sort_order
  121. );
  122. FreshRSS_Context::$number = Minz_Request::param(
  123. 'nb', FreshRSS_Context::$conf->posts_per_page
  124. );
  125. FreshRSS_Context::$first_id = Minz_Request::param('next', '');
  126. }
  127. /**
  128. * This method returns a list of entries based on the Context object.
  129. */
  130. private function listEntriesByContext() {
  131. $entryDAO = FreshRSS_Factory::createEntryDao();
  132. $get = FreshRSS_Context::currentGet(true);
  133. if (count($get) > 1) {
  134. $type = $get[0];
  135. $id = $get[1];
  136. } else {
  137. $type = $get;
  138. $id = '';
  139. }
  140. return $entryDAO->listWhere(
  141. $type, $id, FreshRSS_Context::$state, FreshRSS_Context::$order,
  142. FreshRSS_Context::$number + 1, FreshRSS_Context::$first_id,
  143. FreshRSS_Context::$search
  144. );
  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. }