indexController.php 8.4 KB

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