indexController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * This class handles main actions of FreshRSS.
  4. */
  5. class FreshRSS_index_Controller extends Minz_ActionController {
  6. private $nb_not_read_cat = 0;
  7. public function indexAction() {
  8. // TODO: update the context with information from request.
  9. // TODO: then, in dedicated action, get corresponding entries
  10. $prefered_output = FreshRSS_Context::$conf->view_mode;
  11. Minz_Request::forward(array(
  12. 'c' => 'index',
  13. 'a' => $prefered_output
  14. ));
  15. return;
  16. $this->view->nb_favorites = $entryDAO->countUnreadReadFavorites();
  17. $this->view->nb_not_read = FreshRSS_CategoryDAO::CountUnreads($this->view->cat_aside, 1);
  18. $this->view->currentName = '';
  19. $this->view->get_c = '';
  20. $this->view->get_f = '';
  21. $get = Minz_Request::param('get', 'a');
  22. $getType = $get[0];
  23. $getId = substr($get, 2);
  24. if (!$this->checkAndProcessType($getType, $getId)) {
  25. Minz_Log::debug('Not found [' . $getType . '][' . $getId . ']');
  26. Minz_Error::error(404);
  27. return;
  28. }
  29. // mise à jour des titres
  30. $this->view->rss_title = $this->view->currentName . ' | ' . Minz_View::title();
  31. Minz_View::prependTitle(
  32. ($this->nb_not_read_cat > 0 ? '(' . formatNumber($this->nb_not_read_cat) . ') ' : '') .
  33. $this->view->currentName .
  34. ' · '
  35. );
  36. // On récupère les différents éléments de filtrage
  37. $this->view->state = Minz_Request::param('state', FreshRSS_Context::$conf->default_view);
  38. $state_param = Minz_Request::param('state', null);
  39. $filter = Minz_Request::param('search', '');
  40. $this->view->order = $order = Minz_Request::param('order', FreshRSS_Context::$conf->sort_order);
  41. $nb = Minz_Request::param('nb', FreshRSS_Context::$conf->posts_per_page);
  42. $first = Minz_Request::param('next', '');
  43. $ajax_request = Minz_Request::param('ajax', false);
  44. if ($output === 'reader') {
  45. $nb = max(1, round($nb / 2));
  46. }
  47. if ($this->view->state === FreshRSS_Entry::STATE_NOT_READ) { //Any unread article in this category at all?
  48. switch ($getType) {
  49. case 'a':
  50. $hasUnread = $this->view->nb_not_read > 0;
  51. break;
  52. case 's':
  53. // This is deprecated. The favorite button does not exist anymore
  54. $hasUnread = $this->view->nb_favorites['unread'] > 0;
  55. break;
  56. case 'c':
  57. $hasUnread = (!isset($this->view->cat_aside[$getId]) ||
  58. $this->view->cat_aside[$getId]->nbNotRead() > 0);
  59. break;
  60. case 'f':
  61. $myFeed = FreshRSS_CategoryDAO::findFeed($this->view->cat_aside, $getId);
  62. $hasUnread = ($myFeed === null) || ($myFeed->nbNotRead() > 0);
  63. break;
  64. default:
  65. $hasUnread = true;
  66. break;
  67. }
  68. if (!$hasUnread && ($state_param === null)) {
  69. $this->view->state = FreshRSS_Entry::STATE_ALL;
  70. }
  71. }
  72. $this->view->today = @strtotime('today');
  73. try {
  74. $entries = $entryDAO->listWhere($getType, $getId, $this->view->state, $order, $nb + 1, $first, $filter);
  75. // Si on a récupéré aucun article "non lus"
  76. // on essaye de récupérer tous les articles
  77. if ($this->view->state === FreshRSS_Entry::STATE_NOT_READ && empty($entries) && ($state_param === null) && ($filter == '')) {
  78. Minz_Log::debug('Conflicting information about nbNotRead!');
  79. $feedDAO = FreshRSS_Factory::createFeedDao();
  80. try {
  81. $feedDAO->updateCachedValues();
  82. } catch (Exception $ex) {
  83. Minz_Log::notice('Failed to automatically correct nbNotRead! ' + $ex->getMessage());
  84. }
  85. $this->view->state = FreshRSS_Entry::STATE_ALL;
  86. $entries = $entryDAO->listWhere($getType, $getId, $this->view->state, $order, $nb, $first, $filter);
  87. }
  88. Minz_Request::_param('state', $this->view->state);
  89. if (count($entries) <= $nb) {
  90. $this->view->nextId = '';
  91. } else { //We have more elements for pagination
  92. $lastEntry = array_pop($entries);
  93. $this->view->nextId = $lastEntry->id();
  94. }
  95. $this->view->entries = $entries;
  96. } catch (FreshRSS_EntriesGetter_Exception $e) {
  97. Minz_Log::notice($e->getMessage());
  98. Minz_Error::error(404);
  99. }
  100. }
  101. /*
  102. * Vérifie que la catégorie / flux sélectionné existe
  103. * + Initialise correctement les variables de vue get_c et get_f
  104. * + Met à jour la variable $this->nb_not_read_cat
  105. */
  106. private function checkAndProcessType($getType, $getId) {
  107. switch($getType) {
  108. case 'a':
  109. $this->view->currentName = _t('your_rss_feeds');
  110. $this->nb_not_read_cat = $this->view->nb_not_read;
  111. $this->view->get_c = $getType;
  112. return true;
  113. case 's':
  114. $this->view->currentName = _t('your_favorites');
  115. $this->nb_not_read_cat = $this->view->nb_favorites['unread'];
  116. $this->view->get_c = $getType;
  117. return true;
  118. case 'c':
  119. $cat = isset($this->view->cat_aside[$getId]) ? $this->view->cat_aside[$getId] : null;
  120. if ($cat === null) {
  121. $catDAO = new FreshRSS_CategoryDAO();
  122. $cat = $catDAO->searchById($getId);
  123. }
  124. if ($cat) {
  125. $this->view->currentName = $cat->name();
  126. $this->nb_not_read_cat = $cat->nbNotRead();
  127. $this->view->get_c = $getId;
  128. return true;
  129. } else {
  130. return false;
  131. }
  132. case 'f':
  133. $feed = FreshRSS_CategoryDAO::findFeed($this->view->cat_aside, $getId);
  134. if (empty($feed)) {
  135. $feedDAO = FreshRSS_Factory::createFeedDao();
  136. $feed = $feedDAO->searchById($getId);
  137. }
  138. if ($feed) {
  139. $this->view->currentName = $feed->name();
  140. $this->nb_not_read_cat = $feed->nbNotRead();
  141. $this->view->get_f = $getId;
  142. $this->view->get_c = $feed->category();
  143. return true;
  144. } else {
  145. return false;
  146. }
  147. default:
  148. return false;
  149. }
  150. }
  151. /**
  152. * This action displays the normal view of FreshRSS.
  153. */
  154. public function normalAction() {
  155. if (!FreshRSS_Auth::hasAccess() && !Minz_Configuration::allowAnonymous()) {
  156. Minz_Request::forward(array('c' => 'auth', 'a' => 'login'));
  157. return;
  158. }
  159. $catDAO = new FreshRSS_CategoryDAO();
  160. $entryDAO = FreshRSS_Factory::createEntryDao();
  161. $this->view->categories = $catDAO->listCategories();
  162. }
  163. /**
  164. * This action displays the global view of FreshRSS.
  165. */
  166. public function globalAction() {
  167. if (!FreshRSS_Auth::hasAccess() && !Minz_Configuration::allowAnonymous()) {
  168. Minz_Request::forward(array('c' => 'auth', 'a' => 'login'));
  169. return;
  170. }
  171. Minz_View::appendScript(Minz_Url::display('/scripts/global_view.js?' . @filemtime(PUBLIC_PATH . '/scripts/global_view.js')));
  172. $catDAO = new FreshRSS_CategoryDAO();
  173. $this->view->categories = $catDAO->listCategories();
  174. Minz_View::prependTitle(_t('gen.title.global_view') . ' · ');
  175. }
  176. /**
  177. * This action displays the RSS feed of FreshRSS.
  178. */
  179. public function rssAction() {
  180. $token = FreshRSS_Context::$conf->token;
  181. $token_param = Minz_Request::param('token', '');
  182. $token_is_ok = ($token != '' && $token === $token_param);
  183. // Check if user has access.
  184. if (!FreshRSS_Auth::hasAccess() &&
  185. !Minz_Configuration::allowAnonymous() &&
  186. !$token_is_ok) {
  187. Minz_Error::error(403);
  188. }
  189. // No layout for RSS output.
  190. $this->view->_useLayout(false);
  191. header('Content-Type: application/rss+xml; charset=utf-8');
  192. }
  193. /**
  194. * This action displays the about page of FreshRSS.
  195. */
  196. public function aboutAction() {
  197. Minz_View::prependTitle(_t('about') . ' · ');
  198. }
  199. /**
  200. * This action displays logs of FreshRSS for the current user.
  201. */
  202. public function logsAction() {
  203. if (!FreshRSS_Auth::hasAccess()) {
  204. Minz_Error::error(403);
  205. }
  206. Minz_View::prependTitle(_t('logs') . ' · ');
  207. if (Minz_Request::isPost()) {
  208. FreshRSS_LogDAO::truncate();
  209. }
  210. $logs = FreshRSS_LogDAO::lines(); //TODO: ask only the necessary lines
  211. //gestion pagination
  212. $page = Minz_Request::param('page', 1);
  213. $this->view->logsPaginator = new Minz_Paginator($logs);
  214. $this->view->logsPaginator->_nbItemsPerPage(50);
  215. $this->view->logsPaginator->_currentPage($page);
  216. }
  217. }