indexController.php 12 KB

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