indexController.php 7.9 KB

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