indexController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This class handles main actions of FreshRSS.
  5. */
  6. class FreshRSS_index_Controller extends FreshRSS_ActionController {
  7. #[\Override]
  8. public function firstAction(): void {
  9. $this->view->html_url = Minz_Url::display(['c' => 'index', 'a' => 'index'], 'html', 'root');
  10. }
  11. /**
  12. * This action only redirect on the default view mode (normal or global)
  13. */
  14. public function indexAction(): void {
  15. $preferred_output = FreshRSS_Context::userConf()->view_mode;
  16. Minz_Request::forward([
  17. 'c' => 'index',
  18. 'a' => $preferred_output,
  19. ]);
  20. }
  21. /**
  22. * This action displays the normal view of FreshRSS.
  23. */
  24. public function normalAction(): void {
  25. $allow_anonymous = FreshRSS_Context::systemConf()->allow_anonymous;
  26. if (!FreshRSS_Auth::hasAccess() && !$allow_anonymous) {
  27. Minz_Request::forward(['c' => 'auth', 'a' => 'login']);
  28. return;
  29. }
  30. $id = Minz_Request::paramInt('id');
  31. if ($id !== 0) {
  32. $view = Minz_Request::paramString('a');
  33. $url_redirect = ['c' => 'subscription', 'a' => 'feed', 'params' => ['id' => (string)$id, 'from' => $view]];
  34. Minz_Request::forward($url_redirect, true);
  35. return;
  36. }
  37. try {
  38. FreshRSS_Context::updateUsingRequest(true);
  39. } catch (FreshRSS_Context_Exception $e) {
  40. Minz_Error::error(404);
  41. }
  42. $this->_csp([
  43. 'default-src' => "'self'",
  44. 'frame-src' => '*',
  45. 'img-src' => '* data:',
  46. 'frame-ancestors' => "'none'",
  47. 'media-src' => '*',
  48. ]);
  49. $this->view->categories = FreshRSS_Context::categories();
  50. $this->view->rss_title = FreshRSS_Context::$name . ' | ' . FreshRSS_View::title();
  51. $title = FreshRSS_Context::$name;
  52. if (FreshRSS_Context::$get_unread > 0) {
  53. $title = '(' . FreshRSS_Context::$get_unread . ') ' . $title;
  54. }
  55. FreshRSS_View::prependTitle($title . ' · ');
  56. if (FreshRSS_Context::$id_max === '0') {
  57. FreshRSS_Context::$id_max = uTimeString();
  58. }
  59. $this->view->callbackBeforeFeeds = static function (FreshRSS_View $view) {
  60. $view->tags = FreshRSS_Context::labels(true);
  61. $view->nbUnreadTags = 0;
  62. foreach ($view->tags as $tag) {
  63. $view->nbUnreadTags += $tag->nbUnread();
  64. }
  65. };
  66. $this->view->callbackBeforeEntries = static function (FreshRSS_View $view) {
  67. try {
  68. // +1 to account for paging logic
  69. $view->entries = FreshRSS_index_Controller::listEntriesByContext(FreshRSS_Context::$number + 1);
  70. ob_start(); //Buffer "one entry at a time"
  71. } catch (FreshRSS_EntriesGetter_Exception $e) {
  72. Minz_Log::notice($e->getMessage());
  73. Minz_Error::error(404);
  74. }
  75. };
  76. $this->view->callbackBeforePagination = static function (?FreshRSS_View $view, int $nbEntries, FreshRSS_Entry $lastEntry) {
  77. if ($nbEntries > FreshRSS_Context::$number) {
  78. //We have enough entries: we discard the last one to use it for the next articles' page
  79. ob_clean();
  80. FreshRSS_Context::$continuation_id = $lastEntry->id();
  81. } else {
  82. FreshRSS_Context::$continuation_id = '0';
  83. }
  84. ob_end_flush();
  85. };
  86. }
  87. /**
  88. * This action displays the reader view of FreshRSS.
  89. *
  90. * @todo: change this view into specific CSS rules?
  91. */
  92. public function readerAction(): void {
  93. $this->normalAction();
  94. }
  95. /**
  96. * This action displays the global view of FreshRSS.
  97. */
  98. public function globalAction(): void {
  99. $allow_anonymous = FreshRSS_Context::systemConf()->allow_anonymous;
  100. if (!FreshRSS_Auth::hasAccess() && !$allow_anonymous) {
  101. Minz_Request::forward(['c' => 'auth', 'a' => 'login']);
  102. return;
  103. }
  104. FreshRSS_View::appendScript(Minz_Url::display('/scripts/extra.js?' . @filemtime(PUBLIC_PATH . '/scripts/extra.js')));
  105. FreshRSS_View::appendScript(Minz_Url::display('/scripts/global_view.js?' . @filemtime(PUBLIC_PATH . '/scripts/global_view.js')));
  106. try {
  107. FreshRSS_Context::updateUsingRequest(true);
  108. } catch (FreshRSS_Context_Exception) {
  109. Minz_Error::error(404);
  110. }
  111. $this->view->categories = FreshRSS_Context::categories();
  112. $this->view->rss_title = FreshRSS_Context::$name . ' | ' . FreshRSS_View::title();
  113. $title = _t('index.feed.title_global');
  114. if (FreshRSS_Context::$get_unread > 0) {
  115. $title = '(' . FreshRSS_Context::$get_unread . ') ' . $title;
  116. }
  117. FreshRSS_View::prependTitle($title . ' · ');
  118. $this->_csp([
  119. 'default-src' => "'self'",
  120. 'frame-src' => '*',
  121. 'img-src' => '* data:',
  122. 'frame-ancestors' => "'none'",
  123. 'media-src' => '*',
  124. ]);
  125. }
  126. /**
  127. * This action displays the RSS feed of FreshRSS.
  128. * @deprecated See user query RSS sharing instead
  129. */
  130. public function rssAction(): void {
  131. $allow_anonymous = FreshRSS_Context::systemConf()->allow_anonymous;
  132. $token = FreshRSS_Context::userConf()->token;
  133. $token_param = Minz_Request::paramString('token');
  134. $token_is_ok = ($token != '' && $token === $token_param);
  135. // Check if user has access.
  136. if (!FreshRSS_Auth::hasAccess() &&
  137. !$allow_anonymous &&
  138. !$token_is_ok) {
  139. Minz_Error::error(403);
  140. }
  141. try {
  142. FreshRSS_Context::updateUsingRequest(false);
  143. } catch (FreshRSS_Context_Exception $e) {
  144. Minz_Error::error(404);
  145. }
  146. try {
  147. $this->view->entries = FreshRSS_index_Controller::listEntriesByContext();
  148. } catch (FreshRSS_EntriesGetter_Exception $e) {
  149. Minz_Log::notice($e->getMessage());
  150. Minz_Error::error(404);
  151. }
  152. $this->view->html_url = Minz_Url::display('', 'html', true);
  153. $this->view->rss_title = FreshRSS_Context::$name . ' | ' . FreshRSS_View::title();
  154. $queryString = $_SERVER['QUERY_STRING'] ?? '';
  155. $this->view->rss_url = htmlspecialchars(
  156. PUBLIC_TO_INDEX_PATH . '/' . ($queryString === '' || !is_string($queryString) ? '' : '?' . $queryString), ENT_COMPAT, 'UTF-8');
  157. // No layout for RSS output.
  158. $this->view->_layout(null);
  159. header('Content-Type: application/rss+xml; charset=utf-8');
  160. }
  161. /**
  162. * @deprecated See user query OPML sharing instead
  163. */
  164. public function opmlAction(): void {
  165. $allow_anonymous = FreshRSS_Context::systemConf()->allow_anonymous;
  166. $token = FreshRSS_Context::userConf()->token;
  167. $token_param = Minz_Request::paramString('token');
  168. $token_is_ok = ($token != '' && $token === $token_param);
  169. // Check if user has access.
  170. if (!FreshRSS_Auth::hasAccess() && !$allow_anonymous && !$token_is_ok) {
  171. Minz_Error::error(403);
  172. }
  173. try {
  174. FreshRSS_Context::updateUsingRequest(false);
  175. } catch (FreshRSS_Context_Exception) {
  176. Minz_Error::error(404);
  177. }
  178. $get = FreshRSS_Context::currentGet(true);
  179. $type = (string)$get[0];
  180. $id = (int)$get[1];
  181. $this->view->excludeMutedFeeds = $type !== 'f'; // Exclude muted feeds except when we focus on a feed
  182. switch ($type) {
  183. case 'a': // All PRIORITY_MAIN_STREAM
  184. case 'A': // All except PRIORITY_ARCHIVED
  185. case 'Z': // All including PRIORITY_ARCHIVED
  186. $this->view->categories = FreshRSS_Context::categories();
  187. break;
  188. case 'c':
  189. $cat = FreshRSS_Context::categories()[$id] ?? null;
  190. if ($cat == null) {
  191. Minz_Error::error(404);
  192. return;
  193. }
  194. $this->view->categories = [$cat->id() => $cat];
  195. break;
  196. case 'f':
  197. // We most likely already have the feed object in cache
  198. $feed = FreshRSS_Category::findFeed(FreshRSS_Context::categories(), $id);
  199. if ($feed === null) {
  200. $feedDAO = FreshRSS_Factory::createFeedDao();
  201. $feed = $feedDAO->searchById($id);
  202. if ($feed == null) {
  203. Minz_Error::error(404);
  204. return;
  205. }
  206. }
  207. $this->view->feeds = [$feed->id() => $feed];
  208. break;
  209. case 's':
  210. case 't':
  211. case 'T':
  212. default:
  213. Minz_Error::error(404);
  214. return;
  215. }
  216. // No layout for OPML output.
  217. $this->view->_layout(null);
  218. header('Content-Type: application/xml; charset=utf-8');
  219. }
  220. /**
  221. * This method returns a list of entries based on the Context object.
  222. * @param int $postsPerPage override `FreshRSS_Context::$number`
  223. * @return Traversable<FreshRSS_Entry>
  224. * @throws FreshRSS_EntriesGetter_Exception
  225. */
  226. public static function listEntriesByContext(?int $postsPerPage = null): Traversable {
  227. $entryDAO = FreshRSS_Factory::createEntryDao();
  228. $get = FreshRSS_Context::currentGet(true);
  229. if (is_array($get)) {
  230. $type = $get[0];
  231. $id = (int)($get[1]);
  232. } else {
  233. $type = $get;
  234. $id = 0;
  235. }
  236. $id_min = '0';
  237. if (FreshRSS_Context::$sinceHours > 0) {
  238. $id_min = (time() - (FreshRSS_Context::$sinceHours * 3600)) . '000000';
  239. }
  240. $continuation_value = 0;
  241. if (FreshRSS_Context::$continuation_id !== '0') {
  242. if (in_array(FreshRSS_Context::$sort, ['date', 'link', 'title'], true)) {
  243. $pagingEntry = $entryDAO->searchById(FreshRSS_Context::$continuation_id);
  244. $continuation_value = $pagingEntry === null ? 0 : match (FreshRSS_Context::$sort) {
  245. 'date' => $pagingEntry->date(true),
  246. 'link' => $pagingEntry->link(true),
  247. 'title' => $pagingEntry->title(),
  248. };
  249. } elseif (FreshRSS_Context::$sort === 'rand') {
  250. FreshRSS_Context::$continuation_id = '0';
  251. }
  252. }
  253. foreach ($entryDAO->listWhere(
  254. $type, $id, FreshRSS_Context::$state, FreshRSS_Context::$search,
  255. id_min: $id_min, id_max: FreshRSS_Context::$id_max, sort: FreshRSS_Context::$sort, order: FreshRSS_Context::$order,
  256. continuation_id: FreshRSS_Context::$continuation_id, continuation_value: $continuation_value,
  257. limit: $postsPerPage ?? FreshRSS_Context::$number, offset: FreshRSS_Context::$offset) as $entry) {
  258. yield $entry;
  259. }
  260. }
  261. /**
  262. * This action displays the about page of FreshRSS.
  263. */
  264. public function aboutAction(): void {
  265. FreshRSS_View::prependTitle(_t('index.about.title') . ' · ');
  266. }
  267. /**
  268. * This action displays the EULA/TOS (Terms of Service) page of FreshRSS.
  269. * This page is enabled only if admin created a data/tos.html file.
  270. * The content of the page is the content of data/tos.html.
  271. * It returns 404 if there is no EULA/TOS.
  272. */
  273. public function tosAction(): void {
  274. $terms_of_service = file_get_contents(TOS_FILENAME);
  275. if ($terms_of_service === false) {
  276. Minz_Error::error(404);
  277. return;
  278. }
  279. $this->view->terms_of_service = $terms_of_service;
  280. $this->view->can_register = !max_registrations_reached();
  281. FreshRSS_View::prependTitle(_t('index.tos.title') . ' · ');
  282. }
  283. /**
  284. * This action displays logs of FreshRSS for the current user.
  285. */
  286. public function logsAction(): void {
  287. if (!FreshRSS_Auth::hasAccess()) {
  288. Minz_Error::error(403);
  289. }
  290. FreshRSS_View::prependTitle(_t('index.log.title') . ' · ');
  291. if (Minz_Request::isPost()) {
  292. FreshRSS_LogDAO::truncate();
  293. }
  294. $logs = FreshRSS_LogDAO::lines(); //TODO: ask only the necessary lines
  295. //gestion pagination
  296. $page = Minz_Request::paramInt('page') ?: 1;
  297. $this->view->logsPaginator = new Minz_Paginator($logs);
  298. $this->view->logsPaginator->_nbItemsPerPage(50);
  299. $this->view->logsPaginator->_currentPage($page);
  300. }
  301. }