indexController.php 4.9 KB

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