indexController.php 7.2 KB

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