4
0

configureController.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Controller to handle every configuration options.
  5. */
  6. class FreshRSS_configure_Controller extends FreshRSS_ActionController {
  7. /**
  8. * This action is called before every other action in that class. It is
  9. * the common boilerplate for every action. It is triggered by the
  10. * underlying framework.
  11. */
  12. #[\Override]
  13. public function firstAction(): void {
  14. if (!FreshRSS_Auth::hasAccess()) {
  15. Minz_Error::error(403);
  16. }
  17. }
  18. /**
  19. * This action handles the display configuration page.
  20. *
  21. * It displays the display configuration page.
  22. * If this action is reached through a POST request, it stores all new
  23. * configuration values then sends a notification to the user.
  24. *
  25. * The options available on the page are:
  26. * - language (default: en)
  27. * - theme (default: Origin)
  28. * - darkMode (default: auto)
  29. * - content width (default: thin)
  30. * - display of read action in header
  31. * - display of favorite action in header
  32. * - display of date in header
  33. * - display of open action in header
  34. * - display of read action in footer
  35. * - display of favorite action in footer
  36. * - display of sharing action in footer
  37. * - display of article tags in footer
  38. * - display of my Labels in footer
  39. * - display of date in footer
  40. * - display of open action in footer
  41. * - html5 notification timeout (default: 0)
  42. * Default values are false unless specified.
  43. */
  44. public function displayAction(): void {
  45. if (Minz_Request::isPost()) {
  46. $language = Minz_Request::paramString('language') ?: Minz_Translate::DEFAULT_LANGUAGE;
  47. if (Minz_Translate::exists($language)) {
  48. FreshRSS_Context::userConf()->language = $language;
  49. }
  50. FreshRSS_Context::userConf()->timezone = Minz_Request::paramString('timezone');
  51. $theme = Minz_Request::paramString('theme') ?: FreshRSS_Themes::$defaultTheme;
  52. if (FreshRSS_Themes::exists($theme)) {
  53. FreshRSS_Context::userConf()->theme = $theme;
  54. }
  55. FreshRSS_Context::userConf()->darkMode = Minz_Request::paramString('darkMode') ?: 'auto';
  56. FreshRSS_Context::userConf()->content_width = Minz_Request::paramString('content_width') ?: 'thin';
  57. FreshRSS_Context::userConf()->topline_read = Minz_Request::paramBoolean('topline_read');
  58. FreshRSS_Context::userConf()->topline_favorite = Minz_Request::paramBoolean('topline_favorite');
  59. FreshRSS_Context::userConf()->topline_myLabels = Minz_Request::paramBoolean('topline_myLabels');
  60. FreshRSS_Context::userConf()->topline_sharing = Minz_Request::paramBoolean('topline_sharing');
  61. FreshRSS_Context::userConf()->topline_date = Minz_Request::paramBoolean('topline_date');
  62. FreshRSS_Context::userConf()->topline_link = Minz_Request::paramBoolean('topline_link');
  63. FreshRSS_Context::userConf()->topline_website = Minz_Request::paramString('topline_website');
  64. FreshRSS_Context::userConf()->topline_thumbnail = Minz_Request::paramString('topline_thumbnail');
  65. FreshRSS_Context::userConf()->topline_summary = Minz_Request::paramBoolean('topline_summary');
  66. FreshRSS_Context::userConf()->topline_display_authors = Minz_Request::paramBoolean('topline_display_authors');
  67. FreshRSS_Context::userConf()->bottomline_read = Minz_Request::paramBoolean('bottomline_read');
  68. FreshRSS_Context::userConf()->bottomline_favorite = Minz_Request::paramBoolean('bottomline_favorite');
  69. FreshRSS_Context::userConf()->bottomline_sharing = Minz_Request::paramBoolean('bottomline_sharing');
  70. FreshRSS_Context::userConf()->bottomline_tags = Minz_Request::paramBoolean('bottomline_tags');
  71. FreshRSS_Context::userConf()->bottomline_myLabels = Minz_Request::paramBoolean('bottomline_myLabels');
  72. FreshRSS_Context::userConf()->bottomline_date = Minz_Request::paramBoolean('bottomline_date');
  73. FreshRSS_Context::userConf()->bottomline_link = Minz_Request::paramBoolean('bottomline_link');
  74. FreshRSS_Context::userConf()->show_nav_buttons = Minz_Request::paramBoolean('show_nav_buttons');
  75. FreshRSS_Context::userConf()->show_title_unread = Minz_Request::paramBoolean('show_title_unread');
  76. FreshRSS_Context::userConf()->sidebar_hidden_by_default = Minz_Request::paramBoolean('sidebar_hidden_by_default');
  77. FreshRSS_Context::userConf()->html5_notif_timeout = max(0, Minz_Request::paramInt('html5_notif_timeout'));
  78. FreshRSS_Context::userConf()->html5_enable_notif = Minz_Request::paramBoolean('html5_enable_notif');
  79. FreshRSS_Context::userConf()->good_notification_timeout = max(0, Minz_Request::paramInt('good_notification_timeout'));
  80. FreshRSS_Context::userConf()->bad_notification_timeout = max(1, Minz_Request::paramInt('bad_notification_timeout'));
  81. FreshRSS_Context::userConf()->save();
  82. Minz_Session::_param('language', FreshRSS_Context::userConf()->language);
  83. Minz_Translate::reset(FreshRSS_Context::userConf()->language);
  84. invalidateHttpCache();
  85. Minz_Request::good(
  86. _t('feedback.conf.updated'),
  87. [ 'c' => 'configure', 'a' => 'display' ],
  88. notificationName: 'displayAction',
  89. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0);
  90. }
  91. $this->view->themes = FreshRSS_Themes::get();
  92. FreshRSS_View::prependTitle(_t('conf.display.title') . ' · ');
  93. }
  94. /**
  95. * This action handles the reading configuration page.
  96. *
  97. * It displays the reading configuration page.
  98. * If this action is reached through a POST request, it stores all new
  99. * configuration values then sends a notification to the user.
  100. *
  101. * The options available on the page are:
  102. * - number of posts per page (default: 10)
  103. * - view mode (default: normal)
  104. * - default article view (default: all)
  105. * - load automatically articles
  106. * - display expanded articles
  107. * - display expanded categories
  108. * - hide categories and feeds without unread articles
  109. * - jump on next category or feed when marked as read
  110. * - image lazy loading
  111. * - stick open articles to the top
  112. * - display a confirmation when reading all articles
  113. * - auto remove article after reading
  114. * - article order (default: DESC)
  115. * - mark articles as read when:
  116. * - displayed
  117. * - opened on site
  118. * - scrolled
  119. * - received
  120. * - focus
  121. * Default values are false unless specified.
  122. */
  123. public function readingAction(): void {
  124. if (Minz_Request::isPost()) {
  125. FreshRSS_Context::userConf()->posts_per_page = Minz_Request::paramInt('posts_per_page') ?: 10;
  126. FreshRSS_Context::userConf()->view_mode = Minz_Request::paramStringNull('view_mode', true) ?? 'normal';
  127. FreshRSS_Context::userConf()->default_view = Minz_Request::paramStringNull('default_view') ?? 'adaptive';
  128. FreshRSS_Context::userConf()->show_fav_unread = Minz_Request::paramBoolean('show_fav_unread');
  129. FreshRSS_Context::userConf()->auto_load_more = Minz_Request::paramBoolean('auto_load_more');
  130. FreshRSS_Context::userConf()->display_posts = Minz_Request::paramBoolean('display_posts');
  131. FreshRSS_Context::userConf()->display_categories = Minz_Request::paramStringNull('display_categories') ?? 'active';
  132. FreshRSS_Context::userConf()->show_tags = Minz_Request::paramStringNull('show_tags') ?? '0';
  133. FreshRSS_Context::userConf()->show_tags_max = Minz_Request::paramInt('show_tags_max');
  134. FreshRSS_Context::userConf()->show_author_date = Minz_Request::paramStringNull('show_author_date') ?? '0';
  135. FreshRSS_Context::userConf()->show_feed_name = Minz_Request::paramStringNull('show_feed_name') ?? 't';
  136. FreshRSS_Context::userConf()->show_article_icons = Minz_Request::paramStringNull('show_article_icons') ?? 't';
  137. FreshRSS_Context::userConf()->hide_read_feeds = Minz_Request::paramBoolean('hide_read_feeds');
  138. FreshRSS_Context::userConf()->onread_jump_next = Minz_Request::paramBoolean('onread_jump_next');
  139. FreshRSS_Context::userConf()->lazyload = Minz_Request::paramBoolean('lazyload');
  140. FreshRSS_Context::userConf()->sides_close_article = Minz_Request::paramBoolean('sides_close_article');
  141. FreshRSS_Context::userConf()->sticky_post = Minz_Request::paramBoolean('sticky_post');
  142. $markReadButton = Minz_Request::paramStringNull('mark_read_button', plaintext: true);
  143. FreshRSS_Context::userConf()->mark_read_button = in_array($markReadButton, ['big', 'small', 'none'], true) ? $markReadButton : 'big';
  144. FreshRSS_Context::userConf()->reading_confirm = Minz_Request::paramBoolean('reading_confirm');
  145. FreshRSS_Context::userConf()->auto_remove_article = Minz_Request::paramBoolean('auto_remove_article');
  146. FreshRSS_Context::userConf()->mark_updated_article_unread = Minz_Request::paramBoolean('mark_updated_article_unread');
  147. $sorting = Minz_Request::paramString('primary_sort', plaintext: true);
  148. if (str_ends_with($sorting, '_asc')) {
  149. FreshRSS_Context::userConf()->sort_order = 'ASC';
  150. $sorting = substr($sorting, 0, -strlen('_asc'));
  151. } elseif (str_ends_with($sorting, '_desc')) {
  152. FreshRSS_Context::userConf()->sort_order = 'DESC';
  153. $sorting = substr($sorting, 0, -strlen('_desc'));
  154. } else {
  155. FreshRSS_Context::userConf()->sort_order = 'DESC';
  156. }
  157. if (in_array($sorting, ['id', 'c.name', 'date', 'f.name', 'length', 'link', 'title', 'rand'], true)) {
  158. FreshRSS_Context::userConf()->sort = $sorting;
  159. } else {
  160. FreshRSS_Context::userConf()->sort = 'id';
  161. }
  162. $sorting = Minz_Request::paramString('secondary_sort', plaintext: true);
  163. if (str_ends_with($sorting, '_asc')) {
  164. FreshRSS_Context::userConf()->secondary_sort_order = 'ASC';
  165. $sorting = substr($sorting, 0, -strlen('_asc'));
  166. } elseif (str_ends_with($sorting, '_desc')) {
  167. FreshRSS_Context::userConf()->secondary_sort_order = 'DESC';
  168. $sorting = substr($sorting, 0, -strlen('_desc'));
  169. } else {
  170. FreshRSS_Context::userConf()->secondary_sort_order = 'DESC';
  171. }
  172. if (in_array($sorting, ['id', 'date', 'link', 'title'], true)) {
  173. FreshRSS_Context::userConf()->secondary_sort = $sorting;
  174. } else {
  175. FreshRSS_Context::userConf()->secondary_sort = 'id';
  176. }
  177. FreshRSS_Context::userConf()->mark_when = [
  178. 'article' => Minz_Request::paramBoolean('mark_open_article'),
  179. 'gone' => Minz_Request::paramBoolean('read_upon_gone'),
  180. 'max_n_unread' => Minz_Request::paramBoolean('enable_keep_max_n_unread') ? Minz_Request::paramInt('keep_max_n_unread') : false,
  181. 'reception' => Minz_Request::paramBoolean('mark_upon_reception'),
  182. 'same_title_in_feed' => Minz_Request::paramBoolean('enable_read_when_same_title_in_feed') ?
  183. Minz_Request::paramInt('read_when_same_title_in_feed') : false,
  184. 'scroll' => Minz_Request::paramBoolean('mark_scroll'),
  185. 'site' => Minz_Request::paramBoolean('mark_open_site'),
  186. 'focus' => Minz_Request::paramBoolean('mark_focus'),
  187. ];
  188. FreshRSS_Context::userConf()->_filtersAction('read', Minz_Request::paramTextToArray('filteractions_read', plaintext: true));
  189. FreshRSS_Context::userConf()->_filtersAction('star', Minz_Request::paramTextToArray('filteractions_star', plaintext: true));
  190. FreshRSS_Context::userConf()->save();
  191. invalidateHttpCache();
  192. Minz_Request::good(
  193. _t('feedback.conf.updated'),
  194. [ 'c' => 'configure', 'a' => 'reading' ],
  195. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  196. );
  197. }
  198. $this->view->viewModes = FreshRSS_ViewMode::getAllModes();
  199. FreshRSS_View::prependTitle(_t('conf.reading.title') . ' · ');
  200. }
  201. public function viewFilterAction(): void {
  202. $search = '';
  203. $filters_name = Minz_Request::paramString('filters_name', plaintext: true);
  204. $filteractions = Minz_Request::paramTextToArray($filters_name, plaintext: true);
  205. $filteractions = array_map(fn(string $action): string => trim($action), $filteractions);
  206. $filteractions = array_filter($filteractions, fn(string $action): bool => $action !== '');
  207. foreach ($filteractions as $action) {
  208. $search .= "($action) OR ";
  209. }
  210. $search = preg_replace('/ OR $/', '', $search);
  211. Minz_Request::forward([
  212. 'c' => 'index',
  213. 'a' => 'index',
  214. 'params' => [
  215. 'search' => $search,
  216. ],
  217. ], redirect: true);
  218. }
  219. /**
  220. * This action handles the integration configuration page.
  221. *
  222. * It displays the integration configuration page.
  223. * If this action is reached through a POST request, it stores all
  224. * configuration values then sends a notification to the user.
  225. *
  226. * Before v1.16, we used sharing instead of integration. This has
  227. * some unwanted behavior when the end-user was using an ad-blocker.
  228. */
  229. public function integrationAction(): void {
  230. FreshRSS_View::appendScript(Minz_Url::display('/scripts/integration.js?' . @filemtime(PUBLIC_PATH . '/scripts/integration.js')));
  231. FreshRSS_View::appendScript(Minz_Url::display('/scripts/draggable.js?' . @filemtime(PUBLIC_PATH . '/scripts/draggable.js')));
  232. if (Minz_Request::isPost()) {
  233. $share = $_POST['share'] ?? [];
  234. if (is_array($share)) {
  235. $share = array_filter($share, fn($value, $key): bool =>
  236. is_int($key) && is_array($value) &&
  237. is_array_values_string($value),
  238. ARRAY_FILTER_USE_BOTH);
  239. /** @var array<int,array<string,string>> $share */
  240. FreshRSS_Context::userConf()->sharing = $share;
  241. FreshRSS_Context::userConf()->save();
  242. invalidateHttpCache();
  243. }
  244. Minz_Request::good(
  245. _t('feedback.conf.updated'),
  246. [ 'c' => 'configure', 'a' => 'integration' ],
  247. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  248. );
  249. }
  250. FreshRSS_View::prependTitle(_t('conf.sharing.title') . ' · ');
  251. }
  252. private const SHORTCUT_KEYS = [
  253. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  254. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  255. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  256. 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12',
  257. 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowUp', 'Backspace', 'Delete',
  258. 'End', 'Enter', 'Escape', 'Home', 'Insert', 'PageDown', 'PageUp', 'Space', 'Tab',
  259. ];
  260. /**
  261. * @param array<string> $shortcuts
  262. * @return list<string>
  263. */
  264. public static function getNonStandardShortcuts(array $shortcuts): array {
  265. $standard = strtolower(implode(' ', self::SHORTCUT_KEYS));
  266. $nonStandard = array_filter($shortcuts, static function (string $shortcut) use ($standard) {
  267. $shortcut = trim($shortcut);
  268. return $shortcut !== '' && stripos($standard, $shortcut) === false;
  269. });
  270. return array_values($nonStandard);
  271. }
  272. /**
  273. * This action handles the shortcut configuration page.
  274. *
  275. * It displays the shortcut configuration page.
  276. * If this action is reached through a POST request, it stores all new
  277. * configuration values then sends a notification to the user.
  278. *
  279. * The authorized values for shortcuts are letters (a to z), numbers (0
  280. * to 9), function keys (f1 to f12), backspace, delete, down, end, enter,
  281. * escape, home, insert, left, page down, page up, return, right, space,
  282. * tab and up.
  283. */
  284. public function shortcutAction(): void {
  285. $this->view->list_keys = self::SHORTCUT_KEYS;
  286. if (Minz_Request::isPost()) {
  287. $shortcuts = Minz_Request::paramArray('shortcuts', plaintext: true);
  288. if (Minz_Request::paramBoolean('load_default_shortcuts')) {
  289. $default = Minz_Configuration::load(FRESHRSS_PATH . '/config-user.default.php');
  290. $shortcuts = $default['shortcuts'];
  291. }
  292. /** @var array<string,string> $shortcuts */
  293. FreshRSS_Context::userConf()->shortcuts = array_map('trim', $shortcuts);
  294. FreshRSS_Context::userConf()->save();
  295. invalidateHttpCache();
  296. Minz_Request::good(
  297. _t('feedback.conf.shortcuts_updated'),
  298. ['c' => 'configure', 'a' => 'shortcut'],
  299. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  300. );
  301. }
  302. FreshRSS_View::prependTitle(_t('conf.shortcut.title') . ' · ');
  303. }
  304. /**
  305. * This action handles the archive configuration page.
  306. *
  307. * It displays the archive configuration page.
  308. * If this action is reached through a POST request, it stores all new
  309. * configuration values then sends a notification to the user.
  310. *
  311. * The options available on that page are:
  312. * - duration to retain old article (default: 3)
  313. * - number of article to retain per feed (default: 0)
  314. * - refresh frequency (default: 0)
  315. */
  316. public function archivingAction(): void {
  317. if (Minz_Request::isPost()) {
  318. if (Minz_Request::paramBoolean('enable_keep_max')) {
  319. $keepMax = Minz_Request::paramInt('keep_max') ?: FreshRSS_Feed::ARCHIVING_RETENTION_COUNT_LIMIT;
  320. } else {
  321. $keepMax = false;
  322. }
  323. if (Minz_Request::paramBoolean('enable_keep_period')) {
  324. $keepPeriod = FreshRSS_Feed::ARCHIVING_RETENTION_PERIOD;
  325. if (is_numeric(Minz_Request::paramString('keep_period_count')) && preg_match('/^PT?1[YMWDH]$/', Minz_Request::paramString('keep_period_unit'))) {
  326. $keepPeriod = str_replace('1', Minz_Request::paramString('keep_period_count'), Minz_Request::paramString('keep_period_unit'));
  327. }
  328. } else {
  329. $keepPeriod = false;
  330. }
  331. FreshRSS_Context::userConf()->ttl_default = Minz_Request::paramInt('ttl_default') ?: FreshRSS_Feed::TTL_DEFAULT;
  332. FreshRSS_Context::userConf()->archiving = [
  333. 'keep_period' => $keepPeriod,
  334. 'keep_max' => $keepMax,
  335. 'keep_min' => Minz_Request::paramInt('keep_min_default'),
  336. 'keep_favourites' => Minz_Request::paramBoolean('keep_favourites'),
  337. 'keep_labels' => Minz_Request::paramBoolean('keep_labels'),
  338. 'keep_unreads' => Minz_Request::paramBoolean('keep_unreads'),
  339. ];
  340. FreshRSS_Context::userConf()->keep_history_default = null; //Legacy < FreshRSS 1.15
  341. FreshRSS_Context::userConf()->old_entries = null; //Legacy < FreshRSS 1.15
  342. FreshRSS_Context::userConf()->save();
  343. invalidateHttpCache();
  344. Minz_Request::good(
  345. _t('feedback.conf.updated'),
  346. [ 'c' => 'configure', 'a' => 'archiving' ],
  347. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  348. );
  349. }
  350. $volatile = [
  351. 'enable_keep_period' => false,
  352. 'keep_period_count' => '3',
  353. 'keep_period_unit' => 'P1M',
  354. ];
  355. if (!empty(FreshRSS_Context::userConf()->archiving['keep_period'])) {
  356. $keepPeriod = FreshRSS_Context::userConf()->archiving['keep_period'];
  357. if (preg_match('/^PT?(?P<count>\d+)[YMWDH]$/', $keepPeriod, $matches)) {
  358. $volatile = [
  359. 'enable_keep_period' => true,
  360. 'keep_period_count' => $matches['count'],
  361. 'keep_period_unit' => str_replace($matches['count'], '1', $keepPeriod),
  362. ];
  363. }
  364. }
  365. FreshRSS_Context::userConf()->volatile = $volatile;
  366. $entryDAO = FreshRSS_Factory::createEntryDao();
  367. $this->view->nb_total = $entryDAO->count();
  368. $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
  369. $this->view->size_user = $databaseDAO->size();
  370. if (FreshRSS_Auth::hasAccess('admin')) {
  371. $this->view->size_total = $databaseDAO->size(all: true);
  372. }
  373. FreshRSS_View::prependTitle(_t('conf.archiving.title') . ' · ');
  374. }
  375. /**
  376. * This action handles the user queries configuration page.
  377. *
  378. * If this action is reached through a POST request, it stores all new
  379. * configuration values then sends a notification to the user then
  380. * redirect to the same page.
  381. * If this action is not reached through a POST request, it displays the
  382. * configuration page and verifies that every user query is runnable by
  383. * checking if categories and feeds are still in use.
  384. */
  385. public function queriesAction(): void {
  386. FreshRSS_View::appendScript(Minz_Url::display('/scripts/draggable.js?' . @filemtime(PUBLIC_PATH . '/scripts/draggable.js')));
  387. if (Minz_Request::isPost()) {
  388. /** @var array<int,array{get?:string,name?:string,order?:string,search?:string,state?:int,url?:string,token?:string,
  389. * shareRss?:bool|numeric-string,shareOpml?:bool|numeric-string,description?:string,imageUrl?:string}> $params */
  390. $params = Minz_Request::paramArray('queries');
  391. $queries = [];
  392. foreach ($params as $key => $query) {
  393. $key = (int)$key;
  394. if (empty($query['name'])) {
  395. $query['name'] = _t('conf.query.number', $key + 1);
  396. }
  397. if (!empty($query['search'])) {
  398. $query['search'] = urldecode($query['search']);
  399. }
  400. $shareRss = $query['shareRss'] ?? null;
  401. $query['shareRss'] = (is_string($shareRss) && ctype_digit($shareRss)) ? (bool)$shareRss : false;
  402. $shareOpml = $query['shareOpml'] ?? null;
  403. $query['shareOpml'] = (is_string($shareOpml) && ctype_digit($shareOpml)) ? (bool)$shareOpml : false;
  404. $queries[$key] = (new FreshRSS_UserQuery($query, FreshRSS_Context::categories(), FreshRSS_Context::labels()))->toArray();
  405. }
  406. FreshRSS_Context::userConf()->queries = $queries;
  407. FreshRSS_Context::userConf()->save();
  408. Minz_Request::good(
  409. _t('feedback.conf.updated'),
  410. [ 'c' => 'configure', 'a' => 'queries' ],
  411. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  412. );
  413. } else {
  414. $this->view->queries = [];
  415. foreach (FreshRSS_Context::userConf()->queries as $key => $query) {
  416. $this->view->queries[intval($key)] = new FreshRSS_UserQuery($query, FreshRSS_Context::categories(), FreshRSS_Context::labels());
  417. }
  418. }
  419. $this->view->categories = FreshRSS_Context::categories();
  420. $this->view->feeds = FreshRSS_Context::feeds();
  421. $this->view->tags = FreshRSS_Context::labels();
  422. if (Minz_Request::paramTernary('id') !== null) {
  423. $id = Minz_Request::paramInt('id');
  424. $this->view->query = $this->view->queries[$id];
  425. $this->view->queryId = $id;
  426. $this->view->displaySlider = true;
  427. } else {
  428. $this->view->displaySlider = false;
  429. }
  430. FreshRSS_View::prependTitle(_t('conf.query.title') . ' · ');
  431. }
  432. /**
  433. * Handles query configuration.
  434. * It displays the query configuration page and handles modifications
  435. * applied to the selected query.
  436. */
  437. public function queryAction(): void {
  438. if (Minz_Request::paramBoolean('ajax')) {
  439. $this->view->_layout(null);
  440. }
  441. $id = Minz_Request::paramInt('id');
  442. if (Minz_Request::paramTernary('id') === null || empty(FreshRSS_Context::userConf()->queries[$id])) {
  443. Minz_Error::error(404);
  444. return;
  445. }
  446. $query = new FreshRSS_UserQuery(FreshRSS_Context::userConf()->queries[$id], FreshRSS_Context::categories(), FreshRSS_Context::labels());
  447. $this->view->query = $query;
  448. $this->view->queryId = $id;
  449. $this->view->categories = FreshRSS_Context::categories();
  450. $this->view->feeds = FreshRSS_Context::feeds();
  451. $this->view->tags = FreshRSS_Context::labels();
  452. if (Minz_Request::isPost()) {
  453. $params = Minz_Request::paramArray('query');
  454. $queryParams = [];
  455. $name = Minz_Request::paramString('name') ?: _t('conf.query.number', $id + 1);
  456. if ('' === $name) {
  457. $name = _t('conf.query.number', $id + 1);
  458. }
  459. if (!empty($params['get']) && is_string($params['get'])) {
  460. $queryParams['get'] = $params['get'];
  461. }
  462. if (!empty($params['order']) && is_string($params['order'])) {
  463. $queryParams['order'] = $params['order'];
  464. }
  465. if (!empty($params['search']) && is_string($params['search'])) {
  466. // Search must be as plain text to be XML-encoded or URL-encoded depending on the situation
  467. $queryParams['search'] = htmlspecialchars_decode($params['search'], ENT_QUOTES);
  468. }
  469. if (!empty($params['state']) && is_array($params['state'])) {
  470. $queryParams['state'] = (int)array_sum(array_map('intval', $params['state']));
  471. }
  472. if (empty($params['token']) || !is_string($params['token'])) {
  473. $queryParams['token'] = FreshRSS_UserQuery::generateToken($name);
  474. } else {
  475. $queryParams['token'] = $params['token'];
  476. }
  477. $queryParams['url'] = Minz_Url::display(['params' => $queryParams]);
  478. $queryParams['name'] = $name;
  479. if (!empty($params['description']) && is_string($params['description'])) {
  480. $queryParams['description'] = $params['description'];
  481. }
  482. if (!empty($params['imageUrl']) && is_string($params['imageUrl'])) {
  483. $queryParams['imageUrl'] = $params['imageUrl'];
  484. }
  485. if (!empty($params['shareOpml']) && ctype_digit($params['shareOpml'])) {
  486. $queryParams['shareOpml'] = (bool)$params['shareOpml'];
  487. }
  488. if (!empty($params['shareRss']) && ctype_digit($params['shareRss'])) {
  489. $queryParams['shareRss'] = (bool)$params['shareRss'];
  490. }
  491. if (!empty($params['publishLabelsInsteadOfTags']) && ctype_digit($params['publishLabelsInsteadOfTags'])) {
  492. $queryParams['publishLabelsInsteadOfTags'] = (bool)$params['publishLabelsInsteadOfTags'];
  493. }
  494. $queries = FreshRSS_Context::userConf()->queries;
  495. $queries[$id] = (new FreshRSS_UserQuery($queryParams, FreshRSS_Context::categories(), FreshRSS_Context::labels()))->toArray();
  496. FreshRSS_Context::userConf()->queries = $queries;
  497. FreshRSS_Context::userConf()->save();
  498. Minz_Request::good(
  499. _t('feedback.conf.updated'),
  500. [ 'c' => 'configure', 'a' => Minz_Request::paramStringNull('from') ?? 'queries', 'params' => ['id' => (string)$id] ],
  501. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0);
  502. }
  503. FreshRSS_View::prependTitle($query->getName() . ' · ' . _t('conf.query.title') . ' · ');
  504. }
  505. /**
  506. * Handles query deletion
  507. */
  508. public function deleteQueryAction(): void {
  509. if (!Minz_Request::isPost()) {
  510. Minz_Error::error(403);
  511. return;
  512. }
  513. $id = Minz_Request::paramInt('id');
  514. if (Minz_Request::paramTernary('id') === null || empty(FreshRSS_Context::userConf()->queries[$id])) {
  515. Minz_Error::error(404);
  516. return;
  517. }
  518. $queries = FreshRSS_Context::userConf()->queries;
  519. unset($queries[$id]);
  520. FreshRSS_Context::userConf()->queries = $queries;
  521. FreshRSS_Context::userConf()->save();
  522. Minz_Request::good(
  523. _t('feedback.conf.updated'),
  524. [ 'c' => 'configure', 'a' => 'queries' ],
  525. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  526. );
  527. }
  528. /**
  529. * This action handles the creation of a user query.
  530. *
  531. * It gets the GET or POST parameters and stores them in the configuration query
  532. * storage.
  533. */
  534. public function bookmarkQueryAction(): void {
  535. if (!Minz_Request::isPost()) {
  536. Minz_Error::error(403);
  537. return;
  538. }
  539. $queries = FreshRSS_Context::userConf()->queries;
  540. $id = count($queries);
  541. /** @var array{get?:string,name?:string,order?:string,search?:string,state?:int,shareRss?:bool,shareOpml?:bool,description?:string,imageUrl?:string} $params */
  542. $params = Minz_Request::paramArray('query') ?: array_filter($_GET, 'is_string', ARRAY_FILTER_USE_KEY);
  543. $name = ($params['name'] ?? '') ?: _t('conf.query.number', $id + 1);
  544. $queryParams = [];
  545. if (is_string($params['get'] ?? null)) {
  546. $queryParams['get'] = $params['get'];
  547. }
  548. if (is_string($params['order'] ?? null)) {
  549. $queryParams['order'] = $params['order'];
  550. }
  551. if (is_string($params['search'] ?? null)) {
  552. // Search must be as plain text to be XML-encoded or URL-encoded depending on the situation
  553. $queryParams['search'] = htmlspecialchars_decode($params['search'], ENT_QUOTES);
  554. }
  555. if (is_array($params['state'] ?? null)) {
  556. $queryParams['state'] = (int)array_sum(array_map('intval', $params['state']));
  557. }
  558. $queryParams['token'] = FreshRSS_UserQuery::generateToken($name);
  559. $queryParams['url'] = Minz_Url::display(['params' => $queryParams]);
  560. $queryParams['name'] = $name;
  561. if (is_string($params['description'] ?? null)) {
  562. $queryParams['description'] = $params['description'];
  563. }
  564. if (is_string($params['imageUrl'] ?? null)) {
  565. $queryParams['imageUrl'] = $params['imageUrl'];
  566. }
  567. if (ctype_digit($params['shareOpml'] ?? '')) {
  568. $queryParams['shareOpml'] = (bool)$params['shareOpml'];
  569. }
  570. if (ctype_digit($params['shareRss'] ?? '')) {
  571. $queryParams['shareRss'] = (bool)$params['shareRss'];
  572. }
  573. $queries[$id] = (new FreshRSS_UserQuery($queryParams, FreshRSS_Context::categories(), FreshRSS_Context::labels()))->toArray();
  574. FreshRSS_Context::userConf()->queries = $queries;
  575. FreshRSS_Context::userConf()->save();
  576. Minz_Request::good(
  577. _t('feedback.conf.query_created', $name),
  578. [ 'c' => 'configure', 'a' => 'queries' ],
  579. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  580. );
  581. }
  582. /**
  583. * This action handles the system configuration page.
  584. *
  585. * It displays the system configuration page.
  586. * If this action is reach through a POST request, it stores all new
  587. * configuration values then sends a notification to the user.
  588. *
  589. * The options available on the page are:
  590. * - instance name (default: FreshRSS)
  591. * - auto update URL (default: false)
  592. * - force emails validation (default: false)
  593. * - user limit (default: 1)
  594. * - user category limit (default: 16384)
  595. * - user feed limit (default: 16384)
  596. * - user login duration for form auth (default: FreshRSS_Auth::DEFAULT_COOKIE_DURATION)
  597. */
  598. public function systemAction(): void {
  599. if (!FreshRSS_Auth::hasAccess('admin')) {
  600. Minz_Error::error(403);
  601. }
  602. if (Minz_Request::isPost()) {
  603. $limits = FreshRSS_Context::systemConf()->limits;
  604. $limits['max_registrations'] = Minz_Request::paramIntNull('max-registrations') ?? 1;
  605. $limits['max_feeds'] = Minz_Request::paramInt('max-feeds') ?: 16384;
  606. $limits['max_categories'] = Minz_Request::paramInt('max-categories') ?: 16384;
  607. $limits['cookie_duration'] = Minz_Request::paramInt('cookie-duration') ?: FreshRSS_Auth::DEFAULT_COOKIE_DURATION;
  608. FreshRSS_Context::systemConf()->limits = $limits;
  609. FreshRSS_Context::systemConf()->title = Minz_Request::paramString('instance-name') ?: 'FreshRSS';
  610. FreshRSS_Context::systemConf()->force_email_validation = Minz_Request::paramBoolean('force-email-validation');
  611. FreshRSS_Context::systemConf()->closed_registration_message = Minz_Request::paramString('closed_registration_message') ?: '';
  612. FreshRSS_Context::systemConf()->save();
  613. invalidateHttpCache();
  614. Minz_Request::good(
  615. _t('feedback.conf.updated'),
  616. [ 'c' => 'configure', 'a' => 'system' ],
  617. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  618. );
  619. }
  620. }
  621. public function privacyAction(): void {
  622. if (Minz_Request::isPost()) {
  623. FreshRSS_Context::userConf()->retrieve_extension_list = Minz_Request::paramBoolean('retrieve_extension_list');
  624. FreshRSS_Context::userConf()->send_referrer_allowlist = Minz_Request::paramTextToArray('send_referrer_allowlist');
  625. FreshRSS_Context::userConf()->save();
  626. invalidateHttpCache();
  627. Minz_Request::good(
  628. _t('feedback.conf.updated'),
  629. ['c' => 'configure', 'a' => 'privacy'],
  630. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  631. );
  632. }
  633. FreshRSS_View::prependTitle(_t('conf.privacy') . ' · ');
  634. }
  635. }