|
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
|
|
|
]; |
|
60
|
|
|
protected $debug = false; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Method that load the configuration data into the system |
|
64
|
|
|
* @return Config |
|
65
|
|
|
*/ |
|
66
|
1 |
|
protected function init() |
|
67
|
|
|
{ |
|
68
|
1 |
|
if (file_exists(CONFIG_DIR . DIRECTORY_SEPARATOR . self::CONFIG_FILE)) { |
|
69
|
|
|
$this->loadConfigData(); |
|
70
|
|
|
} |
|
71
|
1 |
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function isLoaded() { |
|
78
|
1 |
|
return !empty($this->config); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Method that saves the configuration |
|
83
|
|
|
* @param array $data |
|
84
|
|
|
* @param array $extra |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
2 |
|
protected static function saveConfigParams(array $data, array $extra) |
|
88
|
|
|
{ |
|
89
|
2 |
|
Logger::log('Saving required config parameters'); |
|
90
|
|
|
//En caso de tener parámetros nuevos los guardamos |
|
91
|
2 |
|
if (array_key_exists('label', $extra) && is_array($extra['label'])) { |
|
92
|
1 |
|
foreach ($extra['label'] as $index => $field) { |
|
93
|
1 |
|
if (array_key_exists($index, $extra['value']) && !empty($extra['value'][$index])) { |
|
94
|
|
|
/** @var $data array */ |
|
95
|
1 |
|
$data[$field] = $extra['value'][$index]; |
|
96
|
1 |
|
} |
|
97
|
1 |
|
} |
|
98
|
1 |
|
} |
|
99
|
2 |
|
return $data; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Method that saves the extra parameters into the configuration |
|
104
|
|
|
* @param array $data |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
2 |
|
protected static function saveExtraParams(array $data) |
|
108
|
|
|
{ |
|
109
|
2 |
|
$final_data = array(); |
|
110
|
2 |
|
if (count($data) > 0) { |
|
111
|
2 |
|
Logger::log('Saving extra configuration parameters'); |
|
112
|
2 |
|
foreach ($data as $key => $value) { |
|
113
|
2 |
|
if (null !== $value || $value !== '') { |
|
114
|
2 |
|
$final_data[$key] = $value; |
|
115
|
2 |
|
} |
|
116
|
2 |
|
} |
|
117
|
2 |
|
} |
|
118
|
2 |
|
return $final_data; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Method that returns if the system is in debug mode |
|
123
|
|
|
* @return boolean |
|
124
|
|
|
*/ |
|
125
|
8 |
|
public function getDebugMode() |
|
126
|
|
|
{ |
|
127
|
8 |
|
return $this->debug; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param bool $debug |
|
132
|
|
|
*/ |
|
133
|
1 |
|
public function setDebugMode($debug = true) { |
|
134
|
1 |
|
$this->debug = $debug; |
|
135
|
1 |
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Method that checks if the platform is proper configured |
|
139
|
|
|
* @return boolean |
|
140
|
|
|
*/ |
|
141
|
2 |
|
public function isConfigured() |
|
142
|
|
|
{ |
|
143
|
2 |
|
Logger::log('Checking configuration'); |
|
144
|
2 |
|
$configured = (count($this->config) > 0); |
|
145
|
2 |
|
if ($configured) { |
|
146
|
1 |
|
foreach (static::$required as $required) { |
|
147
|
1 |
|
if (!array_key_exists($required, $this->config)) { |
|
148
|
1 |
|
$configured = false; |
|
149
|
1 |
|
break; |
|
150
|
|
|
} |
|
151
|
1 |
|
} |
|
152
|
1 |
|
} |
|
153
|
2 |
|
return ($configured || $this->checkTryToSaveConfig()); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Method that check if the user is trying to save the config |
|
158
|
|
|
* @return bool |
|
159
|
|
|
*/ |
|
160
|
3 |
|
public function checkTryToSaveConfig() |
|
161
|
|
|
{ |
|
162
|
3 |
|
$uri = Request::getInstance()->getRequestUri(); |
|
163
|
3 |
|
$method = Request::getInstance()->getMethod(); |
|
164
|
3 |
|
return (preg_match('/^\/admin\/(config|setup)$/', $uri) !== false && strtoupper($method) === 'POST'); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Method that saves all the configuration in the system |
|
169
|
|
|
* |
|
170
|
|
|
* @param array $data |
|
171
|
|
|
* @param array|null $extra |
|
172
|
|
|
* @return boolean |
|
173
|
|
|
*/ |
|
174
|
2 |
|
public static function save(array $data, array $extra = null) |
|
175
|
|
|
{ |
|
176
|
2 |
|
$data = self::saveConfigParams($data, $extra); |
|
177
|
2 |
|
$final_data = self::saveExtraParams($data); |
|
178
|
2 |
|
$saved = false; |
|
179
|
|
|
try { |
|
180
|
2 |
|
$final_data = array_filter($final_data, function($key, $value) { |
|
181
|
2 |
|
return in_array($key, Config::$required) || !empty($value); |
|
182
|
2 |
|
}, ARRAY_FILTER_USE_BOTH); |
|
183
|
2 |
|
$saved = (false !== file_put_contents(CONFIG_DIR . DIRECTORY_SEPARATOR . self::CONFIG_FILE, json_encode($final_data, JSON_PRETTY_PRINT))); |
|
|
|
|
|
|
184
|
2 |
|
Config::getInstance()->loadConfigData(); |
|
185
|
2 |
|
$saved = true; |
|
186
|
2 |
|
} catch (ConfigException $e) { |
|
187
|
|
|
Logger::log($e->getMessage(), LOG_ERR); |
|
188
|
|
|
} |
|
189
|
2 |
|
return $saved; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Method that returns a config value |
|
194
|
|
|
* @param string $param |
|
195
|
|
|
* @param mixed $defaultValue |
|
196
|
|
|
* |
|
197
|
|
|
* @return mixed|null |
|
198
|
|
|
*/ |
|
199
|
7 |
|
public function get($param, $defaultValue = null) |
|
200
|
|
|
{ |
|
201
|
7 |
|
return array_key_exists($param, $this->config) ? $this->config[$param] : $defaultValue; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Method that returns all the configuration |
|
206
|
|
|
* @return array |
|
207
|
|
|
*/ |
|
208
|
2 |
|
public function dumpConfig() |
|
209
|
|
|
{ |
|
210
|
2 |
|
return $this->config ?: []; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Method that reloads config file |
|
215
|
|
|
*/ |
|
216
|
2 |
|
public function loadConfigData() |
|
217
|
|
|
{ |
|
218
|
2 |
|
$this->config = json_decode(file_get_contents(CONFIG_DIR . DIRECTORY_SEPARATOR . self::CONFIG_FILE), true) ?: []; |
|
|
|
|
|
|
219
|
2 |
|
$this->debug = (array_key_exists('debug', $this->config)) ? (bool)$this->config['debug'] : FALSE; |
|
220
|
2 |
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Clear configuration set |
|
224
|
|
|
*/ |
|
225
|
1 |
|
public function clearConfig() |
|
226
|
|
|
{ |
|
227
|
1 |
|
$this->config = []; |
|
228
|
1 |
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Static wrapper for extracting params |
|
232
|
|
|
* @param string $key |
|
233
|
|
|
* @param mixed|null $defaultValue |
|
234
|
|
|
* @return mixed|null |
|
235
|
|
|
*/ |
|
236
|
5 |
|
public static function getParam($key, $defaultValue = null) |
|
237
|
|
|
{ |
|
238
|
5 |
|
$param = Config::getInstance()->get($key); |
|
239
|
5 |
|
return (null !== $param) ? $param : $defaultValue; |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
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.