categoryController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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(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 = checkUrl(Minz_Request::paramString('opml_url'));
  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(_t('feedback.sub.category.created', $cat->name()), $url_redirect);
  61. } else {
  62. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  63. }
  64. }
  65. Minz_Request::forward($url_redirect, true);
  66. }
  67. /**
  68. * This action updates the given category.
  69. */
  70. public function updateAction(): void {
  71. if (Minz_Request::paramBoolean('ajax')) {
  72. $this->view->_layout(null);
  73. }
  74. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  75. $id = Minz_Request::paramInt('id');
  76. $category = $categoryDAO->searchById($id);
  77. if ($id === 0 || null === $category) {
  78. Minz_Error::error(404);
  79. return;
  80. }
  81. $this->view->category = $category;
  82. FreshRSS_View::prependTitle($category->name() . ' · ' . _t('sub.title') . ' · ');
  83. if (Minz_Request::isPost()) {
  84. $category->_filtersAction('read', Minz_Request::paramTextToArray('filteractions_read'));
  85. if (Minz_Request::paramBoolean('use_default_purge_options')) {
  86. $category->_attribute('archiving', null);
  87. } else {
  88. if (!Minz_Request::paramBoolean('enable_keep_max')) {
  89. $keepMax = false;
  90. } elseif (($keepMax = Minz_Request::paramInt('keep_max')) !== 0) {
  91. $keepMax = FreshRSS_Feed::ARCHIVING_RETENTION_COUNT_LIMIT;
  92. }
  93. if (Minz_Request::paramBoolean('enable_keep_period')) {
  94. $keepPeriod = FreshRSS_Feed::ARCHIVING_RETENTION_PERIOD;
  95. if (is_numeric(Minz_Request::paramString('keep_period_count')) && preg_match('/^PT?1[YMWDH]$/', Minz_Request::paramString('keep_period_unit'))) {
  96. $keepPeriod = str_replace('1', Minz_Request::paramString('keep_period_count'), Minz_Request::paramString('keep_period_unit'));
  97. }
  98. } else {
  99. $keepPeriod = false;
  100. }
  101. $category->_attribute('archiving', [
  102. 'keep_period' => $keepPeriod,
  103. 'keep_max' => $keepMax,
  104. 'keep_min' => Minz_Request::paramInt('keep_min'),
  105. 'keep_favourites' => Minz_Request::paramBoolean('keep_favourites'),
  106. 'keep_labels' => Minz_Request::paramBoolean('keep_labels'),
  107. 'keep_unreads' => Minz_Request::paramBoolean('keep_unreads'),
  108. ]);
  109. }
  110. $position = Minz_Request::paramInt('position') ?: null;
  111. $category->_attribute('position', $position);
  112. $opml_url = checkUrl(Minz_Request::paramString('opml_url'));
  113. if ($opml_url != '') {
  114. $category->_kind(FreshRSS_Category::KIND_DYNAMIC_OPML);
  115. $category->_attribute('opml_url', $opml_url);
  116. } else {
  117. $category->_kind(FreshRSS_Category::KIND_NORMAL);
  118. $category->_attribute('opml_url', null);
  119. }
  120. $values = [
  121. 'kind' => $category->kind(),
  122. 'name' => Minz_Request::paramString('name'),
  123. 'attributes' => $category->attributes(),
  124. ];
  125. invalidateHttpCache();
  126. $url_redirect = ['c' => 'subscription', 'params' => ['id' => $id, 'type' => 'category']];
  127. if (false !== $categoryDAO->updateCategory($id, $values)) {
  128. Minz_Request::good(_t('feedback.sub.category.updated'), $url_redirect);
  129. } else {
  130. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  131. }
  132. }
  133. }
  134. /**
  135. * This action deletes a category.
  136. * Feeds in the given category are moved in the default category.
  137. * Related user queries are deleted too.
  138. *
  139. * Request parameter is:
  140. * - id (of a category)
  141. */
  142. public function deleteAction(): void {
  143. $feedDAO = FreshRSS_Factory::createFeedDao();
  144. $catDAO = FreshRSS_Factory::createCategoryDao();
  145. $url_redirect = ['c' => 'subscription', 'a' => 'index'];
  146. if (Minz_Request::isPost()) {
  147. invalidateHttpCache();
  148. $id = Minz_Request::paramInt('id');
  149. if ($id === 0) {
  150. Minz_Request::bad(_t('feedback.sub.category.no_id'), $url_redirect);
  151. }
  152. if ($id === FreshRSS_CategoryDAO::DEFAULTCATEGORYID) {
  153. Minz_Request::bad(_t('feedback.sub.category.not_delete_default'), $url_redirect);
  154. }
  155. if ($feedDAO->changeCategory($id, FreshRSS_CategoryDAO::DEFAULTCATEGORYID) === false) {
  156. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  157. }
  158. if ($catDAO->deleteCategory($id) === false) {
  159. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  160. }
  161. // Remove related queries.
  162. /** @var array<array{'get'?:string,'name'?:string,'order'?:string,'search'?:string,'state'?:int,'url'?:string}> $queries */
  163. $queries = remove_query_by_get('c_' . $id, FreshRSS_Context::userConf()->queries);
  164. FreshRSS_Context::userConf()->queries = $queries;
  165. FreshRSS_Context::userConf()->save();
  166. Minz_Request::good(_t('feedback.sub.category.deleted'), $url_redirect);
  167. }
  168. Minz_Request::forward($url_redirect, true);
  169. }
  170. /**
  171. * This action deletes all the feeds relative to a given category.
  172. * Feed-related queries are deleted.
  173. *
  174. * Request parameter is:
  175. * - id (of a category)
  176. * - muted (truthy to remove only muted feeds, or falsy otherwise)
  177. */
  178. public function emptyAction(): void {
  179. $feedDAO = FreshRSS_Factory::createFeedDao();
  180. $url_redirect = ['c' => 'subscription', 'a' => 'index'];
  181. if (Minz_Request::isPost()) {
  182. invalidateHttpCache();
  183. $id = Minz_Request::paramInt('id');
  184. if ($id === 0) {
  185. Minz_Request::bad(_t('feedback.sub.category.no_id'), $url_redirect);
  186. }
  187. $muted = Minz_Request::paramTernary('muted');
  188. // List feeds to remove then related user queries.
  189. $feeds = $feedDAO->listByCategory($id, $muted);
  190. if ($feedDAO->deleteFeedByCategory($id, $muted)) {
  191. // TODO: Delete old favicons
  192. // Remove related queries
  193. foreach ($feeds as $feed) {
  194. /** @var array<array{'get'?:string,'name'?:string,'order'?:string,'search'?:string,'state'?:int,'url'?:string}> */
  195. $queries = remove_query_by_get('f_' . $feed->id(), FreshRSS_Context::userConf()->queries);
  196. FreshRSS_Context::userConf()->queries = $queries;
  197. }
  198. FreshRSS_Context::userConf()->save();
  199. Minz_Request::good(_t('feedback.sub.category.emptied'), $url_redirect);
  200. } else {
  201. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  202. }
  203. }
  204. Minz_Request::forward($url_redirect, true);
  205. }
  206. /**
  207. * Request parameter is:
  208. * - id (of a category)
  209. */
  210. public function refreshOpmlAction(): void {
  211. $catDAO = FreshRSS_Factory::createCategoryDao();
  212. $url_redirect = ['c' => 'subscription', 'a' => 'index'];
  213. if (Minz_Request::isPost()) {
  214. invalidateHttpCache();
  215. $id = Minz_Request::paramInt('id');
  216. if ($id === 0) {
  217. Minz_Request::bad(_t('feedback.sub.category.no_id'), $url_redirect);
  218. return;
  219. }
  220. $category = $catDAO->searchById($id);
  221. if ($category === null) {
  222. Minz_Request::bad(_t('feedback.sub.category.not_exist'), $url_redirect);
  223. return;
  224. }
  225. invalidateHttpCache();
  226. $ok = $category->refreshDynamicOpml();
  227. if (Minz_Request::paramBoolean('ajax')) {
  228. Minz_Request::setGoodNotification(_t('feedback.sub.category.updated'));
  229. $this->view->_layout(null);
  230. } else {
  231. if ($ok) {
  232. Minz_Request::good(_t('feedback.sub.category.updated'), $url_redirect);
  233. } else {
  234. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  235. }
  236. Minz_Request::forward($url_redirect, true);
  237. }
  238. }
  239. }
  240. /** @return array<string,int> */
  241. public static function refreshDynamicOpmls(): array {
  242. $successes = 0;
  243. $errors = 0;
  244. $catDAO = FreshRSS_Factory::createCategoryDao();
  245. $categories = $catDAO->listCategoriesOrderUpdate(FreshRSS_Context::userConf()->dynamic_opml_ttl_default ?? 86400);
  246. foreach ($categories as $category) {
  247. if ($category->refreshDynamicOpml()) {
  248. $successes++;
  249. } else {
  250. $errors++;
  251. }
  252. }
  253. return [
  254. 'successes' => $successes,
  255. 'errors' => $errors,
  256. ];
  257. }
  258. }