4
0

indexController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. FreshRSS_Context::$id_max = time() . '000000';
  44. $this->view->callbackBeforeFeeds = function ($view) {
  45. try {
  46. $tagDAO = FreshRSS_Factory::createTagDao();
  47. $view->tags = $tagDAO->listTags(true);
  48. $view->nbUnreadTags = 0;
  49. foreach ($view->tags as $tag) {
  50. $view->nbUnreadTags += $tag->nbUnread();
  51. }
  52. } catch (Exception $e) {
  53. Minz_Log::notice($e->getMessage());
  54. }
  55. };
  56. $this->view->callbackBeforeEntries = function ($view) {
  57. try {
  58. FreshRSS_Context::$number++; //+1 for pagination
  59. $view->entries = FreshRSS_index_Controller::listEntriesByContext();
  60. FreshRSS_Context::$number--;
  61. ob_start(); //Buffer "one entry at a time"
  62. } catch (FreshRSS_EntriesGetter_Exception $e) {
  63. Minz_Log::notice($e->getMessage());
  64. Minz_Error::error(404);
  65. }
  66. };
  67. $this->view->callbackBeforePagination = function ($view, $nbEntries, $lastEntry) {
  68. if ($nbEntries >= FreshRSS_Context::$number) {
  69. //We have enough entries: we discard the last one to use it for the next pagination
  70. ob_clean();
  71. FreshRSS_Context::$next_id = $lastEntry->id();
  72. }
  73. ob_end_flush();
  74. };
  75. }
  76. /**
  77. * This action displays the reader view of FreshRSS.
  78. *
  79. * @todo: change this view into specific CSS rules?
  80. */
  81. public function readerAction() {
  82. $this->normalAction();
  83. }
  84. /**
  85. * This action displays the global view of FreshRSS.
  86. */
  87. public function globalAction() {
  88. $allow_anonymous = FreshRSS_Context::$system_conf->allow_anonymous;
  89. if (!FreshRSS_Auth::hasAccess() && !$allow_anonymous) {
  90. Minz_Request::forward(array('c' => 'auth', 'a' => 'login'));
  91. return;
  92. }
  93. Minz_View::appendScript(Minz_Url::display('/scripts/extra.js?' . @filemtime(PUBLIC_PATH . '/scripts/extra.js')));
  94. Minz_View::appendScript(Minz_Url::display('/scripts/global_view.js?' . @filemtime(PUBLIC_PATH . '/scripts/global_view.js')));
  95. try {
  96. $this->updateContext();
  97. } catch (FreshRSS_Context_Exception $e) {
  98. Minz_Error::error(404);
  99. }
  100. $this->view->categories = FreshRSS_Context::$categories;
  101. $this->view->rss_title = FreshRSS_Context::$name . ' | ' . Minz_View::title();
  102. $title = _t('index.feed.title_global');
  103. if (FreshRSS_Context::$get_unread > 0) {
  104. $title = '(' . FreshRSS_Context::$get_unread . ') ' . $title;
  105. }
  106. Minz_View::prependTitle($title . ' · ');
  107. $this->_csp([
  108. 'default-src' => "'self'",
  109. 'frame-src' => '*',
  110. 'img-src' => '* data:',
  111. 'media-src' => '*',
  112. ]);
  113. }
  114. /**
  115. * This action displays the RSS feed of FreshRSS.
  116. */
  117. public function rssAction() {
  118. $allow_anonymous = FreshRSS_Context::$system_conf->allow_anonymous;
  119. $token = FreshRSS_Context::$user_conf->token;
  120. $token_param = Minz_Request::param('token', '');
  121. $token_is_ok = ($token != '' && $token === $token_param);
  122. // Check if user has access.
  123. if (!FreshRSS_Auth::hasAccess() &&
  124. !$allow_anonymous &&
  125. !$token_is_ok) {
  126. Minz_Error::error(403);
  127. }
  128. try {
  129. $this->updateContext();
  130. } catch (FreshRSS_Context_Exception $e) {
  131. Minz_Error::error(404);
  132. }
  133. try {
  134. $this->view->entries = FreshRSS_index_Controller::listEntriesByContext();
  135. } catch (FreshRSS_EntriesGetter_Exception $e) {
  136. Minz_Log::notice($e->getMessage());
  137. Minz_Error::error(404);
  138. }
  139. // No layout for RSS output.
  140. $this->view->url = PUBLIC_TO_INDEX_PATH . '/' . (empty($_SERVER['QUERY_STRING']) ? '' : '?' . $_SERVER['QUERY_STRING']);
  141. $this->view->rss_title = FreshRSS_Context::$name . ' | ' . Minz_View::title();
  142. $this->view->_layout(false);
  143. header('Content-Type: application/rss+xml; charset=utf-8');
  144. }
  145. /**
  146. * This action updates the Context object by using request parameters.
  147. *
  148. * Parameters are:
  149. * - state (default: conf->default_view)
  150. * - search (default: empty string)
  151. * - order (default: conf->sort_order)
  152. * - nb (default: conf->posts_per_page)
  153. * - next (default: empty string)
  154. * - hours (default: 0)
  155. */
  156. private function updateContext() {
  157. if (empty(FreshRSS_Context::$categories)) {
  158. $catDAO = FreshRSS_Factory::createCategoryDao();
  159. FreshRSS_Context::$categories = $catDAO->listSortedCategories();
  160. }
  161. // Update number of read / unread variables.
  162. $entryDAO = FreshRSS_Factory::createEntryDao();
  163. FreshRSS_Context::$total_starred = $entryDAO->countUnreadReadFavorites();
  164. FreshRSS_Context::$total_unread = FreshRSS_CategoryDAO::CountUnreads(
  165. FreshRSS_Context::$categories, 1
  166. );
  167. FreshRSS_Context::_get(Minz_Request::param('get', 'a'));
  168. FreshRSS_Context::$state = Minz_Request::param(
  169. 'state', FreshRSS_Context::$user_conf->default_state
  170. );
  171. $state_forced_by_user = Minz_Request::param('state', false) !== false;
  172. if (FreshRSS_Context::$user_conf->default_view === 'adaptive' &&
  173. FreshRSS_Context::$get_unread <= 0 &&
  174. !FreshRSS_Context::isStateEnabled(FreshRSS_Entry::STATE_READ) &&
  175. !$state_forced_by_user) {
  176. FreshRSS_Context::$state |= FreshRSS_Entry::STATE_READ;
  177. }
  178. FreshRSS_Context::$search = new FreshRSS_BooleanSearch(Minz_Request::param('search', ''));
  179. FreshRSS_Context::$order = Minz_Request::param(
  180. 'order', FreshRSS_Context::$user_conf->sort_order
  181. );
  182. FreshRSS_Context::$number = intval(Minz_Request::param('nb', FreshRSS_Context::$user_conf->posts_per_page));
  183. if (FreshRSS_Context::$number > FreshRSS_Context::$user_conf->max_posts_per_rss) {
  184. FreshRSS_Context::$number = max(
  185. FreshRSS_Context::$user_conf->max_posts_per_rss,
  186. FreshRSS_Context::$user_conf->posts_per_page);
  187. }
  188. FreshRSS_Context::$first_id = Minz_Request::param('next', '');
  189. FreshRSS_Context::$sinceHours = intval(Minz_Request::param('hours', 0));
  190. }
  191. /**
  192. * This method returns a list of entries based on the Context object.
  193. */
  194. public static function listEntriesByContext() {
  195. $entryDAO = FreshRSS_Factory::createEntryDao();
  196. $get = FreshRSS_Context::currentGet(true);
  197. if (is_array($get)) {
  198. $type = $get[0];
  199. $id = $get[1];
  200. } else {
  201. $type = $get;
  202. $id = '';
  203. }
  204. $limit = FreshRSS_Context::$number;
  205. $date_min = 0;
  206. if (FreshRSS_Context::$sinceHours) {
  207. $date_min = time() - (FreshRSS_Context::$sinceHours * 3600);
  208. $limit = FreshRSS_Context::$user_conf->max_posts_per_rss;
  209. }
  210. foreach ($entryDAO->listWhere(
  211. $type, $id, FreshRSS_Context::$state, FreshRSS_Context::$order,
  212. $limit, FreshRSS_Context::$first_id,
  213. FreshRSS_Context::$search, $date_min)
  214. as $entry) {
  215. yield $entry;
  216. }
  217. }
  218. /**
  219. * This action displays the about page of FreshRSS.
  220. */
  221. public function aboutAction() {
  222. Minz_View::prependTitle(_t('index.about.title') . ' · ');
  223. }
  224. /**
  225. * This action displays the EULA page of FreshRSS.
  226. * This page is enabled only if admin created a data/tos.html file.
  227. * The content of the page is the content of data/tos.html.
  228. * It returns 404 if there is no EULA.
  229. */
  230. public function tosAction() {
  231. $terms_of_service = file_get_contents(join_path(DATA_PATH, 'tos.html'));
  232. if (!$terms_of_service) {
  233. Minz_Error::error(404);
  234. }
  235. $this->view->terms_of_service = $terms_of_service;
  236. $this->view->can_register = !max_registrations_reached();
  237. Minz_View::prependTitle(_t('index.tos.title') . ' · ');
  238. }
  239. /**
  240. * This action displays logs of FreshRSS for the current user.
  241. */
  242. public function logsAction() {
  243. if (!FreshRSS_Auth::hasAccess()) {
  244. Minz_Error::error(403);
  245. }
  246. Minz_View::prependTitle(_t('index.log.title') . ' · ');
  247. if (Minz_Request::isPost()) {
  248. FreshRSS_LogDAO::truncate();
  249. }
  250. $logs = FreshRSS_LogDAO::lines(); //TODO: ask only the necessary lines
  251. //gestion pagination
  252. $page = Minz_Request::param('page', 1);
  253. $this->view->logsPaginator = new Minz_Paginator($logs);
  254. $this->view->logsPaginator->_nbItemsPerPage(50);
  255. $this->view->logsPaginator->_currentPage($page);
  256. }
  257. }