indexController.php 6.0 KB

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