indexController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. class indexController extends ActionController {
  3. public function indexAction () {
  4. View::appendScript (Url::display ('/scripts/smoothscroll.js'));
  5. View::appendScript (Url::display ('/scripts/shortcut.js'));
  6. View::appendScript (Url::display (array ('c' => 'javascript', 'a' => 'main')));
  7. $entryDAO = new EntryDAO ();
  8. $feedDAO = new FeedDAO ();
  9. $catDAO = new CategoryDAO ();
  10. // pour optimiser
  11. $page = Request::param ('page', 1);
  12. $entryDAO->_nbItemsPerPage ($this->view->conf->postsPerPage ());
  13. $entryDAO->_currentPage ($page);
  14. $default_view = $this->view->conf->defaultView ();
  15. $mode = Session::param ('mode');
  16. if ($mode == false) {
  17. if ($default_view == 'not_read' && $this->view->nb_not_read < 1) {
  18. $mode = 'all';
  19. } else {
  20. $mode = $default_view;
  21. }
  22. }
  23. $get = Request::param ('get');
  24. $order = $this->view->conf->sortOrder ();
  25. $error = false;
  26. // Récupère les flux par catégorie, favoris ou tous
  27. if ($get == 'favoris') {
  28. $entries = $entryDAO->listFavorites ($mode, $order);
  29. View::prependTitle ('Vos favoris - ');
  30. } elseif ($get != false) {
  31. $typeGet = $get[0];
  32. $get = substr ($get, 2);
  33. if ($typeGet == 'c') {
  34. $entries = $entryDAO->listByCategory ($get, $mode, $order);
  35. $cat = $catDAO->searchById ($get);
  36. if ($cat) {
  37. View::prependTitle ($cat->name () . ' - ');
  38. } else {
  39. $error = true;
  40. }
  41. } elseif ($typeGet == 'f') {
  42. $entries = $entryDAO->listByFeed ($get, $mode, $order);
  43. $feed = $feedDAO->searchById ($get);
  44. if ($feed) {
  45. View::prependTitle ($feed->name () . ' - ');
  46. } else {
  47. $error = true;
  48. }
  49. } else {
  50. $error = true;
  51. }
  52. } else {
  53. View::prependTitle ('Vos flux RSS - ');
  54. }
  55. $this->view->get = $get;
  56. $this->view->mode = $mode;
  57. // Cas où on ne choisie ni catégorie ni les favoris
  58. // ou si la catégorie ne correspond à aucune
  59. if (!isset ($entries)) {
  60. $entries = $entryDAO->listEntries ($mode, $order);
  61. }
  62. try {
  63. $this->view->entryPaginator = $entryDAO->getPaginator ($entries);
  64. } catch (CurrentPagePaginationException $e) {
  65. $error = true;
  66. }
  67. $this->view->cat_aside = $catDAO->listCategories ();
  68. $this->view->nb_favorites = $entryDAO->countFavorites ();
  69. $this->view->nb_total = $entryDAO->count ();
  70. if ($error) {
  71. Error::error (
  72. 404,
  73. array ('error' => array ('La page que vous cherchez n\'existe pas'))
  74. );
  75. }
  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 loginAction () {
  87. $this->view->_useLayout (false);
  88. $url = 'https://verifier.login.persona.org/verify';
  89. $assert = Request::param ('assertion');
  90. $params = 'assertion=' . $assert . '&audience=' .
  91. urlencode (Url::display () . ':80');
  92. $ch = curl_init ();
  93. $options = array (
  94. CURLOPT_URL => $url,
  95. CURLOPT_RETURNTRANSFER => TRUE,
  96. CURLOPT_POST => 2,
  97. CURLOPT_POSTFIELDS => $params
  98. );
  99. curl_setopt_array ($ch, $options);
  100. $result = curl_exec ($ch);
  101. curl_close ($ch);
  102. $res = json_decode ($result, true);
  103. if ($res['status'] == 'okay' && $res['email'] == $this->view->conf->mailLogin ()) {
  104. Session::_param ('mail', $res['email']);
  105. } else {
  106. $res = array ();
  107. $res['status'] = 'failure';
  108. $res['reason'] = 'L\'identifiant est invalide';
  109. }
  110. $this->view->res = json_encode ($res);
  111. }
  112. public function logoutAction () {
  113. $this->view->_useLayout (false);
  114. Session::_param ('mail');
  115. }
  116. }