indexController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. $nb_entries = count($entries);
  32. if ($nb_entries > FreshRSS_Context::$number) {
  33. // We have more elements for pagination
  34. $last_entry = array_pop($entries);
  35. FreshRSS_Context::$next_id = $last_entry->id();
  36. }
  37. $first_entry = $nb_entries > 0 ? $entries[0] : null;
  38. FreshRSS_Context::$id_max = $first_entry === null ?
  39. (time() - 1) . '000000' :
  40. $first_entry->id();
  41. if (FreshRSS_Context::$order === 'ASC') {
  42. // In this case we do not know but we guess id_max
  43. $id_max = (time() - 1) . '000000';
  44. if (strcmp($id_max, FreshRSS_Context::$id_max) > 0) {
  45. FreshRSS_Context::$id_max = $id_max;
  46. }
  47. }
  48. $this->view->entries = $entries;
  49. } catch (FreshRSS_EntriesGetter_Exception $e) {
  50. Minz_Log::notice($e->getMessage());
  51. Minz_Error::error(404);
  52. }
  53. $this->view->categories = FreshRSS_Context::$categories;
  54. $this->view->rss_title = FreshRSS_Context::$name . ' | ' . Minz_View::title();
  55. $title = FreshRSS_Context::$name;
  56. if (FreshRSS_Context::$get_unread > 0) {
  57. $title = '(' . FreshRSS_Context::$get_unread . ') · ' . $title;
  58. }
  59. Minz_View::prependTitle($title . ' · ');
  60. }
  61. /**
  62. * This action displays the global view of FreshRSS.
  63. */
  64. public function globalAction() {
  65. if (!FreshRSS_Auth::hasAccess() && !Minz_Configuration::allowAnonymous()) {
  66. Minz_Request::forward(array('c' => 'auth', 'a' => 'login'));
  67. return;
  68. }
  69. Minz_View::appendScript(Minz_Url::display('/scripts/global_view.js?' . @filemtime(PUBLIC_PATH . '/scripts/global_view.js')));
  70. try {
  71. $this->updateContext();
  72. } catch (FreshRSS_Context_Exception $e) {
  73. Minz_Error::error(404);
  74. }
  75. $this->view->categories = FreshRSS_Context::$categories;
  76. $this->view->rss_title = FreshRSS_Context::$name . ' | ' . Minz_View::title();
  77. Minz_View::prependTitle(_t('gen.title.global_view') . ' · ');
  78. }
  79. /**
  80. * This action displays the RSS feed of FreshRSS.
  81. */
  82. public function rssAction() {
  83. $token = FreshRSS_Context::$conf->token;
  84. $token_param = Minz_Request::param('token', '');
  85. $token_is_ok = ($token != '' && $token === $token_param);
  86. // Check if user has access.
  87. if (!FreshRSS_Auth::hasAccess() &&
  88. !Minz_Configuration::allowAnonymous() &&
  89. !$token_is_ok) {
  90. Minz_Error::error(403);
  91. }
  92. try {
  93. $this->updateContext();
  94. } catch (FreshRSS_Context_Exception $e) {
  95. Minz_Error::error(404);
  96. }
  97. try {
  98. $this->view->entries = $this->listEntriesByContext();
  99. } catch (FreshRSS_EntriesGetter_Exception $e) {
  100. Minz_Log::notice($e->getMessage());
  101. Minz_Error::error(404);
  102. }
  103. // No layout for RSS output.
  104. $this->view->rss_title = FreshRSS_Context::$name . ' | ' . Minz_View::title();
  105. $this->view->_useLayout(false);
  106. header('Content-Type: application/rss+xml; charset=utf-8');
  107. }
  108. /**
  109. * This action updates the Context object by using request parameters.
  110. *
  111. * Parameters are:
  112. * - state (default: conf->default_view)
  113. * - search (default: empty string)
  114. * - order (default: conf->sort_order)
  115. * - nb (default: conf->posts_per_page)
  116. * - next (default: empty string)
  117. */
  118. private function updateContext() {
  119. FreshRSS_Context::_get(Minz_Request::param('get', 'a'));
  120. FreshRSS_Context::$state = Minz_Request::param(
  121. 'state', FreshRSS_Context::$conf->default_state
  122. );
  123. $state_forced_by_user = Minz_Request::param('state', false) !== false;
  124. if (FreshRSS_Context::$conf->default_view === 'adaptive' &&
  125. FreshRSS_Context::$get_unread <= 0 &&
  126. !FreshRSS_Context::isStateEnabled(FreshRSS_Entry::STATE_READ) &&
  127. !$state_forced_by_user) {
  128. FreshRSS_Context::$state |= FreshRSS_Entry::STATE_READ;
  129. }
  130. FreshRSS_Context::$search = Minz_Request::param('search', '');
  131. FreshRSS_Context::$order = Minz_Request::param(
  132. 'order', FreshRSS_Context::$conf->sort_order
  133. );
  134. FreshRSS_Context::$number = Minz_Request::param(
  135. 'nb', FreshRSS_Context::$conf->posts_per_page
  136. );
  137. FreshRSS_Context::$first_id = Minz_Request::param('next', '');
  138. }
  139. /**
  140. * This method returns a list of entries based on the Context object.
  141. */
  142. private function listEntriesByContext() {
  143. $entryDAO = FreshRSS_Factory::createEntryDao();
  144. $get = FreshRSS_Context::currentGet(true);
  145. if (count($get) > 1) {
  146. $type = $get[0];
  147. $id = $get[1];
  148. } else {
  149. $type = $get;
  150. $id = '';
  151. }
  152. return $entryDAO->listWhere(
  153. $type, $id, FreshRSS_Context::$state, FreshRSS_Context::$order,
  154. FreshRSS_Context::$number + 1, FreshRSS_Context::$first_id,
  155. FreshRSS_Context::$search
  156. );
  157. }
  158. /**
  159. * This action displays the about page of FreshRSS.
  160. */
  161. public function aboutAction() {
  162. Minz_View::prependTitle(_t('about') . ' · ');
  163. }
  164. /**
  165. * This action displays logs of FreshRSS for the current user.
  166. */
  167. public function logsAction() {
  168. if (!FreshRSS_Auth::hasAccess()) {
  169. Minz_Error::error(403);
  170. }
  171. Minz_View::prependTitle(_t('logs') . ' · ');
  172. if (Minz_Request::isPost()) {
  173. FreshRSS_LogDAO::truncate();
  174. }
  175. $logs = FreshRSS_LogDAO::lines(); //TODO: ask only the necessary lines
  176. //gestion pagination
  177. $page = Minz_Request::param('page', 1);
  178. $this->view->logsPaginator = new Minz_Paginator($logs);
  179. $this->view->logsPaginator->_nbItemsPerPage(50);
  180. $this->view->logsPaginator->_currentPage($page);
  181. }
  182. }