indexController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * This class handles main actions of FreshRSS.
  4. */
  5. class FreshRSS_index_Controller extends Minz_ActionController {
  6. public function indexAction() {
  7. // TODO: update the context with information from request.
  8. // TODO: then, in dedicated action, get corresponding entries
  9. $prefered_output = FreshRSS_Context::$conf->view_mode;
  10. Minz_Request::forward(array(
  11. 'c' => 'index',
  12. 'a' => $prefered_output
  13. ));
  14. }
  15. /**
  16. * This action displays the normal view of FreshRSS.
  17. */
  18. public function normalAction() {
  19. if (!FreshRSS_Auth::hasAccess() && !Minz_Configuration::allowAnonymous()) {
  20. Minz_Request::forward(array('c' => 'auth', 'a' => 'login'));
  21. return;
  22. }
  23. try {
  24. $this->updateContext();
  25. } catch (Minz_Exception $e) {
  26. Minz_Error::error(404);
  27. }
  28. try {
  29. $entries = $this->listByContext();
  30. if (count($entries) > FreshRSS_Context::$number) {
  31. // We have more elements for pagination
  32. $last_entry = array_pop($entries);
  33. FreshRSS_Context::$next_id = $last_entry->id();
  34. }
  35. $this->view->entries = $entries;
  36. } catch (FreshRSS_EntriesGetter_Exception $e) {
  37. Minz_Log::notice($e->getMessage());
  38. Minz_Error::error(404);
  39. }
  40. $this->view->categories = FreshRSS_Context::$categories;
  41. $title = FreshRSS_Context::$name;
  42. if (FreshRSS_Context::$get_unread > 0) {
  43. $title = '(' . FreshRSS_Context::$get_unread . ') · ' . $title;
  44. }
  45. Minz_View::prependTitle($title . ' · ');
  46. }
  47. /**
  48. * This action displays the global view of FreshRSS.
  49. */
  50. public function globalAction() {
  51. if (!FreshRSS_Auth::hasAccess() && !Minz_Configuration::allowAnonymous()) {
  52. Minz_Request::forward(array('c' => 'auth', 'a' => 'login'));
  53. return;
  54. }
  55. Minz_View::appendScript(Minz_Url::display('/scripts/global_view.js?' . @filemtime(PUBLIC_PATH . '/scripts/global_view.js')));
  56. try {
  57. $this->updateContext();
  58. } catch (Minz_Exception $e) {
  59. Minz_Error::error(404);
  60. }
  61. $this->view->categories = FreshRSS_Context::$categories;
  62. Minz_View::prependTitle(_t('gen.title.global_view') . ' · ');
  63. }
  64. /**
  65. * This action displays the RSS feed of FreshRSS.
  66. */
  67. public function rssAction() {
  68. $token = FreshRSS_Context::$conf->token;
  69. $token_param = Minz_Request::param('token', '');
  70. $token_is_ok = ($token != '' && $token === $token_param);
  71. // Check if user has access.
  72. if (!FreshRSS_Auth::hasAccess() &&
  73. !Minz_Configuration::allowAnonymous() &&
  74. !$token_is_ok) {
  75. Minz_Error::error(403);
  76. }
  77. try {
  78. $this->updateContext();
  79. } catch (Minz_Exception $e) {
  80. Minz_Error::error(404);
  81. }
  82. try {
  83. $this->view->entries = $this->listByContext();
  84. } catch (FreshRSS_EntriesGetter_Exception $e) {
  85. Minz_Log::notice($e->getMessage());
  86. Minz_Error::error(404);
  87. }
  88. // No layout for RSS output.
  89. $this->view->rss_title = FreshRSS_Context::$name . ' | ' . Minz_View::title();
  90. $this->view->_useLayout(false);
  91. header('Content-Type: application/rss+xml; charset=utf-8');
  92. }
  93. /**
  94. * This action updates the Context object by using request parameters.
  95. */
  96. private function updateContext() {
  97. FreshRSS_Context::_get(Minz_Request::param('get', 'a'));
  98. FreshRSS_Context::$state |= Minz_Request::param(
  99. 'state', FreshRSS_Context::$conf->default_view
  100. );
  101. if (FreshRSS_Context::$state & FreshRSS_Entry::STATE_NOT_READ &&
  102. FreshRSS_Context::$get_unread <= 0) {
  103. FreshRSS_Context::$state |= FreshRSS_Entry::STATE_READ;
  104. }
  105. FreshRSS_Context::$search = Minz_Request::param('search', '');
  106. FreshRSS_Context::$order = Minz_Request::param(
  107. 'order', FreshRSS_Context::$conf->sort_order
  108. );
  109. FreshRSS_Context::$number = Minz_Request::param(
  110. 'nb', FreshRSS_Context::$conf->posts_per_page
  111. );
  112. FreshRSS_Context::$first_id = Minz_Request::param('next', '');
  113. }
  114. private function listByContext() {
  115. $entryDAO = FreshRSS_Factory::createEntryDao();
  116. $get = FreshRSS_Context::currentGet(true);
  117. if (count($get) > 1) {
  118. $type = $get[0];
  119. $id = $get[1];
  120. } else {
  121. $type = $get;
  122. $id = '';
  123. }
  124. return $entryDAO->listWhere(
  125. $type, $id, FreshRSS_Context::$state, FreshRSS_Context::$order,
  126. FreshRSS_Context::$number + 1, FreshRSS_Context::$first_id,
  127. FreshRSS_Context::$search
  128. );
  129. }
  130. /**
  131. * This action displays the about page of FreshRSS.
  132. */
  133. public function aboutAction() {
  134. Minz_View::prependTitle(_t('about') . ' · ');
  135. }
  136. /**
  137. * This action displays logs of FreshRSS for the current user.
  138. */
  139. public function logsAction() {
  140. if (!FreshRSS_Auth::hasAccess()) {
  141. Minz_Error::error(403);
  142. }
  143. Minz_View::prependTitle(_t('logs') . ' · ');
  144. if (Minz_Request::isPost()) {
  145. FreshRSS_LogDAO::truncate();
  146. }
  147. $logs = FreshRSS_LogDAO::lines(); //TODO: ask only the necessary lines
  148. //gestion pagination
  149. $page = Minz_Request::param('page', 1);
  150. $this->view->logsPaginator = new Minz_Paginator($logs);
  151. $this->view->logsPaginator->_nbItemsPerPage(50);
  152. $this->view->logsPaginator->_currentPage($page);
  153. }
  154. }