1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle Framework |
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
5
|
|
|
* @copyright Copyright (c) 2016, Nazar Mokrynskyi |
6
|
|
|
* @license MIT License, see license.txt |
7
|
|
|
*/ |
8
|
|
|
namespace cs\Config; |
9
|
|
|
use |
10
|
|
|
cs\Config, |
11
|
|
|
cs\DB; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class for getting of db and storage configuration of module |
15
|
|
|
*/ |
16
|
|
|
class Options { |
17
|
|
|
/** |
18
|
|
|
* Get formats for all supported options |
19
|
|
|
* |
20
|
|
|
* Format includes option type, default value, supported values or ranges, whether option is password or is multilingual |
21
|
|
|
* |
22
|
|
|
* @return array[] |
23
|
|
|
*/ |
24
|
70 |
|
public static function get_formatting () { |
25
|
70 |
|
return static::get_formatting_normalize( |
26
|
|
|
[ |
27
|
|
|
'array' => [ |
28
|
|
|
'url' => [], |
29
|
|
|
'cookie_domain' => [] |
30
|
70 |
|
], |
31
|
|
|
'int_bool' => [ |
32
|
58 |
|
'site_mode' => 1, |
33
|
58 |
|
'title_reverse' => 0, |
34
|
58 |
|
'cache_compress_js_css' => 1, |
35
|
58 |
|
'frontend_load_optimization' => 1, |
36
|
58 |
|
'vulcanization' => 1, |
37
|
58 |
|
'put_js_after_body' => 1, |
38
|
58 |
|
'disable_webcomponents' => 0, |
39
|
58 |
|
'multilingual' => 0, |
40
|
58 |
|
'db_balance' => 0, |
41
|
58 |
|
'db_mirror_mode' => DB::MIRROR_MODE_MASTER_MASTER, |
42
|
58 |
|
'gravatar_support' => 0, |
43
|
58 |
|
'smtp' => 0, |
44
|
58 |
|
'smtp_auth' => 0, |
45
|
58 |
|
'allow_user_registration' => 1, |
46
|
58 |
|
'require_registration_confirmation' => 1, |
47
|
58 |
|
'auto_sign_in_after_registration' => 1, |
48
|
58 |
|
'registration_confirmation_time' => 1, |
49
|
58 |
|
'remember_user_ip' => 0, |
50
|
58 |
|
'simple_admin_mode' => 1 |
51
|
|
|
], |
52
|
|
|
'int_range' => [ |
53
|
|
|
'inserts_limit' => [ |
54
|
|
|
'min' => 1, |
55
|
|
|
'value' => 1000 |
56
|
|
|
], |
57
|
|
|
'key_expire' => [ |
58
|
|
|
'min' => 1, |
59
|
|
|
'value' => 60 * 2 |
60
|
|
|
], |
61
|
|
|
'session_expire' => [ |
62
|
|
|
'min' => 1, |
63
|
|
|
'value' => 3600 * 24 * 30 |
64
|
|
|
], |
65
|
|
|
'update_ratio' => [ |
66
|
|
|
'min' => 0, |
67
|
|
|
'max' => 100, |
68
|
|
|
'value' => 75 |
69
|
|
|
], |
70
|
|
|
'sign_in_attempts_block_count' => [ |
71
|
|
|
'min' => 0, |
72
|
|
|
'value' => 0 |
73
|
|
|
], |
74
|
|
|
'sign_in_attempts_block_time' => [ |
75
|
|
|
'min' => 1, |
76
|
|
|
'value' => 5 |
77
|
|
|
], |
78
|
|
|
'password_min_length' => [ |
79
|
|
|
'min' => 1, |
80
|
|
|
'value' => 4 |
81
|
|
|
], |
82
|
|
|
'password_min_strength' => [ |
83
|
|
|
'min' => 0, |
84
|
|
|
'max' => 7, |
85
|
|
|
'value' => 3 |
86
|
|
|
] |
87
|
|
|
], |
88
|
|
|
'set_single' => [ |
89
|
|
|
'smtp_secure' => [ |
90
|
|
|
'value' => '', |
91
|
|
|
'values' => ['', 'ssl', 'tls'] |
92
|
|
|
], |
93
|
|
|
'language' => [ |
94
|
70 |
|
'value' => 'English', |
95
|
70 |
|
'values' => static::get_active_languages(), |
96
|
70 |
|
'source' => 'active_languages' |
97
|
|
|
], |
98
|
|
|
'timezone' => [ |
99
|
70 |
|
'value' => 'UTC', |
100
|
70 |
|
'values' => get_timezones_list() |
101
|
|
|
], |
102
|
|
|
'default_module' => [ |
103
|
|
|
'value' => Config::SYSTEM_MODULE, |
104
|
70 |
|
'values' => static::get_modules_that_can_be_default() |
105
|
|
|
], |
106
|
|
|
'theme' => [ |
107
|
|
|
'value' => Config::SYSTEM_THEME, |
108
|
70 |
|
'values' => static::get_themes() |
109
|
|
|
] |
110
|
|
|
], |
111
|
|
|
'set_multiple' => [ |
112
|
|
|
'active_languages' => [ |
113
|
|
|
'value' => [ |
114
|
|
|
'English' |
115
|
|
|
], |
116
|
70 |
|
'values' => static::get_languages() |
117
|
|
|
] |
118
|
|
|
], |
119
|
|
|
'string' => [ |
120
|
|
|
'admin_email' => '', |
121
|
|
|
'cookie_prefix' => '', |
122
|
|
|
'smtp_host' => '', |
123
|
|
|
'smtp_port' => '', |
124
|
|
|
'smtp_user' => '', |
125
|
|
|
'smtp_password' => [ |
126
|
|
|
'value' => '', |
127
|
|
|
'password' => true |
128
|
|
|
], |
129
|
|
|
'mail_from' => '' |
130
|
|
|
], |
131
|
|
|
'text' => [ |
132
|
|
|
'title_delimiter' => ' | ', |
133
|
|
|
'site_name' => [ |
134
|
|
|
'multilingual' => true, |
135
|
|
|
'value' => '' |
136
|
|
|
], |
137
|
|
|
'closed_title' => [ |
138
|
|
|
'multilingual' => true, |
139
|
|
|
'value' => 'Site closed' |
140
|
|
|
], |
141
|
|
|
'mail_from_name' => [ |
142
|
|
|
'multilingual' => true, |
143
|
|
|
'value' => 'Administrator' |
144
|
|
|
] |
145
|
|
|
], |
146
|
|
|
'html' => [ |
147
|
|
|
'closed_text' => [ |
148
|
|
|
'multilingual' => true, |
149
|
|
|
'value' => '<p>Site closed for maintenance</p>' |
150
|
|
|
], |
151
|
|
|
'mail_signature' => [ |
152
|
|
|
'multilingual' => true, |
153
|
|
|
'value' => '' |
154
|
|
|
] |
155
|
|
|
] |
156
|
|
|
] |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
/** |
160
|
|
|
* @return string[] |
161
|
|
|
*/ |
162
|
70 |
|
protected static function get_languages () { |
163
|
70 |
|
$languages = defined('LANGUAGES') |
164
|
70 |
|
? array_unique( |
165
|
|
|
array_merge( |
166
|
70 |
|
_mb_substr(get_files_list(LANGUAGES, '/^.*?\.php$/i', 'f'), 0, -4) ?: [], |
167
|
70 |
|
_mb_substr(get_files_list(LANGUAGES, '/^.*?\.json$/i', 'f'), 0, -5) ?: [] |
168
|
|
|
) |
169
|
|
|
) |
170
|
70 |
|
: ['English']; |
171
|
70 |
|
asort($languages); |
172
|
70 |
|
return $languages; |
173
|
|
|
} |
174
|
|
|
/** |
175
|
|
|
* @return string[] |
176
|
|
|
*/ |
177
|
70 |
|
protected static function get_themes () { |
178
|
70 |
|
$themes = defined('THEMES') ? get_files_list(THEMES, false, 'd') : [Config::SYSTEM_THEME]; |
179
|
70 |
|
asort($themes); |
180
|
70 |
|
return $themes; |
181
|
|
|
} |
182
|
|
|
/** |
183
|
|
|
* @return string[] |
184
|
|
|
*/ |
185
|
70 |
|
protected static function get_modules_that_can_be_default () { |
186
|
70 |
|
$Config = Config::instance(true); |
187
|
70 |
|
if (!defined('MODULES') || !isset($Config->components['modules'])) { |
188
|
70 |
|
return [Config::SYSTEM_MODULE]; |
189
|
|
|
} |
190
|
|
|
/** @noinspection PhpParamsInspection */ |
191
|
8 |
|
return array_filter( |
192
|
8 |
|
array_keys($Config->components['modules']), |
193
|
|
|
function ($module) use ($Config) { |
194
|
8 |
|
return $Config->module($module) && file_exists_with_extension(MODULES."/$module/index", ['php', 'html', 'json']); |
195
|
8 |
|
} |
196
|
|
|
); |
197
|
|
|
} |
198
|
|
|
/** |
199
|
|
|
* @return string[] |
200
|
|
|
*/ |
201
|
70 |
|
protected static function get_active_languages () { |
202
|
70 |
|
$Config = Config::instance(true); |
203
|
70 |
|
if (!isset($Config->core['active_languages'])) { |
204
|
70 |
|
return ['English']; |
205
|
|
|
} |
206
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
207
|
8 |
|
return $Config->core['active_languages']; |
208
|
|
|
} |
209
|
|
|
/** |
210
|
|
|
* @param array[] $format |
211
|
|
|
* |
212
|
|
|
* @return array[] |
213
|
|
|
*/ |
214
|
70 |
|
protected static function get_formatting_normalize ($format) { |
215
|
70 |
|
foreach ($format as $type => &$items) { |
216
|
70 |
|
foreach ($items as $item => &$data) { |
217
|
70 |
|
if (!is_array($data) || !isset($data['value'])) { |
218
|
|
|
$data = [ |
219
|
70 |
|
'value' => $data |
220
|
|
|
]; |
221
|
|
|
} |
222
|
70 |
|
$data['type'] = $type; |
223
|
70 |
|
if (($type == 'set_single' || $type == 'set_multiple') && !is_array_assoc($data['values'])) { |
224
|
70 |
|
$data['values'] = array_combine($data['values'], $data['values']); |
225
|
|
|
} |
226
|
|
|
} |
227
|
70 |
|
unset($data); |
228
|
|
|
} |
229
|
70 |
|
return array_merge(...array_values($format)); |
230
|
|
|
} |
231
|
|
|
/** |
232
|
|
|
* Get default values for all supported options |
233
|
|
|
* |
234
|
|
|
* @return array |
235
|
|
|
*/ |
236
|
70 |
|
public static function get_defaults () { |
237
|
70 |
|
return array_map( |
238
|
|
|
function ($option) { |
239
|
70 |
|
return $option['value']; |
240
|
70 |
|
}, |
241
|
70 |
|
static::get_formatting() |
242
|
|
|
); |
243
|
|
|
} |
244
|
|
|
/** |
245
|
|
|
* Get list of multilingual options |
246
|
|
|
* |
247
|
|
|
* @return string[] |
248
|
|
|
*/ |
249
|
8 |
|
public static function get_multilingual () { |
250
|
8 |
|
return array_values( |
251
|
|
|
array_keys( |
252
|
|
|
array_filter( |
253
|
8 |
|
static::get_formatting(), |
254
|
|
|
function ($option) { |
255
|
8 |
|
return @$option['multilingual']; |
256
|
8 |
|
} |
257
|
|
|
) |
258
|
|
|
) |
259
|
|
|
); |
260
|
|
|
} |
261
|
|
|
/** |
262
|
|
|
* Take options and check each value according to needed format, correct value or use default if needed |
263
|
|
|
* |
264
|
|
|
* @param array $target_options |
265
|
|
|
* |
266
|
|
|
* @return array |
267
|
|
|
*/ |
268
|
2 |
|
public static function apply_formatting ($target_options) { |
269
|
2 |
|
$options = static::get_formatting(); |
270
|
2 |
|
foreach ($target_options as $option => &$value) { |
271
|
2 |
|
if (!isset($options[$option])) { |
272
|
2 |
|
unset($target_options[$option]); |
273
|
|
|
} else { |
274
|
2 |
|
$format = $options[$option]; |
275
|
2 |
|
switch ($format['type']) { |
276
|
2 |
|
case 'array': |
277
|
2 |
|
$value = xap((array)$value); |
278
|
2 |
|
break; |
279
|
2 |
|
case 'int_bool': |
280
|
2 |
|
$value = (int)(bool)$value; |
281
|
2 |
|
break; |
282
|
2 |
|
case 'int_range': |
283
|
2 |
|
if (isset($format['min'])) { |
284
|
2 |
|
$value = max($format['min'], (int)$value); |
285
|
|
|
} |
286
|
2 |
|
if (isset($format['max'])) { |
287
|
2 |
|
$value = min($format['max'], (int)$value); |
288
|
|
|
} |
289
|
2 |
|
break; |
290
|
2 |
|
case 'set_single': |
291
|
2 |
|
$value = (string)$value; |
292
|
2 |
|
if (!in_array($value, $format['values'], true)) { |
293
|
2 |
|
$value = $format['value']; |
294
|
|
|
} |
295
|
2 |
|
break; |
296
|
2 |
|
case 'set_multiple': |
297
|
2 |
|
$value = array_filter( |
298
|
2 |
|
(array)$value, |
299
|
2 |
|
function ($value) use ($format) { |
300
|
2 |
|
return in_array((string)$value, $format['values'], true); |
301
|
2 |
|
} |
302
|
|
|
); |
303
|
2 |
|
$value = $value ?: [$format['value']]; |
304
|
2 |
|
break; |
305
|
2 |
|
case 'text': |
306
|
2 |
|
$value = xap($value); |
307
|
2 |
|
break; |
308
|
2 |
|
case 'html': |
309
|
2 |
|
$value = xap($value, true); |
310
|
2 |
|
break; |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
} |
314
|
2 |
|
return $target_options; |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|