indexController.php 11 KB

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