1
|
|
|
<?php |
2
|
|
|
namespace PSFS\base\config; |
3
|
|
|
|
4
|
|
|
use PSFS\base\exception\ConfigException; |
5
|
|
|
use PSFS\base\Logger; |
6
|
|
|
use PSFS\base\Request; |
7
|
|
|
use PSFS\base\types\traits\SingletonTrait; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Config |
11
|
|
|
* @package PSFS\base\config |
12
|
|
|
*/ |
13
|
|
|
class Config |
14
|
|
|
{ |
15
|
|
|
use SingletonTrait; |
16
|
|
|
|
17
|
|
|
const DEFAULT_LANGUAGE = 'es'; |
18
|
|
|
const DEFAULT_ENCODE = 'UTF-8'; |
19
|
|
|
const DEFAULT_CTYPE = 'text/html'; |
20
|
|
|
const DEFAULT_DATETIMEZONE = 'Europe/Madrid'; |
21
|
|
|
|
22
|
|
|
const CONFIG_FILE = 'config.json'; |
23
|
|
|
|
24
|
|
|
protected $config = array(); |
25
|
|
|
static public $defaults = array( |
26
|
|
|
'db.host' => 'localhost', |
27
|
|
|
'db.port' => '3306', |
28
|
|
|
'default.language' => 'es_ES', |
29
|
|
|
'debug' => true, |
30
|
|
|
'front.version' => 'v1', |
31
|
|
|
'version' => 'v1', |
32
|
|
|
); |
33
|
|
|
static public $required = array('db.host', 'db.port', 'db.name', 'db.user', 'db.password', 'home.action', 'default.language', 'debug'); |
|
|
|
|
34
|
|
|
static public $encrypted = array('db.password'); |
35
|
|
|
static public $optional = [ |
36
|
|
|
'platform.name', // Platform name |
37
|
|
|
'restricted', // Restrict the web access |
38
|
|
|
'admin.login', // Enable web login for admin |
39
|
|
|
'logger.phpFire', // Enable phpFire to trace the logs in the browser |
40
|
|
|
'logger.memory', // Enable log memory usage un traces |
41
|
|
|
'poweredBy', // Show PoweredBy header customized |
42
|
|
|
'author', // Author for auto generated files |
43
|
|
|
'author.email', // Author email for auto generated files |
44
|
|
|
'version', // Platform version(for cache purposes) |
45
|
|
|
'front.version', // Static resources version |
46
|
|
|
'cors.enabled', // Enable CORS (regex with the domains, * for all) |
47
|
|
|
'pagination.limit', // Pagination limit for autogenerated api admin |
48
|
|
|
'api.secret', // Secret passphrase to securize the api |
49
|
|
|
'api.admin', // Enable de autogenerated api admin(wok) |
50
|
|
|
'log.level', // Max log level(default INFO) |
51
|
|
|
'admin_action', // Default admin url when access to /admin |
52
|
|
|
'cache.var', // Static cache var |
53
|
|
|
'twig.autoreload', // Enable or disable auto reload templates for twig |
54
|
|
|
'modules.extend', // Variable for extending the current functionality |
55
|
|
|
'psfs.auth', // Variable for extending PSFS with the AUTH module |
56
|
|
|
'errors.strict', // Variable to trace all strict errors |
57
|
|
|
'psfs.memcache', // Add Memcache to prod cache process, ONLY for PROD environments |
58
|
|
|
'angular.protection', // Add an angular suggested prefix in order to avoid JSONP injections |
59
|
|
|
'cors.headers', // Add extra headers to the CORS check |
60
|
|
|
'json.encodeUTF8', // Encode the json response |
61
|
|
|
'cache.data.enable', // Enable data caching with PSFS |
62
|
|
|
'profiling.enable', // Enable the profiling headers |
63
|
|
|
'api.extrafields.compat', // Disbale retro compatibility with extra field mapping in api |
64
|
|
|
'output.json.strict_numbers', // Enable strict numbers in json responses |
65
|
|
|
'admin.version', // Determines the version for the admin ui |
66
|
|
|
'api.block.limit', // Determine the number of rows for bulk insert |
67
|
|
|
'api.field.types', // Extract __fields from api with their types |
68
|
|
|
]; |
69
|
|
|
protected $debug = false; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Method that load the configuration data into the system |
73
|
|
|
* @return Config |
74
|
|
|
*/ |
75
|
1 |
|
protected function init() |
76
|
|
|
{ |
77
|
1 |
|
if (file_exists(CONFIG_DIR . DIRECTORY_SEPARATOR . self::CONFIG_FILE)) { |
78
|
|
|
$this->loadConfigData(); |
79
|
|
|
} |
80
|
1 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
1 |
|
public function isLoaded() { |
87
|
1 |
|
return !empty($this->config); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Method that saves the configuration |
92
|
|
|
* @param array $data |
93
|
|
|
* @param array $extra |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
4 |
|
protected static function saveConfigParams(array $data, array $extra) |
97
|
|
|
{ |
98
|
4 |
|
Logger::log('Saving required config parameters'); |
99
|
|
|
//En caso de tener parámetros nuevos los guardamos |
100
|
4 |
|
if (array_key_exists('label', $extra) && is_array($extra['label'])) { |
101
|
2 |
|
foreach ($extra['label'] as $index => $field) { |
102
|
2 |
|
if (array_key_exists($index, $extra['value']) && !empty($extra['value'][$index])) { |
103
|
|
|
/** @var $data array */ |
104
|
2 |
|
$data[$field] = $extra['value'][$index]; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
4 |
|
return $data; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Method that saves the extra parameters into the configuration |
113
|
|
|
* @param array $data |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
4 |
|
protected static function saveExtraParams(array $data) |
117
|
|
|
{ |
118
|
4 |
|
$final_data = array(); |
119
|
4 |
|
if (count($data) > 0) { |
120
|
4 |
|
Logger::log('Saving extra configuration parameters'); |
121
|
4 |
|
foreach ($data as $key => $value) { |
122
|
4 |
|
if (null !== $value || $value !== '') { |
123
|
4 |
|
$final_data[$key] = $value; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
4 |
|
return $final_data; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Method that returns if the system is in debug mode |
132
|
|
|
* @return boolean |
133
|
|
|
*/ |
134
|
7 |
|
public function getDebugMode() |
135
|
|
|
{ |
136
|
7 |
|
return $this->debug; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param bool $debug |
141
|
|
|
*/ |
142
|
1 |
|
public function setDebugMode($debug = true) { |
143
|
1 |
|
$this->debug = $debug; |
144
|
1 |
|
$this->config['debug'] = $this->debug; |
145
|
1 |
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Method that checks if the platform is proper configured |
149
|
|
|
* @return boolean |
150
|
|
|
*/ |
151
|
2 |
|
public function isConfigured() |
152
|
|
|
{ |
153
|
2 |
|
Logger::log('Checking configuration'); |
154
|
2 |
|
$configured = (count($this->config) > 0); |
155
|
2 |
|
if ($configured) { |
156
|
1 |
|
foreach (static::$required as $required) { |
157
|
1 |
|
if (!array_key_exists($required, $this->config)) { |
158
|
1 |
|
$configured = false; |
159
|
1 |
|
break; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
2 |
|
return ($configured || $this->checkTryToSaveConfig()); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Method that check if the user is trying to save the config |
168
|
|
|
* @return bool |
169
|
|
|
*/ |
170
|
2 |
|
public function checkTryToSaveConfig() |
171
|
|
|
{ |
172
|
2 |
|
$uri = Request::getInstance()->getRequestUri(); |
173
|
2 |
|
$method = Request::getInstance()->getMethod(); |
174
|
2 |
|
return (preg_match('/^\/admin\/(config|setup)$/', $uri) !== false && strtoupper($method) === 'POST'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Method that saves all the configuration in the system |
179
|
|
|
* |
180
|
|
|
* @param array $data |
181
|
|
|
* @param array|null $extra |
182
|
|
|
* @return boolean |
183
|
|
|
*/ |
184
|
4 |
|
public static function save(array $data, array $extra = null) |
185
|
|
|
{ |
186
|
4 |
|
$data = self::saveConfigParams($data, $extra); |
187
|
4 |
|
$final_data = self::saveExtraParams($data); |
188
|
4 |
|
$saved = false; |
189
|
|
|
try { |
190
|
4 |
|
$final_data = array_filter($final_data, function($key, $value) { |
191
|
4 |
|
return in_array($key, self::$required, true) || !empty($value); |
192
|
4 |
|
}, ARRAY_FILTER_USE_BOTH); |
193
|
4 |
|
$saved = (false !== file_put_contents(CONFIG_DIR . DIRECTORY_SEPARATOR . self::CONFIG_FILE, json_encode($final_data, JSON_PRETTY_PRINT))); |
|
|
|
|
194
|
4 |
|
self::getInstance()->loadConfigData(); |
195
|
4 |
|
$saved = true; |
196
|
|
|
} catch (ConfigException $e) { |
197
|
|
|
Logger::log($e->getMessage(), LOG_ERR); |
198
|
|
|
} |
199
|
4 |
|
return $saved; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Method that returns a config value |
204
|
|
|
* @param string $param |
205
|
|
|
* @param mixed $defaultValue |
206
|
|
|
* |
207
|
|
|
* @return mixed|null |
208
|
|
|
*/ |
209
|
22 |
|
public function get($param, $defaultValue = null) |
210
|
|
|
{ |
211
|
22 |
|
return array_key_exists($param, $this->config) ? $this->config[$param] : $defaultValue; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Method that returns all the configuration |
216
|
|
|
* @return array |
217
|
|
|
*/ |
218
|
4 |
|
public function dumpConfig() |
219
|
|
|
{ |
220
|
4 |
|
return $this->config ?: []; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Method that reloads config file |
225
|
|
|
*/ |
226
|
4 |
|
public function loadConfigData() |
227
|
|
|
{ |
228
|
4 |
|
$this->config = json_decode(file_get_contents(CONFIG_DIR . DIRECTORY_SEPARATOR . self::CONFIG_FILE), true) ?: []; |
|
|
|
|
229
|
4 |
|
$this->debug = array_key_exists('debug', $this->config) ? (bool)$this->config['debug'] : FALSE; |
230
|
4 |
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Clear configuration set |
234
|
|
|
*/ |
235
|
1 |
|
public function clearConfig() |
236
|
|
|
{ |
237
|
1 |
|
$this->config = []; |
238
|
1 |
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Static wrapper for extracting params |
242
|
|
|
* @param string $key |
243
|
|
|
* @param mixed|null $defaultValue |
244
|
|
|
* @param string $module |
|
|
|
|
245
|
|
|
* @return mixed|null |
246
|
|
|
*/ |
247
|
22 |
|
public static function getParam($key, $defaultValue = null, $module = null) |
248
|
|
|
{ |
249
|
22 |
|
if(null !== $module) { |
250
|
|
|
return self::getParam(strtolower($module) . '.' . $key, self::getParam($key, $defaultValue)); |
251
|
|
|
} |
252
|
22 |
|
$param = self::getInstance()->get($key); |
253
|
22 |
|
return (null !== $param) ? $param : $defaultValue; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.