indexController.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. FreshRSS_Context::$id_max = time() . '000000';
  56. $this->view->callbackBeforeFeeds = static function (FreshRSS_View $view) {
  57. $view->tags = FreshRSS_Context::labels(true);
  58. $view->nbUnreadTags = 0;
  59. foreach ($view->tags as $tag) {
  60. $view->nbUnreadTags += $tag->nbUnread();
  61. }
  62. };
  63. $this->view->callbackBeforeEntries = static function (FreshRSS_View $view) {
  64. try {
  65. // +1 to account for paging logic
  66. $view->entries = FreshRSS_index_Controller::listEntriesByContext(FreshRSS_Context::$number + 1);
  67. ob_start(); //Buffer "one entry at a time"
  68. } catch (FreshRSS_EntriesGetter_Exception $e) {
  69. Minz_Log::notice($e->getMessage());
  70. Minz_Error::error(404);
  71. }
  72. };
  73. $this->view->callbackBeforePagination = static function (?FreshRSS_View $view, int $nbEntries, FreshRSS_Entry $lastEntry) {
  74. if ($nbEntries >= FreshRSS_Context::$number) {
  75. //We have enough entries: we discard the last one to use it for the next articles' page
  76. ob_clean();
  77. FreshRSS_Context::$next_id = $lastEntry->id();
  78. }
  79. ob_end_flush();
  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(): void {
  88. $this->normalAction();
  89. }
  90. /**
  91. * This action displays the global view of FreshRSS.
  92. */
  93. public function globalAction(): void {
  94. $allow_anonymous = FreshRSS_Context::systemConf()->allow_anonymous;
  95. if (!FreshRSS_Auth::hasAccess() && !$allow_anonymous) {
  96. Minz_Request::forward(['c' => 'auth', 'a' => 'login']);
  97. return;
  98. }
  99. FreshRSS_View::appendScript(Minz_Url::display('/scripts/extra.js?' . @filemtime(PUBLIC_PATH . '/scripts/extra.js')));
  100. FreshRSS_View::appendScript(Minz_Url::display('/scripts/global_view.js?' . @filemtime(PUBLIC_PATH . '/scripts/global_view.js')));
  101. try {
  102. FreshRSS_Context::updateUsingRequest(true);
  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 . ' | ' . FreshRSS_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. FreshRSS_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. * @deprecated See user query RSS sharing instead
  123. */
  124. public function rssAction(): void {
  125. $allow_anonymous = FreshRSS_Context::systemConf()->allow_anonymous;
  126. $token = FreshRSS_Context::userConf()->token;
  127. $token_param = Minz_Request::paramString('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. FreshRSS_Context::updateUsingRequest(false);
  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. $this->view->html_url = Minz_Url::display('', 'html', true);
  147. $this->view->rss_title = FreshRSS_Context::$name . ' | ' . FreshRSS_View::title();
  148. $this->view->rss_url = htmlspecialchars(
  149. PUBLIC_TO_INDEX_PATH . '/' . (empty($_SERVER['QUERY_STRING']) ? '' : '?' . $_SERVER['QUERY_STRING']), ENT_COMPAT, 'UTF-8');
  150. // No layout for RSS output.
  151. $this->view->_layout(null);
  152. header('Content-Type: application/rss+xml; charset=utf-8');
  153. }
  154. /**
  155. * @deprecated See user query OPML sharing instead
  156. */
  157. public function opmlAction(): void {
  158. $allow_anonymous = FreshRSS_Context::systemConf()->allow_anonymous;
  159. $token = FreshRSS_Context::userConf()->token;
  160. $token_param = Minz_Request::paramString('token');
  161. $token_is_ok = ($token != '' && $token === $token_param);
  162. // Check if user has access.
  163. if (!FreshRSS_Auth::hasAccess() && !$allow_anonymous && !$token_is_ok) {
  164. Minz_Error::error(403);
  165. }
  166. try {
  167. FreshRSS_Context::updateUsingRequest(false);
  168. } catch (FreshRSS_Context_Exception $e) {
  169. Minz_Error::error(404);
  170. }
  171. $get = FreshRSS_Context::currentGet(true);
  172. $type = (string)$get[0];
  173. $id = (int)$get[1];
  174. $this->view->excludeMutedFeeds = true;
  175. switch ($type) {
  176. case 'a':
  177. $this->view->categories = FreshRSS_Context::categories();
  178. break;
  179. case 'c':
  180. $cat = FreshRSS_Context::categories()[$id] ?? null;
  181. if ($cat == null) {
  182. Minz_Error::error(404);
  183. return;
  184. }
  185. $this->view->categories = [ $cat->id() => $cat ];
  186. break;
  187. case 'f':
  188. // We most likely already have the feed object in cache
  189. $feed = FreshRSS_Category::findFeed(FreshRSS_Context::categories(), $id);
  190. if ($feed === null) {
  191. $feedDAO = FreshRSS_Factory::createFeedDao();
  192. $feed = $feedDAO->searchById($id);
  193. if ($feed == null) {
  194. Minz_Error::error(404);
  195. return;
  196. }
  197. }
  198. $this->view->feeds = [ $feed->id() => $feed ];
  199. break;
  200. case 's':
  201. case 't':
  202. case 'T':
  203. default:
  204. Minz_Error::error(404);
  205. return;
  206. }
  207. // No layout for OPML output.
  208. $this->view->_layout(null);
  209. header('Content-Type: application/xml; charset=utf-8');
  210. }
  211. /**
  212. * This method returns a list of entries based on the Context object.
  213. * @param int $postsPerPage override `FreshRSS_Context::$number`
  214. * @return Traversable<FreshRSS_Entry>
  215. * @throws FreshRSS_EntriesGetter_Exception
  216. */
  217. public static function listEntriesByContext(?int $postsPerPage = null): Traversable {
  218. $entryDAO = FreshRSS_Factory::createEntryDao();
  219. $get = FreshRSS_Context::currentGet(true);
  220. if (is_array($get)) {
  221. $type = $get[0];
  222. $id = (int)($get[1]);
  223. } else {
  224. $type = $get;
  225. $id = 0;
  226. }
  227. $date_min = 0;
  228. if (FreshRSS_Context::$sinceHours > 0) {
  229. $date_min = time() - (FreshRSS_Context::$sinceHours * 3600);
  230. }
  231. foreach ($entryDAO->listWhere(
  232. $type, $id, FreshRSS_Context::$state, FreshRSS_Context::$order,
  233. $postsPerPage ?? FreshRSS_Context::$number, FreshRSS_Context::$offset, FreshRSS_Context::$first_id,
  234. FreshRSS_Context::$search, $date_min)
  235. as $entry) {
  236. yield $entry;
  237. }
  238. }
  239. /**
  240. * This action displays the about page of FreshRSS.
  241. */
  242. public function aboutAction(): void {
  243. FreshRSS_View::prependTitle(_t('index.about.title') . ' · ');
  244. }
  245. /**
  246. * This action displays the EULA/TOS (Terms of Service) page of FreshRSS.
  247. * This page is enabled only if admin created a data/tos.html file.
  248. * The content of the page is the content of data/tos.html.
  249. * It returns 404 if there is no EULA/TOS.
  250. */
  251. public function tosAction(): void {
  252. $terms_of_service = file_get_contents(TOS_FILENAME);
  253. if ($terms_of_service === false) {
  254. Minz_Error::error(404);
  255. return;
  256. }
  257. $this->view->terms_of_service = $terms_of_service;
  258. $this->view->can_register = !max_registrations_reached();
  259. FreshRSS_View::prependTitle(_t('index.tos.title') . ' · ');
  260. }
  261. /**
  262. * This action displays logs of FreshRSS for the current user.
  263. */
  264. public function logsAction(): void {
  265. if (!FreshRSS_Auth::hasAccess()) {
  266. Minz_Error::error(403);
  267. }
  268. FreshRSS_View::prependTitle(_t('index.log.title') . ' · ');
  269. if (Minz_Request::isPost()) {
  270. FreshRSS_LogDAO::truncate();
  271. }
  272. $logs = FreshRSS_LogDAO::lines(); //TODO: ask only the necessary lines
  273. //gestion pagination
  274. $page = Minz_Request::paramInt('page') ?: 1;
  275. $this->view->logsPaginator = new Minz_Paginator($logs);
  276. $this->view->logsPaginator->_nbItemsPerPage(50);
  277. $this->view->logsPaginator->_currentPage($page);
  278. }
  279. }