indexController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. // Update number of read / unread variables.
  120. $entryDAO = FreshRSS_Factory::createEntryDao();
  121. FreshRSS_Context::$total_starred = $entryDAO->countUnreadReadFavorites();
  122. FreshRSS_Context::$total_unread = FreshRSS_CategoryDAO::CountUnreads(
  123. FreshRSS_Context::$categories, 1
  124. );
  125. FreshRSS_Context::_get(Minz_Request::param('get', 'a'));
  126. FreshRSS_Context::$state = Minz_Request::param(
  127. 'state', FreshRSS_Context::$conf->default_state
  128. );
  129. $state_forced_by_user = Minz_Request::param('state', false) !== false;
  130. if (FreshRSS_Context::$conf->default_view === 'adaptive' &&
  131. FreshRSS_Context::$get_unread <= 0 &&
  132. !FreshRSS_Context::isStateEnabled(FreshRSS_Entry::STATE_READ) &&
  133. !$state_forced_by_user) {
  134. FreshRSS_Context::$state |= FreshRSS_Entry::STATE_READ;
  135. }
  136. FreshRSS_Context::$search = Minz_Request::param('search', '');
  137. FreshRSS_Context::$order = Minz_Request::param(
  138. 'order', FreshRSS_Context::$conf->sort_order
  139. );
  140. FreshRSS_Context::$number = Minz_Request::param(
  141. 'nb', FreshRSS_Context::$conf->posts_per_page
  142. );
  143. FreshRSS_Context::$first_id = Minz_Request::param('next', '');
  144. }
  145. /**
  146. * This method returns a list of entries based on the Context object.
  147. */
  148. private function listEntriesByContext() {
  149. $entryDAO = FreshRSS_Factory::createEntryDao();
  150. $get = FreshRSS_Context::currentGet(true);
  151. if (count($get) > 1) {
  152. $type = $get[0];
  153. $id = $get[1];
  154. } else {
  155. $type = $get;
  156. $id = '';
  157. }
  158. return $entryDAO->listWhere(
  159. $type, $id, FreshRSS_Context::$state, FreshRSS_Context::$order,
  160. FreshRSS_Context::$number + 1, FreshRSS_Context::$first_id,
  161. FreshRSS_Context::$search
  162. );
  163. }
  164. /**
  165. * This action displays the about page of FreshRSS.
  166. */
  167. public function aboutAction() {
  168. Minz_View::prependTitle(_t('about') . ' · ');
  169. }
  170. /**
  171. * This action displays logs of FreshRSS for the current user.
  172. */
  173. public function logsAction() {
  174. if (!FreshRSS_Auth::hasAccess()) {
  175. Minz_Error::error(403);
  176. }
  177. Minz_View::prependTitle(_t('logs') . ' · ');
  178. if (Minz_Request::isPost()) {
  179. FreshRSS_LogDAO::truncate();
  180. }
  181. $logs = FreshRSS_LogDAO::lines(); //TODO: ask only the necessary lines
  182. //gestion pagination
  183. $page = Minz_Request::param('page', 1);
  184. $this->view->logsPaginator = new Minz_Paginator($logs);
  185. $this->view->logsPaginator->_nbItemsPerPage(50);
  186. $this->view->logsPaginator->_currentPage($page);
  187. }
  188. }