indexController.php 6.2 KB

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