3.4.0-rc1
[phpmyadmin/thilanka.git] / setup / lib / ConfigGenerator.class.php
blob95c33251df6231b55cf0b92c2c4e6ffdb62103c5
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Config file generator
6 * @package phpMyAdmin-setup
7 */
9 /**
10 * Config file generation class
12 * @package phpMyAdmin
14 class ConfigGenerator
16 /**
17 * Creates config file
19 * @return string
21 public static function getConfigFile()
23 $cf = ConfigFile::getInstance();
25 $crlf = (isset($_SESSION['eol']) && $_SESSION['eol'] == 'win') ? "\r\n" : "\n";
26 $c = $cf->getConfig();
28 // header
29 $ret = '<?php' . $crlf
30 . '/*' . $crlf
31 . ' * Generated configuration file' . $crlf
32 . ' * Generated by: phpMyAdmin '
33 . $GLOBALS['PMA_Config']->get('PMA_VERSION')
34 . ' setup script' . $crlf
35 . ' * Date: ' . date(DATE_RFC1123) . $crlf
36 . ' */' . $crlf . $crlf;
38 // servers
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
43 . '$i++;' . $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))
50 . ';' . $crlf;
52 $ret .= $crlf;
54 $ret .= '/* End of servers configuration */' . $crlf . $crlf;
56 unset($c['Servers']);
58 // other settings
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);
75 $ret .= '?>';
77 return $ret;
80 /**
81 * Returns exported configuration variable
83 * @param string $var_name
84 * @param mixed $var_value
85 * @param string $crlf
86 * @return string
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;
93 $ret = '';
94 if (self::_isZeroBasedArray($var_value)) {
95 $ret = "\$cfg['$var_name'] = " . self::_exportZeroBasedArray($var_value, $crlf)
96 . ';' . $crlf;
97 } else {
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;
104 return $ret;
108 * Check whether $array is a continuous 0-based array
110 * @param array $array
111 * @return boolean
113 private static function _isZeroBasedArray(array $array)
115 for ($i = 0; $i < count($array); $i++) {
116 if (!isset($array[$i])) {
117 return false;
120 return true;
124 * Exports continuous 0-based array
126 * @param array $array
127 * @param string $crlf
128 * @return string
130 private static function _exportZeroBasedArray(array $array, $crlf)
132 $retv = array();
133 foreach ($array as $v) {
134 $retv[] = var_export($v, true);
136 $ret = "array(";
137 if (count($retv) <= 4) {
138 // up to 4 values - one line
139 $ret .= implode(', ', $retv);
140 } else {
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];
147 $ret .= ')';
148 return $ret;