4
0

ConfigurationSetter.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. } elseif (is_array($value)) {
  106. $data['queries'][] = $value;
  107. }
  108. }
  109. }
  110. private function _sharing(&$data, $values) {
  111. $data['sharing'] = array();
  112. foreach ($values as $value) {
  113. if (!is_array($value)) {
  114. continue;
  115. }
  116. // Verify URL and add default value when needed
  117. if (isset($value['url'])) {
  118. $is_url = (
  119. filter_var($value['url'], FILTER_VALIDATE_URL) ||
  120. (version_compare(PHP_VERSION, '5.3.3', '<') &&
  121. (strpos($value, '-') > 0) &&
  122. ($value === filter_var($value, FILTER_SANITIZE_URL)))
  123. ); //PHP bug #51192
  124. if (!$is_url) {
  125. continue;
  126. }
  127. } else {
  128. $value['url'] = null;
  129. }
  130. $data['sharing'][] = $value;
  131. }
  132. }
  133. private function _shortcuts(&$data, $values) {
  134. if (!is_array($values)) {
  135. return;
  136. }
  137. $data['shortcuts'] = $values;
  138. }
  139. private function _sort_order(&$data, $value) {
  140. $data['sort_order'] = $value === 'ASC' ? 'ASC' : 'DESC';
  141. }
  142. private function _ttl_default(&$data, $value) {
  143. $value = intval($value);
  144. $data['ttl_default'] = $value >= -1 ? $value : 3600;
  145. }
  146. private function _view_mode(&$data, $value) {
  147. $value = strtolower($value);
  148. if (!in_array($value, array('global', 'normal', 'reader'))) {
  149. $value = 'normal';
  150. }
  151. $data['view_mode'] = $value;
  152. }
  153. /**
  154. * A list of boolean setters.
  155. */
  156. private function _anon_access(&$data, $value) {
  157. $data['anon_access'] = $this->handleBool($value);
  158. }
  159. private function _auto_load_more(&$data, $value) {
  160. $data['auto_load_more'] = $this->handleBool($value);
  161. }
  162. private function _auto_remove_article(&$data, $value) {
  163. $data['auto_remove_article'] = $this->handleBool($value);
  164. }
  165. private function _mark_updated_article_unread(&$data, $value) {
  166. $data['mark_updated_article_unread'] = $this->handleBool($value);
  167. }
  168. private function _display_categories(&$data, $value) {
  169. $data['display_categories'] = $this->handleBool($value);
  170. }
  171. private function _display_posts(&$data, $value) {
  172. $data['display_posts'] = $this->handleBool($value);
  173. }
  174. private function _hide_read_feeds(&$data, $value) {
  175. $data['hide_read_feeds'] = $this->handleBool($value);
  176. }
  177. private function _lazyload(&$data, $value) {
  178. $data['lazyload'] = $this->handleBool($value);
  179. }
  180. private function _mark_when(&$data, $values) {
  181. foreach ($values as $key => $value) {
  182. $data['mark_when'][$key] = $this->handleBool($value);
  183. }
  184. }
  185. private function _onread_jump_next(&$data, $value) {
  186. $data['onread_jump_next'] = $this->handleBool($value);
  187. }
  188. private function _reading_confirm(&$data, $value) {
  189. $data['reading_confirm'] = $this->handleBool($value);
  190. }
  191. private function _sticky_post(&$data, $value) {
  192. $data['sticky_post'] = $this->handleBool($value);
  193. }
  194. private function _bottomline_date(&$data, $value) {
  195. $data['bottomline_date'] = $this->handleBool($value);
  196. }
  197. private function _bottomline_favorite(&$data, $value) {
  198. $data['bottomline_favorite'] = $this->handleBool($value);
  199. }
  200. private function _bottomline_link(&$data, $value) {
  201. $data['bottomline_link'] = $this->handleBool($value);
  202. }
  203. private function _bottomline_read(&$data, $value) {
  204. $data['bottomline_read'] = $this->handleBool($value);
  205. }
  206. private function _bottomline_sharing(&$data, $value) {
  207. $data['bottomline_sharing'] = $this->handleBool($value);
  208. }
  209. private function _bottomline_tags(&$data, $value) {
  210. $data['bottomline_tags'] = $this->handleBool($value);
  211. }
  212. private function _topline_date(&$data, $value) {
  213. $data['topline_date'] = $this->handleBool($value);
  214. }
  215. private function _topline_favorite(&$data, $value) {
  216. $data['topline_favorite'] = $this->handleBool($value);
  217. }
  218. private function _topline_link(&$data, $value) {
  219. $data['topline_link'] = $this->handleBool($value);
  220. }
  221. private function _topline_read(&$data, $value) {
  222. $data['topline_read'] = $this->handleBool($value);
  223. }
  224. /**
  225. * The (not so long) list of setters for system configuration.
  226. */
  227. private function _allow_anonymous(&$data, $value) {
  228. $data['allow_anonymous'] = $this->handleBool($value) && FreshRSS_Auth::accessNeedsAction();
  229. }
  230. private function _allow_anonymous_refresh(&$data, $value) {
  231. $data['allow_anonymous_refresh'] = $this->handleBool($value) && $data['allow_anonymous'];
  232. }
  233. private function _api_enabled(&$data, $value) {
  234. $data['api_enabled'] = $this->handleBool($value);
  235. }
  236. private function _auth_type(&$data, $value) {
  237. $value = strtolower($value);
  238. if (!in_array($value, array('form', 'http_auth', 'persona', 'none'))) {
  239. $value = 'none';
  240. }
  241. $data['auth_type'] = $value;
  242. $this->_allow_anonymous($data, $data['allow_anonymous']);
  243. }
  244. private function _db(&$data, $value) {
  245. if (!isset($value['type'])) {
  246. return;
  247. }
  248. switch ($value['type']) {
  249. case 'mysql':
  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. 'cache_duration' => array(
  292. 'min' => 0,
  293. ),
  294. 'timeout' => array(
  295. 'min' => 0,
  296. ),
  297. 'max_inactivity' => array(
  298. 'min' => 0,
  299. ),
  300. 'max_feeds' => array(
  301. 'min' => 0,
  302. 'max' => $max_small_int,
  303. ),
  304. 'max_categories' => array(
  305. 'min' => 0,
  306. 'max' => $max_small_int,
  307. ),
  308. 'max_registrations' => array(
  309. 'min' => 0,
  310. ),
  311. );
  312. foreach ($values as $key => $value) {
  313. if (!isset($limits_keys[$key])) {
  314. continue;
  315. }
  316. $value = intval($value);
  317. $limits = $limits_keys[$key];
  318. if (
  319. (!isset($limits['min']) || $value >= $limits['min']) &&
  320. (!isset($limits['max']) || $value <= $limits['max'])
  321. ) {
  322. $data['limits'][$key] = $value;
  323. }
  324. }
  325. }
  326. private function _unsafe_autologin_enabled(&$data, $value) {
  327. $data['unsafe_autologin_enabled'] = $this->handleBool($value);
  328. }
  329. private function _auto_update_url(&$data, $value) {
  330. if (!$value) {
  331. return;
  332. }
  333. $data['auto_update_url'] = $value;
  334. }
  335. }