indexController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. class FreshRSS_index_Controller extends Minz_ActionController {
  3. private $nb_not_read_cat = 0;
  4. public function indexAction() {
  5. $output = Minz_Request::param('output');
  6. $token = $this->view->conf->token;
  7. // check if user is logged in
  8. if (!FreshRSS_Auth::hasAccess() && !Minz_Configuration::allowAnonymous()) {
  9. $token_param = Minz_Request::param('token', '');
  10. $token_is_ok = ($token != '' && $token === $token_param);
  11. if ($output === 'rss' && !$token_is_ok) {
  12. Minz_Error::error(
  13. 403,
  14. array('error' => array(_t('access_denied')))
  15. );
  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' => 'index', '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', $this->view->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', $this->view->conf->sort_order);
  71. $nb = Minz_Request::param('nb', $this->view->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. public function aboutAction() {
  185. Minz_View::prependTitle(_t('about') . ' · ');
  186. }
  187. public function logsAction() {
  188. if (!FreshRSS_Auth::hasAccess()) {
  189. Minz_Error::error(
  190. 403,
  191. array('error' => array(_t('access_denied')))
  192. );
  193. }
  194. Minz_View::prependTitle(_t('logs') . ' · ');
  195. if (Minz_Request::isPost()) {
  196. FreshRSS_LogDAO::truncate();
  197. }
  198. $logs = FreshRSS_LogDAO::lines(); //TODO: ask only the necessary lines
  199. //gestion pagination
  200. $page = Minz_Request::param('page', 1);
  201. $this->view->logsPaginator = new Minz_Paginator($logs);
  202. $this->view->logsPaginator->_nbItemsPerPage(50);
  203. $this->view->logsPaginator->_currentPage($page);
  204. }
  205. /**
  206. * This action handles the login page.
  207. */
  208. public function loginAction() {
  209. if (FreshRSS_Auth::hasAccess()) {
  210. Minz_Request::forward(array('c' => 'index', 'a' => 'index'), true);
  211. }
  212. invalidateHttpCache();
  213. $auth_type = Minz_Configuration::authType();
  214. switch ($auth_type) {
  215. case 'form':
  216. Minz_Request::forward(array('c' => 'index', 'a' => 'formLogin'));
  217. break;
  218. case 'http_auth':
  219. case 'none':
  220. // It should not happened!
  221. Minz_Error::error(404);
  222. default:
  223. // TODO load plugin instead
  224. Minz_Error::error(404);
  225. }
  226. }
  227. /**
  228. *
  229. */
  230. public function formLoginAction() {
  231. if (FreshRSS_Auth::hasAccess()) {
  232. Minz_Request::forward(array('c' => 'index', 'a' => 'index'), true);
  233. }
  234. invalidateHttpCache();
  235. $file_mtime = @filemtime(PUBLIC_PATH . '/scripts/bcrypt.min.js');
  236. Minz_View::appendScript(Minz_Url::display('/scripts/bcrypt.min.js?' . $file_mtime));
  237. if (Minz_Request::isPost()) {
  238. $nonce = Minz_Session::param('nonce');
  239. $username = Minz_Request::param('username', '');
  240. $challenge = Minz_Request::param('challenge', '');
  241. try {
  242. $conf = new FreshRSS_Configuration($username);
  243. } catch(Minz_Exception $e) {
  244. // $username is not a valid user, nor the configuration file!
  245. Minz_Log::warning('Login failure: ' . $e->getMessage());
  246. Minz_Request::bad(_t('invalid_login'),
  247. array('c' => 'index', 'a' => 'login'));
  248. }
  249. $ok = FreshRSS_FormAuth::checkCredentials(
  250. $username, $conf->passwordHash, $nonce, $challenge
  251. );
  252. if ($ok) {
  253. // Set session parameter to give access to the user.
  254. Minz_Session::_param('currentUser', $username);
  255. Minz_Session::_param('passwordHash', $conf->passwordHash);
  256. FreshRSS_Auth::giveAccess();
  257. // Set cookie parameter if nedded.
  258. if (Minz_Request::param('keep_logged_in', false)) {
  259. FreshRSS_FormAuth::makeCookie($username, $conf->passwordHash);
  260. } else {
  261. FreshRSS_FormAuth::deleteCookie();
  262. }
  263. // All is good, go back to the index.
  264. Minz_Request::good(_t('login'),
  265. array('c' => 'index', 'a' => 'index'));
  266. } else {
  267. Minz_Log::warning('Password mismatch for' .
  268. ' user=' . $username .
  269. ', nonce=' . $nonce .
  270. ', c=' . $challenge);
  271. Minz_Request::bad(_t('invalid_login'),
  272. array('c' => 'index', 'a' => 'login'));
  273. }
  274. }
  275. }
  276. public function logoutAction() {
  277. invalidateHttpCache();
  278. FreshRSS_Auth::removeAccess();
  279. Minz_Request::good(_t('disconnected'),
  280. array('c' => 'index', 'a' => 'index'));
  281. }
  282. }