indexController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. class indexController extends ActionController {
  3. private $get = false;
  4. private $nb_not_read = 0;
  5. private $mode = 'all';
  6. public function indexAction () {
  7. $output = Request::param ('output');
  8. if ($output == 'rss') {
  9. $this->view->_useLayout (false);
  10. } else {
  11. View::appendScript (Url::display ('/scripts/shortcut.js'));
  12. View::appendScript (Url::display (array ('c' => 'javascript', 'a' => 'main')));
  13. View::appendScript (Url::display (array ('c' => 'javascript', 'a' => 'actualize')));
  14. View::appendScript (Url::display ('/scripts/endless_mode.js'));
  15. if(!$output) {
  16. Request::_param ('output', $this->view->conf->viewMode());
  17. }
  18. }
  19. $entryDAO = new EntryDAO ();
  20. $feedDAO = new FeedDAO ();
  21. $catDAO = new CategoryDAO ();
  22. $this->view->cat_aside = $catDAO->listCategories ();
  23. $this->view->nb_favorites = $entryDAO->countFavorites ();
  24. $this->view->nb_total = $entryDAO->count ();
  25. $this->view->get_c = '';
  26. $this->view->get_f = '';
  27. $type = $this->getType ();
  28. $error = $this->checkAndProcessType ($type);
  29. if (!$error) {
  30. // On récupère les différents éléments de filtrage
  31. $this->view->state = $state = Request::param ('state', $this->view->conf->defaultView ());
  32. $filter = Request::param ('search', '');
  33. $this->view->order = $order = Request::param ('order', $this->view->conf->sortOrder ());
  34. $nb = Request::param ('nb', $this->view->conf->postsPerPage ());
  35. $first = Request::param ('next', '');
  36. try {
  37. // EntriesGetter permet de déporter la complexité du filtrage
  38. $getter = new EntriesGetter ($type, $state, $filter, $order, $nb, $first);
  39. $getter->execute ();
  40. $entries = $getter->getPaginator ();
  41. // Si on a récupéré aucun article "non lus"
  42. // on essaye de récupérer tous les articles
  43. if ($state == 'not_read' && $entries->isEmpty ()) {
  44. $this->view->state = 'all';
  45. $getter->_state ('all');
  46. $getter->execute ();
  47. $entries = $getter->getPaginator ();
  48. }
  49. $this->view->entryPaginator = $entries;
  50. } catch(EntriesGetterException $e) {
  51. Log::record ($e->getMessage (), Log::NOTICE);
  52. Error::error (
  53. 404,
  54. array ('error' => array (Translate::t ('page_not_found')))
  55. );
  56. }
  57. } else {
  58. Error::error (
  59. 404,
  60. array ('error' => array (Translate::t ('page_not_found')))
  61. );
  62. }
  63. }
  64. /*
  65. * Détermine le type d'article à récupérer :
  66. * "tous", "favoris", "public", "catégorie" ou "flux"
  67. */
  68. private function getType () {
  69. $get = Request::param ('get', 'all');
  70. $typeGet = $get[0];
  71. $id = substr ($get, 2);
  72. $type = null;
  73. if ($get == 'all' || $get == 'favoris' || $get == 'public') {
  74. $type = array (
  75. 'type' => $get,
  76. 'id' => $get
  77. );
  78. } elseif ($typeGet == 'f' || $typeGet == 'c') {
  79. $type = array (
  80. 'type' => $typeGet,
  81. 'id' => $id
  82. );
  83. }
  84. return $type;
  85. }
  86. /*
  87. * Vérifie que la catégorie / flux sélectionné existe
  88. * + Initialise correctement les variables de vue get_c et get_f
  89. * + Initialise le titre
  90. */
  91. private function checkAndProcessType ($type) {
  92. if ($type['type'] == 'all') {
  93. View::prependTitle (Translate::t ('your_rss_feeds') . ' - ');
  94. $this->view->get_c = $type['type'];
  95. return false;
  96. } elseif ($type['type'] == 'favoris') {
  97. View::prependTitle (Translate::t ('your_favorites') . ' - ');
  98. $this->view->get_c = $type['type'];
  99. return false;
  100. } elseif ($type['type'] == 'public') {
  101. View::prependTitle (Translate::t ('public') . ' - ');
  102. $this->view->get_c = $type['type'];
  103. return false;
  104. } elseif ($type['type'] == 'c') {
  105. $catDAO = new CategoryDAO ();
  106. $cat = $catDAO->searchById ($type['id']);
  107. if ($cat) {
  108. View::prependTitle ($cat->name () . ' - ');
  109. $this->view->get_c = $type['id'];
  110. return false;
  111. } else {
  112. return true;
  113. }
  114. } elseif ($type['type'] == 'f') {
  115. $feedDAO = new FeedDAO ();
  116. $feed = $feedDAO->searchById ($type['id']);
  117. if ($feed) {
  118. View::prependTitle ($feed->name () . ' - ');
  119. $this->view->get_f = $type['id'];
  120. $this->view->get_c = $feed->category ();
  121. return false;
  122. } else {
  123. return true;
  124. }
  125. } else {
  126. return true;
  127. }
  128. }
  129. public function aboutAction () {
  130. View::prependTitle (Translate::t ('about') . ' - ');
  131. }
  132. public function logsAction () {
  133. if (login_is_conf ($this->view->conf) && !is_logged ()) {
  134. Error::error (
  135. 403,
  136. array ('error' => array (Translate::t ('access_denied')))
  137. );
  138. }
  139. View::prependTitle (Translate::t ('see_logs') . ' - ');
  140. $logs = array();
  141. try {
  142. $logDAO = new LogDAO ();
  143. $logs = $logDAO->lister ();
  144. $logs = array_reverse ($logs);
  145. } catch(FileNotExistException $e) {
  146. }
  147. //gestion pagination
  148. $page = Request::param ('page', 1);
  149. $this->view->logsPaginator = new Paginator ($logs);
  150. $this->view->logsPaginator->_nbItemsPerPage (50);
  151. $this->view->logsPaginator->_currentPage ($page);
  152. }
  153. public function loginAction () {
  154. $this->view->_useLayout (false);
  155. $url = 'https://verifier.login.persona.org/verify';
  156. $assert = Request::param ('assertion');
  157. $params = 'assertion=' . $assert . '&audience=' .
  158. urlencode (Url::display () . ':80');
  159. $ch = curl_init ();
  160. $options = array (
  161. CURLOPT_URL => $url,
  162. CURLOPT_RETURNTRANSFER => TRUE,
  163. CURLOPT_POST => 2,
  164. CURLOPT_POSTFIELDS => $params
  165. );
  166. curl_setopt_array ($ch, $options);
  167. $result = curl_exec ($ch);
  168. curl_close ($ch);
  169. $res = json_decode ($result, true);
  170. if ($res['status'] == 'okay' && $res['email'] == $this->view->conf->mailLogin ()) {
  171. Session::_param ('mail', $res['email']);
  172. } else {
  173. $res = array ();
  174. $res['status'] = 'failure';
  175. $res['reason'] = Translate::t ('invalid_login');
  176. }
  177. $this->view->res = json_encode ($res);
  178. }
  179. public function logoutAction () {
  180. $this->view->_useLayout (false);
  181. Session::_param ('mail');
  182. }
  183. }