indexController.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /**
  3. * This class handles main actions of FreshRSS.
  4. */
  5. class FreshRSS_index_Controller extends Minz_ActionController {
  6. /**
  7. * This action only redirect on the default view mode (normal or global)
  8. */
  9. public function indexAction() {
  10. $prefered_output = FreshRSS_Context::$user_conf->view_mode;
  11. Minz_Request::forward(array(
  12. 'c' => 'index',
  13. 'a' => $prefered_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. try {
  26. $this->updateContext();
  27. } catch (FreshRSS_Context_Exception $e) {
  28. Minz_Error::error(404);
  29. }
  30. $this->_csp([
  31. 'default-src' => "'self'",
  32. 'frame-src' => '*',
  33. 'img-src' => '* data:',
  34. 'media-src' => '*',
  35. ]);
  36. $this->view->categories = FreshRSS_Context::$categories;
  37. $this->view->rss_title = FreshRSS_Context::$name . ' | ' . Minz_View::title();
  38. $title = FreshRSS_Context::$name;
  39. if (FreshRSS_Context::$get_unread > 0) {
  40. $title = '(' . FreshRSS_Context::$get_unread . ') ' . $title;
  41. }
  42. Minz_View::prependTitle($title . ' · ');
  43. $this->view->callbackBeforeFeeds = function ($view) {
  44. try {
  45. $tagDAO = FreshRSS_Factory::createTagDao();
  46. $view->tags = $tagDAO->listTags(true);
  47. $view->nbUnreadTags = 0;
  48. foreach ($view->tags as $tag) {
  49. $view->nbUnreadTags += $tag->nbUnread();
  50. }
  51. } catch (Exception $e) {
  52. Minz_Log::notice($e->getMessage());
  53. }
  54. };
  55. $this->view->callbackBeforeEntries = function ($view) {
  56. try {
  57. FreshRSS_Context::$number++; //+1 for pagination
  58. $view->entries = FreshRSS_index_Controller::listEntriesByContext();
  59. FreshRSS_Context::$number--;
  60. ob_start();
  61. } catch (FreshRSS_EntriesGetter_Exception $e) {
  62. Minz_Log::notice($e->getMessage());
  63. Minz_Error::error(404);
  64. }
  65. };
  66. $this->view->callbackBeforePagination = function ($view, $nbEntries, $firstEntry, $lastEntry) {
  67. if ($nbEntries >= FreshRSS_Context::$number) {
  68. //We have enough entries: we discard the last one to use it for the next pagination
  69. ob_clean();
  70. FreshRSS_Context::$next_id = $lastEntry->id();
  71. }
  72. ob_end_flush();
  73. FreshRSS_Context::$id_max = $firstEntry === null ? (time() - 1) . '000000' : $firstEntry->id();
  74. if (FreshRSS_Context::$order === 'ASC') {
  75. // In this case we do not know but we guess id_max
  76. $id_max = (time() - 1) . '000000';
  77. if (strcmp($id_max, FreshRSS_Context::$id_max) > 0) {
  78. FreshRSS_Context::$id_max = $id_max;
  79. }
  80. }
  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. Minz_View::appendScript(Minz_Url::display('/scripts/extra.js?' . @filemtime(PUBLIC_PATH . '/scripts/extra.js')));
  101. Minz_View::appendScript(Minz_Url::display('/scripts/global_view.js?' . @filemtime(PUBLIC_PATH . '/scripts/global_view.js')));
  102. try {
  103. $this->updateContext();
  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 . ' | ' . Minz_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. Minz_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. $this->updateContext();
  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->url = PUBLIC_TO_INDEX_PATH . '/' . (empty($_SERVER['QUERY_STRING']) ? '' : '?' . $_SERVER['QUERY_STRING']);
  148. $this->view->rss_title = FreshRSS_Context::$name . ' | ' . Minz_View::title();
  149. $this->view->_layout(false);
  150. header('Content-Type: application/rss+xml; charset=utf-8');
  151. }
  152. /**
  153. * This action updates the Context object by using request parameters.
  154. *
  155. * Parameters are:
  156. * - state (default: conf->default_view)
  157. * - search (default: empty string)
  158. * - order (default: conf->sort_order)
  159. * - nb (default: conf->posts_per_page)
  160. * - next (default: empty string)
  161. * - hours (default: 0)
  162. */
  163. private function updateContext() {
  164. if (empty(FreshRSS_Context::$categories)) {
  165. $catDAO = FreshRSS_Factory::createCategoryDao();
  166. FreshRSS_Context::$categories = $catDAO->listSortedCategories();
  167. }
  168. // Update number of read / unread variables.
  169. $entryDAO = FreshRSS_Factory::createEntryDao();
  170. FreshRSS_Context::$total_starred = $entryDAO->countUnreadReadFavorites();
  171. FreshRSS_Context::$total_unread = FreshRSS_CategoryDAO::CountUnreads(
  172. FreshRSS_Context::$categories, 1
  173. );
  174. FreshRSS_Context::_get(Minz_Request::param('get', 'a'));
  175. FreshRSS_Context::$state = Minz_Request::param(
  176. 'state', FreshRSS_Context::$user_conf->default_state
  177. );
  178. $state_forced_by_user = Minz_Request::param('state', false) !== false;
  179. if (FreshRSS_Context::$user_conf->default_view === 'adaptive' &&
  180. FreshRSS_Context::$get_unread <= 0 &&
  181. !FreshRSS_Context::isStateEnabled(FreshRSS_Entry::STATE_READ) &&
  182. !$state_forced_by_user) {
  183. FreshRSS_Context::$state |= FreshRSS_Entry::STATE_READ;
  184. }
  185. FreshRSS_Context::$search = new FreshRSS_BooleanSearch(Minz_Request::param('search', ''));
  186. FreshRSS_Context::$order = Minz_Request::param(
  187. 'order', FreshRSS_Context::$user_conf->sort_order
  188. );
  189. FreshRSS_Context::$number = intval(Minz_Request::param('nb', FreshRSS_Context::$user_conf->posts_per_page));
  190. if (FreshRSS_Context::$number > FreshRSS_Context::$user_conf->max_posts_per_rss) {
  191. FreshRSS_Context::$number = max(
  192. FreshRSS_Context::$user_conf->max_posts_per_rss,
  193. FreshRSS_Context::$user_conf->posts_per_page);
  194. }
  195. FreshRSS_Context::$first_id = Minz_Request::param('next', '');
  196. FreshRSS_Context::$sinceHours = intval(Minz_Request::param('hours', 0));
  197. }
  198. /**
  199. * This method returns a list of entries based on the Context object.
  200. */
  201. public static function listEntriesByContext() {
  202. $entryDAO = FreshRSS_Factory::createEntryDao();
  203. $get = FreshRSS_Context::currentGet(true);
  204. if (is_array($get)) {
  205. $type = $get[0];
  206. $id = $get[1];
  207. } else {
  208. $type = $get;
  209. $id = '';
  210. }
  211. $limit = FreshRSS_Context::$number;
  212. $date_min = 0;
  213. if (FreshRSS_Context::$sinceHours) {
  214. $date_min = time() - (FreshRSS_Context::$sinceHours * 3600);
  215. $limit = FreshRSS_Context::$user_conf->max_posts_per_rss;
  216. }
  217. foreach ($entryDAO->listWhere(
  218. $type, $id, FreshRSS_Context::$state, FreshRSS_Context::$order,
  219. $limit, FreshRSS_Context::$first_id,
  220. FreshRSS_Context::$search, $date_min)
  221. as $entry) {
  222. yield $entry;
  223. }
  224. }
  225. /**
  226. * This action displays the about page of FreshRSS.
  227. */
  228. public function aboutAction() {
  229. Minz_View::prependTitle(_t('index.about.title') . ' · ');
  230. }
  231. /**
  232. * This action displays the EULA page of FreshRSS.
  233. * This page is enabled only if admin created a data/tos.html file.
  234. * The content of the page is the content of data/tos.html.
  235. * It returns 404 if there is no EULA.
  236. */
  237. public function tosAction() {
  238. $terms_of_service = file_get_contents(join_path(DATA_PATH, 'tos.html'));
  239. if (!$terms_of_service) {
  240. Minz_Error::error(404);
  241. }
  242. $this->view->terms_of_service = $terms_of_service;
  243. $this->view->can_register = !max_registrations_reached();
  244. Minz_View::prependTitle(_t('index.tos.title') . ' · ');
  245. }
  246. /**
  247. * This action displays logs of FreshRSS for the current user.
  248. */
  249. public function logsAction() {
  250. if (!FreshRSS_Auth::hasAccess()) {
  251. Minz_Error::error(403);
  252. }
  253. Minz_View::prependTitle(_t('index.log.title') . ' · ');
  254. if (Minz_Request::isPost()) {
  255. FreshRSS_LogDAO::truncate();
  256. }
  257. $logs = FreshRSS_LogDAO::lines(); //TODO: ask only the necessary lines
  258. //gestion pagination
  259. $page = Minz_Request::param('page', 1);
  260. $this->view->logsPaginator = new Minz_Paginator($logs);
  261. $this->view->logsPaginator->_nbItemsPerPage(50);
  262. $this->view->logsPaginator->_currentPage($page);
  263. }
  264. }