indexController.php 11 KB

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