indexController.php 8.4 KB

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