indexController.php 9.2 KB

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