indexController.php 6.6 KB

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