indexController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. $output = Request::param ('output');
  8. if ($output == 'rss') {
  9. $this->view->_useLayout (false);
  10. } else {
  11. View::appendScript (Url::display ('/scripts/shortcut.js'));
  12. View::appendScript (Url::display (array ('c' => 'javascript', 'a' => 'main')));
  13. View::appendScript (Url::display (array ('c' => 'javascript', 'a' => 'actualize')));
  14. View::appendScript (Url::display ('/scripts/endless_mode.js'));
  15. if(!$output) {
  16. Request::_param ('output', $this->view->conf->viewMode());
  17. }
  18. }
  19. $nb_not_read = $this->view->nb_not_read;
  20. if($nb_not_read > 0) {
  21. View::prependTitle (' (' . $nb_not_read . ') - ');
  22. } else {
  23. View::prependTitle (' - ');
  24. }
  25. $entryDAO = new EntryDAO ();
  26. $feedDAO = new FeedDAO ();
  27. $catDAO = new CategoryDAO ();
  28. $this->view->cat_aside = $catDAO->listCategories ();
  29. $this->view->nb_favorites = $entryDAO->countFavorites ();
  30. $this->view->nb_total = $entryDAO->count ();
  31. $this->view->get_c = '';
  32. $this->view->get_f = '';
  33. $type = $this->getType ();
  34. $error = $this->checkAndProcessType ($type);
  35. if (!$error) {
  36. // On récupère les différents éléments de filtrage
  37. $this->view->state = $state = Request::param ('state', $this->view->conf->defaultView ());
  38. $filter = Request::param ('search', '');
  39. $this->view->order = $order = Request::param ('order', $this->view->conf->sortOrder ());
  40. $nb = Request::param ('nb', $this->view->conf->postsPerPage ());
  41. $first = Request::param ('next', '');
  42. try {
  43. // EntriesGetter permet de déporter la complexité du filtrage
  44. $getter = new EntriesGetter ($type, $state, $filter, $order, $nb, $first);
  45. $getter->execute ();
  46. $entries = $getter->getPaginator ();
  47. // Si on a récupéré aucun article "non lus"
  48. // on essaye de récupérer tous les articles
  49. if ($state == 'not_read' && $entries->isEmpty ()) {
  50. $this->view->state = 'all';
  51. $getter->_state ('all');
  52. $getter->execute ();
  53. $entries = $getter->getPaginator ();
  54. }
  55. $this->view->entryPaginator = $entries;
  56. } catch(EntriesGetterException $e) {
  57. Minz_Log::record ($e->getMessage (), Minz_Log::NOTICE);
  58. Error::error (
  59. 404,
  60. array ('error' => array (Translate::t ('page_not_found')))
  61. );
  62. }
  63. } else {
  64. Error::error (
  65. 404,
  66. array ('error' => array (Translate::t ('page_not_found')))
  67. );
  68. }
  69. }
  70. /*
  71. * Détermine le type d'article à récupérer :
  72. * "tous", "favoris", "public", "catégorie" ou "flux"
  73. */
  74. private function getType () {
  75. $get = Request::param ('get', 'all');
  76. $typeGet = $get[0];
  77. $id = substr ($get, 2);
  78. $type = null;
  79. if ($get == 'all' || $get == 'favoris' || $get == 'public') {
  80. $type = array (
  81. 'type' => $get,
  82. 'id' => $get
  83. );
  84. } elseif ($typeGet == 'f' || $typeGet == 'c') {
  85. $type = array (
  86. 'type' => $typeGet,
  87. 'id' => $id
  88. );
  89. }
  90. return $type;
  91. }
  92. /*
  93. * Vérifie que la catégorie / flux sélectionné existe
  94. * + Initialise correctement les variables de vue get_c et get_f
  95. * + Initialise le titre
  96. */
  97. private function checkAndProcessType ($type) {
  98. if ($type['type'] == 'all') {
  99. View::prependTitle (Translate::t ('your_rss_feeds'));
  100. $this->view->get_c = $type['type'];
  101. return false;
  102. } elseif ($type['type'] == 'favoris') {
  103. View::prependTitle (Translate::t ('your_favorites'));
  104. $this->view->get_c = $type['type'];
  105. return false;
  106. } elseif ($type['type'] == 'public') {
  107. View::prependTitle (Translate::t ('public'));
  108. $this->view->get_c = $type['type'];
  109. return false;
  110. } elseif ($type['type'] == 'c') {
  111. $catDAO = new CategoryDAO ();
  112. $cat = $catDAO->searchById ($type['id']);
  113. if ($cat) {
  114. View::prependTitle ($cat->name ());
  115. $this->view->get_c = $type['id'];
  116. return false;
  117. } else {
  118. return true;
  119. }
  120. } elseif ($type['type'] == 'f') {
  121. $feedDAO = new FeedDAO ();
  122. $feed = $feedDAO->searchById ($type['id']);
  123. if ($feed) {
  124. View::prependTitle ($feed->name ());
  125. $this->view->get_f = $type['id'];
  126. $this->view->get_c = $feed->category ();
  127. return false;
  128. } else {
  129. return true;
  130. }
  131. } else {
  132. return true;
  133. }
  134. }
  135. public function aboutAction () {
  136. View::prependTitle (Translate::t ('about') . ' - ');
  137. }
  138. public function logsAction () {
  139. if (login_is_conf ($this->view->conf) && !is_logged ()) {
  140. Error::error (
  141. 403,
  142. array ('error' => array (Translate::t ('access_denied')))
  143. );
  144. }
  145. View::prependTitle (Translate::t ('logs') . ' - ');
  146. $logs = array();
  147. try {
  148. $logDAO = new LogDAO ();
  149. $logs = $logDAO->lister ();
  150. $logs = array_reverse ($logs);
  151. } catch(FileNotExistException $e) {
  152. }
  153. //gestion pagination
  154. $page = Request::param ('page', 1);
  155. $this->view->logsPaginator = new Paginator ($logs);
  156. $this->view->logsPaginator->_nbItemsPerPage (50);
  157. $this->view->logsPaginator->_currentPage ($page);
  158. }
  159. public function loginAction () {
  160. $this->view->_useLayout (false);
  161. $url = 'https://verifier.login.persona.org/verify';
  162. $assert = Request::param ('assertion');
  163. $params = 'assertion=' . $assert . '&audience=' .
  164. urlencode (Url::display () . ':80');
  165. $ch = curl_init ();
  166. $options = array (
  167. CURLOPT_URL => $url,
  168. CURLOPT_RETURNTRANSFER => TRUE,
  169. CURLOPT_POST => 2,
  170. CURLOPT_POSTFIELDS => $params
  171. );
  172. curl_setopt_array ($ch, $options);
  173. $result = curl_exec ($ch);
  174. curl_close ($ch);
  175. $res = json_decode ($result, true);
  176. if ($res['status'] == 'okay' && $res['email'] == $this->view->conf->mailLogin ()) {
  177. Session::_param ('mail', $res['email']);
  178. } else {
  179. $res = array ();
  180. $res['status'] = 'failure';
  181. $res['reason'] = Translate::t ('invalid_login');
  182. }
  183. $this->view->res = json_encode ($res);
  184. }
  185. public function logoutAction () {
  186. $this->view->_useLayout (false);
  187. Session::_param ('mail');
  188. }
  189. }