indexController.php 8.1 KB

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