configureController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <?php
  2. /**
  3. * Controller to handle every configuration options.
  4. */
  5. class FreshRSS_configure_Controller extends FreshRSS_ActionController {
  6. /**
  7. * This action is called before every other action in that class. It is
  8. * the common boiler plate for every action. It is triggered by the
  9. * underlying framework.
  10. */
  11. public function firstAction() {
  12. if (!FreshRSS_Auth::hasAccess()) {
  13. Minz_Error::error(403);
  14. }
  15. }
  16. /**
  17. * This action handles the display configuration page.
  18. *
  19. * It displays the display configuration page.
  20. * If this action is reached through a POST request, it stores all new
  21. * configuration values then sends a notification to the user.
  22. *
  23. * The options available on the page are:
  24. * - language (default: en)
  25. * - theme (default: Origin)
  26. * - darkMode (default: no)
  27. * - content width (default: thin)
  28. * - display of read action in header
  29. * - display of favorite action in header
  30. * - display of date in header
  31. * - display of open action in header
  32. * - display of read action in footer
  33. * - display of favorite action in footer
  34. * - display of sharing action in footer
  35. * - display of tags in footer
  36. * - display of date in footer
  37. * - display of open action in footer
  38. * - html5 notification timeout (default: 0)
  39. * Default values are false unless specified.
  40. */
  41. public function displayAction() {
  42. if (Minz_Request::isPost()) {
  43. FreshRSS_Context::$user_conf->language = Minz_Request::param('language', 'en');
  44. FreshRSS_Context::$user_conf->timezone = Minz_Request::param('timezone', '');
  45. FreshRSS_Context::$user_conf->theme = Minz_Request::param('theme', FreshRSS_Themes::$defaultTheme);
  46. FreshRSS_Context::$user_conf->darkMode = Minz_Request::param('darkMode', 'no');
  47. FreshRSS_Context::$user_conf->content_width = Minz_Request::param('content_width', 'thin');
  48. FreshRSS_Context::$user_conf->topline_read = Minz_Request::param('topline_read', false);
  49. FreshRSS_Context::$user_conf->topline_favorite = Minz_Request::param('topline_favorite', false);
  50. FreshRSS_Context::$user_conf->topline_date = Minz_Request::param('topline_date', false);
  51. FreshRSS_Context::$user_conf->topline_link = Minz_Request::param('topline_link', false);
  52. FreshRSS_Context::$user_conf->topline_website = Minz_Request::param('topline_website', false);
  53. FreshRSS_Context::$user_conf->topline_thumbnail = Minz_Request::param('topline_thumbnail', false);
  54. FreshRSS_Context::$user_conf->topline_summary = Minz_Request::param('topline_summary', false);
  55. FreshRSS_Context::$user_conf->topline_display_authors = Minz_Request::param('topline_display_authors', false);
  56. FreshRSS_Context::$user_conf->bottomline_read = Minz_Request::param('bottomline_read', false);
  57. FreshRSS_Context::$user_conf->bottomline_favorite = Minz_Request::param('bottomline_favorite', false);
  58. FreshRSS_Context::$user_conf->bottomline_sharing = Minz_Request::param('bottomline_sharing', false);
  59. FreshRSS_Context::$user_conf->bottomline_tags = Minz_Request::param('bottomline_tags', false);
  60. FreshRSS_Context::$user_conf->bottomline_date = Minz_Request::param('bottomline_date', false);
  61. FreshRSS_Context::$user_conf->bottomline_link = Minz_Request::param('bottomline_link', false);
  62. FreshRSS_Context::$user_conf->html5_notif_timeout = Minz_Request::param('html5_notif_timeout', 0);
  63. FreshRSS_Context::$user_conf->show_nav_buttons = Minz_Request::param('show_nav_buttons', false);
  64. FreshRSS_Context::$user_conf->save();
  65. Minz_Session::_param('language', FreshRSS_Context::$user_conf->language);
  66. Minz_Translate::reset(FreshRSS_Context::$user_conf->language);
  67. invalidateHttpCache();
  68. Minz_Request::good(_t('feedback.conf.updated'), [ 'c' => 'configure', 'a' => 'display' ]);
  69. }
  70. $this->view->themes = FreshRSS_Themes::get();
  71. FreshRSS_View::prependTitle(_t('conf.display.title') . ' · ');
  72. }
  73. /**
  74. * This action handles the reading configuration page.
  75. *
  76. * It displays the reading configuration page.
  77. * If this action is reached through a POST request, it stores all new
  78. * configuration values then sends a notification to the user.
  79. *
  80. * The options available on the page are:
  81. * - number of posts per page (default: 10)
  82. * - view mode (default: normal)
  83. * - default article view (default: all)
  84. * - load automatically articles
  85. * - display expanded articles
  86. * - display expanded categories
  87. * - hide categories and feeds without unread articles
  88. * - jump on next category or feed when marked as read
  89. * - image lazy loading
  90. * - stick open articles to the top
  91. * - display a confirmation when reading all articles
  92. * - auto remove article after reading
  93. * - article order (default: DESC)
  94. * - mark articles as read when:
  95. * - displayed
  96. * - opened on site
  97. * - scrolled
  98. * - received
  99. * Default values are false unless specified.
  100. */
  101. public function readingAction() {
  102. if (Minz_Request::isPost()) {
  103. FreshRSS_Context::$user_conf->posts_per_page = Minz_Request::param('posts_per_page', 10);
  104. FreshRSS_Context::$user_conf->view_mode = Minz_Request::param('view_mode', 'normal');
  105. FreshRSS_Context::$user_conf->default_view = Minz_Request::param('default_view', 'adaptive');
  106. FreshRSS_Context::$user_conf->show_fav_unread = Minz_Request::paramBoolean('show_fav_unread');
  107. FreshRSS_Context::$user_conf->auto_load_more = Minz_Request::paramBoolean('auto_load_more');
  108. FreshRSS_Context::$user_conf->display_posts = Minz_Request::paramBoolean('display_posts');
  109. FreshRSS_Context::$user_conf->display_categories = Minz_Request::param('display_categories', 'active');
  110. FreshRSS_Context::$user_conf->show_tags = Minz_Request::param('show_tags', '0');
  111. FreshRSS_Context::$user_conf->show_tags_max = Minz_Request::param('show_tags_max', '0');
  112. FreshRSS_Context::$user_conf->show_author_date = Minz_Request::param('show_author_date', '0');
  113. FreshRSS_Context::$user_conf->show_feed_name = Minz_Request::param('show_feed_name', 't');
  114. FreshRSS_Context::$user_conf->hide_read_feeds = Minz_Request::paramBoolean('hide_read_feeds');
  115. FreshRSS_Context::$user_conf->onread_jump_next = Minz_Request::paramBoolean('onread_jump_next');
  116. FreshRSS_Context::$user_conf->lazyload = Minz_Request::paramBoolean('lazyload');
  117. FreshRSS_Context::$user_conf->sides_close_article = Minz_Request::paramBoolean('sides_close_article');
  118. FreshRSS_Context::$user_conf->sticky_post = Minz_Request::paramBoolean('sticky_post');
  119. FreshRSS_Context::$user_conf->reading_confirm = Minz_Request::paramBoolean('reading_confirm');
  120. FreshRSS_Context::$user_conf->auto_remove_article = Minz_Request::paramBoolean('auto_remove_article');
  121. FreshRSS_Context::$user_conf->mark_updated_article_unread = Minz_Request::paramBoolean('mark_updated_article_unread');
  122. FreshRSS_Context::$user_conf->sort_order = Minz_Request::param('sort_order', 'DESC');
  123. FreshRSS_Context::$user_conf->mark_when = array(
  124. 'article' => Minz_Request::paramBoolean('mark_open_article'),
  125. 'gone' => Minz_Request::paramBoolean('read_upon_gone'),
  126. 'max_n_unread' => Minz_Request::paramBoolean('enable_keep_max_n_unread') ? Minz_Request::param('keep_max_n_unread', false) : false,
  127. 'reception' => Minz_Request::paramBoolean('mark_upon_reception'),
  128. 'same_title_in_feed' => Minz_Request::paramBoolean('enable_read_when_same_title_in_feed') ?
  129. Minz_Request::param('read_when_same_title_in_feed', false) : false,
  130. 'scroll' => Minz_Request::paramBoolean('mark_scroll'),
  131. 'site' => Minz_Request::paramBoolean('mark_open_site'),
  132. );
  133. FreshRSS_Context::$user_conf->save();
  134. invalidateHttpCache();
  135. Minz_Request::good(_t('feedback.conf.updated'), [ 'c' => 'configure', 'a' => 'reading' ]);
  136. }
  137. FreshRSS_View::prependTitle(_t('conf.reading.title') . ' · ');
  138. }
  139. /**
  140. * This action handles the integration configuration page.
  141. *
  142. * It displays the integration configuration page.
  143. * If this action is reached through a POST request, it stores all
  144. * configuration values then sends a notification to the user.
  145. *
  146. * Before v1.16, we used sharing instead of integration. This has
  147. * some unwanted behavior when the end-user was using an ad-blocker.
  148. */
  149. public function integrationAction() {
  150. FreshRSS_View::appendScript(Minz_Url::display('/scripts/integration.js?' . @filemtime(PUBLIC_PATH . '/scripts/integration.js')));
  151. FreshRSS_View::appendScript(Minz_Url::display('/scripts/draggable.js?' . @filemtime(PUBLIC_PATH . '/scripts/draggable.js')));
  152. if (Minz_Request::isPost()) {
  153. $params = $_POST;
  154. FreshRSS_Context::$user_conf->sharing = $params['share'];
  155. FreshRSS_Context::$user_conf->save();
  156. invalidateHttpCache();
  157. Minz_Request::good(_t('feedback.conf.updated'), [ 'c' => 'configure', 'a' => 'integration' ]);
  158. }
  159. FreshRSS_View::prependTitle(_t('conf.sharing.title') . ' · ');
  160. }
  161. /**
  162. * This action handles the shortcut configuration page.
  163. *
  164. * It displays the shortcut configuration page.
  165. * If this action is reached through a POST request, it stores all new
  166. * configuration values then sends a notification to the user.
  167. *
  168. * The authorized values for shortcuts are letters (a to z), numbers (0
  169. * to 9), function keys (f1 to f12), backspace, delete, down, end, enter,
  170. * escape, home, insert, left, page down, page up, return, right, space,
  171. * tab and up.
  172. */
  173. public function shortcutAction() {
  174. $this->view->list_keys = SHORTCUT_KEYS;
  175. if (Minz_Request::isPost()) {
  176. $shortcuts = Minz_Request::param('shortcuts');
  177. if (false !== Minz_Request::param('load_default_shortcuts')) {
  178. $default = Minz_Configuration::load(FRESHRSS_PATH . '/config-user.default.php');
  179. $shortcuts = $default['shortcuts'];
  180. }
  181. FreshRSS_Context::$user_conf->shortcuts = array_map('trim', $shortcuts);
  182. FreshRSS_Context::$user_conf->save();
  183. invalidateHttpCache();
  184. Minz_Request::good(_t('feedback.conf.shortcuts_updated'), array('c' => 'configure', 'a' => 'shortcut'));
  185. }
  186. FreshRSS_View::prependTitle(_t('conf.shortcut.title') . ' · ');
  187. }
  188. /**
  189. * This action handles the archive configuration page.
  190. *
  191. * It displays the archive configuration page.
  192. * If this action is reached through a POST request, it stores all new
  193. * configuration values then sends a notification to the user.
  194. *
  195. * The options available on that page are:
  196. * - duration to retain old article (default: 3)
  197. * - number of article to retain per feed (default: 0)
  198. * - refresh frequency (default: 0)
  199. */
  200. public function archivingAction() {
  201. if (Minz_Request::isPost()) {
  202. if (!Minz_Request::paramBoolean('enable_keep_max')) {
  203. $keepMax = false;
  204. } elseif (!$keepMax = Minz_Request::param('keep_max')) {
  205. $keepMax = FreshRSS_Feed::ARCHIVING_RETENTION_COUNT_LIMIT;
  206. }
  207. if (Minz_Request::paramBoolean('enable_keep_period')) {
  208. $keepPeriod = FreshRSS_Feed::ARCHIVING_RETENTION_PERIOD;
  209. if (is_numeric(Minz_Request::param('keep_period_count')) && preg_match('/^PT?1[YMWDH]$/', Minz_Request::param('keep_period_unit'))) {
  210. $keepPeriod = str_replace('1', Minz_Request::param('keep_period_count'), Minz_Request::param('keep_period_unit'));
  211. }
  212. } else {
  213. $keepPeriod = false;
  214. }
  215. FreshRSS_Context::$user_conf->ttl_default = Minz_Request::param('ttl_default', FreshRSS_Feed::TTL_DEFAULT);
  216. FreshRSS_Context::$user_conf->archiving = [
  217. 'keep_period' => $keepPeriod,
  218. 'keep_max' => $keepMax,
  219. 'keep_min' => Minz_Request::param('keep_min_default', 0),
  220. 'keep_favourites' => Minz_Request::paramBoolean('keep_favourites'),
  221. 'keep_labels' => Minz_Request::paramBoolean('keep_labels'),
  222. 'keep_unreads' => Minz_Request::paramBoolean('keep_unreads'),
  223. ];
  224. FreshRSS_Context::$user_conf->keep_history_default = null; //Legacy < FreshRSS 1.15
  225. FreshRSS_Context::$user_conf->old_entries = null; //Legacy < FreshRSS 1.15
  226. FreshRSS_Context::$user_conf->save();
  227. invalidateHttpCache();
  228. Minz_Request::good(_t('feedback.conf.updated'), [ 'c' => 'configure', 'a' => 'archiving' ]);
  229. }
  230. $volatile = [
  231. 'enable_keep_period' => false,
  232. 'keep_period_count' => '3',
  233. 'keep_period_unit' => 'P1M',
  234. ];
  235. $keepPeriod = FreshRSS_Context::$user_conf->archiving['keep_period'];
  236. if (preg_match('/^PT?(?P<count>\d+)[YMWDH]$/', $keepPeriod, $matches)) {
  237. $volatile = [
  238. 'enable_keep_period' => true,
  239. 'keep_period_count' => $matches['count'],
  240. 'keep_period_unit' => str_replace($matches['count'], '1', $keepPeriod),
  241. ];
  242. }
  243. FreshRSS_Context::$user_conf->volatile = $volatile;
  244. $entryDAO = FreshRSS_Factory::createEntryDao();
  245. $this->view->nb_total = $entryDAO->count();
  246. $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
  247. $this->view->size_user = $databaseDAO->size();
  248. if (FreshRSS_Auth::hasAccess('admin')) {
  249. $this->view->size_total = $databaseDAO->size(true);
  250. }
  251. FreshRSS_View::prependTitle(_t('conf.archiving.title') . ' · ');
  252. }
  253. /**
  254. * This action handles the user queries configuration page.
  255. *
  256. * If this action is reached through a POST request, it stores all new
  257. * configuration values then sends a notification to the user then
  258. * redirect to the same page.
  259. * If this action is not reached through a POST request, it displays the
  260. * configuration page and verifies that every user query is runable by
  261. * checking if categories and feeds are still in use.
  262. */
  263. public function queriesAction() {
  264. FreshRSS_View::appendScript(Minz_Url::display('/scripts/draggable.js?' . @filemtime(PUBLIC_PATH . '/scripts/draggable.js')));
  265. $category_dao = FreshRSS_Factory::createCategoryDao();
  266. $feed_dao = FreshRSS_Factory::createFeedDao();
  267. $tag_dao = FreshRSS_Factory::createTagDao();
  268. if (Minz_Request::isPost()) {
  269. $params = Minz_Request::param('queries', array());
  270. $queries = [];
  271. foreach ($params as $key => $query) {
  272. if (!$query['name']) {
  273. $query['name'] = _t('conf.query.number', $key + 1);
  274. }
  275. if ($query['search']) {
  276. $query['search'] = urldecode($query['search']);
  277. }
  278. $queries[intval($key)] = new FreshRSS_UserQuery($query, $feed_dao, $category_dao, $tag_dao);
  279. }
  280. FreshRSS_Context::$user_conf->queries = $queries;
  281. FreshRSS_Context::$user_conf->save();
  282. Minz_Request::good(_t('feedback.conf.updated'), [ 'c' => 'configure', 'a' => 'queries' ]);
  283. } else {
  284. $this->view->queries = array();
  285. foreach (FreshRSS_Context::$user_conf->queries as $key => $query) {
  286. $this->view->queries[intval($key)] = new FreshRSS_UserQuery($query, $feed_dao, $category_dao, $tag_dao);
  287. }
  288. }
  289. $this->view->categories = $category_dao->listCategories(false);
  290. $this->view->feeds = $feed_dao->listFeeds();
  291. $this->view->tags = $tag_dao->listTags();
  292. $id = Minz_Request::param('id');
  293. $this->view->displaySlider = false;
  294. if (false !== $id) {
  295. $id = intval($id);
  296. $this->view->displaySlider = true;
  297. $this->view->query = $this->view->queries[$id];
  298. $this->view->queryId = $id;
  299. }
  300. FreshRSS_View::prependTitle(_t('conf.query.title') . ' · ');
  301. }
  302. /**
  303. * Handles query configuration.
  304. * It displays the query configuration page and handles modifications
  305. * applied to the selected query.
  306. */
  307. public function queryAction() {
  308. $this->view->_layout(false);
  309. $id = Minz_Request::param('id');
  310. if (false === $id || !isset(FreshRSS_Context::$user_conf->queries[$id])) {
  311. Minz_Error::error(404);
  312. return;
  313. }
  314. $category_dao = FreshRSS_Factory::createCategoryDao();
  315. $feed_dao = FreshRSS_Factory::createFeedDao();
  316. $tag_dao = FreshRSS_Factory::createTagDao();
  317. $query = new FreshRSS_UserQuery(FreshRSS_Context::$user_conf->queries[$id], $feed_dao, $category_dao, $tag_dao);
  318. $this->view->query = $query;
  319. $this->view->queryId = $id;
  320. $this->view->categories = $category_dao->listCategories(false);
  321. $this->view->feeds = $feed_dao->listFeeds();
  322. $this->view->tags = $tag_dao->listTags();
  323. if (Minz_Request::isPost()) {
  324. $params = array_filter(Minz_Request::param('query', []));
  325. if (!empty($params['search'])) {
  326. $params['search'] = htmlspecialchars_decode($params['search'], ENT_QUOTES);
  327. }
  328. if (!empty($params['state'])) {
  329. $params['state'] = array_sum($params['state']);
  330. }
  331. $params['url'] = Minz_Url::display(['params' => $params]);
  332. $name = Minz_Request::param('name', _t('conf.query.number', $id + 1));
  333. if ('' === $name) {
  334. $name = _t('conf.query.number', $id + 1);
  335. }
  336. $params['name'] = $name;
  337. $queries = FreshRSS_Context::$user_conf->queries;
  338. $queries[$id] = new FreshRSS_UserQuery($params, $feed_dao, $category_dao, $tag_dao);
  339. FreshRSS_Context::$user_conf->queries = $queries;
  340. FreshRSS_Context::$user_conf->save();
  341. Minz_Request::good(_t('feedback.conf.updated'), [ 'c' => 'configure', 'a' => 'queries', 'params' => ['id' => $id] ]);
  342. }
  343. FreshRSS_View::prependTitle(_t('conf.query.title') . ' · ' . $query->getName() . ' · ');
  344. }
  345. /**
  346. * Handles query deletion
  347. */
  348. public function deleteQueryAction() {
  349. $id = Minz_Request::param('id');
  350. if (false === $id || !isset(FreshRSS_Context::$user_conf->queries[$id])) {
  351. Minz_Error::error(404);
  352. return;
  353. }
  354. $queries = FreshRSS_Context::$user_conf->queries;
  355. unset($queries[$id]);
  356. FreshRSS_Context::$user_conf->queries = $queries;
  357. FreshRSS_Context::$user_conf->save();
  358. Minz_Request::good(_t('feedback.conf.updated'), [ 'c' => 'configure', 'a' => 'queries' ]);
  359. }
  360. /**
  361. * This action handles the creation of a user query.
  362. *
  363. * It gets the GET parameters and stores them in the configuration query
  364. * storage. Before it is saved, the unwanted parameters are unset to keep
  365. * lean data.
  366. */
  367. public function bookmarkQueryAction() {
  368. $category_dao = FreshRSS_Factory::createCategoryDao();
  369. $feed_dao = FreshRSS_Factory::createFeedDao();
  370. $tag_dao = FreshRSS_Factory::createTagDao();
  371. $queries = array();
  372. foreach (FreshRSS_Context::$user_conf->queries as $key => $query) {
  373. $queries[$key] = new FreshRSS_UserQuery($query, $feed_dao, $category_dao, $tag_dao);
  374. }
  375. $params = $_GET;
  376. unset($params['rid']);
  377. $params['url'] = Minz_Url::display(array('params' => $params));
  378. $params['name'] = _t('conf.query.number', count($queries) + 1);
  379. $queries[] = new FreshRSS_UserQuery($params, $feed_dao, $category_dao, $tag_dao);
  380. FreshRSS_Context::$user_conf->queries = $queries;
  381. FreshRSS_Context::$user_conf->save();
  382. Minz_Request::good(_t('feedback.conf.query_created', $params['name']), [ 'c' => 'configure', 'a' => 'queries' ]);
  383. }
  384. /**
  385. * This action handles the system configuration page.
  386. *
  387. * It displays the system configuration page.
  388. * If this action is reach through a POST request, it stores all new
  389. * configuration values then sends a notification to the user.
  390. *
  391. * The options available on the page are:
  392. * - instance name (default: FreshRSS)
  393. * - auto update URL (default: false)
  394. * - force emails validation (default: false)
  395. * - user limit (default: 1)
  396. * - user category limit (default: 16384)
  397. * - user feed limit (default: 16384)
  398. * - user login duration for form auth (default: FreshRSS_Auth::DEFAULT_COOKIE_DURATION)
  399. *
  400. * The `force-email-validation` is ignored with PHP < 5.5
  401. */
  402. public function systemAction() {
  403. if (!FreshRSS_Auth::hasAccess('admin')) {
  404. Minz_Error::error(403);
  405. }
  406. if (Minz_Request::isPost()) {
  407. $limits = FreshRSS_Context::$system_conf->limits;
  408. $limits['max_registrations'] = Minz_Request::param('max-registrations', 1);
  409. $limits['max_feeds'] = Minz_Request::param('max-feeds', 16384);
  410. $limits['max_categories'] = Minz_Request::param('max-categories', 16384);
  411. $limits['cookie_duration'] = Minz_Request::param('cookie-duration', FreshRSS_Auth::DEFAULT_COOKIE_DURATION);
  412. FreshRSS_Context::$system_conf->limits = $limits;
  413. FreshRSS_Context::$system_conf->title = Minz_Request::param('instance-name', 'FreshRSS');
  414. FreshRSS_Context::$system_conf->auto_update_url = Minz_Request::param('auto-update-url', false);
  415. FreshRSS_Context::$system_conf->force_email_validation = Minz_Request::param('force-email-validation', false);
  416. FreshRSS_Context::$system_conf->save();
  417. invalidateHttpCache();
  418. Minz_Request::good(_t('feedback.conf.updated'), [ 'c' => 'configure', 'a' => 'system' ]);
  419. }
  420. }
  421. }