indexController.php 9.4 KB

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