configureController.php 30 KB

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