2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Config file generator
6 * @package phpMyAdmin-setup
10 * Config file generation class
21 public static function getConfigFile()
23 $cf = ConfigFile
::getInstance();
25 $crlf = (isset($_SESSION['eol']) && $_SESSION['eol'] == 'win') ?
"\r\n" : "\n";
26 $c = $cf->getConfig();
29 $ret = '<?php' . $crlf
31 . ' * Generated configuration file' . $crlf
32 . ' * Generated by: phpMyAdmin '
33 . $GLOBALS['PMA_Config']->get('PMA_VERSION')
34 . ' setup script by Piotr Przybylski <piotrprz@gmail.com>' . $crlf
35 . ' * Date: ' . date(DATE_RFC1123
) . $crlf
36 . ' */' . $crlf . $crlf;
39 if ($cf->getServerCount() > 0) {
40 $ret .= "/* Servers configuration */$crlf\$i = 0;" . $crlf . $crlf;
41 foreach ($c['Servers'] as $id => $server) {
42 $ret .= '/* Server: ' . strtr($cf->getServerName($id), '*/', '-') . " [$id] */" . $crlf
44 foreach ($server as $k => $v) {
45 $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
46 $ret .= "\$cfg['Servers'][\$i]['$k'] = "
47 . (is_array($v) && self
::_isZeroBasedArray($v)
48 ? self
::_exportZeroBasedArray($v, $crlf)
49 : var_export($v, true))
54 $ret .= '/* End of servers configuration */' . $crlf . $crlf;
59 $persistKeys = $cf->getPersistKeysMap();
61 foreach ($c as $k => $v) {
62 $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
63 $ret .= self
::_getVarExport($k, $v, $crlf);
64 if (isset($persistKeys[$k])) {
65 unset($persistKeys[$k]);
68 // keep 1d array keys which are present in $persist_keys (config.values.php)
69 foreach (array_keys($persistKeys) as $k) {
70 if (strpos($k, '/') === false) {
71 $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
72 $ret .= self
::_getVarExport($k, $cf->getDefault($k), $crlf);
81 * Returns exported configuration variable
83 * @param string $var_name
84 * @param mixed $var_value
88 private static function _getVarExport($var_name, $var_value, $crlf)
90 if (!is_array($var_value) ||
empty($var_value)) {
91 return "\$cfg['$var_name'] = " . var_export($var_value, true) . ';' . $crlf;
94 if (self
::_isZeroBasedArray($var_value)) {
95 $ret = "\$cfg['$var_name'] = " . self
::_exportZeroBasedArray($var_value, $crlf)
98 // string keys: $cfg[key][subkey] = value
99 foreach ($var_value as $k => $v) {
100 $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
101 $ret .= "\$cfg['$var_name']['$k'] = " . var_export($v, true) . ';' . $crlf;
108 * Check whether $array is a continuous 0-based array
110 * @param array $array
113 private static function _isZeroBasedArray(array $array)
115 for ($i = 0; $i < count($array); $i++
) {
116 if (!isset($array[$i])) {
124 * Exports continuous 0-based array
126 * @param array $array
127 * @param string $crlf
130 private static function _exportZeroBasedArray(array $array, $crlf)
133 foreach ($array as $v) {
134 $retv[] = var_export($v, true);
137 if (count($retv) <= 4) {
138 // up to 4 values - one line
139 $ret .= implode(', ', $retv);
141 // more than 4 values - value per line
142 $imax = count($retv)-1;
143 for ($i = 0; $i <= $imax; $i++
) {
144 $ret .= ($i < $imax ?
($i > 0 ?
',' : '') : '') . $crlf . ' ' . $retv[$i];