4
0

configureController.php 30 KB

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