indexController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. /**
  8. * This action only redirect on the default view mode (normal or global)
  9. */
  10. public function indexAction(): void {
  11. $preferred_output = FreshRSS_Context::$user_conf->view_mode;
  12. Minz_Request::forward([
  13. 'c' => 'index',
  14. 'a' => $preferred_output,
  15. ]);
  16. }
  17. /**
  18. * This action displays the normal view of FreshRSS.
  19. */
  20. public function normalAction(): void {
  21. $allow_anonymous = FreshRSS_Context::$system_conf->allow_anonymous;
  22. if (!FreshRSS_Auth::hasAccess() && !$allow_anonymous) {
  23. Minz_Request::forward(['c' => 'auth', 'a' => 'login']);
  24. return;
  25. }
  26. $id = Minz_Request::paramInt('id');
  27. if ($id !== 0) {
  28. $view = Minz_Request::paramString('a');
  29. $url_redirect = ['c' => 'subscription', 'a' => 'feed', 'params' => ['id' => (string)$id, 'from' => $view]];
  30. Minz_Request::forward($url_redirect, true);
  31. return;
  32. }
  33. try {
  34. FreshRSS_Context::updateUsingRequest();
  35. } catch (FreshRSS_Context_Exception $e) {
  36. Minz_Error::error(404);
  37. }
  38. $this->_csp([
  39. 'default-src' => "'self'",
  40. 'frame-src' => '*',
  41. 'img-src' => '* data:',
  42. 'media-src' => '*',
  43. ]);
  44. $this->view->categories = FreshRSS_Context::$categories;
  45. $this->view->rss_title = FreshRSS_Context::$name . ' | ' . FreshRSS_View::title();
  46. $title = FreshRSS_Context::$name;
  47. if (FreshRSS_Context::$get_unread > 0) {
  48. $title = '(' . FreshRSS_Context::$get_unread . ') ' . $title;
  49. }
  50. FreshRSS_View::prependTitle($title . ' · ');
  51. FreshRSS_Context::$id_max = time() . '000000';
  52. $this->view->callbackBeforeFeeds = static function (FreshRSS_View $view) {
  53. try {
  54. $tagDAO = FreshRSS_Factory::createTagDao();
  55. $view->tags = $tagDAO->listTags(true) ?: [];
  56. $view->nbUnreadTags = 0;
  57. foreach ($view->tags as $tag) {
  58. $view->nbUnreadTags += $tag->nbUnread();
  59. }
  60. } catch (Exception $e) {
  61. Minz_Log::notice($e->getMessage());
  62. }
  63. };
  64. $this->view->callbackBeforeEntries = static function (FreshRSS_View $view) {
  65. try {
  66. FreshRSS_Context::$number++; //+1 for articles' page
  67. $view->entries = FreshRSS_index_Controller::listEntriesByContext();
  68. FreshRSS_Context::$number--;
  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::$next_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::$system_conf->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();
  105. } catch (FreshRSS_Context_Exception $e) {
  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. */
  125. public function rssAction(): void {
  126. $allow_anonymous = FreshRSS_Context::$system_conf->allow_anonymous;
  127. $token = FreshRSS_Context::$user_conf->token;
  128. $token_param = Minz_Request::paramString('token');
  129. $token_is_ok = ($token != '' && $token === $token_param);
  130. // Check if user has access.
  131. if (!FreshRSS_Auth::hasAccess() &&
  132. !$allow_anonymous &&
  133. !$token_is_ok) {
  134. Minz_Error::error(403);
  135. }
  136. try {
  137. FreshRSS_Context::updateUsingRequest();
  138. } catch (FreshRSS_Context_Exception $e) {
  139. Minz_Error::error(404);
  140. }
  141. try {
  142. $this->view->entries = FreshRSS_index_Controller::listEntriesByContext();
  143. } catch (FreshRSS_EntriesGetter_Exception $e) {
  144. Minz_Log::notice($e->getMessage());
  145. Minz_Error::error(404);
  146. }
  147. // No layout for RSS output.
  148. $this->view->rss_url = PUBLIC_TO_INDEX_PATH . '/' . (empty($_SERVER['QUERY_STRING']) ? '' : '?' . $_SERVER['QUERY_STRING']);
  149. $this->view->rss_title = FreshRSS_Context::$name . ' | ' . FreshRSS_View::title();
  150. $this->view->_layout(null);
  151. header('Content-Type: application/rss+xml; charset=utf-8');
  152. }
  153. public function opmlAction(): void {
  154. $allow_anonymous = FreshRSS_Context::$system_conf->allow_anonymous;
  155. $token = FreshRSS_Context::$user_conf->token;
  156. $token_param = Minz_Request::paramString('token');
  157. $token_is_ok = ($token != '' && $token === $token_param);
  158. // Check if user has access.
  159. if (!FreshRSS_Auth::hasAccess() && !$allow_anonymous && !$token_is_ok) {
  160. Minz_Error::error(403);
  161. }
  162. try {
  163. FreshRSS_Context::updateUsingRequest();
  164. } catch (FreshRSS_Context_Exception $e) {
  165. Minz_Error::error(404);
  166. }
  167. $get = FreshRSS_Context::currentGet(true);
  168. $type = (string)$get[0];
  169. $id = (int)$get[1];
  170. $catDAO = FreshRSS_Factory::createCategoryDao();
  171. $categories = $catDAO->listCategories(true, true);
  172. $this->view->excludeMutedFeeds = true;
  173. switch ($type) {
  174. case 'a':
  175. $this->view->categories = $categories;
  176. break;
  177. case 'c':
  178. $cat = $categories[$id] ?? null;
  179. if ($cat == null) {
  180. Minz_Error::error(404);
  181. return;
  182. }
  183. $this->view->categories = [ $cat ];
  184. break;
  185. case 'f':
  186. // We most likely already have the feed object in cache
  187. $feed = FreshRSS_CategoryDAO::findFeed($categories, $id);
  188. if ($feed === null) {
  189. $feedDAO = FreshRSS_Factory::createFeedDao();
  190. $feed = $feedDAO->searchById($id);
  191. if ($feed == null) {
  192. Minz_Error::error(404);
  193. return;
  194. }
  195. }
  196. $this->view->feeds = [ $feed ];
  197. break;
  198. case 's':
  199. case 't':
  200. case 'T':
  201. default:
  202. Minz_Error::error(404);
  203. return;
  204. }
  205. // No layout for OPML output.
  206. $this->view->_layout(null);
  207. header('Content-Type: application/xml; charset=utf-8');
  208. }
  209. /**
  210. * This method returns a list of entries based on the Context object.
  211. * @return Traversable<FreshRSS_Entry>
  212. */
  213. public static function listEntriesByContext(): Traversable {
  214. $entryDAO = FreshRSS_Factory::createEntryDao();
  215. $get = FreshRSS_Context::currentGet(true);
  216. if (is_array($get)) {
  217. $type = $get[0];
  218. $id = (int)($get[1]);
  219. } else {
  220. $type = $get;
  221. $id = 0;
  222. }
  223. $limit = FreshRSS_Context::$number;
  224. $date_min = 0;
  225. if (FreshRSS_Context::$sinceHours) {
  226. $date_min = time() - (FreshRSS_Context::$sinceHours * 3600);
  227. $limit = FreshRSS_Context::$user_conf->max_posts_per_rss;
  228. }
  229. foreach ($entryDAO->listWhere(
  230. $type, $id, FreshRSS_Context::$state, FreshRSS_Context::$order,
  231. $limit, FreshRSS_Context::$first_id,
  232. FreshRSS_Context::$search, $date_min)
  233. as $entry) {
  234. yield $entry;
  235. }
  236. }
  237. /**
  238. * This action displays the about page of FreshRSS.
  239. */
  240. public function aboutAction(): void {
  241. FreshRSS_View::prependTitle(_t('index.about.title') . ' · ');
  242. }
  243. /**
  244. * This action displays the EULA/TOS (Terms of Service) page of FreshRSS.
  245. * This page is enabled only if admin created a data/tos.html file.
  246. * The content of the page is the content of data/tos.html.
  247. * It returns 404 if there is no EULA/TOS.
  248. */
  249. public function tosAction(): void {
  250. $terms_of_service = file_get_contents(TOS_FILENAME);
  251. if ($terms_of_service === false) {
  252. Minz_Error::error(404);
  253. return;
  254. }
  255. $this->view->terms_of_service = $terms_of_service;
  256. $this->view->can_register = !max_registrations_reached();
  257. FreshRSS_View::prependTitle(_t('index.tos.title') . ' · ');
  258. }
  259. /**
  260. * This action displays logs of FreshRSS for the current user.
  261. */
  262. public function logsAction(): void {
  263. if (!FreshRSS_Auth::hasAccess()) {
  264. Minz_Error::error(403);
  265. }
  266. FreshRSS_View::prependTitle(_t('index.log.title') . ' · ');
  267. if (Minz_Request::isPost()) {
  268. FreshRSS_LogDAO::truncate();
  269. }
  270. $logs = FreshRSS_LogDAO::lines(); //TODO: ask only the necessary lines
  271. //gestion pagination
  272. $page = Minz_Request::paramInt('page') ?: 1;
  273. $this->view->logsPaginator = new Minz_Paginator($logs);
  274. $this->view->logsPaginator->_nbItemsPerPage(50);
  275. $this->view->logsPaginator->_currentPage($page);
  276. }
  277. }