indexController.php 12 KB

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