indexController.php 6.1 KB

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