|
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\types\SingletonTrait; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class Config |
|
13
|
|
|
* @package PSFS\base\config |
|
14
|
|
|
*/ |
|
15
|
|
|
class Config { |
|
16
|
|
|
|
|
17
|
|
|
use SingletonTrait; |
|
18
|
|
|
const DEFAULT_LANGUAGE = "es"; |
|
19
|
|
|
const DEFAULT_ENCODE = "UTF-8"; |
|
20
|
|
|
const DEFAULT_CTYPE = "text/html"; |
|
21
|
|
|
const DEFAULT_DATETIMEZONE = "Europe/Madrid"; |
|
22
|
|
|
|
|
23
|
|
|
protected $config = array(); |
|
24
|
|
|
static public $defaults = array( |
|
25
|
|
|
"db_host" => "localhost", |
|
26
|
|
|
"db_port" => "3306", |
|
27
|
|
|
"default_language" => "es_ES", |
|
28
|
|
|
); |
|
29
|
|
|
static public $required = array('db_host', 'db_port', 'db_name', 'db_user', 'db_password', 'home_action', 'default_language'); |
|
|
|
|
|
|
30
|
|
|
static public $encrypted = array('db_password'); |
|
31
|
|
|
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'); |
|
|
|
|
|
|
32
|
|
|
protected $debug = false; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Config Constructor |
|
36
|
|
|
*/ |
|
37
|
8 |
|
public function __construct() { |
|
38
|
8 |
|
$this->init(); |
|
39
|
8 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Método que carga la configuración del sistema |
|
43
|
|
|
* @return Config |
|
44
|
|
|
*/ |
|
45
|
8 |
|
protected function init() { |
|
46
|
8 |
|
if (file_exists(CONFIG_DIR.DIRECTORY_SEPARATOR."config.json")) |
|
47
|
8 |
|
{ |
|
48
|
|
|
$this->loadConfigData(); |
|
49
|
|
|
} else { |
|
50
|
|
|
$this->debug = true; |
|
51
|
8 |
|
} |
|
52
|
|
|
return $this; |
|
53
|
8 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Método que guarda los datos de la configuración |
|
57
|
|
|
* @param array $data |
|
58
|
|
|
* @param array $extra |
|
59
|
|
|
* @return array |
|
60
|
|
|
*/ |
|
61
|
|
|
protected static function saveConfigParams(array $data, array $extra) { |
|
62
|
|
|
//En caso de tener parámetros nuevos los guardamos |
|
63
|
|
|
if (!empty($extra['label'])) { |
|
64
|
|
|
foreach ($extra['label'] as $index => $field) { |
|
65
|
|
|
if (array_key_exists($index, $extra['value']) && !empty($extra['value'][$index])) { |
|
66
|
|
|
/** @var $data array */ |
|
67
|
|
|
$data[$field] = $extra['value'][$index]; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
return $data; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Método que guarda los parámetros adicionales de la configuración |
|
76
|
|
|
* @param array $data |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
|
|
protected static function saveExtraParams(array $data) { |
|
80
|
|
|
$final_data = array(); |
|
81
|
|
|
if (count($data) > 0) { |
|
82
|
|
|
foreach ($data as $key => $value) { |
|
83
|
|
|
if (null !== $value || $value !== '') { |
|
84
|
|
|
$final_data[$key] = $value; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
return $final_data; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Método que devuelve si la plataforma está en modo debug |
|
93
|
|
|
* @return boolean |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getDebugMode() { return $this->debug; } |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Método que devuelve el path de cache |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getCachePath() { return CACHE_DIR; } |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Método que devuelve el path general de templates de PSFS |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getTemplatePath() { |
|
108
|
|
|
$path = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'templates'.DIRECTORY_SEPARATOR; |
|
|
|
|
|
|
109
|
|
|
return realpath($path); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Método que indica si se ha configurado correctamente la plataforma |
|
114
|
|
|
* @return boolean |
|
115
|
|
|
*/ |
|
116
|
|
|
public function isConfigured() |
|
117
|
1 |
|
{ |
|
118
|
|
|
$configured = true; |
|
119
|
1 |
|
foreach (static::$required as $required) { |
|
120
|
1 |
|
if (!array_key_exists($required, $this->config)) { |
|
121
|
1 |
|
$configured = false; |
|
122
|
1 |
|
break; |
|
123
|
1 |
|
} |
|
124
|
|
|
} |
|
125
|
1 |
|
return $configured; |
|
126
|
1 |
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Método que guarda la configuración del framework |
|
130
|
|
|
* |
|
131
|
|
|
* @param array $data |
|
132
|
|
|
* @param array|null $extra |
|
133
|
|
|
* @return boolean |
|
134
|
|
|
*/ |
|
135
|
|
|
public static function save(array $data, array $extra = null) { |
|
136
|
|
|
$data = self::saveConfigParams($data, $extra); |
|
137
|
|
|
$final_data = self::saveExtraParams($data); |
|
138
|
|
|
$saved = false; |
|
139
|
|
|
try { |
|
140
|
|
|
Cache::getInstance()->storeData(CONFIG_DIR.DIRECTORY_SEPARATOR."config.json", $final_data, Cache::JSON, true); |
|
|
|
|
|
|
141
|
|
|
$saved = true; |
|
142
|
|
|
}catch (ConfigException $e) { |
|
143
|
|
|
Logger::getInstance()->errorLog($e->getMessage()); |
|
144
|
|
|
} |
|
145
|
|
|
return $saved; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Método que devuelve un parámetro de configuración |
|
150
|
|
|
* @param string $param |
|
151
|
|
|
* |
|
152
|
|
|
* @return mixed|null |
|
153
|
|
|
*/ |
|
154
|
|
|
public function get($param) { |
|
155
|
3 |
|
return array_key_exists($param, $this->config) ? $this->config[$param] : null; |
|
156
|
3 |
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Método que devuelve toda la configuración en un array |
|
160
|
|
|
* @return array|null |
|
161
|
|
|
*/ |
|
162
|
|
|
public function dumpConfig() { |
|
163
|
|
|
return $this->config; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Servicio que devuelve los parámetros de configuración de Propel para las BD |
|
168
|
|
|
* @return array|null |
|
|
|
|
|
|
169
|
|
|
*/ |
|
170
|
|
|
public function getPropelParams() { |
|
171
|
|
|
return Cache::getInstance()->getDataFromFile(__DIR__.DIRECTORY_SEPARATOR.'properties.json', Cache::JSON, true); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Método estático para la generación de directorios |
|
176
|
|
|
* @param string $dir |
|
177
|
|
|
* throws ConfigException |
|
178
|
|
|
*/ |
|
179
|
|
|
public static function createDir($dir) { |
|
180
|
2 |
|
try { |
|
181
|
|
|
if (@mkdir($dir, 0775, true) === false) { |
|
182
|
2 |
|
throw new \Exception(_('Can\'t create directory ').$dir); |
|
183
|
1 |
|
} |
|
184
|
|
|
}catch (\Exception $e) { |
|
185
|
2 |
|
if (!file_exists(dirname($dir))) { |
|
186
|
2 |
|
throw new ConfigException($e->getMessage().$dir); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
2 |
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Método estático que elimina los directorio del document root |
|
193
|
|
|
*/ |
|
194
|
|
|
public static function clearDocumentRoot() { |
|
195
|
|
|
$rootDirs = array("css", "js", "media", "font"); |
|
196
|
|
|
foreach ($rootDirs as $dir) { |
|
197
|
|
|
if (file_exists(WEB_DIR.DIRECTORY_SEPARATOR.$dir)) { |
|
198
|
|
|
try { |
|
199
|
|
|
@shell_exec("rm -rf ".WEB_DIR.DIRECTORY_SEPARATOR.$dir); |
|
200
|
|
|
} catch(\Exception $e) { |
|
201
|
|
|
Logger::getInstance()->errorLog($e->getMessage()); |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Method that reloads config file |
|
209
|
|
|
*/ |
|
210
|
|
|
public function loadConfigData() |
|
211
|
|
|
{ |
|
212
|
|
|
$this->config = Cache::getInstance()->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "config.json", Cache::JSON, TRUE) ?: array(); |
|
|
|
|
|
|
213
|
|
|
$this->debug = (array_key_exists('debug', $this->config)) ? (bool)$this->config['debug'] : FALSE; |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
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.