indexController.php 7.1 KB

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