indexController.php 8.2 KB

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