indexController.php 6.1 KB

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