configureController.php 29 KB

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