indexController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. View::appendScript (Url::display ('/scripts/shortcut.js'));
  8. View::appendScript (Url::display (array ('c' => 'javascript', 'a' => 'main')));
  9. $entryDAO = new EntryDAO ();
  10. $feedDAO = new FeedDAO ();
  11. $catDAO = new CategoryDAO ();
  12. $error = false;
  13. // pour optimiser
  14. $page = Request::param ('page', 1);
  15. $entryDAO->_nbItemsPerPage ($this->view->conf->postsPerPage ());
  16. $entryDAO->_currentPage ($page);
  17. // récupération de la catégorie/flux à filtrer
  18. $this->initFilter ();
  19. // Compte le nombre d'articles non lus en prenant en compte le filtre
  20. $this->countNotRead ();
  21. // mode de vue (tout ou seulement non lus)
  22. $this->initCurrentMode ();
  23. // ordre de listage des flux
  24. $order = Session::param ('order', $this->view->conf->sortOrder ());
  25. // recherche sur les titres (pour le moment)
  26. $search = Request::param ('search');
  27. // Récupère les flux par catégorie, favoris ou tous
  28. if ($this->get['type'] == 'all') {
  29. $entries = $entryDAO->listEntries ($this->mode, $search, $order);
  30. View::prependTitle ('Vos flux RSS - ');
  31. } elseif ($this->get['type'] == 'favoris') {
  32. $entries = $entryDAO->listFavorites ($this->mode, $search, $order);
  33. View::prependTitle ('Vos favoris - ');
  34. } elseif ($this->get != false) {
  35. if ($this->get['type'] == 'c') {
  36. $cat = $catDAO->searchById ($this->get['filter']);
  37. if ($cat) {
  38. $entries = $entryDAO->listByCategory ($this->get['filter'], $this->mode, $search, $order);
  39. View::prependTitle ($cat->name () . ' - ');
  40. } else {
  41. $error = true;
  42. }
  43. } elseif ($this->get['type'] == 'f') {
  44. $feed = $feedDAO->searchById ($this->get['filter']);
  45. if ($feed) {
  46. $entries = $entryDAO->listByFeed ($this->get['filter'], $this->mode, $search, $order);
  47. $this->view->get_c = $feed->category ();
  48. View::prependTitle ($feed->name () . ' - ');
  49. } else {
  50. $error = true;
  51. }
  52. } else {
  53. $error = true;
  54. }
  55. } else {
  56. $error = true;
  57. }
  58. if ($error) {
  59. Error::error (
  60. 404,
  61. array ('error' => array ('La page que vous cherchez n\'existe pas'))
  62. );
  63. } else {
  64. $this->view->mode = $this->mode;
  65. $this->view->order = $order;
  66. try {
  67. $this->view->entryPaginator = $entryDAO->getPaginator ($entries);
  68. } catch (CurrentPagePaginationException $e) {
  69. }
  70. $this->view->cat_aside = $catDAO->listCategories ();
  71. $this->view->nb_favorites = $entryDAO->countFavorites ();
  72. $this->view->nb_total = $entryDAO->count ();
  73. }
  74. }
  75. public function aboutAction () {
  76. View::prependTitle ('À propos - ');
  77. }
  78. public function changeModeAction () {
  79. $mode = Request::param ('mode');
  80. if ($mode == 'not_read') {
  81. Session::_param ('mode', 'not_read');
  82. } else {
  83. Session::_param ('mode', 'all');
  84. }
  85. Request::forward (array (), true);
  86. }
  87. public function changeOrderAction () {
  88. $order = Request::param ('order');
  89. if ($order == 'low_to_high') {
  90. Session::_param ('order', 'low_to_high');
  91. } else {
  92. Session::_param ('order', 'high_to_low');
  93. }
  94. Request::forward (array (), true);
  95. }
  96. public function loginAction () {
  97. $this->view->_useLayout (false);
  98. $url = 'https://verifier.login.persona.org/verify';
  99. $assert = Request::param ('assertion');
  100. $params = 'assertion=' . $assert . '&audience=' .
  101. urlencode (Url::display () . ':80');
  102. $ch = curl_init ();
  103. $options = array (
  104. CURLOPT_URL => $url,
  105. CURLOPT_RETURNTRANSFER => TRUE,
  106. CURLOPT_POST => 2,
  107. CURLOPT_POSTFIELDS => $params
  108. );
  109. curl_setopt_array ($ch, $options);
  110. $result = curl_exec ($ch);
  111. curl_close ($ch);
  112. $res = json_decode ($result, true);
  113. if ($res['status'] == 'okay' && $res['email'] == $this->view->conf->mailLogin ()) {
  114. Session::_param ('mail', $res['email']);
  115. } else {
  116. $res = array ();
  117. $res['status'] = 'failure';
  118. $res['reason'] = 'L\'identifiant est invalide';
  119. }
  120. $this->view->res = json_encode ($res);
  121. }
  122. public function logoutAction () {
  123. $this->view->_useLayout (false);
  124. Session::_param ('mail');
  125. }
  126. private function initFilter () {
  127. $get = Request::param ('get');
  128. $this->view->get_c = false;
  129. $this->view->get_f = false;
  130. $typeGet = $get[0];
  131. $filter = substr ($get, 2);
  132. if ($get == 'favoris') {
  133. $this->view->get_c = $get;
  134. $this->get = array (
  135. 'type' => $get,
  136. 'filter' => $get
  137. );
  138. } elseif ($get == false) {
  139. $this->get = array (
  140. 'type' => 'all',
  141. 'filter' => 'all'
  142. );
  143. } else {
  144. if ($typeGet == 'f') {
  145. $this->view->get_f = $filter;
  146. $this->get = array (
  147. 'type' => $typeGet,
  148. 'filter' => $filter
  149. );
  150. } elseif ($typeGet == 'c') {
  151. $this->view->get_c = $filter;
  152. $this->get = array (
  153. 'type' => $typeGet,
  154. 'filter' => $filter
  155. );
  156. } else {
  157. $this->get = false;
  158. }
  159. }
  160. }
  161. private function countNotRead () {
  162. $entryDAO = new EntryDAO ();
  163. if ($this->get != false) {
  164. if ($this->get['type'] == 'all') {
  165. $this->nb_not_read = $this->view->nb_not_read;
  166. } elseif ($this->get['type'] == 'favoris') {
  167. $this->nb_not_read = $entryDAO->countNotReadFavorites ();
  168. } elseif ($this->get['type'] == 'c') {
  169. $this->nb_not_read = $entryDAO->countNotReadByCat ($this->get['filter']);
  170. } elseif ($this->get['type'] == 'f') {
  171. $this->nb_not_read = $entryDAO->countNotReadByFeed ($this->get['filter']);
  172. }
  173. }
  174. }
  175. private function initCurrentMode () {
  176. $default_view = $this->view->conf->defaultView ();
  177. $mode = Session::param ('mode');
  178. if ($mode == false) {
  179. if ($default_view == 'not_read' && $this->nb_not_read < 1) {
  180. $mode = 'all';
  181. } else {
  182. $mode = $default_view;
  183. }
  184. }
  185. $this->mode = $mode;
  186. }
  187. }