categoryController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Controller to handle actions relative to categories.
  5. * User needs to be connected.
  6. */
  7. class FreshRSS_category_Controller extends FreshRSS_ActionController {
  8. /**
  9. * This action is called before every other action in that class. It is
  10. * the common boiler plate for every action. It is triggered by the
  11. * underlying framework.
  12. *
  13. */
  14. #[\Override]
  15. public function firstAction(): void {
  16. if (!FreshRSS_Auth::hasAccess()) {
  17. Minz_Error::error(403);
  18. }
  19. $catDAO = FreshRSS_Factory::createCategoryDao();
  20. $catDAO->checkDefault();
  21. }
  22. /**
  23. * This action creates a new category.
  24. *
  25. * Request parameter is:
  26. * - new-category
  27. */
  28. public function createAction(): void {
  29. $catDAO = FreshRSS_Factory::createCategoryDao();
  30. $tagDAO = FreshRSS_Factory::createTagDao();
  31. $url_redirect = ['c' => 'subscription', 'a' => 'add'];
  32. $limits = FreshRSS_Context::systemConf()->limits;
  33. $this->view->categories = $catDAO->listCategories(prePopulateFeeds: false);
  34. if (count($this->view->categories) >= $limits['max_categories']) {
  35. Minz_Request::bad(_t('feedback.sub.category.over_max', $limits['max_categories']), $url_redirect);
  36. }
  37. if (Minz_Request::isPost()) {
  38. invalidateHttpCache();
  39. $cat_name = Minz_Request::paramString('new-category');
  40. if ($cat_name === '') {
  41. Minz_Request::bad(_t('feedback.sub.category.no_name'), $url_redirect);
  42. }
  43. $cat = new FreshRSS_Category($cat_name);
  44. if ($catDAO->searchByName($cat->name()) != null) {
  45. Minz_Request::bad(_t('feedback.sub.category.name_exists'), $url_redirect);
  46. }
  47. if ($tagDAO->searchByName($cat->name()) != null) {
  48. Minz_Request::bad(_t('feedback.tag.name_exists', $cat->name()), $url_redirect);
  49. }
  50. $opml_url = FreshRSS_http_Util::checkUrl(Minz_Request::paramString('opml_url', plaintext: true));
  51. if ($opml_url != '') {
  52. $cat->_kind(FreshRSS_Category::KIND_DYNAMIC_OPML);
  53. $cat->_attribute('opml_url', $opml_url);
  54. } else {
  55. $cat->_kind(FreshRSS_Category::KIND_NORMAL);
  56. $cat->_attribute('opml_url', null);
  57. }
  58. if ($catDAO->addCategoryObject($cat)) {
  59. $url_redirect['a'] = 'index';
  60. Minz_Request::good(
  61. _t('feedback.sub.category.created', $cat->name()),
  62. $url_redirect,
  63. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  64. );
  65. } else {
  66. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  67. }
  68. }
  69. Minz_Request::forward($url_redirect, true);
  70. }
  71. /**
  72. * This action updates the given category.
  73. */
  74. public function updateAction(): void {
  75. if (Minz_Request::paramBoolean('ajax')) {
  76. $this->view->_layout(null);
  77. }
  78. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  79. $id = Minz_Request::paramInt('id');
  80. $category = $categoryDAO->searchById($id);
  81. if ($id === 0 || null === $category) {
  82. Minz_Error::error(404);
  83. return;
  84. }
  85. $this->view->category = $category;
  86. FreshRSS_View::prependTitle($category->name() . ' · ' . _t('sub.title') . ' · ');
  87. if (Minz_Request::isPost()) {
  88. if (Minz_Request::paramBoolean('enable_read_when_same_title_in_category')) {
  89. $category->_attribute('read_when_same_title_in_category', Minz_Request::paramInt('read_when_same_title_in_category'));
  90. } else {
  91. $category->_attribute('read_when_same_title_in_category', null);
  92. }
  93. $category->_filtersAction('read', Minz_Request::paramTextToArray('filteractions_read')); // Keep as HTML
  94. if (Minz_Request::paramBoolean('use_default_purge_options')) {
  95. $category->_attribute('archiving', null);
  96. } else {
  97. if (!Minz_Request::paramBoolean('enable_keep_max')) {
  98. $keepMax = false;
  99. } elseif (($keepMax = Minz_Request::paramInt('keep_max')) === 0) {
  100. $keepMax = FreshRSS_Feed::ARCHIVING_RETENTION_COUNT_LIMIT;
  101. }
  102. if (Minz_Request::paramBoolean('enable_keep_period')) {
  103. $keepPeriod = FreshRSS_Feed::ARCHIVING_RETENTION_PERIOD;
  104. if (is_numeric(Minz_Request::paramString('keep_period_count')) && preg_match('/^PT?1[YMWDH]$/', Minz_Request::paramString('keep_period_unit'))) {
  105. $keepPeriod = str_replace('1', Minz_Request::paramString('keep_period_count'), Minz_Request::paramString('keep_period_unit'));
  106. }
  107. } else {
  108. $keepPeriod = false;
  109. }
  110. $category->_attribute('archiving', [
  111. 'keep_period' => $keepPeriod,
  112. 'keep_max' => $keepMax,
  113. 'keep_min' => Minz_Request::paramInt('keep_min'),
  114. 'keep_favourites' => Minz_Request::paramBoolean('keep_favourites'),
  115. 'keep_labels' => Minz_Request::paramBoolean('keep_labels'),
  116. 'keep_unreads' => Minz_Request::paramBoolean('keep_unreads'),
  117. ]);
  118. }
  119. $position = Minz_Request::paramInt('position') ?: null;
  120. $category->_attribute('position', $position);
  121. $opml_url = FreshRSS_http_Util::checkUrl(Minz_Request::paramString('opml_url', plaintext: true));
  122. if ($opml_url != '') {
  123. $category->_kind(FreshRSS_Category::KIND_DYNAMIC_OPML);
  124. $category->_attribute('opml_url', $opml_url);
  125. } else {
  126. $category->_kind(FreshRSS_Category::KIND_NORMAL);
  127. $category->_attribute('opml_url', null);
  128. }
  129. $values = [
  130. 'kind' => $category->kind(),
  131. 'name' => Minz_Request::paramString('name'),
  132. 'attributes' => $category->attributes(),
  133. ];
  134. invalidateHttpCache();
  135. $url_redirect = ['c' => 'subscription', 'params' => ['id' => $id, 'type' => 'category']];
  136. if (false !== $categoryDAO->updateCategory($id, $values)) {
  137. Minz_Request::good(
  138. _t('feedback.sub.category.updated'),
  139. $url_redirect,
  140. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  141. );
  142. } else {
  143. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  144. }
  145. }
  146. }
  147. public function viewFilterAction(): void {
  148. $id = Minz_Request::paramInt('id');
  149. if ($id === 0) {
  150. Minz_Error::error(400);
  151. return;
  152. }
  153. $filteractions = Minz_Request::paramTextToArray('filteractions_read', plaintext: true);
  154. $filteractions = array_map(fn(string $action): string => trim($action), $filteractions);
  155. $filteractions = array_filter($filteractions, fn(string $action): bool => $action !== '');
  156. $search = "c:$id (";
  157. foreach ($filteractions as $action) {
  158. $search .= "($action) OR ";
  159. }
  160. $search = preg_replace('/ OR $/', '', $search);
  161. $search .= ')';
  162. Minz_Request::forward([
  163. 'c' => 'index',
  164. 'a' => 'index',
  165. 'params' => [
  166. 'search' => $search,
  167. ],
  168. ], redirect: true);
  169. }
  170. /**
  171. * This action deletes a category.
  172. * Feeds in the given category are moved in the default category.
  173. * Related user queries are deleted too.
  174. *
  175. * Request parameter is:
  176. * - id (of a category)
  177. */
  178. public function deleteAction(): void {
  179. $feedDAO = FreshRSS_Factory::createFeedDao();
  180. $catDAO = FreshRSS_Factory::createCategoryDao();
  181. $url_redirect = ['c' => 'subscription', 'a' => 'index'];
  182. if (Minz_Request::isPost()) {
  183. invalidateHttpCache();
  184. $id = Minz_Request::paramInt('id');
  185. if ($id === 0) {
  186. Minz_Request::bad(_t('feedback.sub.category.no_id'), $url_redirect);
  187. }
  188. if ($id === FreshRSS_CategoryDAO::DEFAULTCATEGORYID) {
  189. Minz_Request::bad(_t('feedback.sub.category.not_delete_default'), $url_redirect);
  190. }
  191. if ($feedDAO->changeCategory($id, FreshRSS_CategoryDAO::DEFAULTCATEGORYID) === false) {
  192. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  193. }
  194. if ($catDAO->deleteCategory($id) === false) {
  195. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  196. }
  197. // Remove related queries.
  198. $queries = FreshRSS_UserQuery::remove_query_by_get('c_' . $id, FreshRSS_Context::userConf()->queries);
  199. FreshRSS_Context::userConf()->queries = $queries;
  200. FreshRSS_Context::userConf()->save();
  201. Minz_Request::good(
  202. _t('feedback.sub.category.deleted'),
  203. $url_redirect,
  204. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  205. );
  206. }
  207. Minz_Request::forward($url_redirect, true);
  208. }
  209. /**
  210. * This action deletes all the feeds relative to a given category.
  211. * Feed-related queries are deleted.
  212. *
  213. * Request parameter is:
  214. * - id (of a category)
  215. * - muted (truthy to remove only muted feeds, or falsy otherwise)
  216. */
  217. public function emptyAction(): void {
  218. $feedDAO = FreshRSS_Factory::createFeedDao();
  219. $url_redirect = ['c' => 'subscription', 'a' => 'index'];
  220. if (Minz_Request::isPost()) {
  221. invalidateHttpCache();
  222. $id = Minz_Request::paramInt('id');
  223. if ($id === 0) {
  224. Minz_Request::bad(_t('feedback.sub.category.no_id'), $url_redirect);
  225. }
  226. $muted = Minz_Request::paramTernary('muted');
  227. $errored = Minz_Request::paramTernary('errored');
  228. // List feeds to remove then related user queries.
  229. $feeds = $feedDAO->listByCategory($id, $muted, $errored);
  230. if ($feedDAO->deleteFeedByCategory($id, $muted, $errored)) {
  231. // TODO: Delete old favicons
  232. // Remove related queries
  233. foreach ($feeds as $feed) {
  234. $queries = FreshRSS_UserQuery::remove_query_by_get('f_' . $feed->id(), FreshRSS_Context::userConf()->queries);
  235. FreshRSS_Context::userConf()->queries = $queries;
  236. }
  237. FreshRSS_Context::userConf()->save();
  238. Minz_Request::good(
  239. _t('feedback.sub.category.emptied'),
  240. $url_redirect,
  241. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  242. );
  243. } else {
  244. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  245. }
  246. }
  247. Minz_Request::forward($url_redirect, true);
  248. }
  249. /**
  250. * Request parameter is:
  251. * - id (of a category)
  252. */
  253. public function refreshOpmlAction(): void {
  254. $catDAO = FreshRSS_Factory::createCategoryDao();
  255. $url_redirect = ['c' => 'subscription', 'a' => 'index'];
  256. if (Minz_Request::isPost()) {
  257. invalidateHttpCache();
  258. $id = Minz_Request::paramInt('id');
  259. if ($id === 0) {
  260. Minz_Request::bad(_t('feedback.sub.category.no_id'), $url_redirect);
  261. return;
  262. }
  263. $category = $catDAO->searchById($id);
  264. if ($category === null) {
  265. Minz_Request::bad(_t('feedback.sub.category.not_exist'), $url_redirect);
  266. return;
  267. }
  268. invalidateHttpCache();
  269. $ok = $category->refreshDynamicOpml();
  270. if (Minz_Request::paramBoolean('ajax')) {
  271. Minz_Request::setGoodNotification(_t('feedback.sub.category.updated'));
  272. $this->view->_layout(null);
  273. } else {
  274. if ($ok) {
  275. Minz_Request::good(
  276. _t('feedback.sub.category.updated'),
  277. $url_redirect,
  278. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  279. );
  280. } else {
  281. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  282. }
  283. Minz_Request::forward($url_redirect, true);
  284. }
  285. }
  286. }
  287. /** @return array<string,int> */
  288. public static function refreshDynamicOpmls(): array {
  289. $successes = 0;
  290. $errors = 0;
  291. $catDAO = FreshRSS_Factory::createCategoryDao();
  292. $categories = $catDAO->listCategoriesOrderUpdate(FreshRSS_Context::userConf()->dynamic_opml_ttl_default ?? 86400);
  293. foreach ($categories as $category) {
  294. if ($category->refreshDynamicOpml()) {
  295. $successes++;
  296. } else {
  297. $errors++;
  298. }
  299. }
  300. return [
  301. 'successes' => $successes,
  302. 'errors' => $errors,
  303. ];
  304. }
  305. }