indexController.php 7.5 KB

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