ConfigurationSetter.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. // 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 = filter_var($value['url'], FILTER_VALIDATE_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. $data['display_categories'] = $this->handleBool($value);
  158. }
  159. private function _display_posts(&$data, $value) {
  160. $data['display_posts'] = $this->handleBool($value);
  161. }
  162. private function _hide_read_feeds(&$data, $value) {
  163. $data['hide_read_feeds'] = $this->handleBool($value);
  164. }
  165. private function _sides_close_article(&$data, $value) {
  166. $data['sides_close_article'] = $this->handleBool($value);
  167. }
  168. private function _lazyload(&$data, $value) {
  169. $data['lazyload'] = $this->handleBool($value);
  170. }
  171. private function _mark_when(&$data, $values) {
  172. foreach ($values as $key => $value) {
  173. $data['mark_when'][$key] = $this->handleBool($value);
  174. }
  175. }
  176. private function _onread_jump_next(&$data, $value) {
  177. $data['onread_jump_next'] = $this->handleBool($value);
  178. }
  179. private function _reading_confirm(&$data, $value) {
  180. $data['reading_confirm'] = $this->handleBool($value);
  181. }
  182. private function _sticky_post(&$data, $value) {
  183. $data['sticky_post'] = $this->handleBool($value);
  184. }
  185. private function _bottomline_date(&$data, $value) {
  186. $data['bottomline_date'] = $this->handleBool($value);
  187. }
  188. private function _bottomline_favorite(&$data, $value) {
  189. $data['bottomline_favorite'] = $this->handleBool($value);
  190. }
  191. private function _bottomline_link(&$data, $value) {
  192. $data['bottomline_link'] = $this->handleBool($value);
  193. }
  194. private function _bottomline_read(&$data, $value) {
  195. $data['bottomline_read'] = $this->handleBool($value);
  196. }
  197. private function _bottomline_sharing(&$data, $value) {
  198. $data['bottomline_sharing'] = $this->handleBool($value);
  199. }
  200. private function _bottomline_tags(&$data, $value) {
  201. $data['bottomline_tags'] = $this->handleBool($value);
  202. }
  203. private function _topline_date(&$data, $value) {
  204. $data['topline_date'] = $this->handleBool($value);
  205. }
  206. private function _topline_favorite(&$data, $value) {
  207. $data['topline_favorite'] = $this->handleBool($value);
  208. }
  209. private function _topline_link(&$data, $value) {
  210. $data['topline_link'] = $this->handleBool($value);
  211. }
  212. private function _topline_read(&$data, $value) {
  213. $data['topline_read'] = $this->handleBool($value);
  214. }
  215. private function _topline_display_authors(&$data, $value) {
  216. $data['topline_display_authors'] = $this->handleBool($value);
  217. }
  218. /**
  219. * The (not so long) list of setters for system configuration.
  220. */
  221. private function _allow_anonymous(&$data, $value) {
  222. $data['allow_anonymous'] = $this->handleBool($value) && FreshRSS_Auth::accessNeedsAction();
  223. }
  224. private function _allow_anonymous_refresh(&$data, $value) {
  225. $data['allow_anonymous_refresh'] = $this->handleBool($value) && $data['allow_anonymous'];
  226. }
  227. private function _api_enabled(&$data, $value) {
  228. $data['api_enabled'] = $this->handleBool($value);
  229. }
  230. private function _auth_type(&$data, $value) {
  231. $value = strtolower($value);
  232. if (!in_array($value, array('form', 'http_auth', 'none'))) {
  233. $value = 'none';
  234. }
  235. $data['auth_type'] = $value;
  236. $this->_allow_anonymous($data, $data['allow_anonymous']);
  237. }
  238. private function _db(&$data, $value) {
  239. if (!isset($value['type'])) {
  240. return;
  241. }
  242. switch ($value['type']) {
  243. case 'mysql':
  244. case 'pgsql':
  245. if (empty($value['host']) ||
  246. empty($value['user']) ||
  247. empty($value['base']) ||
  248. !isset($value['password'])) {
  249. return;
  250. }
  251. $data['db']['type'] = $value['type'];
  252. $data['db']['host'] = $value['host'];
  253. $data['db']['user'] = $value['user'];
  254. $data['db']['base'] = $value['base'];
  255. $data['db']['password'] = $value['password'];
  256. $data['db']['prefix'] = isset($value['prefix']) ? $value['prefix'] : '';
  257. break;
  258. case 'sqlite':
  259. $data['db']['type'] = $value['type'];
  260. $data['db']['host'] = '';
  261. $data['db']['user'] = '';
  262. $data['db']['base'] = '';
  263. $data['db']['password'] = '';
  264. $data['db']['prefix'] = '';
  265. break;
  266. default:
  267. return;
  268. }
  269. }
  270. private function _default_user(&$data, $value) {
  271. $user_list = listUsers();
  272. if (in_array($value, $user_list)) {
  273. $data['default_user'] = $value;
  274. }
  275. }
  276. private function _environment(&$data, $value) {
  277. $value = strtolower($value);
  278. if (!in_array($value, array('silent', 'development', 'production'))) {
  279. $value = 'production';
  280. }
  281. $data['environment'] = $value;
  282. }
  283. private function _limits(&$data, $values) {
  284. $max_small_int = 16384;
  285. $limits_keys = array(
  286. 'cookie_duration' => array(
  287. 'min' => 0,
  288. ),
  289. 'cache_duration' => array(
  290. 'min' => 0,
  291. ),
  292. 'timeout' => array(
  293. 'min' => 0,
  294. ),
  295. 'max_inactivity' => array(
  296. 'min' => 0,
  297. ),
  298. 'max_feeds' => array(
  299. 'min' => 0,
  300. 'max' => $max_small_int,
  301. ),
  302. 'max_categories' => array(
  303. 'min' => 0,
  304. 'max' => $max_small_int,
  305. ),
  306. 'max_registrations' => array(
  307. 'min' => 0,
  308. ),
  309. );
  310. foreach ($values as $key => $value) {
  311. if (!isset($limits_keys[$key])) {
  312. continue;
  313. }
  314. $value = intval($value);
  315. $limits = $limits_keys[$key];
  316. if ((!isset($limits['min']) || $value >= $limits['min']) &&
  317. (!isset($limits['max']) || $value <= $limits['max'])
  318. ) {
  319. $data['limits'][$key] = $value;
  320. }
  321. }
  322. }
  323. private function _unsafe_autologin_enabled(&$data, $value) {
  324. $data['unsafe_autologin_enabled'] = $this->handleBool($value);
  325. }
  326. private function _auto_update_url(&$data, $value) {
  327. if (!$value) {
  328. return;
  329. }
  330. $data['auto_update_url'] = $value;
  331. }
  332. private function _force_email_validation(&$data, $value) {
  333. $data['force_email_validation'] = $this->handleBool($value);
  334. }
  335. }