4
0

categoryController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. if (Minz_Request::paramBoolean('enable_read_when_same_guid_in_category')) {
  94. $category->_attribute('read_when_same_guid_in_category', Minz_Request::paramInt('read_when_same_guid_in_category'));
  95. } else {
  96. $category->_attribute('read_when_same_guid_in_category', null);
  97. }
  98. $category->_filtersAction('read', Minz_Request::paramTextToArray('filteractions_read', plaintext: true));
  99. if (Minz_Request::paramBoolean('use_default_purge_options')) {
  100. $category->_attribute('archiving', null);
  101. } else {
  102. if (!Minz_Request::paramBoolean('enable_keep_max')) {
  103. $keepMax = false;
  104. } elseif (($keepMax = Minz_Request::paramInt('keep_max')) === 0) {
  105. $keepMax = FreshRSS_Feed::ARCHIVING_RETENTION_COUNT_LIMIT;
  106. }
  107. if (Minz_Request::paramBoolean('enable_keep_period')) {
  108. $keepPeriod = FreshRSS_Feed::ARCHIVING_RETENTION_PERIOD;
  109. if (is_numeric(Minz_Request::paramString('keep_period_count')) && preg_match('/^PT?1[YMWDH]$/', Minz_Request::paramString('keep_period_unit'))) {
  110. $keepPeriod = str_replace('1', Minz_Request::paramString('keep_period_count'), Minz_Request::paramString('keep_period_unit'));
  111. }
  112. } else {
  113. $keepPeriod = false;
  114. }
  115. $category->_attribute('archiving', [
  116. 'keep_period' => $keepPeriod,
  117. 'keep_max' => $keepMax,
  118. 'keep_min' => Minz_Request::paramInt('keep_min'),
  119. 'keep_favourites' => Minz_Request::paramBoolean('keep_favourites'),
  120. 'keep_labels' => Minz_Request::paramBoolean('keep_labels'),
  121. 'keep_unreads' => Minz_Request::paramBoolean('keep_unreads'),
  122. ]);
  123. }
  124. $position = Minz_Request::paramInt('position') ?: null;
  125. $category->_attribute('position', $position);
  126. $opml_url = FreshRSS_http_Util::checkUrl(Minz_Request::paramString('opml_url', plaintext: true));
  127. if ($opml_url != '') {
  128. $category->_kind(FreshRSS_Category::KIND_DYNAMIC_OPML);
  129. $category->_attribute('opml_url', $opml_url);
  130. } else {
  131. $category->_kind(FreshRSS_Category::KIND_NORMAL);
  132. $category->_attribute('opml_url', null);
  133. }
  134. $defaultSortOrder = Minz_Request::paramString('defaultSortOrder', plaintext: true);
  135. if (str_ends_with($defaultSortOrder, '_asc')) {
  136. $category->_attribute('defaultOrder', 'ASC');
  137. $defaultSortOrder = substr($defaultSortOrder, 0, -strlen('_asc'));
  138. } elseif (str_ends_with($defaultSortOrder, '_desc')) {
  139. $category->_attribute('defaultOrder', 'DESC');
  140. $defaultSortOrder = substr($defaultSortOrder, 0, -strlen('_desc'));
  141. } else {
  142. $category->_attribute('defaultOrder');
  143. }
  144. if (in_array($defaultSortOrder, ['id', 'date', 'link', 'title', 'length', 'f.name', 'rand'], true)) {
  145. $category->_attribute('defaultSort', $defaultSortOrder);
  146. } else {
  147. $category->_attribute('defaultSort');
  148. }
  149. $values = [
  150. 'kind' => $category->kind(),
  151. 'name' => Minz_Request::paramString('name'),
  152. 'attributes' => $category->attributes(),
  153. ];
  154. invalidateHttpCache();
  155. $from = Minz_Request::paramString('from');
  156. $prev_controller = $from === 'update' ? 'category' : 'subscription';
  157. $url_redirect = ['c' => $prev_controller, 'a' => $from, 'params' => ['id' => $id, 'type' => 'category']];
  158. if (false !== $categoryDAO->updateCategory($id, $values)) {
  159. Minz_Request::good(
  160. _t('feedback.sub.category.updated'),
  161. $url_redirect,
  162. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  163. );
  164. } else {
  165. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  166. }
  167. }
  168. }
  169. public function viewFilterAction(): void {
  170. $id = Minz_Request::paramInt('id');
  171. if ($id === 0) {
  172. Minz_Error::error(400);
  173. return;
  174. }
  175. $filteractions = Minz_Request::paramTextToArray('filteractions_read', plaintext: true);
  176. $filteractions = array_map(fn(string $action): string => trim($action), $filteractions);
  177. $filteractions = array_filter($filteractions, fn(string $action): bool => $action !== '');
  178. $search = "c:$id (";
  179. foreach ($filteractions as $action) {
  180. $search .= "($action) OR ";
  181. }
  182. $search = preg_replace('/ OR $/', '', $search);
  183. $search .= ')';
  184. Minz_Request::forward([
  185. 'c' => 'index',
  186. 'a' => 'index',
  187. 'params' => [
  188. 'search' => $search,
  189. ],
  190. ], redirect: true);
  191. }
  192. /**
  193. * This action deletes a category.
  194. * Feeds in the given category are moved in the default category.
  195. * Related user queries are deleted too.
  196. *
  197. * Request parameter is:
  198. * - id (of a category)
  199. */
  200. public function deleteAction(): void {
  201. $feedDAO = FreshRSS_Factory::createFeedDao();
  202. $catDAO = FreshRSS_Factory::createCategoryDao();
  203. $url_redirect = ['c' => 'subscription', 'a' => 'index'];
  204. if (Minz_Request::isPost()) {
  205. invalidateHttpCache();
  206. $id = Minz_Request::paramInt('id');
  207. if ($id === 0) {
  208. Minz_Request::bad(_t('feedback.sub.category.no_id'), $url_redirect);
  209. }
  210. if ($id === FreshRSS_CategoryDAO::DEFAULTCATEGORYID) {
  211. Minz_Request::bad(_t('feedback.sub.category.not_delete_default'), $url_redirect);
  212. }
  213. if ($feedDAO->changeCategory($id, FreshRSS_CategoryDAO::DEFAULTCATEGORYID) === false) {
  214. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  215. }
  216. if ($catDAO->deleteCategory($id) === false) {
  217. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  218. }
  219. // Remove related queries.
  220. $queries = FreshRSS_UserQuery::remove_query_by_get('c_' . $id, FreshRSS_Context::userConf()->queries);
  221. FreshRSS_Context::userConf()->queries = $queries;
  222. FreshRSS_Context::userConf()->save();
  223. Minz_Request::good(
  224. _t('feedback.sub.category.deleted'),
  225. $url_redirect,
  226. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  227. );
  228. }
  229. Minz_Request::forward($url_redirect, true);
  230. }
  231. /**
  232. * This action deletes all the feeds relative to a given category.
  233. * Feed-related queries are deleted.
  234. *
  235. * Request parameter is:
  236. * - id (of a category)
  237. * - muted (truthy to remove only muted feeds, or falsy otherwise)
  238. */
  239. public function emptyAction(): void {
  240. $feedDAO = FreshRSS_Factory::createFeedDao();
  241. $url_redirect = ['c' => 'subscription', 'a' => 'index'];
  242. if (Minz_Request::isPost()) {
  243. invalidateHttpCache();
  244. $id = Minz_Request::paramInt('id');
  245. if ($id === 0) {
  246. Minz_Request::bad(_t('feedback.sub.category.no_id'), $url_redirect);
  247. }
  248. $muted = Minz_Request::paramTernary('muted');
  249. $errored = Minz_Request::paramTernary('errored');
  250. // List feeds to remove then related user queries.
  251. $feeds = $feedDAO->listByCategory($id, $muted, $errored);
  252. if ($feedDAO->deleteFeedByCategory($id, $muted, $errored)) {
  253. // TODO: Delete old favicons
  254. // Remove related queries
  255. foreach ($feeds as $feed) {
  256. $queries = FreshRSS_UserQuery::remove_query_by_get('f_' . $feed->id(), FreshRSS_Context::userConf()->queries);
  257. FreshRSS_Context::userConf()->queries = $queries;
  258. }
  259. FreshRSS_Context::userConf()->save();
  260. Minz_Request::good(
  261. _t('feedback.sub.category.emptied'),
  262. $url_redirect,
  263. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  264. );
  265. } else {
  266. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  267. }
  268. }
  269. Minz_Request::forward($url_redirect, true);
  270. }
  271. /**
  272. * Request parameter is:
  273. * - id (of a category)
  274. */
  275. public function refreshOpmlAction(): void {
  276. $catDAO = FreshRSS_Factory::createCategoryDao();
  277. $url_redirect = ['c' => 'subscription', 'a' => 'index'];
  278. if (Minz_Request::isPost()) {
  279. invalidateHttpCache();
  280. $id = Minz_Request::paramInt('id');
  281. if ($id === 0) {
  282. Minz_Request::bad(_t('feedback.sub.category.no_id'), $url_redirect);
  283. return;
  284. }
  285. $category = $catDAO->searchById($id);
  286. if ($category === null) {
  287. Minz_Request::bad(_t('feedback.sub.category.not_exist'), $url_redirect);
  288. return;
  289. }
  290. invalidateHttpCache();
  291. $ok = $category->refreshDynamicOpml();
  292. if (Minz_Request::paramBoolean('ajax')) {
  293. Minz_Request::setGoodNotification(_t('feedback.sub.category.updated'));
  294. $this->view->_layout(null);
  295. } else {
  296. if ($ok) {
  297. Minz_Request::good(
  298. _t('feedback.sub.category.updated'),
  299. $url_redirect,
  300. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  301. );
  302. } else {
  303. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  304. }
  305. Minz_Request::forward($url_redirect, true);
  306. }
  307. }
  308. }
  309. /** @return array<string,int> */
  310. public static function refreshDynamicOpmls(): array {
  311. $successes = 0;
  312. $errors = 0;
  313. $catDAO = FreshRSS_Factory::createCategoryDao();
  314. $categories = $catDAO->listCategoriesOrderUpdate(FreshRSS_Context::userConf()->dynamic_opml_ttl_default ?? 86400);
  315. foreach ($categories as $category) {
  316. if ($category->refreshDynamicOpml()) {
  317. $successes++;
  318. } else {
  319. $errors++;
  320. }
  321. }
  322. return [
  323. 'successes' => $successes,
  324. 'errors' => $errors,
  325. ];
  326. }
  327. }