indexController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. !Minz_Configuration::allowAnonymous() &&
  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. !Minz_Configuration::allowAnonymous() &&
  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->default_view);
  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->sort_order);
  86. $nb = Minz_Request::param ('nb', $this->view->conf->posts_per_page);
  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->old_entries;
  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. $keepHistoryDefault = $this->view->conf->keep_history_default;
  117. try {
  118. $entries = $this->entryDAO->listWhere($getType, $getId, $state, $order, $nb + 1, $first, $filter, $date_min, $keepHistoryDefault);
  119. // Si on a récupéré aucun article "non lus"
  120. // on essaye de récupérer tous les articles
  121. if ($state === 'not_read' && empty($entries)) { //TODO: Remove in v0.8
  122. Minz_Log::record ('Conflicting information about nbNotRead!', Minz_Log::DEBUG);
  123. $this->view->state = 'all';
  124. $entries = $this->entryDAO->listWhere($getType, $getId, 'all', $order, $nb, $first, $filter, $date_min, $keepHistoryDefault);
  125. }
  126. if (count($entries) <= $nb) {
  127. $this->view->nextId = '';
  128. } else { //We have more elements for pagination
  129. $lastEntry = array_pop($entries);
  130. $this->view->nextId = $lastEntry->id();
  131. }
  132. $this->view->entries = $entries;
  133. } catch (FreshRSS_EntriesGetter_Exception $e) {
  134. Minz_Log::record ($e->getMessage (), Minz_Log::NOTICE);
  135. Minz_Error::error (
  136. 404,
  137. array ('error' => array (Minz_Translate::t ('page_not_found')))
  138. );
  139. }
  140. }
  141. /*
  142. * Vérifie que la catégorie / flux sélectionné existe
  143. * + Initialise correctement les variables de vue get_c et get_f
  144. * + Met à jour la variable $this->nb_not_read_cat
  145. */
  146. private function checkAndProcessType ($getType, $getId) {
  147. switch ($getType) {
  148. case 'a':
  149. $this->view->currentName = Minz_Translate::t ('your_rss_feeds');
  150. $this->view->get_c = $getType;
  151. return true;
  152. case 's':
  153. $this->view->currentName = Minz_Translate::t ('your_favorites');
  154. $this->view->get_c = $getType;
  155. return true;
  156. case 'c':
  157. $cat = isset($this->view->cat_aside[$getId]) ? $this->view->cat_aside[$getId] : null;
  158. if ($cat === null) {
  159. $cat = $this->catDAO->searchById ($getId);
  160. }
  161. if ($cat) {
  162. $this->view->currentName = $cat->name ();
  163. $this->nb_not_read_cat = $cat->nbNotRead ();
  164. $this->view->get_c = $getId;
  165. return true;
  166. } else {
  167. return false;
  168. }
  169. case 'f':
  170. $feed = FreshRSS_CategoryDAO::findFeed($this->view->cat_aside, $getId);
  171. if (empty($feed)) {
  172. $feed = $this->feedDAO->searchById ($getId);
  173. }
  174. if ($feed) {
  175. $this->view->currentName = $feed->name ();
  176. $this->nb_not_read_cat = $feed->nbNotRead ();
  177. $this->view->get_f = $getId;
  178. $this->view->get_c = $feed->category ();
  179. return true;
  180. } else {
  181. return false;
  182. }
  183. default:
  184. return false;
  185. }
  186. }
  187. public function aboutAction () {
  188. Minz_View::prependTitle (Minz_Translate::t ('about') . ' - ');
  189. }
  190. public function logsAction () {
  191. if (login_is_conf ($this->view->conf) && !is_logged ()) {
  192. Minz_Error::error (
  193. 403,
  194. array ('error' => array (Minz_Translate::t ('access_denied')))
  195. );
  196. }
  197. Minz_View::prependTitle (Minz_Translate::t ('logs') . ' - ');
  198. if (Minz_Request::isPost ()) {
  199. file_put_contents(LOG_PATH . '/application.log', '');
  200. }
  201. $logs = FreshRSS_LogDAO::lines(); //TODO: ask only the necessary lines
  202. //gestion pagination
  203. $page = Minz_Request::param ('page', 1);
  204. $this->view->logsPaginator = new Minz_Paginator ($logs);
  205. $this->view->logsPaginator->_nbItemsPerPage (50);
  206. $this->view->logsPaginator->_currentPage ($page);
  207. }
  208. public function loginAction () {
  209. $this->view->_useLayout (false);
  210. $url = 'https://verifier.login.persona.org/verify';
  211. $assert = Minz_Request::param ('assertion');
  212. $params = 'assertion=' . $assert . '&audience=' .
  213. urlencode (Minz_Url::display (null, 'php', true));
  214. $ch = curl_init ();
  215. $options = array (
  216. CURLOPT_URL => $url,
  217. CURLOPT_RETURNTRANSFER => TRUE,
  218. CURLOPT_POST => 2,
  219. CURLOPT_POSTFIELDS => $params
  220. );
  221. curl_setopt_array ($ch, $options);
  222. $result = curl_exec ($ch);
  223. curl_close ($ch);
  224. $res = json_decode ($result, true);
  225. if ($res['status'] === 'okay' && $res['email'] === $this->view->conf->mail_login) {
  226. Minz_Session::_param ('mail', $res['email']);
  227. invalidateHttpCache();
  228. } else {
  229. $res = array ();
  230. $res['status'] = 'failure';
  231. $res['reason'] = Minz_Translate::t ('invalid_login');
  232. }
  233. header('Content-Type: application/json; charset=UTF-8');
  234. $this->view->res = json_encode ($res);
  235. }
  236. public function logoutAction () {
  237. $this->view->_useLayout (false);
  238. Minz_Session::_param ('mail');
  239. invalidateHttpCache();
  240. }
  241. }