indexController.php 7.1 KB

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