ConfigurationSetter.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 +
  52. FreshRSS_Entry::STATE_NOT_READ);
  53. break;
  54. case 'adaptive':
  55. case 'unread':
  56. default:
  57. $data['default_view'] = $value;
  58. $data['default_state'] = FreshRSS_Entry::STATE_NOT_READ;
  59. }
  60. }
  61. // It works for system config too!
  62. private function _extensions_enabled(&$data, $value) {
  63. if (!is_array($value)) {
  64. $value = array($value);
  65. }
  66. $data['extensions_enabled'] = $value;
  67. }
  68. private function _html5_notif_timeout(&$data, $value) {
  69. $value = intval($value);
  70. $data['html5_notif_timeout'] = $value >= 0 ? $value : 0;
  71. }
  72. private function _keep_history_default(&$data, $value) {
  73. $value = intval($value);
  74. $data['keep_history_default'] = $value >= -1 ? $value : 0;
  75. }
  76. // It works for system config too!
  77. private function _language(&$data, $value) {
  78. $value = strtolower($value);
  79. $languages = Minz_Translate::availableLanguages();
  80. if (!in_array($value, $languages)) {
  81. $value = 'en';
  82. }
  83. $data['language'] = $value;
  84. }
  85. private function _mail_login(&$data, $value) {
  86. $value = filter_var($value, FILTER_VALIDATE_EMAIL);
  87. $data['mail_login'] = $value ? $value : '';
  88. }
  89. private function _old_entries(&$data, $value) {
  90. $value = intval($value);
  91. $data['old_entries'] = $value > 0 ? $value : 3;
  92. }
  93. private function _passwordHash(&$data, $value) {
  94. $data['passwordHash'] = ctype_graph($value) && (strlen($value) >= 60) ? $value : '';
  95. }
  96. private function _posts_per_page(&$data, $value) {
  97. $value = intval($value);
  98. $data['posts_per_page'] = $value > 0 ? $value : 10;
  99. }
  100. private function _queries(&$data, $values) {
  101. $data['queries'] = array();
  102. foreach ($values as $value) {
  103. if ($value instanceof FreshRSS_UserQuery) {
  104. $data['queries'][] = $value->toArray();
  105. }
  106. }
  107. }
  108. private function _sharing(&$data, $values) {
  109. $data['sharing'] = array();
  110. foreach ($values as $value) {
  111. if (!is_array($value)) {
  112. continue;
  113. }
  114. // Verify URL and add default value when needed
  115. if (isset($value['url'])) {
  116. $is_url = (
  117. filter_var($value['url'], FILTER_VALIDATE_URL) ||
  118. (version_compare(PHP_VERSION, '5.3.3', '<') &&
  119. (strpos($value, '-') > 0) &&
  120. ($value === filter_var($value, FILTER_SANITIZE_URL)))
  121. ); //PHP bug #51192
  122. if (!$is_url) {
  123. continue;
  124. }
  125. } else {
  126. $value['url'] = null;
  127. }
  128. $data['sharing'][] = $value;
  129. }
  130. }
  131. private function _shortcuts(&$data, $values) {
  132. if (!is_array($values)) {
  133. return;
  134. }
  135. $data['shortcuts'] = $values;
  136. }
  137. private function _sort_order(&$data, $value) {
  138. $data['sort_order'] = $value === 'ASC' ? 'ASC' : 'DESC';
  139. }
  140. private function _ttl_default(&$data, $value) {
  141. $value = intval($value);
  142. $data['ttl_default'] = $value >= -1 ? $value : 3600;
  143. }
  144. private function _view_mode(&$data, $value) {
  145. $value = strtolower($value);
  146. if (!in_array($value, array('global', 'normal', 'reader'))) {
  147. $value = 'normal';
  148. }
  149. $data['view_mode'] = $value;
  150. }
  151. /**
  152. * A list of boolean setters.
  153. */
  154. private function _anon_access(&$data, $value) {
  155. $data['anon_access'] = $this->handleBool($value);
  156. }
  157. private function _auto_load_more(&$data, $value) {
  158. $data['auto_load_more'] = $this->handleBool($value);
  159. }
  160. private function _auto_remove_article(&$data, $value) {
  161. $data['auto_remove_article'] = $this->handleBool($value);
  162. }
  163. private function _mark_updated_article_unread(&$data, $value) {
  164. $data['mark_updated_article_unread'] = $this->handleBool($value);
  165. }
  166. private function _display_categories(&$data, $value) {
  167. $data['display_categories'] = $this->handleBool($value);
  168. }
  169. private function _display_posts(&$data, $value) {
  170. $data['display_posts'] = $this->handleBool($value);
  171. }
  172. private function _hide_read_feeds(&$data, $value) {
  173. $data['hide_read_feeds'] = $this->handleBool($value);
  174. }
  175. private function _lazyload(&$data, $value) {
  176. $data['lazyload'] = $this->handleBool($value);
  177. }
  178. private function _mark_when(&$data, $values) {
  179. foreach ($values as $key => $value) {
  180. $data['mark_when'][$key] = $this->handleBool($value);
  181. }
  182. }
  183. private function _onread_jump_next(&$data, $value) {
  184. $data['onread_jump_next'] = $this->handleBool($value);
  185. }
  186. private function _reading_confirm(&$data, $value) {
  187. $data['reading_confirm'] = $this->handleBool($value);
  188. }
  189. private function _sticky_post(&$data, $value) {
  190. $data['sticky_post'] = $this->handleBool($value);
  191. }
  192. private function _bottomline_date(&$data, $value) {
  193. $data['bottomline_date'] = $this->handleBool($value);
  194. }
  195. private function _bottomline_favorite(&$data, $value) {
  196. $data['bottomline_favorite'] = $this->handleBool($value);
  197. }
  198. private function _bottomline_link(&$data, $value) {
  199. $data['bottomline_link'] = $this->handleBool($value);
  200. }
  201. private function _bottomline_read(&$data, $value) {
  202. $data['bottomline_read'] = $this->handleBool($value);
  203. }
  204. private function _bottomline_sharing(&$data, $value) {
  205. $data['bottomline_sharing'] = $this->handleBool($value);
  206. }
  207. private function _bottomline_tags(&$data, $value) {
  208. $data['bottomline_tags'] = $this->handleBool($value);
  209. }
  210. private function _topline_date(&$data, $value) {
  211. $data['topline_date'] = $this->handleBool($value);
  212. }
  213. private function _topline_favorite(&$data, $value) {
  214. $data['topline_favorite'] = $this->handleBool($value);
  215. }
  216. private function _topline_link(&$data, $value) {
  217. $data['topline_link'] = $this->handleBool($value);
  218. }
  219. private function _topline_read(&$data, $value) {
  220. $data['topline_read'] = $this->handleBool($value);
  221. }
  222. /**
  223. * The (not so long) list of setters for system configuration.
  224. */
  225. private function _allow_anonymous(&$data, $value) {
  226. $data['allow_anonymous'] = $this->handleBool($value) && FreshRSS_Auth::accessNeedsAction();
  227. }
  228. private function _allow_anonymous_refresh(&$data, $value) {
  229. $data['allow_anonymous_refresh'] = $this->handleBool($value) && $data['allow_anonymous'];
  230. }
  231. private function _api_enabled(&$data, $value) {
  232. $data['api_enabled'] = $this->handleBool($value);
  233. }
  234. private function _auth_type(&$data, $value) {
  235. $value = strtolower($value);
  236. if (!in_array($value, array('form', 'http_auth', 'persona', 'none'))) {
  237. $value = 'none';
  238. }
  239. $data['auth_type'] = $value;
  240. $this->_allow_anonymous($data, $data['allow_anonymous']);
  241. }
  242. private function _db(&$data, $value) {
  243. if (!isset($value['type'])) {
  244. return;
  245. }
  246. switch ($value['type']) {
  247. case 'mysql':
  248. if (empty($value['host']) ||
  249. empty($value['user']) ||
  250. empty($value['base']) ||
  251. !isset($value['password'])) {
  252. return;
  253. }
  254. $data['db']['type'] = $value['type'];
  255. $data['db']['host'] = $value['host'];
  256. $data['db']['user'] = $value['user'];
  257. $data['db']['base'] = $value['base'];
  258. $data['db']['password'] = $value['password'];
  259. $data['db']['prefix'] = isset($value['prefix']) ? $value['prefix'] : '';
  260. break;
  261. case 'sqlite':
  262. $data['db']['type'] = $value['type'];
  263. $data['db']['host'] = '';
  264. $data['db']['user'] = '';
  265. $data['db']['base'] = '';
  266. $data['db']['password'] = '';
  267. $data['db']['prefix'] = '';
  268. break;
  269. default:
  270. return;
  271. }
  272. }
  273. private function _default_user(&$data, $value) {
  274. $user_list = listUsers();
  275. if (in_array($value, $user_list)) {
  276. $data['default_user'] = $value;
  277. }
  278. }
  279. private function _environment(&$data, $value) {
  280. $value = strtolower($value);
  281. if (!in_array($value, array('silent', 'development', 'production'))) {
  282. $value = 'production';
  283. }
  284. $data['environment'] = $value;
  285. }
  286. private function _limits(&$data, $values) {
  287. $max_small_int = 16384;
  288. $limits_keys = array(
  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. $limits = $limits_keys[$key];
  315. if (
  316. (!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. }