4
0

indexController.php 6.9 KB

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