indexController.php 9.3 KB

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