ConfigurationSetter.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <?php
  2. class FreshRSS_ConfigurationSetter {
  3. /**
  4. * Return if the given key is supported by this setter.
  5. * @param string $key the key to test.
  6. * @return boolean true if the key is supported, false otherwise.
  7. */
  8. public function support($key) {
  9. $name_setter = '_' . $key;
  10. return is_callable(array($this, $name_setter));
  11. }
  12. /**
  13. * Set the given key in data with the current value.
  14. * @param array $data an array containing the list of all configuration data.
  15. * @param string $key the key to update.
  16. * @param mixed $value the value to set.
  17. */
  18. public function handle(&$data, $key, $value) {
  19. $name_setter = '_' . $key;
  20. call_user_func_array(array($this, $name_setter), array(&$data, $value));
  21. }
  22. /**
  23. * A helper to set boolean values.
  24. *
  25. * @param mixed $value the tested value.
  26. * @return boolean true if value is true and different from no, false else.
  27. */
  28. private function handleBool($value) {
  29. return ((bool)$value) && $value !== 'no';
  30. }
  31. /**
  32. * The (long) list of setters for user configuration.
  33. */
  34. private function _apiPasswordHash(&$data, $value) {
  35. $data['apiPasswordHash'] = ctype_graph($value) && (strlen($value) >= 60) ? $value : '';
  36. }
  37. private function _content_width(&$data, $value) {
  38. $value = strtolower($value);
  39. if (!in_array($value, array('thin', 'medium', 'large', 'no_limit'))) {
  40. $value = 'thin';
  41. }
  42. $data['content_width'] = $value;
  43. }
  44. private function _default_state(&$data, $value) {
  45. $data['default_state'] = (int)$value;
  46. }
  47. private function _default_view(&$data, $value) {
  48. switch ($value) {
  49. case 'all':
  50. $data['default_view'] = $value;
  51. $data['default_state'] = (FreshRSS_Entry::STATE_READ + FreshRSS_Entry::STATE_NOT_READ);
  52. break;
  53. case 'adaptive':
  54. case 'unread':
  55. default:
  56. $data['default_view'] = $value;
  57. $data['default_state'] = FreshRSS_Entry::STATE_NOT_READ;
  58. }
  59. }
  60. // It works for system config too!
  61. private function _extensions_enabled(&$data, $value) {
  62. if (!is_array($value)) {
  63. $value = array($value);
  64. }
  65. $data['extensions_enabled'] = $value;
  66. }
  67. private function _html5_notif_timeout(&$data, $value) {
  68. $value = intval($value);
  69. $data['html5_notif_timeout'] = $value >= 0 ? $value : 0;
  70. }
  71. // It works for system config too!
  72. private function _language(&$data, $value) {
  73. $value = strtolower($value);
  74. $languages = Minz_Translate::availableLanguages();
  75. if (!in_array($value, $languages)) {
  76. $value = 'en';
  77. }
  78. $data['language'] = $value;
  79. }
  80. private function _passwordHash(&$data, $value) {
  81. $data['passwordHash'] = ctype_graph($value) && (strlen($value) >= 60) ? $value : '';
  82. }
  83. private function _posts_per_page(&$data, $value) {
  84. $value = intval($value);
  85. $data['posts_per_page'] = $value > 0 ? $value : 10;
  86. }
  87. private function _queries(&$data, $values) {
  88. $data['queries'] = array();
  89. foreach ($values as $value) {
  90. if ($value instanceof FreshRSS_UserQuery) {
  91. $data['queries'][] = $value->toArray();
  92. } elseif (is_array($value)) {
  93. $data['queries'][] = $value;
  94. }
  95. }
  96. }
  97. private function _sharing(&$data, $values) {
  98. $data['sharing'] = array();
  99. foreach ($values as $value) {
  100. if (!is_array($value)) {
  101. continue;
  102. }
  103. // Verify URL and add default value when needed
  104. if (isset($value['url'])) {
  105. $is_url = checkUrl($value['url']);
  106. if (!$is_url) {
  107. continue;
  108. }
  109. } else {
  110. $value['url'] = null;
  111. }
  112. $data['sharing'][] = $value;
  113. }
  114. }
  115. private function _shortcuts(&$data, $values) {
  116. if (!is_array($values)) {
  117. return;
  118. }
  119. $data['shortcuts'] = $values;
  120. }
  121. private function _sort_order(&$data, $value) {
  122. $data['sort_order'] = $value === 'ASC' ? 'ASC' : 'DESC';
  123. }
  124. private function _ttl_default(&$data, $value) {
  125. $value = intval($value);
  126. $data['ttl_default'] = $value > FreshRSS_Feed::TTL_DEFAULT ? $value : 3600;
  127. }
  128. private function _view_mode(&$data, $value) {
  129. $value = strtolower($value);
  130. if (!in_array($value, array('global', 'normal', 'reader'))) {
  131. $value = 'normal';
  132. }
  133. $data['view_mode'] = $value;
  134. }
  135. /**
  136. * A list of boolean setters.
  137. */
  138. private function _anon_access(&$data, $value) {
  139. $data['anon_access'] = $this->handleBool($value);
  140. }
  141. private function _auto_load_more(&$data, $value) {
  142. $data['auto_load_more'] = $this->handleBool($value);
  143. }
  144. private function _auto_remove_article(&$data, $value) {
  145. $data['auto_remove_article'] = $this->handleBool($value);
  146. }
  147. private function _mark_updated_article_unread(&$data, $value) {
  148. $data['mark_updated_article_unread'] = $this->handleBool($value);
  149. }
  150. private function _show_nav_buttons(&$data, $value) {
  151. $data['show_nav_buttons'] = $this->handleBool($value);
  152. }
  153. private function _show_fav_unread(&$data, $value) {
  154. $data['show_fav_unread'] = $this->handleBool($value);
  155. }
  156. private function _display_categories(&$data, $value) {
  157. if (!in_array($value, [ 'active', 'remember', 'all', 'none' ], true)) {
  158. $value = $value === true ? 'all' : 'active';
  159. }
  160. $data['display_categories'] = $value;
  161. }
  162. private function show_tags(&$data, $value) {
  163. $data['show_tags'] = $value;
  164. }
  165. private function show_tags_max(&$data, $value) {
  166. $value = intval($value);
  167. $data['show_tags_max'] = $value >= 0 ? $value : 0;
  168. }
  169. private function _show_author_date(&$data, $value) {
  170. $data['show_author_date'] = $value;
  171. }
  172. private function _show_feed_name(&$data, $value) {
  173. $data['show_feed_name'] = $value;
  174. }
  175. private function _display_posts(&$data, $value) {
  176. $data['display_posts'] = $this->handleBool($value);
  177. }
  178. private function _hide_read_feeds(&$data, $value) {
  179. $data['hide_read_feeds'] = $this->handleBool($value);
  180. }
  181. private function _sides_close_article(&$data, $value) {
  182. $data['sides_close_article'] = $this->handleBool($value);
  183. }
  184. private function _lazyload(&$data, $value) {
  185. $data['lazyload'] = $this->handleBool($value);
  186. }
  187. private function _onread_jump_next(&$data, $value) {
  188. $data['onread_jump_next'] = $this->handleBool($value);
  189. }
  190. private function _reading_confirm(&$data, $value) {
  191. $data['reading_confirm'] = $this->handleBool($value);
  192. }
  193. private function _sticky_post(&$data, $value) {
  194. $data['sticky_post'] = $this->handleBool($value);
  195. }
  196. private function _darkMode(&$data, $value) {
  197. if (!in_array($value, [ 'no', 'auto'], true)) {
  198. $value = 'no';
  199. }
  200. $data['darkMode'] = $value;
  201. }
  202. private function _bottomline_date(&$data, $value) {
  203. $data['bottomline_date'] = $this->handleBool($value);
  204. }
  205. private function _bottomline_favorite(&$data, $value) {
  206. $data['bottomline_favorite'] = $this->handleBool($value);
  207. }
  208. private function _bottomline_link(&$data, $value) {
  209. $data['bottomline_link'] = $this->handleBool($value);
  210. }
  211. private function _bottomline_read(&$data, $value) {
  212. $data['bottomline_read'] = $this->handleBool($value);
  213. }
  214. private function _bottomline_sharing(&$data, $value) {
  215. $data['bottomline_sharing'] = $this->handleBool($value);
  216. }
  217. private function _bottomline_tags(&$data, $value) {
  218. $data['bottomline_tags'] = $this->handleBool($value);
  219. }
  220. private function _topline_date(&$data, $value) {
  221. $data['topline_date'] = $this->handleBool($value);
  222. }
  223. private function _topline_favorite(&$data, $value) {
  224. $data['topline_favorite'] = $this->handleBool($value);
  225. }
  226. private function _topline_link(&$data, $value) {
  227. $data['topline_link'] = $this->handleBool($value);
  228. }
  229. private function _topline_read(&$data, $value) {
  230. $data['topline_read'] = $this->handleBool($value);
  231. }
  232. private function _topline_website(&$data, $value) {
  233. $value = strtolower($value);
  234. if (!in_array($value, array('none', 'icon', 'name', 'full'))) {
  235. $value = 'full';
  236. }
  237. $data['topline_website'] = $value;
  238. }
  239. private function _topline_thumbnail(&$data, $value) {
  240. $value = strtolower($value);
  241. if (!in_array($value, array('none', 'portrait', 'square', 'landscape'))) {
  242. $value = 'none';
  243. }
  244. $data['topline_thumbnail'] = $value;
  245. }
  246. private function _topline_summary(&$data, $value) {
  247. $data['topline_summary'] = $this->handleBool($value);
  248. }
  249. private function _topline_display_authors(&$data, $value) {
  250. $data['topline_display_authors'] = $this->handleBool($value);
  251. }
  252. /**
  253. * The (not so long) list of setters for system configuration.
  254. */
  255. private function _allow_anonymous(&$data, $value) {
  256. $data['allow_anonymous'] = $this->handleBool($value) && FreshRSS_Auth::accessNeedsAction();
  257. }
  258. private function _allow_anonymous_refresh(&$data, $value) {
  259. $data['allow_anonymous_refresh'] = $this->handleBool($value) && $data['allow_anonymous'];
  260. }
  261. private function _api_enabled(&$data, $value) {
  262. $data['api_enabled'] = $this->handleBool($value);
  263. }
  264. private function _auth_type(&$data, $value) {
  265. $value = strtolower($value);
  266. if (!in_array($value, array('form', 'http_auth', 'none'))) {
  267. $value = 'none';
  268. }
  269. $data['auth_type'] = $value;
  270. $this->_allow_anonymous($data, $data['allow_anonymous']);
  271. }
  272. private function _db(&$data, $value) {
  273. if (!isset($value['type'])) {
  274. return;
  275. }
  276. switch ($value['type']) {
  277. case 'mysql':
  278. case 'pgsql':
  279. if (empty($value['host']) ||
  280. empty($value['user']) ||
  281. empty($value['base']) ||
  282. !isset($value['password'])) {
  283. return;
  284. }
  285. $data['db']['type'] = $value['type'];
  286. $data['db']['host'] = $value['host'];
  287. $data['db']['user'] = $value['user'];
  288. $data['db']['base'] = $value['base'];
  289. $data['db']['password'] = $value['password'];
  290. $data['db']['prefix'] = isset($value['prefix']) ? $value['prefix'] : '';
  291. break;
  292. case 'sqlite':
  293. $data['db']['type'] = $value['type'];
  294. $data['db']['host'] = '';
  295. $data['db']['user'] = '';
  296. $data['db']['base'] = '';
  297. $data['db']['password'] = '';
  298. $data['db']['prefix'] = '';
  299. break;
  300. default:
  301. return;
  302. }
  303. }
  304. private function _default_user(&$data, $value) {
  305. $user_list = listUsers();
  306. if (in_array($value, $user_list)) {
  307. $data['default_user'] = $value;
  308. }
  309. }
  310. private function _environment(&$data, $value) {
  311. $value = strtolower($value);
  312. if (!in_array($value, array('silent', 'development', 'production'))) {
  313. $value = 'production';
  314. }
  315. $data['environment'] = $value;
  316. }
  317. private function _limits(&$data, $values) {
  318. $max_small_int = 16384;
  319. $limits_keys = array(
  320. 'cookie_duration' => array(
  321. 'min' => 0,
  322. ),
  323. 'cache_duration' => array(
  324. 'min' => 0,
  325. ),
  326. 'timeout' => array(
  327. 'min' => 0,
  328. ),
  329. 'max_inactivity' => array(
  330. 'min' => 0,
  331. ),
  332. 'max_feeds' => array(
  333. 'min' => 0,
  334. 'max' => $max_small_int,
  335. ),
  336. 'max_categories' => array(
  337. 'min' => 0,
  338. 'max' => $max_small_int,
  339. ),
  340. 'max_registrations' => array(
  341. 'min' => 0,
  342. ),
  343. );
  344. foreach ($values as $key => $value) {
  345. if (!isset($limits_keys[$key])) {
  346. continue;
  347. }
  348. $value = intval($value);
  349. $limits = $limits_keys[$key];
  350. // @phpstan-ignore-next-line
  351. if ((!isset($limits['min']) || $value >= $limits['min']) &&
  352. (!isset($limits['max']) || $value <= $limits['max'])
  353. ) {
  354. $data['limits'][$key] = $value;
  355. }
  356. }
  357. }
  358. private function _unsafe_autologin_enabled(&$data, $value) {
  359. $data['unsafe_autologin_enabled'] = $this->handleBool($value);
  360. }
  361. private function _auto_update_url(&$data, $value) {
  362. if (!$value) {
  363. return;
  364. }
  365. $data['auto_update_url'] = $value;
  366. }
  367. private function _force_email_validation(&$data, $value) {
  368. $data['force_email_validation'] = $this->handleBool($value);
  369. }
  370. }