configureController.php 19 KB

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