indexController.php 9.0 KB

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