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
|
<?php
/**
* Config file generator
*/
declare(strict_types=1);
namespace PhpMyAdmin\Setup;
use PhpMyAdmin\Config\ConfigFile;
use PhpMyAdmin\Version;
use function array_keys;
use function count;
use function gmdate;
use function implode;
use function is_array;
use function is_string;
use function mb_strlen;
use function preg_replace;
use function sodium_bin2hex;
use function sodium_crypto_secretbox_keygen;
use function sprintf;
use function str_contains;
use function strtr;
use function var_export;
use const DATE_RFC1123;
use const SODIUM_CRYPTO_SECRETBOX_KEYBYTES;
/**
* Config file generation class
*/
class ConfigGenerator
{
/**
* Creates config file
*
* @param ConfigFile $cf Config file instance
*
* @return string
*/
public static function getConfigFile(ConfigFile $cf)
{
$eol = isset($_SESSION['eol']) && $_SESSION['eol'] === 'win' ? "\r\n" : "\n";
$conf = $cf->getConfig();
// header
$ret = '<?php' . $eol
. '/**' . $eol
. ' * Generated configuration file' . $eol
. ' * Generated by: phpMyAdmin '
. Version::VERSION
. ' setup script' . $eol
. ' * Date: ' . gmdate(DATE_RFC1123) . $eol
. ' */' . $eol . $eol;
//servers
if (! empty($conf['Servers'])) {
$ret .= self::getServerPart($cf, $eol, $conf['Servers']);
unset($conf['Servers']);
}
// other settings
$persistKeys = $cf->getPersistKeysMap();
foreach ($conf as $k => $v) {
$k = preg_replace('/[^A-Za-z0-9_]/', '_', (string) $k);
$ret .= self::getVarExport($k, $v, $eol);
if (! isset($persistKeys[$k])) {
continue;
}
unset($persistKeys[$k]);
}
// keep 1d array keys which are present in $persist_keys (config.values.php)
foreach (array_keys($persistKeys) as $k) {
if (str_contains($k, '/')) {
continue;
}
$k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
$ret .= self::getVarExport($k, $cf->getDefault($k), $eol);
}
return $ret . $eol;
}
/**
* Returns exported configuration variable
*
* @param string $var_name configuration name
* @param mixed $var_value configuration value(s)
* @param string $eol line ending
*
* @return string
*/
private static function getVarExport($var_name, $var_value, string $eol)
{
if ($var_name === 'blowfish_secret') {
$secret = self::getBlowfishSecretKey($var_value);
return sprintf('$cfg[\'blowfish_secret\'] = \sodium_hex2bin(\'%s\');%s', sodium_bin2hex($secret), $eol);
}
if (! is_array($var_value) || empty($var_value)) {
return "\$cfg['" . $var_name . "'] = "
. var_export($var_value, true) . ';' . $eol;
}
if (self::isZeroBasedArray($var_value)) {
return "\$cfg['" . $var_name . "'] = "
. self::exportZeroBasedArray($var_value, $eol)
. ';' . $eol;
}
$ret = '';
// string keys: $cfg[key][subkey] = value
foreach ($var_value as $k => $v) {
$k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
$ret .= "\$cfg['" . $var_name . "']['" . $k . "'] = "
. var_export($v, true) . ';' . $eol;
}
return $ret;
}
/**
* Check whether $array is a continuous 0-based array
*
* @param array $array Array to check
*/
private static function isZeroBasedArray(array $array): bool
{
for ($i = 0, $nb = count($array); $i < $nb; $i++) {
if (! isset($array[$i])) {
return false;
}
}
return true;
}
/**
* Exports continuous 0-based array
*
* @param array $array Array to export
* @param string $eol Newline string
*
* @return string
*/
private static function exportZeroBasedArray(array $array, string $eol)
{
$retv = [];
foreach ($array as $v) {
$retv[] = var_export($v, true);
}
$ret = '[';
if (count($retv) <= 4) {
// up to 4 values - one line
return $ret . implode(', ', $retv) . ']';
}
// more than 4 values - value per line
$imax = count($retv);
for ($i = 0; $i < $imax; $i++) {
$ret .= ($i > 0 ? ',' : '') . $eol . ' ' . $retv[$i];
}
return $ret . ']';
}
/**
* Generate server part of config file
*
* @param ConfigFile $cf Config file
* @param string $eol Carriage return char
* @param array $servers Servers list
*
* @return string|null
*/
protected static function getServerPart(ConfigFile $cf, string $eol, array $servers)
{
if ($cf->getServerCount() === 0) {
return null;
}
$ret = '/* Servers configuration */' . $eol . '$i = 0;' . $eol . $eol;
foreach ($servers as $id => $server) {
$ret .= '/* Server: '
. strtr($cf->getServerName($id) . ' [' . $id . '] ', '*/', '-')
. '*/' . $eol
. '$i++;' . $eol;
foreach ($server as $k => $v) {
$k = preg_replace('/[^A-Za-z0-9_]/', '_', (string) $k);
$ret .= "\$cfg['Servers'][\$i]['" . $k . "'] = "
. (is_array($v) && self::isZeroBasedArray($v)
? self::exportZeroBasedArray($v, $eol)
: var_export($v, true))
. ';' . $eol;
}
$ret .= $eol;
}
$ret .= '/* End of servers configuration */' . $eol . $eol;
return $ret;
}
/**
* @param mixed $key
*
* @psalm-return non-empty-string
*/
private static function getBlowfishSecretKey($key): string
{
if (is_string($key) && mb_strlen($key, '8bit') === SODIUM_CRYPTO_SECRETBOX_KEYBYTES) {
return $key;
}
return sodium_crypto_secretbox_keygen();
}
}
|