4
0

entryController.php 6.9 KB

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