entryController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. * - maxPubDate (default: 0)
  40. * - is_read (default: true)
  41. */
  42. public function readAction(): void {
  43. $get = Minz_Request::paramString('get', plaintext: true);
  44. $next_get = Minz_Request::paramString('nextGet', plaintext: true) ?: $get;
  45. $id_max = Minz_Request::paramString('idMax', plaintext: true);
  46. if (!ctype_digit($id_max)) {
  47. $id_max = '0';
  48. }
  49. $is_read = Minz_Request::paramTernary('is_read') ?? true;
  50. FreshRSS_Context::$search = new FreshRSS_BooleanSearch(Minz_Request::paramString('search', plaintext: true));
  51. $maxPubDate = Minz_Request::paramInt('maxPubDate');
  52. if ($maxPubDate > 0) {
  53. $search = new FreshRSS_Search('');
  54. $search->setMaxPubdate($maxPubDate);
  55. FreshRSS_Context::$search->prepend($search);
  56. }
  57. FreshRSS_Context::$state = Minz_Request::paramInt('state');
  58. if (FreshRSS_Context::isStateEnabled(FreshRSS_Entry::STATE_FAVORITE)) {
  59. if (!FreshRSS_Context::isStateEnabled(FreshRSS_Entry::STATE_NOT_FAVORITE)) {
  60. FreshRSS_Context::$state = FreshRSS_Entry::STATE_FAVORITE;
  61. }
  62. } elseif (FreshRSS_Context::isStateEnabled(FreshRSS_Entry::STATE_NOT_FAVORITE)) {
  63. FreshRSS_Context::$state = FreshRSS_Entry::STATE_NOT_FAVORITE;
  64. } else {
  65. FreshRSS_Context::$state = 0;
  66. }
  67. $params = [];
  68. $this->view->tagsForEntries = [];
  69. $entryDAO = FreshRSS_Factory::createEntryDao();
  70. if (!Minz_Request::hasParam('id')) {
  71. // No id, then it MUST be a POST request
  72. if (!Minz_Request::isPost()) {
  73. Minz_Request::bad(_t('feedback.access.not_found'), ['c' => 'index', 'a' => 'index']);
  74. return;
  75. }
  76. if ($get === '') {
  77. // No get? Mark all entries as read (from $id_max)
  78. $entryDAO->markReadEntries($id_max, false, FreshRSS_Feed::PRIORITY_MAIN_STREAM, FreshRSS_Feed::PRIORITY_IMPORTANT, null, 0, $is_read);
  79. } else {
  80. $type_get = $get[0];
  81. $get = (int)substr($get, 2);
  82. switch ($type_get) {
  83. case 'c':
  84. $entryDAO->markReadCat($get, $id_max, FreshRSS_Context::$search, FreshRSS_Context::$state, $is_read);
  85. break;
  86. case 'f':
  87. $entryDAO->markReadFeed($get, $id_max, FreshRSS_Context::$search, FreshRSS_Context::$state, $is_read);
  88. break;
  89. case 's':
  90. $entryDAO->markReadEntries($id_max, true, null, FreshRSS_Feed::PRIORITY_IMPORTANT,
  91. FreshRSS_Context::$search, FreshRSS_Context::$state, $is_read);
  92. break;
  93. case 'a':
  94. $entryDAO->markReadEntries($id_max, false, FreshRSS_Feed::PRIORITY_MAIN_STREAM, FreshRSS_Feed::PRIORITY_IMPORTANT,
  95. FreshRSS_Context::$search, FreshRSS_Context::$state, $is_read);
  96. break;
  97. case 'A':
  98. $entryDAO->markReadEntries($id_max, false, FreshRSS_Feed::PRIORITY_CATEGORY, FreshRSS_Feed::PRIORITY_IMPORTANT,
  99. FreshRSS_Context::$search, FreshRSS_Context::$state, $is_read);
  100. break;
  101. case 'Z':
  102. $entryDAO->markReadEntries($id_max, false, FreshRSS_Feed::PRIORITY_HIDDEN, FreshRSS_Feed::PRIORITY_IMPORTANT,
  103. FreshRSS_Context::$search, FreshRSS_Context::$state, $is_read);
  104. break;
  105. case 'i':
  106. $entryDAO->markReadEntries($id_max, false, FreshRSS_Feed::PRIORITY_IMPORTANT, null,
  107. FreshRSS_Context::$search, FreshRSS_Context::$state, $is_read);
  108. break;
  109. case 't':
  110. $entryDAO->markReadTag($get, $id_max, FreshRSS_Context::$search, FreshRSS_Context::$state, $is_read);
  111. // Marking all entries in a tag as read can result in other tags also having all entries marked as read,
  112. // so the next unread tag calculation is deferred by passing next_get = 'a' instead of the current get ID.
  113. if ($next_get === 'a' && $is_read) {
  114. $tagDAO = FreshRSS_Factory::createTagDao();
  115. $tagsList = $tagDAO->listTags();
  116. $found_tag = false;
  117. foreach ($tagsList as $tag) {
  118. if ($found_tag) {
  119. // Found the tag matching our current ID already, so now we're just looking for the first unread
  120. if ($tag->nbUnread() > 0) {
  121. $next_get = 't_' . $tag->id();
  122. break;
  123. }
  124. } else {
  125. // Still looking for the tag ID matching our $get that was just marked as read
  126. if ($tag->id() === $get) {
  127. $found_tag = true;
  128. }
  129. }
  130. }
  131. // Didn't find any unread tags after the current one? Start over from the beginning.
  132. if ($next_get === 'a') {
  133. foreach ($tagsList as $tag) {
  134. // Check this first so we can return to the current tag if it's the only one that's unread
  135. if ($tag->nbUnread() > 0) {
  136. $next_get = 't_' . $tag->id();
  137. break;
  138. }
  139. // Give up if reached our first tag again
  140. if ($tag->id() === $get) {
  141. break;
  142. }
  143. }
  144. }
  145. // If we still haven't found any unread tags, fallback to the full tag list
  146. if ($next_get === 'a') {
  147. $next_get = 'T';
  148. }
  149. }
  150. break;
  151. case 'T':
  152. $entryDAO->markReadTag(0, $id_max, FreshRSS_Context::$search, FreshRSS_Context::$state, $is_read);
  153. break;
  154. }
  155. if ($next_get !== 'a') {
  156. // Redirect to the correct page (category, feed or starred)
  157. // Not "a" because it is the default value if nothing is given.
  158. $params['get'] = $next_get;
  159. }
  160. }
  161. } else {
  162. /** @var list<numeric-string> $idArray */
  163. $idArray = Minz_Request::paramArrayString('id', plaintext: true);
  164. $idString = Minz_Request::paramString('id', plaintext: true);
  165. if (count($idArray) > 0) {
  166. $ids = $idArray;
  167. } elseif (ctype_digit($idString)) {
  168. $ids = [$idString];
  169. } else {
  170. $ids = [];
  171. }
  172. $entryDAO->markRead($ids, $is_read);
  173. $tagDAO = FreshRSS_Factory::createTagDao();
  174. $tagsForEntries = $tagDAO->getTagsForEntries($ids) ?? [];
  175. $tags = [];
  176. foreach ($tagsForEntries as $line) {
  177. $tags['t_' . $line['id_tag']][] = (string)$line['id_entry'];
  178. }
  179. $this->view->tagsForEntries = $tags;
  180. }
  181. if (!$this->ajax) {
  182. if (Minz_Request::hasParam('order')) {
  183. $params['order'] = Minz_Request::paramString('order', plaintext: true);
  184. }
  185. if (Minz_Request::hasParam('sort')) {
  186. $params['sort'] = Minz_Request::paramString('sort', plaintext: true);
  187. }
  188. Minz_Request::good(
  189. $is_read ? _t('feedback.sub.articles.marked_read') : _t('feedback.sub.articles.marked_unread'),
  190. [
  191. 'c' => 'index',
  192. 'a' => 'index',
  193. 'params' => $params,
  194. ],
  195. notificationName: 'readAction ',
  196. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  197. );
  198. }
  199. }
  200. /**
  201. * This action marks an entry as favourite (bookmark) or not.
  202. *
  203. * Parameter is:
  204. * - id (default: false)
  205. * - is_favorite (default: true)
  206. * If id is false, nothing happened.
  207. */
  208. public function bookmarkAction(): void {
  209. $id = Minz_Request::paramString('id', plaintext: true);
  210. $is_favourite = Minz_Request::paramTernary('is_favorite') ?? true;
  211. if ($id != '' && ctype_digit($id)) {
  212. $entryDAO = FreshRSS_Factory::createEntryDao();
  213. $entryDAO->markFavorite($id, $is_favourite);
  214. }
  215. if (!$this->ajax) {
  216. Minz_Request::forward([
  217. 'c' => 'index',
  218. 'a' => 'index',
  219. ], true);
  220. }
  221. }
  222. /**
  223. * This action optimizes database to reduce its size.
  224. *
  225. * This action should be reached by a POST request.
  226. *
  227. * @todo move this action in configure controller.
  228. * @todo call this action through web-cron when available
  229. */
  230. public function optimizeAction(): void {
  231. $url_redirect = [
  232. 'c' => 'configure',
  233. 'a' => 'archiving',
  234. ];
  235. if (!Minz_Request::isPost()) {
  236. Minz_Request::forward($url_redirect, true);
  237. }
  238. if (function_exists('set_time_limit')) {
  239. @set_time_limit(300);
  240. }
  241. $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
  242. $databaseDAO->optimize();
  243. $feedDAO = FreshRSS_Factory::createFeedDao();
  244. $feedDAO->updateCachedValues();
  245. invalidateHttpCache();
  246. Minz_Request::good(
  247. _t('feedback.admin.optimization_complete'),
  248. $url_redirect,
  249. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  250. );
  251. }
  252. /**
  253. * This action purges old entries from feeds.
  254. *
  255. * @todo should be in feedController
  256. */
  257. public function purgeAction(): void {
  258. if (!Minz_Request::isPost()) {
  259. Minz_Error::error(403);
  260. return;
  261. }
  262. if (function_exists('set_time_limit')) {
  263. @set_time_limit(300);
  264. }
  265. $feedDAO = FreshRSS_Factory::createFeedDao();
  266. $feeds = $feedDAO->listFeeds();
  267. $nb_total = 0;
  268. invalidateHttpCache();
  269. $feedDAO->beginTransaction();
  270. foreach ($feeds as $feed) {
  271. $nb_total += ($feed->cleanOldEntries() ?: 0);
  272. }
  273. $feedDAO->updateCachedValues();
  274. $feedDAO->commit();
  275. $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
  276. $databaseDAO->minorDbMaintenance();
  277. invalidateHttpCache();
  278. Minz_Request::good(
  279. _t('feedback.sub.purge_completed', $nb_total),
  280. ['c' => 'configure', 'a' => 'archiving'],
  281. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  282. );
  283. }
  284. }