4
0

indexController.php 6.3 KB

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