indexController.php 11 KB

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