indexController.php 6.6 KB

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