-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathConfig.php
More file actions
741 lines (628 loc) · 21.6 KB
/
Config.php
File metadata and controls
741 lines (628 loc) · 21.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
<?php
declare(strict_types=1);
namespace PhpMyAdmin;
use PhpMyAdmin\Config\Settings;
use PhpMyAdmin\Config\Settings\Server;
use PhpMyAdmin\Dbal\ConnectionType;
use PhpMyAdmin\Exceptions\ConfigException;
use PhpMyAdmin\Routing\Routing;
use Throwable;
use function __;
use function array_key_last;
use function array_shift;
use function array_slice;
use function count;
use function defined;
use function explode;
use function fclose;
use function file_exists;
use function filemtime;
use function fileperms;
use function fopen;
use function fread;
use function function_exists;
use function get_defined_constants;
use function implode;
use function in_array;
use function ini_get;
use function is_array;
use function is_dir;
use function is_numeric;
use function is_readable;
use function is_string;
use function is_writable;
use function mb_strtolower;
use function md5;
use function mkdir;
use function ob_end_clean;
use function ob_start;
use function parse_url;
use function realpath;
use function rtrim;
use function setcookie;
use function sprintf;
use function str_ends_with;
use function stripos;
use function strtolower;
use function sys_get_temp_dir;
use function time;
use function trim;
use const CHANGELOG_FILE;
use const DIRECTORY_SEPARATOR;
use const PHP_OS;
use const PHP_OS_FAMILY;
use const PHP_URL_PATH;
use const PHP_URL_SCHEME;
/**
* Configuration handling
*
* @psalm-import-type ServerSettingsType from Server
* @psalm-import-type SettingsType from Settings
*/
class Config
{
public static self|null $instance = null;
/** @var mixed[] configuration settings, without user preferences applied */
public array $baseSettings;
/** @psalm-var SettingsType */
public array $settings;
private string $source = '';
/** @var int source modification time */
public int $sourceMtime = 0;
private bool|null $isHttps = null;
public Settings $config;
/** @var int<0, max> */
public int $server = 0;
/** @var array<string,string> $tempDir */
private static array $tempDir = [];
private bool $hasSelectedServer = false;
/** @psalm-var ServerSettingsType */
public array $selectedServer;
private bool $isSetup = false;
public function __construct()
{
$this->config = new Settings([]);
$config = $this->config->asArray();
$this->settings = $config;
$this->baseSettings = $config;
$this->selectedServer = (new Server())->asArray();
}
/** @deprecated Use dependency injection instead. */
public static function getInstance(): self
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
public function setSetup(bool $isSetup): void
{
$this->isSetup = $isSetup;
}
public function isSetup(): bool
{
return $this->isSetup;
}
/**
* @param string|null $source source to read config from
*
* @throws ConfigException
*/
public function loadFromFile(string|null $source = null): void
{
if ($source !== null) {
$this->setSource($source);
}
if ($this->configFileExists()) {
$this->load();
}
$this->baseSettings = $this->settings;
}
/**
* Determines if GD2+ is available.
*
* Respects the config override ('yes' / 'no') if set,
* otherwise checks the `GD_MAJOR_VERSION` constant (>= 2).
*/
public function isGd2Available(): bool
{
if ($this->config->GD2Available === 'yes') {
return true;
}
if ($this->config->GD2Available === 'no') {
return false;
}
return (get_defined_constants()['GD_MAJOR_VERSION'] ?? 0) >= 2;
}
public function isWindows(): bool
{
if (PHP_OS_FAMILY === 'Windows') {
return true;
}
// Is it OS/2 (No file permissions like Windows)
return stripos(PHP_OS, 'OS/2') !== false;
}
/**
* loads configuration from $source, usually the config file
* should be called on object creation
*
* @throws ConfigException
*/
private function load(): void
{
/** @var mixed $cfg */
$cfg = [];
ob_start();
try {
/**
* Suppress any warnings generated by require or inside the included file
*
* @psalm-suppress UnresolvableInclude
*/
@require $this->source;
} catch (Throwable) {
throw new ConfigException('Failed to load phpMyAdmin configuration.');
}
ob_end_clean();
$this->sourceMtime = (int) filemtime($this->source);
if (! is_array($cfg)) {
return;
}
$this->config = new Settings($cfg);
$this->settings = $this->config->asArray();
}
/**
* set source
*
* @param string $source source
*/
public function setSource(string $source): void
{
$this->source = trim($source);
}
/** @throws ConfigException */
public function configFileExists(): bool
{
if ($this->source === '') {
// no configuration file set at all
return false;
}
if (! @file_exists($this->source)) {
return false;
}
if (! @is_readable($this->source)) {
// manually check if file is readable
// might be bug #3059806 Supporting running from CIFS/Samba shares
$contents = false;
$handle = @fopen($this->source, 'r');
if ($handle !== false) {
$contents = @fread($handle, 1); // reading 1 byte is enough to test
fclose($handle);
}
if ($contents === false) {
throw new ConfigException(sprintf(
function_exists('__')
? __('Existing configuration file (%s) is not readable.')
: 'Existing configuration file (%s) is not readable.',
$this->source,
));
}
}
return true;
}
/**
* verifies the permissions on config file (if asked by configuration)
* (must be called after config.inc.php has been merged)
*
* @throws ConfigException
*/
public function checkPermissions(): void
{
// Check for permissions (on platforms that support it):
if (! $this->config->CheckConfigurationPermissions || ! @file_exists($this->source)) {
return;
}
$perms = @fileperms($this->source);
if ($perms === false || ! ($perms & 2)) {
return;
}
// This check is normally done after loading configuration
if ($this->isWindows()) {
return;
}
throw new ConfigException(__('Wrong permissions on configuration file, should not be world writable!'));
}
/**
* sets configuration variable
*
* @param string $setting configuration option
* @param mixed $value new value for configuration option
*
* @throws ConfigException
*/
public function set(string $setting, mixed $value): void
{
$parts = explode('/', $setting);
if (! $this->setValueRecursive($this->settings, $parts, $value)) {
return;
}
$this->config = new Settings($this->settings);
$this->settings = $this->config->asArray();
}
/**
* @param mixed[] $array
* @param array-key[] $parts
*
* @throws ConfigException
*/
private function setValueRecursive(array &$array, array $parts, mixed $value): bool
{
$part = array_shift($parts);
if ($parts === []) {
if (isset($array[$part]) && $array[$part] === $value) {
return false;
}
$array[$part] = $value;
return true;
}
if (! is_array($array[$part])) {
throw new ConfigException('Failed to set configuration value.');
}
return $this->setValueRecursive($array[$part], $parts, $value);
}
/**
* returns source for current config
*
* @return string config source
*/
public function getSource(): string
{
return $this->source;
}
public function isUploadEnabled(): bool
{
$iniValue = ini_get('file_uploads');
// if set "php_admin_value file_uploads Off" in httpd.conf
// ini_get() also returns the string "Off" in this case:
return $iniValue !== false && $iniValue !== '' && $iniValue !== '0' && strtolower($iniValue) !== 'off';
}
/**
* Checks if protocol is https
*
* This function checks if the https protocol on the active connection.
*/
public function isHttps(): bool
{
if ($this->isHttps !== null) {
return $this->isHttps;
}
$url = $this->config->PmaAbsoluteUri;
$isHttps = false;
if ($url !== '' && parse_url($url, PHP_URL_SCHEME) === 'https') {
$isHttps = true;
} elseif (strtolower(Core::getEnv('HTTP_SCHEME')) === 'https') {
$isHttps = true;
} elseif (strtolower(Core::getEnv('HTTPS')) === 'on') {
$isHttps = true;
} elseif (stripos(Core::getEnv('REQUEST_URI'), 'https:') === 0) {
$isHttps = true;
} elseif (strtolower(Core::getEnv('HTTP_HTTPS_FROM_LB')) === 'on') {
// A10 Networks load balancer
$isHttps = true;
} elseif (strtolower(Core::getEnv('HTTP_FRONT_END_HTTPS')) === 'on') {
$isHttps = true;
} elseif (strtolower(Core::getEnv('HTTP_X_FORWARDED_PROTO')) === 'https') {
$isHttps = true;
} elseif (strtolower(Core::getEnv('HTTP_CLOUDFRONT_FORWARDED_PROTO')) === 'https') {
// Amazon CloudFront, issue #15621
$isHttps = true;
} elseif (Util::getProtoFromForwardedHeader(Core::getEnv('HTTP_FORWARDED')) === 'https') {
// RFC 7239 Forwarded header
$isHttps = true;
} elseif ((int) Core::getEnv('SERVER_PORT') === 443) {
$isHttps = true;
}
$this->isHttps = $isHttps;
return $isHttps;
}
/**
* Get phpMyAdmin root path
*/
public function getRootPath(): string
{
$url = $this->config->PmaAbsoluteUri;
if ($url !== '') {
$path = parse_url($url, PHP_URL_PATH);
if (! empty($path)) {
if (! str_ends_with($path, '/')) {
return $path . '/';
}
return $path;
}
}
$parsedUrlPath = Routing::getCleanPathInfo();
$parts = explode('/', $parsedUrlPath);
/* Remove filename */
if (str_ends_with($parts[count($parts) - 1], '.php')) {
$parts = array_slice($parts, 0, count($parts) - 1);
}
// Add one more part to make the path end in slash unless it already ends with slash
if (count($parts) < 2 || $parts[array_key_last($parts)] !== '') {
$parts[] = '';
}
return implode('/', $parts);
}
/**
* removes cookie
*
* @param string $cookieName name of cookie to remove
*/
public function removeCookie(string $cookieName): bool
{
$httpCookieName = $this->getCookieName($cookieName);
if ($this->issetCookie($cookieName)) {
unset($_COOKIE[$httpCookieName]);
}
if (defined('TESTSUITE')) {
return true;
}
return setcookie(
$httpCookieName,
'',
time() - 3600,
$this->getRootPath(),
'',
$this->isHttps(),
);
}
/**
* sets cookie if value is different from current cookie value,
* or removes if value is equal to default
*
* @param string $cookie name of cookie to remove
* @param string $value new cookie value
* @param string|null $default default value
* @param int|null $validity validity of cookie in seconds (default is one month)
* @param bool $httponly whether cookie is only for HTTP (and not for scripts)
*/
public function setCookie(
string $cookie,
string $value,
string|null $default = null,
int|null $validity = null,
bool $httponly = true,
): bool {
if ($value !== '' && $value === $default) {
// default value is used
if ($this->issetCookie($cookie)) {
// remove cookie
return $this->removeCookie($cookie);
}
return false;
}
if ($value === '' && $this->issetCookie($cookie)) {
// remove cookie, value is empty
return $this->removeCookie($cookie);
}
$httpCookieName = $this->getCookieName($cookie);
if (! $this->issetCookie($cookie) || $this->getCookie($cookie) !== $value) {
// set cookie with new value
/* Calculate cookie validity */
if ($validity === null) {
/* Valid for one month */
$validity = time() + 2592000;
} elseif ($validity === 0) {
/* Valid for session */
$validity = 0;
} else {
$validity += time();
}
if (defined('TESTSUITE')) {
$_COOKIE[$httpCookieName] = $value;
return true;
}
$cookieSameSite = $this->config->CookieSameSite;
$optionalParams = [
'expires' => $validity,
'path' => $this->getRootPath(),
'domain' => '',
'secure' => $this->isHttps(),
'httponly' => $httponly,
'samesite' => $cookieSameSite,
];
return setcookie($httpCookieName, $value, $optionalParams);
}
// cookie has already $value as value
return true;
}
/**
* get cookie
*
* @param string $cookieName The name of the cookie to get
*
* @return mixed result of getCookie()
*/
public function getCookie(string $cookieName): mixed
{
return $_COOKIE[$this->getCookieName($cookieName)] ?? null;
}
/**
* Get the real cookie name
*
* @param string $cookieName The name of the cookie
*/
public function getCookieName(string $cookieName): string
{
return ($this->isHttps() ? '__Secure-' : '') . $cookieName . ($this->isHttps() ? '_https' : '');
}
/**
* isset cookie
*
* @param string $cookieName The name of the cookie to check
*/
public function issetCookie(string $cookieName): bool
{
return isset($_COOKIE[$this->getCookieName($cookieName)]);
}
/**
* Returns temporary dir path
*
* @param string $name Directory name
*/
public function getTempDir(string $name): string|null
{
if (isset(self::$tempDir[$name])) {
return self::$tempDir[$name];
}
$path = $this->config->TempDir;
if ($path === '') {
return null;
}
$path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $name;
if (! @is_dir($path)) {
@mkdir($path, 0770, true);
}
if (! @is_dir($path) || ! @is_writable($path)) {
return null;
}
self::$tempDir[$name] = $path;
return $path;
}
/**
* Returns temporary directory
*/
public function getUploadTempDir(): string|null
{
// First try configured temp dir
// Fallback to PHP upload_tmp_dir
$dirs = [$this->getTempDir('upload'), ini_get('upload_tmp_dir'), sys_get_temp_dir()];
foreach ($dirs as $dir) {
if (! in_array($dir, ['', '0', null, false], true) && @is_writable($dir)) {
return realpath($dir);
}
}
return null;
}
/** @return int<0, max> */
public function selectServer(mixed $serverParamFromRequest): int
{
$serverNumber = 0;
if (is_numeric($serverParamFromRequest)) {
$serverNumber = (int) $serverParamFromRequest;
$serverNumber = $serverNumber >= 1 ? $serverNumber : 0;
} elseif (is_string($serverParamFromRequest) && $serverParamFromRequest !== '') {
/** Lookup server by name (see FAQ 4.8) */
foreach ($this->config->Servers as $i => $server) {
if ($server->host === $serverParamFromRequest || $server->verbose === $serverParamFromRequest) {
$serverNumber = $i;
break;
}
$verboseToLower = mb_strtolower($server->verbose);
$serverToLower = mb_strtolower($serverParamFromRequest);
if ($verboseToLower === $serverToLower || md5($verboseToLower) === $serverToLower) {
$serverNumber = $i;
break;
}
}
}
/**
* If no server is selected, make sure that $this->settings['Server'] is empty (so
* that nothing will work), and skip server authentication.
* We do NOT exit here, but continue on without logging into any server.
* This way, the welcome page will still come up (with no server info) and
* present a choice of servers in the case that there are multiple servers
* and '$this->config->ServerDefault = 0' is set.
*/
if (isset($this->config->Servers[$serverNumber])) {
$this->hasSelectedServer = true;
$this->selectedServer = $this->config->Servers[$serverNumber]->asArray();
} elseif (isset($this->config->Servers[$this->config->ServerDefault])) {
$this->hasSelectedServer = true;
$serverNumber = $this->config->ServerDefault;
$this->selectedServer = $this->config->Servers[$this->config->ServerDefault]->asArray();
} else {
$this->hasSelectedServer = false;
$serverNumber = 0;
$this->selectedServer = (new Server())->asArray();
}
$this->server = $serverNumber;
return $this->server;
}
/**
* Return connection parameters for the database server
*/
public static function getConnectionParams(Server $currentServer, ConnectionType $connectionType): Server
{
if ($connectionType !== ConnectionType::ControlUser) {
if ($currentServer->host !== '' && $currentServer->port !== '') {
return $currentServer;
}
$server = $currentServer->asArray();
$server['host'] = $server['host'] === '' ? 'localhost' : $server['host'];
$server['port'] = $server['port'] === '' ? '0' : $server['port'];
return new Server($server);
}
$server = [
'user' => $currentServer->controlUser,
'password' => $currentServer->controlPass,
'host' => $currentServer->controlHost !== '' ? $currentServer->controlHost : $currentServer->host,
'port' => '0',
'socket' => null,
'compress' => null,
'ssl' => null,
'ssl_key' => null,
'ssl_cert' => null,
'ssl_ca' => null,
'ssl_ca_path' => null,
'ssl_ciphers' => null,
'ssl_verify' => null,
'hide_connection_errors' => null,
];
// Share the settings if the host is same
if ($server['host'] === $currentServer->host) {
$server['port'] = $currentServer->port !== '' ? $currentServer->port : '0';
$server['socket'] = $currentServer->socket;
$server['compress'] = $currentServer->compress;
$server['ssl'] = $currentServer->ssl;
$server['ssl_key'] = $currentServer->sslKey;
$server['ssl_cert'] = $currentServer->sslCert;
$server['ssl_ca'] = $currentServer->sslCa;
$server['ssl_ca_path'] = $currentServer->sslCaPath;
$server['ssl_ciphers'] = $currentServer->sslCiphers;
$server['ssl_verify'] = $currentServer->sslVerify;
$server['hide_connection_errors'] = $currentServer->hideConnectionErrors;
}
// Set configured port
if ($currentServer->controlPort !== '') {
$server['port'] = $currentServer->controlPort;
}
// Set any configuration with control_ prefix
$server['socket'] = $currentServer->controlSocket ?? $server['socket'];
$server['compress'] = $currentServer->controlCompress ?? $server['compress'];
$server['ssl'] = $currentServer->controlSsl ?? $server['ssl'];
$server['ssl_key'] = $currentServer->controlSslKey ?? $server['ssl_key'];
$server['ssl_cert'] = $currentServer->controlSslCert ?? $server['ssl_cert'];
$server['ssl_ca'] = $currentServer->controlSslCa ?? $server['ssl_ca'];
$server['ssl_ca_path'] = $currentServer->controlSslCaPath ?? $server['ssl_ca_path'];
$server['ssl_ciphers'] = $currentServer->controlSslCiphers ?? $server['ssl_ciphers'];
$server['ssl_verify'] = $currentServer->controlSslVerify ?? $server['ssl_verify'];
$server['hide_connection_errors'] = $currentServer->controlHideConnectionErrors
?? $server['hide_connection_errors'];
if ($server['host'] === '') {
$server['host'] = 'localhost';
}
return new Server($server);
}
public function getSettings(): Settings
{
return $this->config;
}
public function hasSelectedServer(): bool
{
return $this->hasSelectedServer;
}
public function getChangeLogFilePath(): string
{
return CHANGELOG_FILE;
}
}