indexController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. return;
  13. }
  14. }
  15. // construction of RSS url of this feed
  16. $params = Minz_Request::params ();
  17. $params['output'] = 'rss';
  18. if (isset ($params['search'])) {
  19. $params['search'] = urlencode ($params['search']);
  20. }
  21. if (!Minz_Configuration::allowAnonymous()) {
  22. $params['token'] = $token;
  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->nb_not_read = FreshRSS_CategoryDAO::CountUnreads($this->view->cat_aside, 1);
  41. $this->view->currentName = '';
  42. $this->view->get_c = '';
  43. $this->view->get_f = '';
  44. $get = Minz_Request::param ('get', 'a');
  45. $getType = $get[0];
  46. $getId = substr ($get, 2);
  47. if (!$this->checkAndProcessType ($getType, $getId)) {
  48. Minz_Log::record ('Not found [' . $getType . '][' . $getId . ']', Minz_Log::DEBUG);
  49. Minz_Error::error (
  50. 404,
  51. array ('error' => array (Minz_Translate::t ('page_not_found')))
  52. );
  53. return;
  54. }
  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->nb_not_read_cat = $this->view->nb_not_read;
  137. $this->view->get_c = $getType;
  138. return true;
  139. case 's':
  140. $this->view->currentName = Minz_Translate::t ('your_favorites');
  141. $this->nb_not_read_cat = $this->view->nb_favorites['unread'];
  142. $this->view->get_c = $getType;
  143. return true;
  144. case 'c':
  145. $cat = isset($this->view->cat_aside[$getId]) ? $this->view->cat_aside[$getId] : null;
  146. if ($cat === null) {
  147. $catDAO = new FreshRSS_CategoryDAO();
  148. $cat = $catDAO->searchById($getId);
  149. }
  150. if ($cat) {
  151. $this->view->currentName = $cat->name ();
  152. $this->nb_not_read_cat = $cat->nbNotRead ();
  153. $this->view->get_c = $getId;
  154. return true;
  155. } else {
  156. return false;
  157. }
  158. case 'f':
  159. $feed = FreshRSS_CategoryDAO::findFeed($this->view->cat_aside, $getId);
  160. if (empty($feed)) {
  161. $feedDAO = new FreshRSS_FeedDAO();
  162. $feed = $feedDAO->searchById($getId);
  163. }
  164. if ($feed) {
  165. $this->view->currentName = $feed->name ();
  166. $this->nb_not_read_cat = $feed->nbNotRead ();
  167. $this->view->get_f = $getId;
  168. $this->view->get_c = $feed->category ();
  169. return true;
  170. } else {
  171. return false;
  172. }
  173. default:
  174. return false;
  175. }
  176. }
  177. public function statsAction () {
  178. if (!$this->view->loginOk) {
  179. Minz_Error::error (
  180. 403,
  181. array ('error' => array (Minz_Translate::t ('access_denied')))
  182. );
  183. }
  184. Minz_View::prependTitle (Minz_Translate::t ('stats') . ' · ');
  185. $statsDAO = new FreshRSS_StatsDAO ();
  186. Minz_View::appendScript (Minz_Url::display ('/scripts/flotr2.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/flotr2.min.js')));
  187. $this->view->repartition = $statsDAO->calculateEntryRepartition();
  188. $this->view->count = ($statsDAO->calculateEntryCount());
  189. $this->view->feedByCategory = $statsDAO->calculateFeedByCategory();
  190. $this->view->entryByCategory = $statsDAO->calculateEntryByCategory();
  191. $this->view->topFeed = $statsDAO->calculateTopFeed();
  192. }
  193. public function aboutAction () {
  194. Minz_View::prependTitle (Minz_Translate::t ('about') . ' · ');
  195. }
  196. public function logsAction () {
  197. if (!$this->view->loginOk) {
  198. Minz_Error::error (
  199. 403,
  200. array ('error' => array (Minz_Translate::t ('access_denied')))
  201. );
  202. }
  203. Minz_View::prependTitle (Minz_Translate::t ('logs') . ' · ');
  204. if (Minz_Request::isPost ()) {
  205. FreshRSS_LogDAO::truncate();
  206. }
  207. $logs = FreshRSS_LogDAO::lines(); //TODO: ask only the necessary lines
  208. //gestion pagination
  209. $page = Minz_Request::param ('page', 1);
  210. $this->view->logsPaginator = new Minz_Paginator ($logs);
  211. $this->view->logsPaginator->_nbItemsPerPage (50);
  212. $this->view->logsPaginator->_currentPage ($page);
  213. }
  214. public function loginAction () {
  215. $this->view->_useLayout (false);
  216. $url = 'https://verifier.login.persona.org/verify';
  217. $assert = Minz_Request::param ('assertion');
  218. $params = 'assertion=' . $assert . '&audience=' .
  219. urlencode (Minz_Url::display (null, 'php', true));
  220. $ch = curl_init ();
  221. $options = array (
  222. CURLOPT_URL => $url,
  223. CURLOPT_RETURNTRANSFER => TRUE,
  224. CURLOPT_POST => 2,
  225. CURLOPT_POSTFIELDS => $params
  226. );
  227. curl_setopt_array ($ch, $options);
  228. $result = curl_exec ($ch);
  229. curl_close ($ch);
  230. $res = json_decode ($result, true);
  231. $loginOk = false;
  232. $reason = '';
  233. if ($res['status'] === 'okay') {
  234. $email = filter_var($res['email'], FILTER_VALIDATE_EMAIL);
  235. if ($email != '') {
  236. $personaFile = DATA_PATH . '/persona/' . $email . '.txt';
  237. if (($currentUser = @file_get_contents($personaFile)) !== false) {
  238. $currentUser = trim($currentUser);
  239. if (ctype_alnum($currentUser)) {
  240. try {
  241. $this->conf = new FreshRSS_Configuration($currentUser);
  242. $loginOk = strcasecmp($email, $this->conf->mail_login) === 0;
  243. } catch (Minz_Exception $e) {
  244. $reason = 'Invalid configuration for user [' . $currentUser . ']! ' . $e->getMessage(); //Permission denied or conf file does not exist
  245. }
  246. } else {
  247. $reason = 'Invalid username format [' . $currentUser . ']!';
  248. }
  249. }
  250. } else {
  251. $reason = 'Invalid email format [' . $res['email'] . ']!';
  252. }
  253. }
  254. if ($loginOk) {
  255. Minz_Session::_param('currentUser', $currentUser);
  256. Minz_Session::_param ('mail', $email);
  257. $this->view->loginOk = true;
  258. invalidateHttpCache();
  259. } else {
  260. $res = array ();
  261. $res['status'] = 'failure';
  262. $res['reason'] = $reason == '' ? Minz_Translate::t ('invalid_login') : $reason;
  263. Minz_Log::record ('Persona: ' . $res['reason'], Minz_Log::WARNING);
  264. }
  265. header('Content-Type: application/json; charset=UTF-8');
  266. $this->view->res = json_encode ($res);
  267. }
  268. public function logoutAction () {
  269. $this->view->_useLayout(false);
  270. invalidateHttpCache();
  271. Minz_Session::_param('currentUser');
  272. Minz_Session::_param('mail');
  273. Minz_Session::_param('passwordHash');
  274. }
  275. public function formLoginAction () {
  276. if (Minz_Request::isPost()) {
  277. $ok = false;
  278. $nonce = Minz_Session::param('nonce');
  279. $username = Minz_Request::param('username', '');
  280. $c = Minz_Request::param('challenge', '');
  281. if (ctype_alnum($username) && ctype_graph($c) && ctype_alnum($nonce)) {
  282. if (!function_exists('password_verify')) {
  283. include_once(LIB_PATH . '/password_compat.php');
  284. }
  285. try {
  286. $conf = new FreshRSS_Configuration($username);
  287. $s = $conf->passwordHash;
  288. $ok = password_verify($nonce . $s, $c);
  289. if ($ok) {
  290. Minz_Session::_param('currentUser', $username);
  291. Minz_Session::_param('passwordHash', $s);
  292. } else {
  293. Minz_Log::record('Password mismatch for user ' . $username . ', nonce=' . $nonce . ', c=' . $c, Minz_Log::WARNING);
  294. }
  295. } catch (Minz_Exception $me) {
  296. Minz_Log::record('Login failure: ' . $me->getMessage(), Minz_Log::WARNING);
  297. }
  298. } else {
  299. Minz_Log::record('Invalid credential parameters: user=' . $username . ' challenge=' . $c . ' nonce=' . $nonce, Minz_Log::DEBUG);
  300. }
  301. if (!$ok) {
  302. $notif = array(
  303. 'type' => 'bad',
  304. 'content' => Minz_Translate::t('invalid_login')
  305. );
  306. Minz_Session::_param('notification', $notif);
  307. }
  308. $this->view->_useLayout(false);
  309. Minz_Request::forward(array('c' => 'index', 'a' => 'index'), true);
  310. }
  311. invalidateHttpCache();
  312. }
  313. public function formLogoutAction () {
  314. $this->view->_useLayout(false);
  315. invalidateHttpCache();
  316. Minz_Session::_param('currentUser');
  317. Minz_Session::_param('mail');
  318. Minz_Session::_param('passwordHash');
  319. Minz_Request::forward(array('c' => 'index', 'a' => 'index'), true);
  320. }
  321. }