indexController.php 6.2 KB

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