indexController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. class FreshRSS_index_Controller extends Minz_ActionController {
  3. private $get = false;
  4. private $nb_not_read_cat = 0;
  5. private $entryDAO;
  6. private $feedDAO;
  7. private $catDAO;
  8. private $statsDAO;
  9. function __construct($router) {
  10. parent::__construct($router);
  11. $this->entryDAO = new FreshRSS_EntryDAO ();
  12. $this->feedDAO = new FreshRSS_FeedDAO ();
  13. $this->catDAO = new FreshRSS_CategoryDAO ();
  14. $this->statsDAO = new FreshRSS_StatsDAO ();
  15. }
  16. public function indexAction () {
  17. $output = Minz_Request::param ('output');
  18. $token = '';
  19. // check if user is logged in
  20. if (!$this->view->loginOk && !Minz_Configuration::allowAnonymous())
  21. {
  22. $token = $this->view->conf->token;
  23. $token_param = Minz_Request::param ('token', '');
  24. $token_is_ok = ($token != '' && $token === $token_param);
  25. if (!($output === 'rss' && $token_is_ok)) {
  26. return;
  27. }
  28. $params['token'] = $token;
  29. }
  30. // construction of RSS url of this feed
  31. $params = Minz_Request::params ();
  32. $params['output'] = 'rss';
  33. if (isset ($params['search'])) {
  34. $params['search'] = urlencode ($params['search']);
  35. }
  36. $this->view->rss_url = array (
  37. 'c' => 'index',
  38. 'a' => 'index',
  39. 'params' => $params
  40. );
  41. if ($output === 'rss') {
  42. // no layout for RSS output
  43. $this->view->_useLayout (false);
  44. header('Content-Type: application/rss+xml; charset=utf-8');
  45. } elseif ($output === 'global') {
  46. Minz_View::appendScript (Minz_Url::display ('/scripts/global_view.js?' . @filemtime(PUBLIC_PATH . '/scripts/global_view.js')));
  47. }
  48. $this->view->cat_aside = $this->catDAO->listCategories ();
  49. $this->view->nb_favorites = $this->entryDAO->countUnreadReadFavorites ();
  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. $this->view->nb_not_read = FreshRSS_CategoryDAO::CountUnreads($this->view->cat_aside, 1);
  65. // mise à jour des titres
  66. $this->view->rss_title = $this->view->currentName . ' | ' . Minz_View::title();
  67. if ($this->view->nb_not_read > 0) {
  68. Minz_View::appendTitle (' (' . $this->view->nb_not_read . ')');
  69. }
  70. Minz_View::prependTitle (
  71. $this->view->currentName .
  72. ($this->nb_not_read_cat > 0 ? ' (' . $this->nb_not_read_cat . ')' : '') .
  73. ' · '
  74. );
  75. // On récupère les différents éléments de filtrage
  76. $this->view->state = $state = Minz_Request::param ('state', $this->view->conf->default_view);
  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) {
  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 = $this->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)) {
  118. Minz_Log::record ('Conflicting information about nbNotRead!', Minz_Log::DEBUG);
  119. $this->view->state = 'all';
  120. $entries = $this->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->view->get_c = $getType;
  147. return true;
  148. case 's':
  149. $this->view->currentName = Minz_Translate::t ('your_favorites');
  150. $this->view->get_c = $getType;
  151. return true;
  152. case 'c':
  153. $cat = isset($this->view->cat_aside[$getId]) ? $this->view->cat_aside[$getId] : null;
  154. if ($cat === null) {
  155. $cat = $this->catDAO->searchById ($getId);
  156. }
  157. if ($cat) {
  158. $this->view->currentName = $cat->name ();
  159. $this->nb_not_read_cat = $cat->nbNotRead ();
  160. $this->view->get_c = $getId;
  161. return true;
  162. } else {
  163. return false;
  164. }
  165. case 'f':
  166. $feed = FreshRSS_CategoryDAO::findFeed($this->view->cat_aside, $getId);
  167. if (empty($feed)) {
  168. $feed = $this->feedDAO->searchById ($getId);
  169. }
  170. if ($feed) {
  171. $this->view->currentName = $feed->name ();
  172. $this->nb_not_read_cat = $feed->nbNotRead ();
  173. $this->view->get_f = $getId;
  174. $this->view->get_c = $feed->category ();
  175. return true;
  176. } else {
  177. return false;
  178. }
  179. default:
  180. return false;
  181. }
  182. }
  183. public function statsAction () {
  184. Minz_View::appendScript (Minz_Url::display ('/scripts/flotr2.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/flotr2.min.js')));
  185. $this->view->repartition = $this->statsDAO->calculateEntryRepartition();
  186. $this->view->count = ($this->statsDAO->calculateEntryCount());
  187. $this->view->feedByCategory = $this->statsDAO->calculateFeedByCategory();
  188. $this->view->entryByCategory = $this->statsDAO->calculateEntryByCategory();
  189. }
  190. public function aboutAction () {
  191. Minz_View::prependTitle (Minz_Translate::t ('about') . ' · ');
  192. }
  193. public function logsAction () {
  194. if (!$this->view->loginOk) {
  195. Minz_Error::error (
  196. 403,
  197. array ('error' => array (Minz_Translate::t ('access_denied')))
  198. );
  199. }
  200. Minz_View::prependTitle (Minz_Translate::t ('logs') . ' · ');
  201. if (Minz_Request::isPost ()) {
  202. FreshRSS_LogDAO::truncate();
  203. }
  204. $logs = FreshRSS_LogDAO::lines(); //TODO: ask only the necessary lines
  205. //gestion pagination
  206. $page = Minz_Request::param ('page', 1);
  207. $this->view->logsPaginator = new Minz_Paginator ($logs);
  208. $this->view->logsPaginator->_nbItemsPerPage (50);
  209. $this->view->logsPaginator->_currentPage ($page);
  210. }
  211. public function loginAction () {
  212. $this->view->_useLayout (false);
  213. $url = 'https://verifier.login.persona.org/verify';
  214. $assert = Minz_Request::param ('assertion');
  215. $params = 'assertion=' . $assert . '&audience=' .
  216. urlencode (Minz_Url::display (null, 'php', true));
  217. $ch = curl_init ();
  218. $options = array (
  219. CURLOPT_URL => $url,
  220. CURLOPT_RETURNTRANSFER => TRUE,
  221. CURLOPT_POST => 2,
  222. CURLOPT_POSTFIELDS => $params
  223. );
  224. curl_setopt_array ($ch, $options);
  225. $result = curl_exec ($ch);
  226. curl_close ($ch);
  227. $res = json_decode ($result, true);
  228. $loginOk = false;
  229. $reason = '';
  230. if ($res['status'] === 'okay') {
  231. $email = filter_var($res['email'], FILTER_VALIDATE_EMAIL);
  232. if ($email != '') {
  233. $personaFile = DATA_PATH . '/persona/' . $email . '.txt';
  234. if (($currentUser = @file_get_contents($personaFile)) !== false) {
  235. $currentUser = trim($currentUser);
  236. if (ctype_alnum($currentUser)) {
  237. try {
  238. $this->conf = new FreshRSS_Configuration($currentUser);
  239. $loginOk = strcasecmp($email, $this->conf->mail_login) === 0;
  240. } catch (Minz_Exception $e) {
  241. $reason = 'Invalid configuration for user [' . $currentUser . ']! ' . $e->getMessage(); //Permission denied or conf file does not exist
  242. }
  243. } else {
  244. $reason = 'Invalid username format [' . $currentUser . ']!';
  245. }
  246. }
  247. } else {
  248. $reason = 'Invalid email format [' . $res['email'] . ']!';
  249. }
  250. }
  251. if ($loginOk) {
  252. Minz_Session::_param('currentUser', $currentUser);
  253. Minz_Session::_param ('mail', $email);
  254. $this->view->loginOk = true;
  255. invalidateHttpCache();
  256. } else {
  257. $res = array ();
  258. $res['status'] = 'failure';
  259. $res['reason'] = $reason == '' ? Minz_Translate::t ('invalid_login') : $reason;
  260. Minz_Log::record ('Persona: ' . $res['reason'], Minz_Log::WARNING);
  261. }
  262. header('Content-Type: application/json; charset=UTF-8');
  263. $this->view->res = json_encode ($res);
  264. }
  265. public function logoutAction () {
  266. $this->view->_useLayout(false);
  267. invalidateHttpCache();
  268. Minz_Session::_param('currentUser');
  269. Minz_Session::_param('mail');
  270. Minz_Session::_param('passwordHash');
  271. }
  272. public function formLoginAction () {
  273. if (Minz_Request::isPost()) {
  274. $ok = false;
  275. $nonce = Minz_Session::param('nonce');
  276. $username = Minz_Request::param('username', '');
  277. $c = Minz_Request::param('challenge', '');
  278. if (ctype_alnum($username) && ctype_graph($c) && ctype_alnum($nonce)) {
  279. if (!function_exists('password_verify')) {
  280. include_once(LIB_PATH . '/password_compat.php');
  281. }
  282. try {
  283. $conf = new FreshRSS_Configuration($username);
  284. $s = $conf->passwordHash;
  285. $ok = password_verify($nonce . $s, $c);
  286. if ($ok) {
  287. Minz_Session::_param('currentUser', $username);
  288. Minz_Session::_param('passwordHash', $s);
  289. } else {
  290. Minz_Log::record('Password mismatch for user ' . $username . ', nonce=' . $nonce . ', c=' . $c, Minz_Log::WARNING);
  291. }
  292. } catch (Minz_Exception $me) {
  293. Minz_Log::record('Login failure: ' . $me->getMessage(), Minz_Log::WARNING);
  294. }
  295. }
  296. if (!$ok) {
  297. $notif = array(
  298. 'type' => 'bad',
  299. 'content' => Minz_Translate::t('invalid_login')
  300. );
  301. Minz_Session::_param('notification', $notif);
  302. }
  303. $this->view->_useLayout(false);
  304. Minz_Request::forward(array('c' => 'index', 'a' => 'index'), true);
  305. }
  306. invalidateHttpCache();
  307. }
  308. public function formLogoutAction () {
  309. $this->view->_useLayout(false);
  310. invalidateHttpCache();
  311. Minz_Session::_param('currentUser');
  312. Minz_Session::_param('mail');
  313. Minz_Session::_param('passwordHash');
  314. Minz_Request::forward(array('c' => 'index', 'a' => 'index'), true);
  315. }
  316. }