indexController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. class indexController extends ActionController {
  3. private $get = false;
  4. private $nb_not_read = 0;
  5. private $mode = 'all'; //TODO: Is this used?
  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 ('/scripts/main.js'));
  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->currentName = '';
  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. $this->view->currentName = Translate::t ('your_rss_feeds');
  104. View::prependTitle ($this->view->currentName);
  105. $this->view->get_c = $type['type'];
  106. return false;
  107. } elseif ($type['type'] == 'favoris') {
  108. $this->view->currentName = Translate::t ('your_favorites');
  109. View::prependTitle ($this->view->currentName);
  110. $this->view->get_c = $type['type'];
  111. return false;
  112. } elseif ($type['type'] == 'public') {
  113. $this->view->currentName = Translate::t ('public');
  114. View::prependTitle ($this->view->currentName);
  115. $this->view->get_c = $type['type'];
  116. return false;
  117. } elseif ($type['type'] == 'c') {
  118. $catDAO = new CategoryDAO ();
  119. $cat = $catDAO->searchById ($type['id']);
  120. if ($cat) {
  121. $this->view->currentName = $cat->name ();
  122. $nbnr = $cat->nbNotRead ();
  123. View::prependTitle ($this->view->currentName . ($nbnr > 0 ? ' (' . $nbnr . ')' : ''));
  124. $this->view->get_c = $type['id'];
  125. return false;
  126. } else {
  127. return true;
  128. }
  129. } elseif ($type['type'] == 'f') {
  130. $feedDAO = new FeedDAO ();
  131. $feed = $feedDAO->searchById ($type['id']);
  132. if ($feed) {
  133. $this->view->currentName = $feed->name ();
  134. $nbnr = $feed->nbNotRead ();
  135. View::prependTitle ($this->view->currentName . ($nbnr > 0 ? ' (' . $nbnr . ')' : ''));
  136. $this->view->get_f = $type['id'];
  137. $this->view->get_c = $feed->category ();
  138. return false;
  139. } else {
  140. return true;
  141. }
  142. } else {
  143. return true;
  144. }
  145. }
  146. public function aboutAction () {
  147. View::prependTitle (Translate::t ('about') . ' - ');
  148. }
  149. public function logsAction () {
  150. if (login_is_conf ($this->view->conf) && !is_logged ()) {
  151. Error::error (
  152. 403,
  153. array ('error' => array (Translate::t ('access_denied')))
  154. );
  155. }
  156. View::prependTitle (Translate::t ('logs') . ' - ');
  157. $logs = array();
  158. try {
  159. $logDAO = new LogDAO ();
  160. $logs = $logDAO->lister ();
  161. $logs = array_reverse ($logs);
  162. } catch(FileNotExistException $e) {
  163. }
  164. //gestion pagination
  165. $page = Request::param ('page', 1);
  166. $this->view->logsPaginator = new Paginator ($logs);
  167. $this->view->logsPaginator->_nbItemsPerPage (50);
  168. $this->view->logsPaginator->_currentPage ($page);
  169. }
  170. public function loginAction () {
  171. $this->view->_useLayout (false);
  172. $url = 'https://verifier.login.persona.org/verify';
  173. $assert = Request::param ('assertion');
  174. $params = 'assertion=' . $assert . '&audience=' .
  175. urlencode (Url::display () . ':80');
  176. $ch = curl_init ();
  177. $options = array (
  178. CURLOPT_URL => $url,
  179. CURLOPT_RETURNTRANSFER => TRUE,
  180. CURLOPT_POST => 2,
  181. CURLOPT_POSTFIELDS => $params
  182. );
  183. curl_setopt_array ($ch, $options);
  184. $result = curl_exec ($ch);
  185. curl_close ($ch);
  186. $res = json_decode ($result, true);
  187. if ($res['status'] == 'okay' && $res['email'] == $this->view->conf->mailLogin ()) {
  188. Session::_param ('mail', $res['email']);
  189. } else {
  190. $res = array ();
  191. $res['status'] = 'failure';
  192. $res['reason'] = Translate::t ('invalid_login');
  193. }
  194. $this->view->res = json_encode ($res);
  195. }
  196. public function logoutAction () {
  197. $this->view->_useLayout (false);
  198. Session::_param ('mail');
  199. }
  200. }