indexController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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() {
  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() {
  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::param('id');
  26. if ($id) {
  27. $view = Minz_Request::param('a');
  28. $url_redirect = array('c' => 'subscription', 'a' => 'feed', 'params' => array('id' => $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 ($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 ($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 ($view, $nbEntries, $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() {
  89. $this->normalAction();
  90. }
  91. /**
  92. * This action displays the global view of FreshRSS.
  93. */
  94. public function globalAction() {
  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() {
  125. $allow_anonymous = FreshRSS_Context::$system_conf->allow_anonymous;
  126. $token = FreshRSS_Context::$user_conf->token;
  127. $token_param = Minz_Request::param('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(false);
  150. header('Content-Type: application/rss+xml; charset=utf-8');
  151. }
  152. public function opmlAction() {
  153. $allow_anonymous = FreshRSS_Context::$system_conf->allow_anonymous;
  154. $token = FreshRSS_Context::$user_conf->token;
  155. $token_param = Minz_Request::param('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. if (is_array($get)) {
  168. $type = $get[0];
  169. $id = $get[1];
  170. } else {
  171. $type = $get;
  172. $id = '';
  173. }
  174. $catDAO = FreshRSS_Factory::createCategoryDao();
  175. $categories = $catDAO->listCategories(true, true);
  176. $this->view->excludeMutedFeeds = true;
  177. switch ($type) {
  178. case 'a':
  179. $this->view->categories = $categories;
  180. break;
  181. case 'c':
  182. $cat = $categories[$id] ?? null;
  183. if ($cat == null) {
  184. Minz_Error::error(404);
  185. return;
  186. }
  187. $this->view->categories = [ $cat ];
  188. break;
  189. case 'f':
  190. // We most likely already have the feed object in cache
  191. $feed = FreshRSS_CategoryDAO::findFeed($categories, $id);
  192. if ($feed == null) {
  193. $feedDAO = FreshRSS_Factory::createFeedDao();
  194. $feed = $feedDAO->searchById($id);
  195. if ($feed == null) {
  196. Minz_Error::error(404);
  197. return;
  198. }
  199. }
  200. $this->view->feeds = [ $feed ];
  201. break;
  202. case 's':
  203. case 't':
  204. case 'T':
  205. default:
  206. Minz_Error::error(404);
  207. return;
  208. }
  209. require_once(LIB_PATH . '/lib_opml.php');
  210. // No layout for OPML output.
  211. $this->view->_layout(false);
  212. header('Content-Type: application/xml; charset=utf-8');
  213. }
  214. /**
  215. * This method returns a list of entries based on the Context object.
  216. */
  217. public static function listEntriesByContext() {
  218. $entryDAO = FreshRSS_Factory::createEntryDao();
  219. $get = FreshRSS_Context::currentGet(true);
  220. if (is_array($get)) {
  221. $type = $get[0];
  222. $id = $get[1];
  223. } else {
  224. $type = $get;
  225. $id = '';
  226. }
  227. $limit = FreshRSS_Context::$number;
  228. $date_min = 0;
  229. if (FreshRSS_Context::$sinceHours) {
  230. $date_min = time() - (FreshRSS_Context::$sinceHours * 3600);
  231. $limit = FreshRSS_Context::$user_conf->max_posts_per_rss;
  232. }
  233. foreach ($entryDAO->listWhere(
  234. $type, $id, FreshRSS_Context::$state, FreshRSS_Context::$order,
  235. $limit, FreshRSS_Context::$first_id,
  236. FreshRSS_Context::$search, $date_min)
  237. as $entry) {
  238. yield $entry;
  239. }
  240. }
  241. /**
  242. * This action displays the about page of FreshRSS.
  243. */
  244. public function aboutAction() {
  245. FreshRSS_View::prependTitle(_t('index.about.title') . ' · ');
  246. }
  247. /**
  248. * This action displays the EULA page of FreshRSS.
  249. * This page is enabled only if admin created a data/tos.html file.
  250. * The content of the page is the content of data/tos.html.
  251. * It returns 404 if there is no EULA.
  252. */
  253. public function tosAction() {
  254. $terms_of_service = file_get_contents(join_path(DATA_PATH, 'tos.html'));
  255. if (!$terms_of_service) {
  256. Minz_Error::error(404);
  257. }
  258. $this->view->terms_of_service = $terms_of_service;
  259. $this->view->can_register = !max_registrations_reached();
  260. FreshRSS_View::prependTitle(_t('index.tos.title') . ' · ');
  261. }
  262. /**
  263. * This action displays logs of FreshRSS for the current user.
  264. */
  265. public function logsAction() {
  266. if (!FreshRSS_Auth::hasAccess()) {
  267. Minz_Error::error(403);
  268. }
  269. FreshRSS_View::prependTitle(_t('index.log.title') . ' · ');
  270. if (Minz_Request::isPost()) {
  271. FreshRSS_LogDAO::truncate();
  272. }
  273. $logs = FreshRSS_LogDAO::lines(); //TODO: ask only the necessary lines
  274. //gestion pagination
  275. $page = intval(Minz_Request::param('page', 1));
  276. $this->view->logsPaginator = new Minz_Paginator($logs);
  277. $this->view->logsPaginator->_nbItemsPerPage(50);
  278. $this->view->logsPaginator->_currentPage($page);
  279. }
  280. }