indexController.php 10 KB

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