|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PSFS\base\config; |
|
4
|
|
|
|
|
5
|
|
|
use PSFS\base\exception\ConfigException; |
|
6
|
|
|
use PSFS\base\Logger; |
|
7
|
|
|
use PSFS\base\Request; |
|
8
|
|
|
use PSFS\base\Security; |
|
9
|
|
|
use PSFS\base\types\helpers\Inspector; |
|
10
|
|
|
use PSFS\base\types\traits\SingletonTrait; |
|
11
|
|
|
use PSFS\base\types\traits\TestTrait; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Config |
|
15
|
|
|
* @package PSFS\base\config |
|
16
|
|
|
*/ |
|
17
|
|
|
class Config |
|
18
|
|
|
{ |
|
19
|
|
|
use SingletonTrait; |
|
20
|
|
|
use TestTrait; |
|
21
|
|
|
|
|
22
|
|
|
const DEFAULT_LANGUAGE = 'es'; |
|
23
|
|
|
const DEFAULT_ENCODE = 'UTF-8'; |
|
24
|
|
|
const DEFAULT_CTYPE = 'text/html'; |
|
25
|
|
|
const DEFAULT_DATETIMEZONE = 'Europe/Madrid'; |
|
26
|
|
|
|
|
27
|
|
|
const CONFIG_FILE = 'config.json'; |
|
28
|
|
|
|
|
29
|
|
|
protected array $config = []; |
|
30
|
|
|
static public array $defaults = [ |
|
31
|
|
|
'db.host' => 'localhost', |
|
32
|
|
|
'db.port' => '3306', |
|
33
|
|
|
'default.language' => 'es_ES', |
|
34
|
|
|
'debug' => true, |
|
35
|
|
|
'front.version' => 'v1', |
|
36
|
|
|
'version' => 'v1', |
|
37
|
|
|
]; |
|
38
|
|
|
static public array $required = ['db.host', 'db.port', 'db.name', 'db.user', 'db.password', 'home.action', 'default.language', 'debug']; |
|
39
|
|
|
static public array $encrypted = ['db.password']; |
|
40
|
|
|
static public array $optional = [ |
|
41
|
|
|
'platform.name', // Platform name |
|
42
|
|
|
'restricted', // Restrict the web access |
|
43
|
|
|
'admin.login', // Enable web login for admin |
|
44
|
|
|
'logger.phpFire', // Enable phpFire to trace the logs in the browser |
|
45
|
|
|
'logger.memory', // Enable log memory usage un traces |
|
46
|
|
|
'poweredBy', // Show PoweredBy header customized |
|
47
|
|
|
'author', // Author for auto generated files |
|
48
|
|
|
'author.email', // Author email for auto generated files |
|
49
|
|
|
'version', // Platform version(for cache purposes) |
|
50
|
|
|
'front.version', // Static resources version |
|
51
|
|
|
'cors.enabled', // Enable CORS (regex with the domains, * for all) |
|
52
|
|
|
'pagination.limit', // Pagination limit for autogenerated api admin |
|
53
|
|
|
'api.secret', // Secret passphrase to securize the api |
|
54
|
|
|
'api.admin', // Enable the autogenerated api admin(wok) |
|
55
|
|
|
'log.level', // Max log level(default INFO) |
|
56
|
|
|
'admin_action', // Default admin url when access to /admin |
|
57
|
|
|
'cache.var', // Static cache var |
|
58
|
|
|
'twig.autoreload', // Enable or disable auto reload templates for twig |
|
59
|
|
|
'modules.extend', // Variable for extending the current functionality |
|
60
|
|
|
'psfs.auth', // Variable for extending PSFS with the AUTH module |
|
61
|
|
|
'errors.strict', // Variable to trace all strict errors |
|
62
|
|
|
'psfs.memcache', // Add Memcache to prod cache process, ONLY for PROD environments |
|
63
|
|
|
'angular.protection', // Add an angular suggested prefix in order to avoid JSONP injections |
|
64
|
|
|
'cors.headers', // Add extra headers to the CORS check |
|
65
|
|
|
'json.encodeUTF8', // Encode the json response |
|
66
|
|
|
'cache.data.enable', // Enable data caching with PSFS |
|
67
|
|
|
'profiling.enable', // Enable the profiling headers |
|
68
|
|
|
'api.extrafields.compat', // Disbale retro compatibility with extra field mapping in api |
|
69
|
|
|
'output.json.strict_numbers', // Enable strict numbers in json responses |
|
70
|
|
|
'admin.version', // Determines the version for the admin ui |
|
71
|
|
|
'api.block.limit', // Determine the number of rows for bulk insert |
|
72
|
|
|
'api.field.types', // Extract __fields from api with their types |
|
73
|
|
|
'i18n.locales', // Default locales for any project |
|
74
|
|
|
'log.slack.hook', // Hook for slack traces |
|
75
|
|
|
'i18n.autogenerate', // Set PSFS auto generate i18n mode |
|
76
|
|
|
'resources.cdn.url', // CDN URL base path |
|
77
|
|
|
'api.field.case', // Field type for API dtos (phpName|camelName|camelName|fieldName) @see Propel TableMap class |
|
78
|
|
|
'route.404', // Set route for 404 pages |
|
79
|
|
|
'project.timezone', // Set the timezone for the timestamps in PSFS(Europe/madrid by default) |
|
80
|
|
|
'curl.returnTransfer', // Curl option CURLOPT_RETURNTRANSFER |
|
81
|
|
|
'curl.followLocation', // Curl option CURLOPT_FOLLOWLOCATION |
|
82
|
|
|
'curl.sslVerifyHost', // Curl option CURLOPT_SSL_VERIFYHOST |
|
83
|
|
|
'curl.sslVerifyPeer', // Curl option CURLOPT_SSL_VERIFYPEER |
|
84
|
|
|
'assets.obfuscate', // Allow to obfuscate and gzip js and css files |
|
85
|
|
|
'allow.double.slashes', // Allow // in url paths, allowed as default |
|
86
|
|
|
'default.log.path', // Default logger path |
|
87
|
|
|
'force.https', // Force https protocol for static resources |
|
88
|
|
|
'hide.modules', // Hide modules from menu, separated by comma |
|
89
|
|
|
'skip.route_generation', // Skip routes generation |
|
90
|
|
|
'auth.expiration', // Set the expiration time for the auth token |
|
91
|
|
|
]; |
|
92
|
|
|
protected bool $debug = false; |
|
93
|
|
|
public static array $cleanable_config_files = ['domains.json', 'urls.json']; |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Method that load the configuration data into the system |
|
97
|
|
|
* @return Config |
|
98
|
|
|
*/ |
|
99
|
1 |
|
protected function init() |
|
100
|
|
|
{ |
|
101
|
1 |
|
if (file_exists(CONFIG_DIR . DIRECTORY_SEPARATOR . self::CONFIG_FILE)) { |
|
102
|
1 |
|
$this->loadConfigData(); |
|
103
|
|
|
} |
|
104
|
1 |
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
1 |
|
public function isLoaded() |
|
111
|
|
|
{ |
|
112
|
1 |
|
return !empty($this->config); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Method that saves the configuration |
|
117
|
|
|
* @param array $data |
|
118
|
|
|
* @param array $extra |
|
119
|
|
|
* @return array |
|
120
|
|
|
*/ |
|
121
|
8 |
|
protected static function saveConfigParams(array $data, $extra = null) |
|
122
|
|
|
{ |
|
123
|
8 |
|
Logger::log('Saving required config parameters'); |
|
124
|
|
|
//En caso de tener parámetros nuevos los guardamos |
|
125
|
8 |
|
if (!empty($extra) && array_key_exists('label', $extra) && is_array($extra['label'])) { |
|
126
|
3 |
|
foreach ($extra['label'] as $index => $field) { |
|
127
|
3 |
|
if (array_key_exists($index, $extra['value']) && !empty($extra['value'][$index])) { |
|
128
|
|
|
/** @var $data array */ |
|
129
|
3 |
|
$data[$field] = $extra['value'][$index]; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
8 |
|
return $data; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Method that saves the extra parameters into the configuration |
|
138
|
|
|
* @param array $data |
|
139
|
|
|
* @return array |
|
140
|
|
|
*/ |
|
141
|
8 |
|
protected static function saveExtraParams(array $data) |
|
142
|
|
|
{ |
|
143
|
8 |
|
$finalData = array(); |
|
144
|
8 |
|
if (count($data) > 0) { |
|
145
|
8 |
|
Logger::log('Saving extra configuration parameters'); |
|
146
|
8 |
|
foreach ($data as $key => $value) { |
|
147
|
8 |
|
if (null !== $value || $value !== '') { |
|
148
|
8 |
|
$finalData[$key] = $value; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
8 |
|
return $finalData; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Method that returns if the system is in debug mode |
|
157
|
|
|
* @return boolean |
|
158
|
|
|
*/ |
|
159
|
3 |
|
public function getDebugMode() |
|
160
|
|
|
{ |
|
161
|
3 |
|
return $this->debug; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param bool $debug |
|
166
|
|
|
*/ |
|
167
|
2 |
|
public function setDebugMode($debug = true) |
|
168
|
|
|
{ |
|
169
|
2 |
|
$this->debug = $debug; |
|
170
|
2 |
|
$this->config['debug'] = $this->debug; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Method that checks if the platform is proper configured |
|
175
|
|
|
* @return boolean |
|
176
|
|
|
*/ |
|
177
|
2 |
|
public function isConfigured() |
|
178
|
|
|
{ |
|
179
|
2 |
|
Inspector::stats('[Config] Checking configuration', Inspector::SCOPE_DEBUG); |
|
180
|
2 |
|
$configured = (count($this->config) > 0); |
|
181
|
2 |
|
if ($configured) { |
|
182
|
1 |
|
foreach (static::$required as $required) { |
|
183
|
1 |
|
if (!array_key_exists($required, $this->config)) { |
|
184
|
1 |
|
$configured = false; |
|
185
|
1 |
|
break; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
2 |
|
return $configured || $this->checkTryToSaveConfig() || self::isTest(); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Method that check if the user is trying to save the config |
|
194
|
|
|
* @return bool |
|
195
|
|
|
*/ |
|
196
|
5 |
|
public function checkTryToSaveConfig() |
|
197
|
|
|
{ |
|
198
|
5 |
|
$uri = Request::getInstance()->getRequestUri(); |
|
199
|
5 |
|
$method = Request::getInstance()->getMethod(); |
|
200
|
5 |
|
return (preg_match('/^\/admin\/(config|setup)$/', $uri) !== false && strtoupper($method) === 'POST'); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Method that saves all the configuration in the system |
|
205
|
|
|
* |
|
206
|
|
|
* @param array $data |
|
207
|
|
|
* @param array|null $extra |
|
208
|
|
|
* @return boolean |
|
209
|
|
|
*/ |
|
210
|
8 |
|
public static function save(array $data, $extra = null) |
|
211
|
|
|
{ |
|
212
|
8 |
|
$data = self::saveConfigParams($data, $extra); |
|
213
|
8 |
|
$finalData = self::saveExtraParams($data); |
|
214
|
8 |
|
$saved = false; |
|
215
|
|
|
try { |
|
216
|
8 |
|
$finalData = array_filter($finalData, function ($key, $value) { |
|
217
|
8 |
|
return in_array($key, self::$required, true) || !empty($value); |
|
218
|
8 |
|
}, ARRAY_FILTER_USE_BOTH); |
|
219
|
8 |
|
$saved = (false !== file_put_contents(CONFIG_DIR . DIRECTORY_SEPARATOR . self::CONFIG_FILE, json_encode($finalData, JSON_PRETTY_PRINT))); |
|
220
|
8 |
|
self::getInstance()->loadConfigData(); |
|
221
|
8 |
|
$saved = true; |
|
222
|
|
|
} catch (ConfigException $e) { |
|
223
|
|
|
Logger::log($e->getMessage(), LOG_ERR); |
|
224
|
|
|
} |
|
225
|
8 |
|
return $saved; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Method that returns a config value |
|
230
|
|
|
* @param string $param |
|
231
|
|
|
* @param mixed|null $defaultValue |
|
232
|
|
|
* |
|
233
|
|
|
* @return mixed|null |
|
234
|
|
|
*/ |
|
235
|
44 |
|
public function get($param, $defaultValue = null) |
|
236
|
|
|
{ |
|
237
|
44 |
|
return array_key_exists($param, $this->config) ? $this->config[$param] : $defaultValue; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Method that returns all the configuration |
|
242
|
|
|
* @return array |
|
243
|
|
|
*/ |
|
244
|
8 |
|
public function dumpConfig(): array |
|
245
|
|
|
{ |
|
246
|
8 |
|
return $this->config ?: []; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Method that reloads config file |
|
251
|
|
|
*/ |
|
252
|
8 |
|
public function loadConfigData(): void |
|
253
|
|
|
{ |
|
254
|
8 |
|
$this->config = json_decode(file_get_contents(CONFIG_DIR . DIRECTORY_SEPARATOR . self::CONFIG_FILE), true) ?: []; |
|
255
|
8 |
|
$this->debug = array_key_exists('debug', $this->config) ? (bool)$this->config['debug'] : FALSE; |
|
256
|
8 |
|
if (array_key_exists('cache.var', $this->config)) { |
|
257
|
1 |
|
Security::getInstance()->setSessionKey('config.cache.var', $this->config['cache.var']); |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Clear configuration set |
|
263
|
|
|
*/ |
|
264
|
1 |
|
public function clearConfig() |
|
265
|
|
|
{ |
|
266
|
1 |
|
$this->config = []; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Static wrapper for extracting params |
|
271
|
|
|
* @param string $key |
|
272
|
|
|
* @param mixed|null $defaultValue |
|
273
|
|
|
* @param string|null $module |
|
274
|
|
|
* @return mixed|null |
|
275
|
|
|
*/ |
|
276
|
42 |
|
public static function getParam($key, $defaultValue = null, $module = null) |
|
277
|
|
|
{ |
|
278
|
42 |
|
if (null !== $module) { |
|
279
|
2 |
|
return self::getParam(strtolower($module) . '.' . $key, self::getParam($key, $defaultValue)); |
|
280
|
|
|
} |
|
281
|
42 |
|
$param = self::getInstance()->get($key); |
|
282
|
42 |
|
return (null !== $param) ? $param : $defaultValue; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
1 |
|
public static function initialize(): void |
|
286
|
|
|
{ |
|
287
|
1 |
|
Config::getInstance(); |
|
288
|
1 |
|
Logger::getInstance(); |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
1 |
|
public static function clearConfigFiles(): bool |
|
292
|
|
|
{ |
|
293
|
1 |
|
$done = true; |
|
294
|
1 |
|
foreach (self::$cleanable_config_files as $configFile) { |
|
295
|
1 |
|
if (file_exists(CONFIG_DIR . DIRECTORY_SEPARATOR . $configFile)) { |
|
296
|
1 |
|
if (!unlink(CONFIG_DIR . DIRECTORY_SEPARATOR . $configFile)) { |
|
297
|
|
|
$done = false; |
|
298
|
|
|
} |
|
299
|
|
|
} |
|
300
|
|
|
} |
|
301
|
1 |
|
return $done; |
|
302
|
|
|
} |
|
303
|
|
|
} |
|
304
|
|
|
|