indexController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 (!$this->view->loginOk && !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 (Minz_Translate::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' => 'formLogin'));
  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::record ('Not found [' . $getType . '][' . $getId . ']', Minz_Log::DEBUG);
  53. Minz_Error::error (
  54. 404,
  55. array ('error' => array (Minz_Translate::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. if ($this->view->nb_not_read > 0) {
  62. Minz_View::prependTitle('(' . formatNumber($this->view->nb_not_read) . ') ');
  63. }
  64. Minz_View::prependTitle(
  65. ($this->nb_not_read_cat > 0 ? '(' . formatNumber($this->nb_not_read_cat) . ') ' : '') .
  66. $this->view->currentName .
  67. ' · '
  68. );
  69. // On récupère les différents éléments de filtrage
  70. $this->view->state = $state = Minz_Request::param ('state', $this->view->conf->default_view);
  71. $state_param = Minz_Request::param ('state', null);
  72. $filter = Minz_Request::param ('search', '');
  73. $this->view->order = $order = Minz_Request::param ('order', $this->view->conf->sort_order);
  74. $nb = Minz_Request::param ('nb', $this->view->conf->posts_per_page);
  75. $first = Minz_Request::param ('next', '');
  76. if ($state === FreshRSS_Entry::STATE_NOT_READ) { //Any unread article in this category at all?
  77. switch ($getType) {
  78. case 'a':
  79. $hasUnread = $this->view->nb_not_read > 0;
  80. break;
  81. case 's':
  82. // This is deprecated. The favorite button does not exist anymore
  83. $hasUnread = $this->view->nb_favorites['unread'] > 0;
  84. break;
  85. case 'c':
  86. $hasUnread = (!isset($this->view->cat_aside[$getId])) || ($this->view->cat_aside[$getId]->nbNotRead() > 0);
  87. break;
  88. case 'f':
  89. $myFeed = FreshRSS_CategoryDAO::findFeed($this->view->cat_aside, $getId);
  90. $hasUnread = ($myFeed === null) || ($myFeed->nbNotRead() > 0);
  91. break;
  92. default:
  93. $hasUnread = true;
  94. break;
  95. }
  96. if (!$hasUnread && ($state_param === null)) {
  97. $this->view->state = $state = FreshRSS_Entry::STATE_ALL;
  98. }
  99. }
  100. $today = @strtotime('today');
  101. $this->view->today = $today;
  102. // on calcule la date des articles les plus anciens qu'on affiche
  103. $nb_month_old = $this->view->conf->old_entries;
  104. $date_min = $today - (3600 * 24 * 30 * $nb_month_old); //Do not use a fast changing value such as time() to allow SQL caching
  105. $keepHistoryDefault = $this->view->conf->keep_history_default;
  106. try {
  107. $entries = $entryDAO->listWhere($getType, $getId, $state, $order, $nb + 1, $first, $filter, $date_min, true, $keepHistoryDefault);
  108. // Si on a récupéré aucun article "non lus"
  109. // on essaye de récupérer tous les articles
  110. if ($state === FreshRSS_Entry::STATE_NOT_READ && empty($entries) && ($state_param === null) && ($filter == '')) {
  111. Minz_Log::record('Conflicting information about nbNotRead!', Minz_Log::DEBUG);
  112. $feedDAO = FreshRSS_Factory::createFeedDao();
  113. try {
  114. $feedDAO->updateCachedValues();
  115. } catch (Exception $ex) {
  116. Minz_Log::record('Failed to automatically correct nbNotRead! ' + $ex->getMessage(), Minz_Log::NOTICE);
  117. }
  118. $this->view->state = FreshRSS_Entry::STATE_ALL;
  119. $entries = $entryDAO->listWhere($getType, $getId, $this->view->state, $order, $nb, $first, $filter, $date_min, true, $keepHistoryDefault);
  120. }
  121. if (count($entries) <= $nb) {
  122. $this->view->nextId = '';
  123. } else { //We have more elements for pagination
  124. $lastEntry = array_pop($entries);
  125. $this->view->nextId = $lastEntry->id();
  126. }
  127. $this->view->entries = $entries;
  128. } catch (FreshRSS_EntriesGetter_Exception $e) {
  129. Minz_Log::record ($e->getMessage (), Minz_Log::NOTICE);
  130. Minz_Error::error (
  131. 404,
  132. array ('error' => array (Minz_Translate::t ('page_not_found')))
  133. );
  134. }
  135. }
  136. /*
  137. * Vérifie que la catégorie / flux sélectionné existe
  138. * + Initialise correctement les variables de vue get_c et get_f
  139. * + Met à jour la variable $this->nb_not_read_cat
  140. */
  141. private function checkAndProcessType ($getType, $getId) {
  142. switch ($getType) {
  143. case 'a':
  144. $this->view->currentName = Minz_Translate::t ('your_rss_feeds');
  145. $this->nb_not_read_cat = $this->view->nb_not_read;
  146. $this->view->get_c = $getType;
  147. return true;
  148. case 's':
  149. $this->view->currentName = Minz_Translate::t ('your_favorites');
  150. $this->nb_not_read_cat = $this->view->nb_favorites['unread'];
  151. $this->view->get_c = $getType;
  152. return true;
  153. case 'c':
  154. $cat = isset($this->view->cat_aside[$getId]) ? $this->view->cat_aside[$getId] : null;
  155. if ($cat === null) {
  156. $catDAO = new FreshRSS_CategoryDAO();
  157. $cat = $catDAO->searchById($getId);
  158. }
  159. if ($cat) {
  160. $this->view->currentName = $cat->name ();
  161. $this->nb_not_read_cat = $cat->nbNotRead ();
  162. $this->view->get_c = $getId;
  163. return true;
  164. } else {
  165. return false;
  166. }
  167. case 'f':
  168. $feed = FreshRSS_CategoryDAO::findFeed($this->view->cat_aside, $getId);
  169. if (empty($feed)) {
  170. $feedDAO = FreshRSS_Factory::createFeedDao();
  171. $feed = $feedDAO->searchById($getId);
  172. }
  173. if ($feed) {
  174. $this->view->currentName = $feed->name ();
  175. $this->nb_not_read_cat = $feed->nbNotRead ();
  176. $this->view->get_f = $getId;
  177. $this->view->get_c = $feed->category ();
  178. return true;
  179. } else {
  180. return false;
  181. }
  182. default:
  183. return false;
  184. }
  185. }
  186. public function aboutAction () {
  187. Minz_View::prependTitle (Minz_Translate::t ('about') . ' · ');
  188. }
  189. public function logsAction () {
  190. if (!$this->view->loginOk) {
  191. Minz_Error::error (
  192. 403,
  193. array ('error' => array (Minz_Translate::t ('access_denied')))
  194. );
  195. }
  196. Minz_View::prependTitle (Minz_Translate::t ('logs') . ' · ');
  197. if (Minz_Request::isPost ()) {
  198. FreshRSS_LogDAO::truncate();
  199. }
  200. $logs = FreshRSS_LogDAO::lines(); //TODO: ask only the necessary lines
  201. //gestion pagination
  202. $page = Minz_Request::param ('page', 1);
  203. $this->view->logsPaginator = new Minz_Paginator ($logs);
  204. $this->view->logsPaginator->_nbItemsPerPage (50);
  205. $this->view->logsPaginator->_currentPage ($page);
  206. }
  207. public function loginAction () {
  208. $this->view->_useLayout (false);
  209. $url = 'https://verifier.login.persona.org/verify';
  210. $assert = Minz_Request::param ('assertion');
  211. $params = 'assertion=' . $assert . '&audience=' .
  212. urlencode (Minz_Url::display (null, 'php', true));
  213. $ch = curl_init ();
  214. $options = array (
  215. CURLOPT_URL => $url,
  216. CURLOPT_RETURNTRANSFER => TRUE,
  217. CURLOPT_POST => 2,
  218. CURLOPT_POSTFIELDS => $params
  219. );
  220. curl_setopt_array ($ch, $options);
  221. $result = curl_exec ($ch);
  222. curl_close ($ch);
  223. $res = json_decode ($result, true);
  224. $loginOk = false;
  225. $reason = '';
  226. if ($res['status'] === 'okay') {
  227. $email = filter_var($res['email'], FILTER_VALIDATE_EMAIL);
  228. if ($email != '') {
  229. $personaFile = DATA_PATH . '/persona/' . $email . '.txt';
  230. if (($currentUser = @file_get_contents($personaFile)) !== false) {
  231. $currentUser = trim($currentUser);
  232. if (ctype_alnum($currentUser)) {
  233. try {
  234. $this->conf = new FreshRSS_Configuration($currentUser);
  235. $loginOk = strcasecmp($email, $this->conf->mail_login) === 0;
  236. } catch (Minz_Exception $e) {
  237. $reason = 'Invalid configuration for user [' . $currentUser . ']! ' . $e->getMessage(); //Permission denied or conf file does not exist
  238. }
  239. } else {
  240. $reason = 'Invalid username format [' . $currentUser . ']!';
  241. }
  242. }
  243. } else {
  244. $reason = 'Invalid email format [' . $res['email'] . ']!';
  245. }
  246. }
  247. if ($loginOk) {
  248. Minz_Session::_param('currentUser', $currentUser);
  249. Minz_Session::_param ('mail', $email);
  250. $this->view->loginOk = true;
  251. invalidateHttpCache();
  252. } else {
  253. $res = array ();
  254. $res['status'] = 'failure';
  255. $res['reason'] = $reason == '' ? Minz_Translate::t ('invalid_login') : $reason;
  256. Minz_Log::record ('Persona: ' . $res['reason'], Minz_Log::WARNING);
  257. }
  258. header('Content-Type: application/json; charset=UTF-8');
  259. $this->view->res = json_encode ($res);
  260. }
  261. public function logoutAction () {
  262. $this->view->_useLayout(false);
  263. invalidateHttpCache();
  264. Minz_Session::_param('currentUser');
  265. Minz_Session::_param('mail');
  266. Minz_Session::_param('passwordHash');
  267. }
  268. public function formLoginAction () {
  269. if (Minz_Request::isPost()) {
  270. $ok = false;
  271. $nonce = Minz_Session::param('nonce');
  272. $username = Minz_Request::param('username', '');
  273. $c = Minz_Request::param('challenge', '');
  274. if (ctype_alnum($username) && ctype_graph($c) && ctype_alnum($nonce)) {
  275. if (!function_exists('password_verify')) {
  276. include_once(LIB_PATH . '/password_compat.php');
  277. }
  278. try {
  279. $conf = new FreshRSS_Configuration($username);
  280. $s = $conf->passwordHash;
  281. $ok = password_verify($nonce . $s, $c);
  282. if ($ok) {
  283. Minz_Session::_param('currentUser', $username);
  284. Minz_Session::_param('passwordHash', $s);
  285. } else {
  286. Minz_Log::record('Password mismatch for user ' . $username . ', nonce=' . $nonce . ', c=' . $c, Minz_Log::WARNING);
  287. }
  288. } catch (Minz_Exception $me) {
  289. Minz_Log::record('Login failure: ' . $me->getMessage(), Minz_Log::WARNING);
  290. }
  291. } else {
  292. Minz_Log::record('Invalid credential parameters: user=' . $username . ' challenge=' . $c . ' nonce=' . $nonce, Minz_Log::DEBUG);
  293. }
  294. if (!$ok) {
  295. $notif = array(
  296. 'type' => 'bad',
  297. 'content' => Minz_Translate::t('invalid_login')
  298. );
  299. Minz_Session::_param('notification', $notif);
  300. }
  301. $this->view->_useLayout(false);
  302. Minz_Request::forward(array('c' => 'index', 'a' => 'index'), true);
  303. } elseif (Minz_Configuration::unsafeAutologinEnabled() && isset($_GET['u']) && isset($_GET['p'])) {
  304. Minz_Session::_param('currentUser');
  305. Minz_Session::_param('mail');
  306. Minz_Session::_param('passwordHash');
  307. $username = ctype_alnum($_GET['u']) ? $_GET['u'] : '';
  308. $passwordPlain = $_GET['p'];
  309. Minz_Request::_param('p'); //Discard plain-text password ASAP
  310. $_GET['p'] = '';
  311. if (!function_exists('password_verify')) {
  312. include_once(LIB_PATH . '/password_compat.php');
  313. }
  314. try {
  315. $conf = new FreshRSS_Configuration($username);
  316. $s = $conf->passwordHash;
  317. $ok = password_verify($passwordPlain, $s);
  318. unset($passwordPlain);
  319. if ($ok) {
  320. Minz_Session::_param('currentUser', $username);
  321. Minz_Session::_param('passwordHash', $s);
  322. } else {
  323. Minz_Log::record('Unsafe password mismatch for user ' . $username, Minz_Log::WARNING);
  324. }
  325. } catch (Minz_Exception $me) {
  326. Minz_Log::record('Unsafe login failure: ' . $me->getMessage(), Minz_Log::WARNING);
  327. }
  328. Minz_Request::forward(array('c' => 'index', 'a' => 'index'), true);
  329. } elseif (!Minz_Configuration::canLogIn()) {
  330. Minz_Error::error (
  331. 403,
  332. array ('error' => array (Minz_Translate::t ('access_denied')))
  333. );
  334. }
  335. invalidateHttpCache();
  336. }
  337. public function formLogoutAction () {
  338. $this->view->_useLayout(false);
  339. invalidateHttpCache();
  340. Minz_Session::_param('currentUser');
  341. Minz_Session::_param('mail');
  342. Minz_Session::_param('passwordHash');
  343. Minz_Request::forward(array('c' => 'index', 'a' => 'index'), true);
  344. }
  345. }