4
0

indexController.php 6.8 KB

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