ConfigurationSetter.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. class FreshRSS_ConfigurationSetter {
  3. /**
  4. * Return if the given key is supported by this setter.
  5. * @param $key the key to test.
  6. * @return true if the key is supported, false else.
  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 $data an array containing the list of all configuration data.
  15. * @param $key the key to update.
  16. * @param $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 $value the tested value.
  26. * @return 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. private function _keep_history_default(&$data, $value) {
  72. $value = intval($value);
  73. $data['keep_history_default'] = $value >= FreshRSS_Feed::KEEP_HISTORY_INFINITE ? $value : 0;
  74. }
  75. // It works for system config too!
  76. private function _language(&$data, $value) {
  77. $value = strtolower($value);
  78. $languages = Minz_Translate::availableLanguages();
  79. if (!in_array($value, $languages)) {
  80. $value = 'en';
  81. }
  82. $data['language'] = $value;
  83. }
  84. private function _old_entries(&$data, $value) {
  85. $value = intval($value);
  86. $data['old_entries'] = $value > 0 ? $value : 3;
  87. }
  88. private function _passwordHash(&$data, $value) {
  89. $data['passwordHash'] = ctype_graph($value) && (strlen($value) >= 60) ? $value : '';
  90. }
  91. private function _posts_per_page(&$data, $value) {
  92. $value = intval($value);
  93. $data['posts_per_page'] = $value > 0 ? $value : 10;
  94. }
  95. private function _queries(&$data, $values) {
  96. $data['queries'] = array();
  97. foreach ($values as $value) {
  98. if ($value instanceof FreshRSS_UserQuery) {
  99. $data['queries'][] = $value->toArray();
  100. } elseif (is_array($value)) {
  101. $data['queries'][] = $value;
  102. }
  103. }
  104. }
  105. private function _sharing(&$data, $values) {
  106. $data['sharing'] = array();
  107. foreach ($values as $value) {
  108. if (!is_array($value)) {
  109. continue;
  110. }
  111. // Verify URL and add default value when needed
  112. if (isset($value['url'])) {
  113. $is_url = filter_var($value['url'], FILTER_VALIDATE_URL);
  114. if (!$is_url) {
  115. continue;
  116. }
  117. } else {
  118. $value['url'] = null;
  119. }
  120. $data['sharing'][] = $value;
  121. }
  122. }
  123. private function _shortcuts(&$data, $values) {
  124. if (!is_array($values)) {
  125. return;
  126. }
  127. $data['shortcuts'] = $values;
  128. }
  129. private function _sort_order(&$data, $value) {
  130. $data['sort_order'] = $value === 'ASC' ? 'ASC' : 'DESC';
  131. }
  132. private function _ttl_default(&$data, $value) {
  133. $value = intval($value);
  134. $data['ttl_default'] = $value > FreshRSS_Feed::TTL_DEFAULT ? $value : 3600;
  135. }
  136. private function _view_mode(&$data, $value) {
  137. $value = strtolower($value);
  138. if (!in_array($value, array('global', 'normal', 'reader'))) {
  139. $value = 'normal';
  140. }
  141. $data['view_mode'] = $value;
  142. }
  143. /**
  144. * A list of boolean setters.
  145. */
  146. private function _anon_access(&$data, $value) {
  147. $data['anon_access'] = $this->handleBool($value);
  148. }
  149. private function _auto_load_more(&$data, $value) {
  150. $data['auto_load_more'] = $this->handleBool($value);
  151. }
  152. private function _auto_remove_article(&$data, $value) {
  153. $data['auto_remove_article'] = $this->handleBool($value);
  154. }
  155. private function _mark_updated_article_unread(&$data, $value) {
  156. $data['mark_updated_article_unread'] = $this->handleBool($value);
  157. }
  158. private function _show_nav_buttons(&$data, $value) {
  159. $data['show_nav_buttons'] = $this->handleBool($value);
  160. }
  161. private function _display_categories(&$data, $value) {
  162. $data['display_categories'] = $this->handleBool($value);
  163. }
  164. private function _display_posts(&$data, $value) {
  165. $data['display_posts'] = $this->handleBool($value);
  166. }
  167. private function _hide_read_feeds(&$data, $value) {
  168. $data['hide_read_feeds'] = $this->handleBool($value);
  169. }
  170. private function _sides_close_article(&$data, $value) {
  171. $data['sides_close_article'] = $this->handleBool($value);
  172. }
  173. private function _lazyload(&$data, $value) {
  174. $data['lazyload'] = $this->handleBool($value);
  175. }
  176. private function _mark_when(&$data, $values) {
  177. foreach ($values as $key => $value) {
  178. $data['mark_when'][$key] = $this->handleBool($value);
  179. }
  180. }
  181. private function _onread_jump_next(&$data, $value) {
  182. $data['onread_jump_next'] = $this->handleBool($value);
  183. }
  184. private function _reading_confirm(&$data, $value) {
  185. $data['reading_confirm'] = $this->handleBool($value);
  186. }
  187. private function _sticky_post(&$data, $value) {
  188. $data['sticky_post'] = $this->handleBool($value);
  189. }
  190. private function _bottomline_date(&$data, $value) {
  191. $data['bottomline_date'] = $this->handleBool($value);
  192. }
  193. private function _bottomline_favorite(&$data, $value) {
  194. $data['bottomline_favorite'] = $this->handleBool($value);
  195. }
  196. private function _bottomline_link(&$data, $value) {
  197. $data['bottomline_link'] = $this->handleBool($value);
  198. }
  199. private function _bottomline_read(&$data, $value) {
  200. $data['bottomline_read'] = $this->handleBool($value);
  201. }
  202. private function _bottomline_sharing(&$data, $value) {
  203. $data['bottomline_sharing'] = $this->handleBool($value);
  204. }
  205. private function _bottomline_tags(&$data, $value) {
  206. $data['bottomline_tags'] = $this->handleBool($value);
  207. }
  208. private function _topline_date(&$data, $value) {
  209. $data['topline_date'] = $this->handleBool($value);
  210. }
  211. private function _topline_favorite(&$data, $value) {
  212. $data['topline_favorite'] = $this->handleBool($value);
  213. }
  214. private function _topline_link(&$data, $value) {
  215. $data['topline_link'] = $this->handleBool($value);
  216. }
  217. private function _topline_read(&$data, $value) {
  218. $data['topline_read'] = $this->handleBool($value);
  219. }
  220. private function _topline_display_authors(&$data, $value) {
  221. $data['topline_display_authors'] = $this->handleBool($value);
  222. }
  223. /**
  224. * The (not so long) list of setters for system configuration.
  225. */
  226. private function _allow_anonymous(&$data, $value) {
  227. $data['allow_anonymous'] = $this->handleBool($value) && FreshRSS_Auth::accessNeedsAction();
  228. }
  229. private function _allow_anonymous_refresh(&$data, $value) {
  230. $data['allow_anonymous_refresh'] = $this->handleBool($value) && $data['allow_anonymous'];
  231. }
  232. private function _api_enabled(&$data, $value) {
  233. $data['api_enabled'] = $this->handleBool($value);
  234. }
  235. private function _auth_type(&$data, $value) {
  236. $value = strtolower($value);
  237. if (!in_array($value, array('form', 'http_auth', 'none'))) {
  238. $value = 'none';
  239. }
  240. $data['auth_type'] = $value;
  241. $this->_allow_anonymous($data, $data['allow_anonymous']);
  242. }
  243. private function _db(&$data, $value) {
  244. if (!isset($value['type'])) {
  245. return;
  246. }
  247. switch ($value['type']) {
  248. case 'mysql':
  249. case 'pgsql':
  250. if (empty($value['host']) ||
  251. empty($value['user']) ||
  252. empty($value['base']) ||
  253. !isset($value['password'])) {
  254. return;
  255. }
  256. $data['db']['type'] = $value['type'];
  257. $data['db']['host'] = $value['host'];
  258. $data['db']['user'] = $value['user'];
  259. $data['db']['base'] = $value['base'];
  260. $data['db']['password'] = $value['password'];
  261. $data['db']['prefix'] = isset($value['prefix']) ? $value['prefix'] : '';
  262. break;
  263. case 'sqlite':
  264. $data['db']['type'] = $value['type'];
  265. $data['db']['host'] = '';
  266. $data['db']['user'] = '';
  267. $data['db']['base'] = '';
  268. $data['db']['password'] = '';
  269. $data['db']['prefix'] = '';
  270. break;
  271. default:
  272. return;
  273. }
  274. }
  275. private function _default_user(&$data, $value) {
  276. $user_list = listUsers();
  277. if (in_array($value, $user_list)) {
  278. $data['default_user'] = $value;
  279. }
  280. }
  281. private function _environment(&$data, $value) {
  282. $value = strtolower($value);
  283. if (!in_array($value, array('silent', 'development', 'production'))) {
  284. $value = 'production';
  285. }
  286. $data['environment'] = $value;
  287. }
  288. private function _limits(&$data, $values) {
  289. $max_small_int = 16384;
  290. $limits_keys = array(
  291. 'cookie_duration' => array(
  292. 'min' => 0,
  293. ),
  294. 'cache_duration' => array(
  295. 'min' => 0,
  296. ),
  297. 'timeout' => array(
  298. 'min' => 0,
  299. ),
  300. 'max_inactivity' => array(
  301. 'min' => 0,
  302. ),
  303. 'max_feeds' => array(
  304. 'min' => 0,
  305. 'max' => $max_small_int,
  306. ),
  307. 'max_categories' => array(
  308. 'min' => 0,
  309. 'max' => $max_small_int,
  310. ),
  311. 'max_registrations' => array(
  312. 'min' => 0,
  313. ),
  314. );
  315. foreach ($values as $key => $value) {
  316. if (!isset($limits_keys[$key])) {
  317. continue;
  318. }
  319. $value = intval($value);
  320. $limits = $limits_keys[$key];
  321. if ((!isset($limits['min']) || $value >= $limits['min']) &&
  322. (!isset($limits['max']) || $value <= $limits['max'])
  323. ) {
  324. $data['limits'][$key] = $value;
  325. }
  326. }
  327. }
  328. private function _unsafe_autologin_enabled(&$data, $value) {
  329. $data['unsafe_autologin_enabled'] = $this->handleBool($value);
  330. }
  331. private function _auto_update_url(&$data, $value) {
  332. if (!$value) {
  333. return;
  334. }
  335. $data['auto_update_url'] = $value;
  336. }
  337. private function _force_email_validation(&$data, $value) {
  338. $data['force_email_validation'] = $this->handleBool($value);
  339. }
  340. }