1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PSFS\base\config; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use PSFS\base\Cache; |
7
|
|
|
use PSFS\base\exception\ConfigException; |
8
|
|
|
use PSFS\base\Logger; |
9
|
|
|
use PSFS\base\Request; |
10
|
|
|
use PSFS\base\types\SingletonTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Config |
14
|
|
|
* @package PSFS\base\config |
15
|
|
|
*/ |
16
|
|
|
class Config |
17
|
|
|
{ |
18
|
|
|
use SingletonTrait; |
19
|
|
|
const DEFAULT_LANGUAGE = "es"; |
20
|
|
|
const DEFAULT_ENCODE = "UTF-8"; |
21
|
|
|
const DEFAULT_CTYPE = "text/html"; |
22
|
|
|
const DEFAULT_DATETIMEZONE = "Europe/Madrid"; |
23
|
|
|
|
24
|
|
|
protected $config = array(); |
25
|
|
|
static public $defaults = array( |
26
|
|
|
"db_host" => "localhost", |
27
|
|
|
"db_port" => "3306", |
28
|
|
|
"default_language" => "es_ES", |
29
|
|
|
); |
30
|
|
|
static public $required = array('db_host', 'db_port', 'db_name', 'db_user', 'db_password', 'home_action', 'default_language'); |
|
|
|
|
31
|
|
|
static public $encrypted = array('db_password'); |
32
|
|
|
static public $optional = array('platform_name', 'debug', 'restricted', 'admin_login', 'logger.phpFire', 'logger.memory', 'poweredBy', 'author', 'author_email', 'version', 'front.version', 'cors.enabled', 'pagination.limit', 'api.secret'); |
|
|
|
|
33
|
|
|
protected $debug = false; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Config Constructor |
37
|
|
|
*/ |
38
|
8 |
|
public function __construct() |
39
|
|
|
{ |
40
|
8 |
|
$this->init(); |
41
|
8 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Method that load the configuration data into the system |
45
|
|
|
* @return Config |
46
|
|
|
*/ |
47
|
8 |
|
protected function init() |
48
|
|
|
{ |
49
|
8 |
|
if (file_exists(CONFIG_DIR . DIRECTORY_SEPARATOR . "config.json")) { |
50
|
7 |
|
$this->loadConfigData(); |
51
|
7 |
|
} |
52
|
8 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Method that saves the configuration |
57
|
|
|
* @param array $data |
58
|
|
|
* @param array $extra |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
1 |
|
protected static function saveConfigParams(array $data, array $extra) |
62
|
|
|
{ |
63
|
1 |
|
Logger::log('Saving required config parameters'); |
64
|
|
|
//En caso de tener parámetros nuevos los guardamos |
65
|
1 |
|
if (array_key_exists('label', $extra) && is_array($extra['label'])) { |
66
|
1 |
|
foreach ($extra['label'] as $index => $field) { |
67
|
1 |
|
if (array_key_exists($index, $extra['value']) && !empty($extra['value'][$index])) { |
68
|
|
|
/** @var $data array */ |
69
|
1 |
|
$data[$field] = $extra['value'][$index]; |
70
|
1 |
|
} |
71
|
1 |
|
} |
72
|
1 |
|
} |
73
|
1 |
|
return $data; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Method that saves the extra parameters into the configuration |
78
|
|
|
* @param array $data |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
1 |
|
protected static function saveExtraParams(array $data) |
82
|
|
|
{ |
83
|
1 |
|
$final_data = array(); |
84
|
1 |
|
if (count($data) > 0) { |
85
|
1 |
|
Logger::log('Saving extra configuration parameters'); |
86
|
1 |
|
foreach ($data as $key => $value) { |
87
|
1 |
|
if (null !== $value || $value !== '') { |
88
|
1 |
|
$final_data[$key] = $value; |
89
|
1 |
|
} |
90
|
1 |
|
} |
91
|
1 |
|
} |
92
|
1 |
|
return $final_data; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Method that returns if the system is in debug mode |
97
|
|
|
* @return boolean |
98
|
|
|
*/ |
99
|
3 |
|
public function getDebugMode() |
100
|
|
|
{ |
101
|
3 |
|
return $this->debug; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Method that returns the cache path |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
1 |
|
public function getCachePath() |
109
|
|
|
{ |
110
|
1 |
|
return CACHE_DIR; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Method that returns the templates path |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
1 |
|
public function getTemplatePath() |
118
|
|
|
{ |
119
|
1 |
|
$path = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR; |
|
|
|
|
120
|
1 |
|
return realpath($path); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Method that checks if the platform is proper configured |
125
|
|
|
* @return boolean |
126
|
|
|
*/ |
127
|
2 |
|
public function isConfigured() |
128
|
|
|
{ |
129
|
2 |
|
Logger::log('Checking configuration'); |
130
|
2 |
|
$configured = (count($this->config) > 0); |
131
|
2 |
|
if ($configured) { |
132
|
1 |
|
foreach (static::$required as $required) { |
133
|
1 |
|
if (!array_key_exists($required, $this->config)) { |
134
|
1 |
|
$configured = false; |
135
|
1 |
|
break; |
136
|
|
|
} |
137
|
1 |
|
} |
138
|
1 |
|
} |
139
|
2 |
|
return ($configured || $this->checkTryToSaveConfig()); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Method that check if the user is trying to save the config |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
2 |
|
public function checkTryToSaveConfig() |
147
|
|
|
{ |
148
|
2 |
|
$uri = Request::getInstance()->getRequestUri(); |
149
|
2 |
|
$method = Request::getInstance()->getMethod(); |
150
|
2 |
|
return (preg_match('/^\/admin\/(config|setup)$/', $uri) !== false && strtoupper($method) === 'POST'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Method that saves all the configuration in the system |
155
|
|
|
* |
156
|
|
|
* @param array $data |
157
|
|
|
* @param array|null $extra |
158
|
|
|
* @return boolean |
159
|
|
|
*/ |
160
|
1 |
|
public static function save(array $data, array $extra = null) |
161
|
|
|
{ |
162
|
1 |
|
$data = self::saveConfigParams($data, $extra); |
163
|
1 |
|
$final_data = self::saveExtraParams($data); |
164
|
1 |
|
$saved = false; |
165
|
|
|
try { |
166
|
1 |
|
Cache::getInstance()->storeData(CONFIG_DIR . DIRECTORY_SEPARATOR . "config.json", $final_data, Cache::JSON, true); |
|
|
|
|
167
|
1 |
|
Config::getInstance()->loadConfigData(); |
168
|
1 |
|
$saved = true; |
169
|
1 |
|
} catch (ConfigException $e) { |
170
|
|
|
Logger::log($e->getMessage(), LOG_ERR); |
171
|
|
|
} |
172
|
1 |
|
return $saved; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Method that returns a config value |
177
|
|
|
* @param string $param |
178
|
|
|
* |
179
|
|
|
* @return mixed|null |
180
|
|
|
*/ |
181
|
2 |
|
public function get($param) |
182
|
|
|
{ |
183
|
2 |
|
return array_key_exists($param, $this->config) ? $this->config[$param] : null; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Method that returns all the configuration |
188
|
|
|
* @return array |
189
|
|
|
*/ |
190
|
1 |
|
public function dumpConfig() |
191
|
|
|
{ |
192
|
1 |
|
return $this->config ?: []; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Method that returns the Propel ORM parameters to setup the models |
197
|
|
|
* @return array|null |
|
|
|
|
198
|
|
|
*/ |
199
|
1 |
|
public function getPropelParams() |
200
|
|
|
{ |
201
|
1 |
|
return Cache::getInstance()->getDataFromFile(__DIR__ . DIRECTORY_SEPARATOR . 'properties.json', Cache::JSON, true); |
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Method that creates any parametrized path |
206
|
|
|
* @param string $dir |
207
|
|
|
* throws ConfigException |
208
|
|
|
*/ |
209
|
3 |
|
public static function createDir($dir) |
210
|
|
|
{ |
211
|
|
|
try { |
212
|
3 |
|
if (!is_dir($dir) && @mkdir($dir, 0775, true) === false) { |
213
|
|
|
throw new \Exception(_('Can\'t create directory ') . $dir); |
214
|
|
|
} |
215
|
3 |
|
} catch (\Exception $e) { |
216
|
1 |
|
Logger::log($e->getMessage(), LOG_WARNING); |
217
|
|
|
if (!file_exists(dirname($dir))) { |
218
|
|
|
throw new ConfigException($e->getMessage() . $dir); |
219
|
|
|
} |
220
|
|
|
} |
221
|
3 |
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Method that remove all data in the document root path |
225
|
|
|
*/ |
226
|
1 |
|
public static function clearDocumentRoot() |
227
|
|
|
{ |
228
|
1 |
|
$rootDirs = array("css", "js", "media", "font"); |
229
|
1 |
|
foreach ($rootDirs as $dir) { |
230
|
1 |
|
if (file_exists(WEB_DIR . DIRECTORY_SEPARATOR . $dir)) { |
231
|
|
|
try { |
232
|
1 |
|
@shell_exec("rm -rf " . WEB_DIR . DIRECTORY_SEPARATOR . $dir); |
233
|
1 |
|
} catch (\Exception $e) { |
234
|
|
|
Logger::log($e->getMessage()); |
235
|
|
|
} |
236
|
1 |
|
} |
237
|
1 |
|
} |
238
|
1 |
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Method that reloads config file |
242
|
|
|
*/ |
243
|
1 |
|
public function loadConfigData() |
244
|
|
|
{ |
245
|
1 |
|
$this->config = Cache::getInstance()->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "config.json", Cache::JSON, TRUE) ?: array(); |
|
|
|
|
246
|
1 |
|
$this->debug = (array_key_exists('debug', $this->config)) ? (bool)$this->config['debug'] : FALSE; |
247
|
1 |
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Clear configuration set |
251
|
|
|
*/ |
252
|
1 |
|
public function clearConfig() |
253
|
|
|
{ |
254
|
1 |
|
$this->config = []; |
255
|
1 |
|
} |
256
|
|
|
} |
257
|
|
|
|
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.