indexController.php 7.2 KB

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