indexController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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['type'] == 'public') {
  35. $entries = $entryDAO->listPublic ($this->mode, $search, $order);
  36. View::prependTitle ('Public - ');
  37. } elseif ($this->get != false) {
  38. if ($this->get['type'] == 'c') {
  39. $cat = $catDAO->searchById ($this->get['filter']);
  40. if ($cat) {
  41. $entries = $entryDAO->listByCategory ($this->get['filter'], $this->mode, $search, $order);
  42. View::prependTitle ($cat->name () . ' - ');
  43. } else {
  44. $error = true;
  45. }
  46. } elseif ($this->get['type'] == 'f') {
  47. $feed = $feedDAO->searchById ($this->get['filter']);
  48. if ($feed) {
  49. $entries = $entryDAO->listByFeed ($this->get['filter'], $this->mode, $search, $order);
  50. $this->view->get_c = $feed->category ();
  51. View::prependTitle ($feed->name () . ' - ');
  52. } else {
  53. $error = true;
  54. }
  55. } else {
  56. $error = true;
  57. }
  58. } else {
  59. $error = true;
  60. }
  61. if ($error) {
  62. Error::error (
  63. 404,
  64. array ('error' => array ('La page que vous cherchez n\'existe pas'))
  65. );
  66. } else {
  67. $this->view->mode = $this->mode;
  68. $this->view->order = $order;
  69. try {
  70. $this->view->entryPaginator = $entryDAO->getPaginator ($entries);
  71. } catch (CurrentPagePaginationException $e) { }
  72. $this->view->cat_aside = $catDAO->listCategories ();
  73. $this->view->nb_favorites = $entryDAO->countFavorites ();
  74. $this->view->nb_total = $entryDAO->count ();
  75. if (Request::param ('output', '') == 'rss') {
  76. $this->view->_useLayout (false);
  77. }
  78. }
  79. }
  80. public function aboutAction () {
  81. View::prependTitle ('À propos - ');
  82. }
  83. public function changeModeAction () {
  84. $mode = Request::param ('mode');
  85. if ($mode == 'not_read') {
  86. Session::_param ('mode', 'not_read');
  87. } else {
  88. Session::_param ('mode', 'all');
  89. }
  90. Request::forward (array (), true);
  91. }
  92. public function changeOrderAction () {
  93. $order = Request::param ('order');
  94. if ($order == 'low_to_high') {
  95. Session::_param ('order', 'low_to_high');
  96. } else {
  97. Session::_param ('order', 'high_to_low');
  98. }
  99. Request::forward (array (), true);
  100. }
  101. public function loginAction () {
  102. $this->view->_useLayout (false);
  103. $url = 'https://verifier.login.persona.org/verify';
  104. $assert = Request::param ('assertion');
  105. $params = 'assertion=' . $assert . '&audience=' .
  106. urlencode (Url::display () . ':80');
  107. $ch = curl_init ();
  108. $options = array (
  109. CURLOPT_URL => $url,
  110. CURLOPT_RETURNTRANSFER => TRUE,
  111. CURLOPT_POST => 2,
  112. CURLOPT_POSTFIELDS => $params
  113. );
  114. curl_setopt_array ($ch, $options);
  115. $result = curl_exec ($ch);
  116. curl_close ($ch);
  117. $res = json_decode ($result, true);
  118. if ($res['status'] == 'okay' && $res['email'] == $this->view->conf->mailLogin ()) {
  119. Session::_param ('mail', $res['email']);
  120. } else {
  121. $res = array ();
  122. $res['status'] = 'failure';
  123. $res['reason'] = 'L\'identifiant est invalide';
  124. }
  125. $this->view->res = json_encode ($res);
  126. }
  127. public function logoutAction () {
  128. $this->view->_useLayout (false);
  129. Session::_param ('mail');
  130. }
  131. private function initFilter () {
  132. $get = Request::param ('get');
  133. $this->view->get_c = false;
  134. $this->view->get_f = false;
  135. $typeGet = $get[0];
  136. $filter = substr ($get, 2);
  137. if ($get == 'favoris') {
  138. $this->view->get_c = $get;
  139. $this->get = array (
  140. 'type' => $get,
  141. 'filter' => $get
  142. );
  143. } elseif ($get == 'public') {
  144. $this->view->get_c = $get;
  145. $this->get = array (
  146. 'type' => $get,
  147. 'filter' => $get
  148. );
  149. } elseif ($get == false) {
  150. $this->get = array (
  151. 'type' => 'all',
  152. 'filter' => 'all'
  153. );
  154. } else {
  155. if ($typeGet == 'f') {
  156. $this->view->get_f = $filter;
  157. $this->get = array (
  158. 'type' => $typeGet,
  159. 'filter' => $filter
  160. );
  161. } elseif ($typeGet == 'c') {
  162. $this->view->get_c = $filter;
  163. $this->get = array (
  164. 'type' => $typeGet,
  165. 'filter' => $filter
  166. );
  167. } else {
  168. $this->get = false;
  169. }
  170. }
  171. }
  172. private function countNotRead () {
  173. $entryDAO = new EntryDAO ();
  174. if ($this->get != false) {
  175. if ($this->get['type'] == 'all') {
  176. $this->nb_not_read = $this->view->nb_not_read;
  177. } elseif ($this->get['type'] == 'favoris') {
  178. $this->nb_not_read = $entryDAO->countNotReadFavorites ();
  179. } elseif ($this->get['type'] == 'c') {
  180. $this->nb_not_read = $entryDAO->countNotReadByCat ($this->get['filter']);
  181. } elseif ($this->get['type'] == 'f') {
  182. $this->nb_not_read = $entryDAO->countNotReadByFeed ($this->get['filter']);
  183. }
  184. }
  185. }
  186. private function initCurrentMode () {
  187. $default_view = $this->view->conf->defaultView ();
  188. $mode = Session::param ('mode');
  189. if ($mode == false) {
  190. if ($default_view == 'not_read' && $this->nb_not_read < 1) {
  191. $mode = 'all';
  192. } else {
  193. $mode = $default_view;
  194. }
  195. }
  196. $this->mode = $mode;
  197. }
  198. }