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