2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Config file generator
6 * @package PhpMyAdmin-Setup
8 namespace PhpMyAdmin\Setup
;
10 use PhpMyAdmin\Config\ConfigFile
;
13 * Config file generation class
22 * @param ConfigFile $cf Config file instance
26 public static function getConfigFile(ConfigFile
$cf)
28 $crlf = (isset($_SESSION['eol']) && $_SESSION['eol'] == 'win')
31 $conf = $cf->getConfig();
34 $ret = '<?php' . $crlf
36 . ' * Generated configuration file' . $crlf
37 . ' * Generated by: phpMyAdmin '
38 . $GLOBALS['PMA_Config']->get('PMA_VERSION')
39 . ' setup script' . $crlf
40 . ' * Date: ' . gmdate(DATE_RFC1123
) . $crlf
41 . ' */' . $crlf . $crlf;
44 if (! empty($conf['Servers'])) {
45 $ret .= self
::getServerPart($cf, $crlf, $conf['Servers']);
46 unset($conf['Servers']);
50 $persistKeys = $cf->getPersistKeysMap();
52 foreach ($conf as $k => $v) {
53 $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
54 $ret .= self
::_getVarExport($k, $v, $crlf);
55 if (isset($persistKeys[$k])) {
56 unset($persistKeys[$k]);
59 // keep 1d array keys which are present in $persist_keys (config.values.php)
60 foreach (array_keys($persistKeys) as $k) {
61 if (mb_strpos($k, '/') === false) {
62 $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
63 $ret .= self
::_getVarExport($k, $cf->getDefault($k), $crlf);
72 * Returns exported configuration variable
74 * @param string $var_name configuration name
75 * @param mixed $var_value configuration value(s)
76 * @param string $crlf line ending
80 private static function _getVarExport($var_name, $var_value, $crlf)
82 if (!is_array($var_value) ||
empty($var_value)) {
83 return "\$cfg['$var_name'] = "
84 . var_export($var_value, true) . ';' . $crlf;
87 if (self
::_isZeroBasedArray($var_value)) {
88 $ret = "\$cfg['$var_name'] = "
89 . self
::_exportZeroBasedArray($var_value, $crlf)
92 // string keys: $cfg[key][subkey] = value
93 foreach ($var_value as $k => $v) {
94 $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
95 $ret .= "\$cfg['$var_name']['$k'] = "
96 . var_export($v, true) . ';' . $crlf;
103 * Check whether $array is a continuous 0-based array
105 * @param array $array Array to check
109 private static function _isZeroBasedArray(array $array)
111 for ($i = 0, $nb = count($array); $i < $nb; $i++
) {
112 if (! isset($array[$i])) {
120 * Exports continuous 0-based array
122 * @param array $array Array to export
123 * @param string $crlf Newline string
127 private static function _exportZeroBasedArray(array $array, $crlf)
130 foreach ($array as $v) {
131 $retv[] = var_export($v, true);
134 if (count($retv) <= 4) {
135 // up to 4 values - one line
136 $ret .= implode(', ', $retv);
138 // more than 4 values - value per line
139 $imax = count($retv);
140 for ($i = 0; $i < $imax; $i++
) {
141 $ret .= ($i > 0 ?
',' : '') . $crlf . ' ' . $retv[$i];
149 * Generate server part of config file
151 * @param ConfigFile $cf Config file
152 * @param string $crlf Carriage return char
153 * @param array $servers Servers list
157 protected static function getServerPart(ConfigFile
$cf, $crlf, array $servers)
159 if ($cf->getServerCount() === 0) {
163 $ret = "/* Servers configuration */$crlf\$i = 0;" . $crlf . $crlf;
164 foreach ($servers as $id => $server) {
165 $ret .= '/* Server: '
166 . strtr($cf->getServerName($id) . " [$id] ", '*/', '-')
169 foreach ($server as $k => $v) {
170 $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
171 $ret .= "\$cfg['Servers'][\$i]['$k'] = "
172 . (is_array($v) && self
::_isZeroBasedArray($v)
173 ? self
::_exportZeroBasedArray($v, $crlf)
174 : var_export($v, true))
179 $ret .= '/* End of servers configuration */' . $crlf . $crlf;