indexController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * This class handles main actions of FreshRSS.
  4. */
  5. class FreshRSS_index_Controller extends Minz_ActionController {
  6. private $nb_not_read_cat = 0;
  7. public function indexAction() {
  8. $output = Minz_Request::param('output');
  9. $token = FreshRSS_Context::$conf->token;
  10. // check if user is logged in
  11. if (!FreshRSS_Auth::hasAccess() && !Minz_Configuration::allowAnonymous()) {
  12. $token_param = Minz_Request::param('token', '');
  13. $token_is_ok = ($token != '' && $token === $token_param);
  14. if ($output === 'rss' && !$token_is_ok) {
  15. Minz_Error::error(403);
  16. return;
  17. } elseif ($output !== 'rss') {
  18. // "hard" redirection is not required, just ask dispatcher to
  19. // forward to the login form without 302 redirection
  20. Minz_Request::forward(array('c' => 'auth', 'a' => 'login'));
  21. return;
  22. }
  23. }
  24. $params = Minz_Request::params();
  25. if (isset($params['search'])) {
  26. $params['search'] = urlencode($params['search']);
  27. }
  28. $this->view->url = array(
  29. 'c' => 'index',
  30. 'a' => 'index',
  31. 'params' => $params
  32. );
  33. if ($output === 'rss') {
  34. // no layout for RSS output
  35. $this->view->_useLayout(false);
  36. header('Content-Type: application/rss+xml; charset=utf-8');
  37. } elseif ($output === 'global') {
  38. Minz_View::appendScript(Minz_Url::display('/scripts/global_view.js?' . @filemtime(PUBLIC_PATH . '/scripts/global_view.js')));
  39. }
  40. $catDAO = new FreshRSS_CategoryDAO();
  41. $entryDAO = FreshRSS_Factory::createEntryDao();
  42. $this->view->cat_aside = $catDAO->listCategories();
  43. $this->view->nb_favorites = $entryDAO->countUnreadReadFavorites();
  44. $this->view->nb_not_read = FreshRSS_CategoryDAO::CountUnreads($this->view->cat_aside, 1);
  45. $this->view->currentName = '';
  46. $this->view->get_c = '';
  47. $this->view->get_f = '';
  48. $get = Minz_Request::param('get', 'a');
  49. $getType = $get[0];
  50. $getId = substr($get, 2);
  51. if (!$this->checkAndProcessType($getType, $getId)) {
  52. Minz_Log::debug('Not found [' . $getType . '][' . $getId . ']');
  53. Minz_Error::error(
  54. 404,
  55. array('error' => array(_t('page_not_found')))
  56. );
  57. return;
  58. }
  59. // mise à jour des titres
  60. $this->view->rss_title = $this->view->currentName . ' | ' . Minz_View::title();
  61. Minz_View::prependTitle(
  62. ($this->nb_not_read_cat > 0 ? '(' . formatNumber($this->nb_not_read_cat) . ') ' : '') .
  63. $this->view->currentName .
  64. ' · '
  65. );
  66. // On récupère les différents éléments de filtrage
  67. $this->view->state = Minz_Request::param('state', FreshRSS_Context::$conf->default_view);
  68. $state_param = Minz_Request::param('state', null);
  69. $filter = Minz_Request::param('search', '');
  70. $this->view->order = $order = Minz_Request::param('order', FreshRSS_Context::$conf->sort_order);
  71. $nb = Minz_Request::param('nb', FreshRSS_Context::$conf->posts_per_page);
  72. $first = Minz_Request::param('next', '');
  73. $ajax_request = Minz_Request::param('ajax', false);
  74. if ($output === 'reader') {
  75. $nb = max(1, round($nb / 2));
  76. }
  77. if ($this->view->state === FreshRSS_Entry::STATE_NOT_READ) { //Any unread article in this category at all?
  78. switch ($getType) {
  79. case 'a':
  80. $hasUnread = $this->view->nb_not_read > 0;
  81. break;
  82. case 's':
  83. // This is deprecated. The favorite button does not exist anymore
  84. $hasUnread = $this->view->nb_favorites['unread'] > 0;
  85. break;
  86. case 'c':
  87. $hasUnread = (!isset($this->view->cat_aside[$getId]) ||
  88. $this->view->cat_aside[$getId]->nbNotRead() > 0);
  89. break;
  90. case 'f':
  91. $myFeed = FreshRSS_CategoryDAO::findFeed($this->view->cat_aside, $getId);
  92. $hasUnread = ($myFeed === null) || ($myFeed->nbNotRead() > 0);
  93. break;
  94. default:
  95. $hasUnread = true;
  96. break;
  97. }
  98. if (!$hasUnread && ($state_param === null)) {
  99. $this->view->state = FreshRSS_Entry::STATE_ALL;
  100. }
  101. }
  102. $this->view->today = @strtotime('today');
  103. try {
  104. $entries = $entryDAO->listWhere($getType, $getId, $this->view->state, $order, $nb + 1, $first, $filter);
  105. // Si on a récupéré aucun article "non lus"
  106. // on essaye de récupérer tous les articles
  107. if ($this->view->state === FreshRSS_Entry::STATE_NOT_READ && empty($entries) && ($state_param === null) && ($filter == '')) {
  108. Minz_Log::debug('Conflicting information about nbNotRead!');
  109. $feedDAO = FreshRSS_Factory::createFeedDao();
  110. try {
  111. $feedDAO->updateCachedValues();
  112. } catch (Exception $ex) {
  113. Minz_Log::notice('Failed to automatically correct nbNotRead! ' + $ex->getMessage());
  114. }
  115. $this->view->state = FreshRSS_Entry::STATE_ALL;
  116. $entries = $entryDAO->listWhere($getType, $getId, $this->view->state, $order, $nb, $first, $filter);
  117. }
  118. Minz_Request::_param('state', $this->view->state);
  119. if (count($entries) <= $nb) {
  120. $this->view->nextId = '';
  121. } else { //We have more elements for pagination
  122. $lastEntry = array_pop($entries);
  123. $this->view->nextId = $lastEntry->id();
  124. }
  125. $this->view->entries = $entries;
  126. } catch (FreshRSS_EntriesGetter_Exception $e) {
  127. Minz_Log::notice($e->getMessage());
  128. Minz_Error::error(
  129. 404,
  130. array('error' => array(_t('page_not_found')))
  131. );
  132. }
  133. }
  134. /*
  135. * Vérifie que la catégorie / flux sélectionné existe
  136. * + Initialise correctement les variables de vue get_c et get_f
  137. * + Met à jour la variable $this->nb_not_read_cat
  138. */
  139. private function checkAndProcessType($getType, $getId) {
  140. switch($getType) {
  141. case 'a':
  142. $this->view->currentName = _t('your_rss_feeds');
  143. $this->nb_not_read_cat = $this->view->nb_not_read;
  144. $this->view->get_c = $getType;
  145. return true;
  146. case 's':
  147. $this->view->currentName = _t('your_favorites');
  148. $this->nb_not_read_cat = $this->view->nb_favorites['unread'];
  149. $this->view->get_c = $getType;
  150. return true;
  151. case 'c':
  152. $cat = isset($this->view->cat_aside[$getId]) ? $this->view->cat_aside[$getId] : null;
  153. if ($cat === null) {
  154. $catDAO = new FreshRSS_CategoryDAO();
  155. $cat = $catDAO->searchById($getId);
  156. }
  157. if ($cat) {
  158. $this->view->currentName = $cat->name();
  159. $this->nb_not_read_cat = $cat->nbNotRead();
  160. $this->view->get_c = $getId;
  161. return true;
  162. } else {
  163. return false;
  164. }
  165. case 'f':
  166. $feed = FreshRSS_CategoryDAO::findFeed($this->view->cat_aside, $getId);
  167. if (empty($feed)) {
  168. $feedDAO = FreshRSS_Factory::createFeedDao();
  169. $feed = $feedDAO->searchById($getId);
  170. }
  171. if ($feed) {
  172. $this->view->currentName = $feed->name();
  173. $this->nb_not_read_cat = $feed->nbNotRead();
  174. $this->view->get_f = $getId;
  175. $this->view->get_c = $feed->category();
  176. return true;
  177. } else {
  178. return false;
  179. }
  180. default:
  181. return false;
  182. }
  183. }
  184. /**
  185. * This action displays the global view of FreshRSS.
  186. */
  187. public function globalAction() {
  188. if (!FreshRSS_Auth::hasAccess() && !Minz_Configuration::allowAnonymous()) {
  189. Minz_Error::error(403);
  190. }
  191. Minz_View::appendScript(Minz_Url::display('/scripts/global_view.js?' . @filemtime(PUBLIC_PATH . '/scripts/global_view.js')));
  192. $catDAO = new FreshRSS_CategoryDAO();
  193. $this->view->categories = $catDAO->listCategories();
  194. }
  195. /**
  196. * This action displays the about page of FreshRSS.
  197. */
  198. public function aboutAction() {
  199. Minz_View::prependTitle(_t('about') . ' · ');
  200. }
  201. /**
  202. * This action displays logs of FreshRSS for the current user.
  203. */
  204. public function logsAction() {
  205. if (!FreshRSS_Auth::hasAccess()) {
  206. Minz_Error::error(403);
  207. }
  208. Minz_View::prependTitle(_t('logs') . ' · ');
  209. if (Minz_Request::isPost()) {
  210. FreshRSS_LogDAO::truncate();
  211. }
  212. $logs = FreshRSS_LogDAO::lines(); //TODO: ask only the necessary lines
  213. //gestion pagination
  214. $page = Minz_Request::param('page', 1);
  215. $this->view->logsPaginator = new Minz_Paginator($logs);
  216. $this->view->logsPaginator->_nbItemsPerPage(50);
  217. $this->view->logsPaginator->_currentPage($page);
  218. }
  219. }