indexController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 = HelperCategory::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 = HelperCategory::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. // on calcule la date des articles les plus anciens qu'on affiche
  112. $nb_month_old = $this->view->conf->oldEntries ();
  113. $date_min = time () - (3600 * 24 * 30 * $nb_month_old);
  114. try {
  115. $entries = $this->entryDAO->listWhere($getType, $getId, $state, $order, $nb + 1, $first, $filter, $date_min);
  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)) { //TODO: Remove in v0.8
  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);
  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 = HelperCategory::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 (login_is_conf ($this->view->conf) && !is_logged ()) {
  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. file_put_contents(LOG_PATH . '/application.log', '');
  197. }
  198. $logs = array();
  199. try {
  200. $logDAO = new FreshRSS_LogDAO ();
  201. $logs = $logDAO->lister ();
  202. $logs = array_reverse ($logs);
  203. } catch (Minz_FileNotExistException $e) {
  204. }
  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. if ($res['status'] === 'okay' && $res['email'] === $this->view->conf->mailLogin ()) {
  229. Minz_Session::_param ('mail', $res['email']);
  230. invalidateHttpCache();
  231. } else {
  232. $res = array ();
  233. $res['status'] = 'failure';
  234. $res['reason'] = Minz_Translate::t ('invalid_login');
  235. }
  236. header('Content-Type: application/json; charset=UTF-8');
  237. $this->view->res = json_encode ($res);
  238. }
  239. public function logoutAction () {
  240. $this->view->_useLayout (false);
  241. Minz_Session::_param ('mail');
  242. invalidateHttpCache();
  243. }
  244. }