indexController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. $this->view->cat_aside = $catDAO->listCategories ();
  70. $this->view->nb_favorites = $entryDAO->countFavorites ();
  71. $this->view->nb_total = $entryDAO->count ();
  72. }
  73. }
  74. public function aboutAction () {
  75. View::prependTitle ('À propos - ');
  76. }
  77. public function changeModeAction () {
  78. $mode = Request::param ('mode');
  79. if ($mode == 'not_read') {
  80. Session::_param ('mode', 'not_read');
  81. } else {
  82. Session::_param ('mode', 'all');
  83. }
  84. Request::forward (array (), true);
  85. }
  86. public function changeOrderAction () {
  87. $order = Request::param ('order');
  88. if ($order == 'low_to_high') {
  89. Session::_param ('order', 'low_to_high');
  90. } else {
  91. Session::_param ('order', 'high_to_low');
  92. }
  93. Request::forward (array (), true);
  94. }
  95. public function loginAction () {
  96. $this->view->_useLayout (false);
  97. $url = 'https://verifier.login.persona.org/verify';
  98. $assert = Request::param ('assertion');
  99. $params = 'assertion=' . $assert . '&audience=' .
  100. urlencode (Url::display () . ':80');
  101. $ch = curl_init ();
  102. $options = array (
  103. CURLOPT_URL => $url,
  104. CURLOPT_RETURNTRANSFER => TRUE,
  105. CURLOPT_POST => 2,
  106. CURLOPT_POSTFIELDS => $params
  107. );
  108. curl_setopt_array ($ch, $options);
  109. $result = curl_exec ($ch);
  110. curl_close ($ch);
  111. $res = json_decode ($result, true);
  112. if ($res['status'] == 'okay' && $res['email'] == $this->view->conf->mailLogin ()) {
  113. Session::_param ('mail', $res['email']);
  114. } else {
  115. $res = array ();
  116. $res['status'] = 'failure';
  117. $res['reason'] = 'L\'identifiant est invalide';
  118. }
  119. $this->view->res = json_encode ($res);
  120. }
  121. public function logoutAction () {
  122. $this->view->_useLayout (false);
  123. Session::_param ('mail');
  124. }
  125. private function initFilter () {
  126. $get = Request::param ('get');
  127. $this->view->get_c = false;
  128. $this->view->get_f = false;
  129. $typeGet = $get[0];
  130. $filter = substr ($get, 2);
  131. if ($get == 'favoris') {
  132. $this->view->get_c = $get;
  133. $this->get = array (
  134. 'type' => $get,
  135. 'filter' => $get
  136. );
  137. } elseif ($get == false) {
  138. $this->get = array (
  139. 'type' => 'all',
  140. 'filter' => 'all'
  141. );
  142. } else {
  143. if ($typeGet == 'f') {
  144. $this->view->get_f = $filter;
  145. $this->get = array (
  146. 'type' => $typeGet,
  147. 'filter' => $filter
  148. );
  149. } elseif ($typeGet == 'c') {
  150. $this->view->get_c = $filter;
  151. $this->get = array (
  152. 'type' => $typeGet,
  153. 'filter' => $filter
  154. );
  155. } else {
  156. $this->get = false;
  157. }
  158. }
  159. }
  160. private function countNotRead () {
  161. $entryDAO = new EntryDAO ();
  162. if ($this->get != false) {
  163. if ($this->get['type'] == 'all') {
  164. $this->nb_not_read = $this->view->nb_not_read;
  165. } elseif ($this->get['type'] == 'favoris') {
  166. $this->nb_not_read = $entryDAO->countNotReadFavorites ();
  167. } elseif ($this->get['type'] == 'c') {
  168. $this->nb_not_read = $entryDAO->countNotReadByCat ($this->get['filter']);
  169. } elseif ($this->get['type'] == 'f') {
  170. $this->nb_not_read = $entryDAO->countNotReadByFeed ($this->get['filter']);
  171. }
  172. }
  173. }
  174. private function initCurrentMode () {
  175. $default_view = $this->view->conf->defaultView ();
  176. $mode = Session::param ('mode');
  177. if ($mode == false) {
  178. if ($default_view == 'not_read' && $this->nb_not_read < 1) {
  179. $mode = 'all';
  180. } else {
  181. $mode = $default_view;
  182. }
  183. }
  184. $this->mode = $mode;
  185. }
  186. }