1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace PHPPgAdmin; |
5
|
|
|
|
6
|
|
|
use Kint\Kint; |
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Slim\Collection; |
9
|
|
|
use Slim\DefaultServicesProvider; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @property array $deploy_info |
13
|
|
|
* @property \Slim\Flash\Messages $flash |
14
|
|
|
* @property \GuzzleHttp\Client $fcIntranetClient |
15
|
|
|
* @property \GuzzleHttp\Client $apiDteClient |
16
|
|
|
* @property \Slim\Views\Twig $view |
17
|
|
|
* @property \Slim\Http\Request $request |
18
|
|
|
* @property \Slim\Http\Response $response |
19
|
|
|
* @property string $BASE_PATH |
20
|
|
|
* @property string $THEME_PATH |
21
|
|
|
* @property string $subFolder |
22
|
|
|
* @property bool $DEBUGMODE |
23
|
|
|
* @property bool $IN_TEST |
24
|
|
|
* @property string $server |
25
|
|
|
* @property string $database |
26
|
|
|
* @property string $schema |
27
|
|
|
*/ |
28
|
|
|
class ContainerUtils extends \Slim\Container implements ContainerInterface |
29
|
|
|
{ use \PHPPgAdmin\Traits\HelperTrait; |
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var self|null |
33
|
|
|
*/ |
34
|
|
|
private static $instance; |
|
|
|
|
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* $appInstance. |
39
|
|
|
* |
40
|
|
|
* @var null|\Slim\App |
41
|
|
|
*/ |
42
|
|
|
private static $appInstance; |
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Default settings. |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private $defaultSettings = [ |
|
|
|
|
50
|
|
|
'httpVersion' => '1.1', |
51
|
|
|
'responseChunkSize' => 4096, |
52
|
|
|
'outputBuffering' => 'append', |
53
|
|
|
'determineRouteBeforeAppMiddleware' => false, |
54
|
|
|
'displayErrorDetails' => false, |
55
|
|
|
'addContentLengthHeader' => true, |
56
|
|
|
'routerCacheFile' => false, |
57
|
|
|
]; |
58
|
|
|
/** |
59
|
|
|
* Undocumented variable |
|
|
|
|
60
|
|
|
* |
|
|
|
|
61
|
|
|
* @var array |
|
|
|
|
62
|
|
|
*/ |
|
|
|
|
63
|
|
|
private static $envConfig=[ |
|
|
|
|
64
|
|
|
'BASE_PATH'=>'', |
65
|
|
|
'subFolder'=>'', |
66
|
|
|
'DEBUGMODE'=>false, |
67
|
|
|
'THEME_PATH'=>'', |
68
|
|
|
|
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param array $values the parameters or objects |
73
|
|
|
*/ |
74
|
|
|
public function __construct(array $values = []) |
75
|
|
|
{ |
76
|
|
|
parent::__construct($values); |
77
|
|
|
|
78
|
|
|
$userSettings = $values['settings'] ?? []; |
79
|
|
|
$this->registerDefaultServices($userSettings); |
80
|
|
|
$this->container = $this; |
81
|
|
|
self::$instance = $this; |
82
|
|
|
} |
83
|
|
|
/** |
|
|
|
|
84
|
|
|
* Gets the subfolder. |
|
|
|
|
85
|
|
|
* |
|
|
|
|
86
|
|
|
* @param string $path The path |
|
|
|
|
87
|
|
|
* |
|
|
|
|
88
|
|
|
* @return string the subfolder |
|
|
|
|
89
|
|
|
*/ |
|
|
|
|
90
|
|
|
public function getSubfolder(string $path = ''): string |
91
|
|
|
{ |
92
|
|
|
|
93
|
|
|
return \implode(\DIRECTORY_SEPARATOR, [$this->subFolder, $path]); |
94
|
|
|
} |
95
|
|
|
public static function getAppInstance(array $config = []): \Slim\App |
96
|
|
|
{ |
97
|
|
|
|
98
|
|
|
$config = \array_merge(self::getDefaultConfig(), $config); |
99
|
|
|
$container = self::getContainerInstance($config); |
100
|
|
|
|
101
|
|
|
if (!self::$appInstance) { |
102
|
|
|
self::$appInstance = new \Slim\App($container); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return self::$appInstance; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public static function getContainerInstance(array $config = []): self |
109
|
|
|
{ |
110
|
|
|
self::$envConfig = [ |
111
|
|
|
'msg' => '', |
112
|
|
|
'appThemes' => [ |
113
|
|
|
'default' => 'Default', |
114
|
|
|
'cappuccino' => 'Cappuccino', |
115
|
|
|
'gotar' => 'Blue/Green', |
116
|
|
|
'bootstrap' => 'Bootstrap3', |
117
|
|
|
], |
118
|
|
|
'BASE_PATH'=>$config['BASE_PATH']??dirname(__DIR__,2), |
119
|
|
|
'subFolder'=>$config['subfolder']??'', |
120
|
|
|
'DEBUGMODE'=>$config['debugmode']??false, |
121
|
|
|
'THEME_PATH'=>$config['theme_path']??dirname(__DIR__,2).'/assets/themes', |
122
|
|
|
'IN_TEST'=>$config['IN_TEST']??false, |
123
|
|
|
'webdbLastTab'=>[] |
124
|
|
|
]; |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
self::$envConfig=array_merge( self::$envConfig,$config); |
|
|
|
|
128
|
|
|
if (!self::$instance) { |
129
|
|
|
self::$instance = new static(self::$envConfig); |
130
|
|
|
|
131
|
|
|
self::$instance |
|
|
|
|
132
|
|
|
->withConf(self::$envConfig) |
|
|
|
|
133
|
|
|
->setExtra() |
|
|
|
|
134
|
|
|
->setMisc() |
|
|
|
|
135
|
|
|
->setViews(); |
|
|
|
|
136
|
|
|
} |
|
|
|
|
137
|
|
|
//ddd($container->subfolder); |
|
|
|
|
138
|
|
|
return self::$instance; |
|
|
|
|
139
|
|
|
} |
|
|
|
|
140
|
|
|
|
141
|
|
|
/** |
|
|
|
|
142
|
|
|
* Determines the redirection url according to query string. |
143
|
|
|
* |
144
|
|
|
* @return string the redirect url |
145
|
|
|
*/ |
146
|
|
|
public function getRedirectUrl() |
147
|
|
|
{$container=self::getContainerInstance(); |
|
|
|
|
148
|
|
|
$query_string = $container->request->getUri()->getQuery(); |
|
|
|
|
149
|
|
|
|
150
|
|
|
// if server_id isn't set, then you will be redirected to intro |
|
|
|
|
151
|
|
|
if (null === $container->request->getQueryParam('server')) { |
152
|
|
|
$destinationurl = self::$envConfig['subFolder'] . '/src/views/intro'; |
|
|
|
|
153
|
|
|
} else { |
154
|
|
|
// otherwise, you'll be redirected to the login page for that server; |
|
|
|
|
155
|
|
|
$destinationurl = self::$envConfig['subFolder'] . '/src/views/login' . ($query_string ? '?' . $query_string : ''); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $destinationurl; |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
|
|
|
|
162
|
|
|
* Adds a flash message to the session that will be displayed on the next request. |
163
|
|
|
* |
164
|
|
|
* @param mixed $content msg content (can be object, array, etc) |
165
|
|
|
* @param string $key The key to associate with the message. Defaults to the stack |
166
|
|
|
* trace of the closure or method that called addFlassh |
167
|
|
|
*/ |
168
|
|
|
public function addFlash($content, $key = ''): void |
169
|
|
|
{ |
|
|
|
|
170
|
|
|
if ('' === $key) { |
171
|
|
|
$key = self::getBackTrace(); |
|
|
|
|
172
|
|
|
}$container=self::getContainerInstance(); |
173
|
|
|
// $this->dump(__METHOD__ . ': addMessage ' . $key . ' ' . json_encode($content)); |
|
|
|
|
174
|
|
|
if ($container->flash) { |
175
|
|
|
$container->flash->addMessage($key, $content); |
|
|
|
|
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
|
|
|
|
180
|
|
|
* Gets the destination with the last active tab selected for that controller |
181
|
|
|
* Usually used after going through a redirect route. |
182
|
|
|
* |
183
|
|
|
* @param string $subject The subject, usually a view name like 'server' or 'table' |
184
|
|
|
* |
185
|
|
|
* @return string The destination url with last tab set in the query string |
186
|
|
|
*/ |
187
|
|
|
public function getDestinationWithLastTab($subject) |
188
|
|
|
{$container=self::getContainerInstance(); |
|
|
|
|
189
|
|
|
$_server_info = $container->misc->getServerInfo(); |
|
|
|
|
190
|
|
|
$this->addFlash($subject, 'getDestinationWithLastTab'); |
|
|
|
|
191
|
|
|
//$this->prtrace('$_server_info', $_server_info); |
|
|
|
|
192
|
|
|
// If username isn't set in server_info, you should login |
|
|
|
|
193
|
|
|
$url = $container->misc->getLastTabURL($subject) ?? ['url' => 'alldb', 'urlvars' => ['subject' => 'server']]; |
|
|
|
|
194
|
|
|
$destinationurl = $this->getRedirectUrl(); |
|
|
|
|
195
|
|
|
|
196
|
|
|
if (!isset($_server_info['username'])) { |
197
|
|
|
return $destinationurl; |
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
if (!\is_array($url)) { |
201
|
|
|
return $this->getRedirectUrl($subject); |
|
|
|
|
202
|
|
|
} |
203
|
|
|
$this->addFlash($url, 'getLastTabURL for ' . $subject); |
|
|
|
|
204
|
|
|
// Load query vars into superglobal arrays |
|
|
|
|
205
|
|
|
if (isset($url['urlvars'])) { |
206
|
|
|
$urlvars = []; |
|
|
|
|
207
|
|
|
|
208
|
|
|
foreach ($url['urlvars'] as $key => $urlvar) { |
209
|
|
|
//$this->prtrace($key, $urlvar); |
|
|
|
|
210
|
|
|
$urlvars[$key] = \PHPPgAdmin\Decorators\Decorator::get_sanitized_value($urlvar, $_REQUEST); |
|
|
|
|
211
|
|
|
} |
212
|
|
|
$_REQUEST = \array_merge($_REQUEST, $urlvars); |
|
|
|
|
213
|
|
|
$_GET = \array_merge($_GET, $urlvars); |
|
|
|
|
214
|
|
|
} |
215
|
|
|
$actionurl = \PHPPgAdmin\Decorators\Decorator::actionurl($url['url'], $_GET); |
|
|
|
|
216
|
|
|
$destinationurl = $actionurl->value($_GET); |
|
|
|
|
217
|
|
|
|
218
|
|
|
return \str_replace('views/?', "views/{$subject}?", $destinationurl); |
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
|
|
|
|
222
|
|
|
* Adds an error to the errors array property of the container. |
223
|
|
|
* |
224
|
|
|
* @param string $errormsg The error msg |
225
|
|
|
* |
226
|
|
|
* @return\Slim\Container The app container |
227
|
|
|
*/ |
228
|
|
|
public function addError(string $errormsg): \Slim\Container |
229
|
|
|
{ |
|
|
|
|
230
|
|
|
$container=self::getContainerInstance(); |
|
|
|
|
231
|
|
|
$errors = $container->get('errors'); |
|
|
|
|
232
|
|
|
$errors[] = $errormsg; |
|
|
|
|
233
|
|
|
$container->offsetSet('errors', $errors); |
|
|
|
|
234
|
|
|
|
235
|
|
|
return $container; |
|
|
|
|
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
|
|
|
|
239
|
|
|
* @param array $conf |
240
|
|
|
*/ |
241
|
|
|
private function withConf($conf): self |
242
|
|
|
{ |
|
|
|
|
243
|
|
|
$container = self::getContainerInstance(); |
|
|
|
|
244
|
|
|
$conf['plugins'] = []; |
|
|
|
|
245
|
|
|
|
246
|
|
|
$container->BASE_PATH=$conf['BASE_PATH']; |
247
|
|
|
$container->subFolder=$conf['subfolder']; |
248
|
|
|
$container->DEBUGMODE=$conf['debugmode']; |
249
|
|
|
$container->THEME_PATH=$conf['theme_path']; |
250
|
|
|
$container->IN_TEST=$conf['IN_TEST']; |
251
|
|
|
$container['errors'] = []; |
252
|
|
|
$container['conf'] = static function (\Slim\Container $c) use ($conf): array { |
|
|
|
|
253
|
|
|
$display_sizes = $conf['display_sizes']; |
|
|
|
|
254
|
|
|
|
255
|
|
|
if (\is_array($display_sizes)) { |
256
|
|
|
$conf['display_sizes'] = [ |
|
|
|
|
257
|
|
|
'schemas' => (bool) isset($display_sizes['schemas']) && true === $display_sizes['schemas'], |
258
|
|
|
'tables' => (bool) isset($display_sizes['tables']) && true === $display_sizes['tables'], |
259
|
|
|
]; |
|
|
|
|
260
|
|
|
} else { |
261
|
|
|
$conf['display_sizes'] = [ |
|
|
|
|
262
|
|
|
'schemas' => (bool) $display_sizes, |
263
|
|
|
'tables' => (bool) $display_sizes, |
264
|
|
|
]; |
|
|
|
|
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
if (!isset($conf['theme'])) { |
268
|
|
|
$conf['theme'] = 'default'; |
|
|
|
|
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
foreach ($conf['servers'] as &$server) { |
272
|
|
|
if (!isset($server['port'])) { |
273
|
|
|
$server['port'] = 5432; |
|
|
|
|
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
if (!isset($server['sslmode'])) { |
277
|
|
|
$server['sslmode'] = 'unspecified'; |
|
|
|
|
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
//self::$envConfig=[ |
|
|
|
|
281
|
|
|
//'BASE_PATH'=>$conf['BASE_PATH'], |
282
|
|
|
//'subFolder'=>$conf['subfolder'], |
283
|
|
|
//'DEBUGMODE'=>$conf['debugmode'], |
284
|
|
|
//'THEME_PATH'=>$conf['theme_path'], |
285
|
|
|
//'IN_TEST'=>$conf['IN_TEST'] |
286
|
|
|
//]; |
|
|
|
|
287
|
|
|
|
288
|
|
|
return $conf; |
|
|
|
|
289
|
|
|
}; |
290
|
|
|
|
291
|
|
|
$container->subfolder = $conf['subfolder']; |
292
|
|
|
|
293
|
|
|
|
294
|
|
|
return $this; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
|
|
|
|
298
|
|
|
* Sets the views. |
299
|
|
|
* |
300
|
|
|
* @return self ( description_of_the_return_value ) |
301
|
|
|
*/ |
302
|
|
|
private function setViews() |
303
|
|
|
{ |
|
|
|
|
304
|
|
|
$container = self::getContainerInstance(); |
|
|
|
|
305
|
|
|
|
306
|
|
|
/** |
|
|
|
|
307
|
|
|
* @return \PHPPgAdmin\ViewManager |
308
|
|
|
*/ |
309
|
|
|
$container['view'] = static function (\Slim\Container $c): \PHPPgAdmin\ViewManager { |
|
|
|
|
310
|
|
|
$misc = $c->misc; |
|
|
|
|
311
|
|
|
$view = new ViewManager(BASE_PATH . '/assets/templates', [ |
|
|
|
|
312
|
|
|
'cache' => BASE_PATH . '/temp/twigcache', |
313
|
|
|
'auto_reload' => $c->get('settings')['debug'], |
314
|
|
|
'debug' => $c->get('settings')['debug'], |
315
|
|
|
], $c); |
|
|
|
|
316
|
|
|
|
317
|
|
|
$misc->setView($view); |
|
|
|
|
318
|
|
|
|
319
|
|
|
return $view; |
|
|
|
|
320
|
|
|
}; |
321
|
|
|
|
322
|
|
|
return $this; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
|
|
|
|
326
|
|
|
* Sets the instance of Misc class. |
327
|
|
|
* |
328
|
|
|
* @return self ( description_of_the_return_value ) |
329
|
|
|
*/ |
330
|
|
|
private function setMisc() |
331
|
|
|
{ |
|
|
|
|
332
|
|
|
$container = self::getContainerInstance(); |
|
|
|
|
333
|
|
|
/** |
|
|
|
|
334
|
|
|
* @return \PHPPgAdmin\Misc |
335
|
|
|
*/ |
336
|
|
|
$container['misc'] = static function (\Slim\Container $c): \PHPPgAdmin\Misc { |
|
|
|
|
337
|
|
|
$misc = new \PHPPgAdmin\Misc($c); |
|
|
|
|
338
|
|
|
|
339
|
|
|
$conf = $c->get('conf'); |
|
|
|
|
340
|
|
|
|
341
|
|
|
// 4. Check for theme by server/db/user |
|
|
|
|
342
|
|
|
$_server_info = $misc->getServerInfo(); |
|
|
|
|
343
|
|
|
|
344
|
|
|
/* starting with PostgreSQL 9.0, we can set the application name */ |
|
|
|
|
345
|
|
|
if (isset($_server_info['pgVersion']) && 9 <= $_server_info['pgVersion']) { |
346
|
|
|
\putenv('PGAPPNAME=' . $c->get('settings')['appName'] . '_' . $c->get('settings')['appVersion']); |
|
|
|
|
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
return $misc; |
|
|
|
|
350
|
|
|
}; |
351
|
|
|
|
352
|
|
|
return $this; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
private function setExtra() |
356
|
|
|
{ |
|
|
|
|
357
|
|
|
$container = self::getContainerInstance(); |
|
|
|
|
358
|
|
|
$container['flash'] = static function (): \Slim\Flash\Messages { |
|
|
|
|
359
|
|
|
return new \Slim\Flash\Messages(); |
|
|
|
|
360
|
|
|
}; |
361
|
|
|
|
362
|
|
|
$container['lang'] = static function (\Slim\Container $c): array { |
363
|
|
|
$translations = new \PHPPgAdmin\Translations($c); |
364
|
|
|
|
365
|
|
|
return $translations->lang; |
366
|
|
|
}; |
367
|
|
|
|
368
|
|
|
return $this; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
public static function getDefaultConfig():array |
372
|
|
|
{ |
373
|
|
|
return [ |
374
|
|
|
'settings' => [ |
375
|
|
|
'displayErrorDetails' => self::$envConfig['DEBUGMODE'], |
376
|
|
|
'determineRouteBeforeAppMiddleware' => true, |
377
|
|
|
'base_path' => \dirname(__DIR__, 2), |
378
|
|
|
'debug' => self::$envConfig['DEBUGMODE'], |
379
|
|
|
'phpMinVer' => '7.1', // PHP minimum version |
380
|
|
|
'addContentLengthHeader' => false, |
381
|
|
|
'appName' => 'PHPPgAdmin6' |
382
|
|
|
], |
383
|
|
|
]; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* This function registers the default services that Slim needs to work. |
388
|
|
|
* |
389
|
|
|
* All services are shared, they are registered such that the |
390
|
|
|
* same instance is returned on subsequent calls. |
391
|
|
|
* |
392
|
|
|
* @param array $userSettings Associative array of application settings |
393
|
|
|
*/ |
394
|
|
|
private function registerDefaultServices($userSettings): void |
395
|
|
|
{ |
396
|
|
|
$defaultSettings = $this->defaultSettings; |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* This service MUST return an array or an instance of ArrayAccess. |
400
|
|
|
* |
401
|
|
|
* @return array|ArrayAccess |
402
|
|
|
*/ |
403
|
|
|
$this['settings'] = static function () use ($userSettings, $defaultSettings):\Slim\Collection { |
404
|
|
|
return new Collection(\array_merge($defaultSettings, $userSettings)); |
405
|
|
|
}; |
406
|
|
|
|
407
|
|
|
$defaultProvider = new DefaultServicesProvider(); |
408
|
|
|
$defaultProvider->register($this); |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|