4
0

indexController.php 6.4 KB

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