entryController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * Controller to handle every entry actions.
  4. */
  5. class FreshRSS_entry_Controller extends FreshRSS_ActionController {
  6. /**
  7. * JavaScript request or not.
  8. * @var bool
  9. */
  10. private $ajax = false;
  11. /**
  12. * This action is called before every other action in that class. It is
  13. * the common boilerplate for every action. It is triggered by the
  14. * underlying framework.
  15. */
  16. public function firstAction(): void {
  17. if (!FreshRSS_Auth::hasAccess()) {
  18. Minz_Error::error(403);
  19. }
  20. // If ajax request, we do not print layout
  21. $this->ajax = Minz_Request::paramBoolean('ajax');
  22. if ($this->ajax) {
  23. $this->view->_layout(null);
  24. Minz_Request::_param('ajax');
  25. }
  26. }
  27. /**
  28. * Mark one or several entries as read (or not!).
  29. *
  30. * If request concerns several entries, it MUST be a POST request.
  31. * If request concerns several entries, only mark them as read is available.
  32. *
  33. * Parameters are:
  34. * - id (default: false)
  35. * - get (default: false) /(c_\d+|f_\d+|s|a)/
  36. * - nextGet (default: $get)
  37. * - idMax (default: 0)
  38. * - is_read (default: true)
  39. */
  40. public function readAction(): void {
  41. $id = Minz_Request::param('id');
  42. $get = Minz_Request::paramString('get');
  43. $next_get = Minz_Request::paramString('nextGet') ?: $get;
  44. $id_max = Minz_Request::paramString('idMax') ?: '0';
  45. $is_read = Minz_Request::paramTernary('is_read') ?? true;
  46. FreshRSS_Context::$search = new FreshRSS_BooleanSearch(Minz_Request::paramString('search'));
  47. FreshRSS_Context::$state = Minz_Request::paramInt('state');
  48. if (FreshRSS_Context::isStateEnabled(FreshRSS_Entry::STATE_FAVORITE)) {
  49. FreshRSS_Context::$state = FreshRSS_Entry::STATE_FAVORITE;
  50. } elseif (FreshRSS_Context::isStateEnabled(FreshRSS_Entry::STATE_NOT_FAVORITE)) {
  51. FreshRSS_Context::$state = FreshRSS_Entry::STATE_NOT_FAVORITE;
  52. } else {
  53. FreshRSS_Context::$state = 0;
  54. }
  55. $params = [];
  56. $this->view->tagsForEntries = [];
  57. $entryDAO = FreshRSS_Factory::createEntryDao();
  58. if ($id == false) {
  59. // id is false? It MUST be a POST request!
  60. if (!Minz_Request::isPost()) {
  61. Minz_Request::bad(_t('feedback.access.not_found'), ['c' => 'index', 'a' => 'index']);
  62. return;
  63. }
  64. if (!$get) {
  65. // No get? Mark all entries as read (from $id_max)
  66. $entryDAO->markReadEntries($id_max, false, 0, null, 0, $is_read);
  67. } else {
  68. $type_get = $get[0];
  69. $get = (int)substr($get, 2);
  70. switch($type_get) {
  71. case 'c':
  72. $entryDAO->markReadCat($get, $id_max, FreshRSS_Context::$search, FreshRSS_Context::$state, $is_read);
  73. break;
  74. case 'f':
  75. $entryDAO->markReadFeed($get, $id_max, FreshRSS_Context::$search, FreshRSS_Context::$state, $is_read);
  76. break;
  77. case 's':
  78. $entryDAO->markReadEntries($id_max, true, 0, FreshRSS_Context::$search, FreshRSS_Context::$state, $is_read);
  79. break;
  80. case 'a':
  81. $entryDAO->markReadEntries($id_max, false, 0, FreshRSS_Context::$search, FreshRSS_Context::$state, $is_read);
  82. break;
  83. case 't':
  84. $entryDAO->markReadTag($get, $id_max, FreshRSS_Context::$search, FreshRSS_Context::$state, $is_read);
  85. break;
  86. case 'T':
  87. $entryDAO->markReadTag(0, $id_max, FreshRSS_Context::$search, FreshRSS_Context::$state, $is_read);
  88. break;
  89. }
  90. if ($next_get !== 'a') {
  91. // Redirect to the correct page (category, feed or starred)
  92. // Not "a" because it is the default value if nothing is given.
  93. $params['get'] = $next_get;
  94. }
  95. }
  96. } else {
  97. $ids = is_array($id) ? $id : [$id];
  98. $entryDAO->markRead($ids, $is_read);
  99. $tagDAO = FreshRSS_Factory::createTagDao();
  100. $tagsForEntries = $tagDAO->getTagsForEntries($ids) ?: [];
  101. $tags = [];
  102. foreach ($tagsForEntries as $line) {
  103. $tags['t_' . $line['id_tag']][] = $line['id_entry'];
  104. }
  105. $this->view->tagsForEntries = $tags;
  106. }
  107. if (!$this->ajax) {
  108. Minz_Request::good(
  109. $is_read ? _t('feedback.sub.articles.marked_read') : _t('feedback.sub.articles.marked_unread'),
  110. [
  111. 'c' => 'index',
  112. 'a' => 'index',
  113. 'params' => $params,
  114. ]
  115. );
  116. }
  117. }
  118. /**
  119. * This action marks an entry as favourite (bookmark) or not.
  120. *
  121. * Parameter is:
  122. * - id (default: false)
  123. * - is_favorite (default: true)
  124. * If id is false, nothing happened.
  125. */
  126. public function bookmarkAction(): void {
  127. $id = Minz_Request::paramString('id');
  128. $is_favourite = Minz_Request::paramTernary('is_favorite') ?? true;
  129. if ($id != '') {
  130. $entryDAO = FreshRSS_Factory::createEntryDao();
  131. $entryDAO->markFavorite($id, $is_favourite);
  132. }
  133. if (!$this->ajax) {
  134. Minz_Request::forward([
  135. 'c' => 'index',
  136. 'a' => 'index',
  137. ], true);
  138. }
  139. }
  140. /**
  141. * This action optimizes database to reduce its size.
  142. *
  143. * This action should be reached by a POST request.
  144. *
  145. * @todo move this action in configure controller.
  146. * @todo call this action through web-cron when available
  147. */
  148. public function optimizeAction(): void {
  149. $url_redirect = [
  150. 'c' => 'configure',
  151. 'a' => 'archiving',
  152. ];
  153. if (!Minz_Request::isPost()) {
  154. Minz_Request::forward($url_redirect, true);
  155. }
  156. if (function_exists('set_time_limit')) {
  157. @set_time_limit(300);
  158. }
  159. $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
  160. $databaseDAO->optimize();
  161. $feedDAO = FreshRSS_Factory::createFeedDao();
  162. $feedDAO->updateCachedValues();
  163. invalidateHttpCache();
  164. Minz_Request::good(_t('feedback.admin.optimization_complete'), $url_redirect);
  165. }
  166. /**
  167. * This action purges old entries from feeds.
  168. *
  169. * @todo should be a POST request
  170. * @todo should be in feedController
  171. */
  172. public function purgeAction(): void {
  173. if (function_exists('set_time_limit')) {
  174. @set_time_limit(300);
  175. }
  176. $feedDAO = FreshRSS_Factory::createFeedDao();
  177. $feeds = $feedDAO->listFeeds();
  178. $nb_total = 0;
  179. invalidateHttpCache();
  180. $feedDAO->beginTransaction();
  181. foreach ($feeds as $feed) {
  182. $nb_total += $feed->cleanOldEntries();
  183. }
  184. $feedDAO->updateCachedValues();
  185. $feedDAO->commit();
  186. $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
  187. $databaseDAO->minorDbMaintenance();
  188. invalidateHttpCache();
  189. Minz_Request::good(_t('feedback.sub.purge_completed', $nb_total), [
  190. 'c' => 'configure',
  191. 'a' => 'archiving',
  192. ]);
  193. }
  194. }