Translated using Weblate (Norwegian Nynorsk)
[phpmyadmin.git] / setup / lib / ConfigGenerator.php
blob93c8e84c3565ab5f8472b105a0d16fcad4a3a8e5
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Config file generator
6 * @package PhpMyAdmin-Setup
7 */
8 namespace PhpMyAdmin\Setup;
10 use PhpMyAdmin\Config\ConfigFile;
12 /**
13 * Config file generation class
15 * @package PhpMyAdmin
17 class ConfigGenerator
19 /**
20 * Creates config file
22 * @param ConfigFile $cf Config file instance
24 * @return string
26 public static function getConfigFile(ConfigFile $cf)
28 $crlf = (isset($_SESSION['eol']) && $_SESSION['eol'] == 'win')
29 ? "\r\n"
30 : "\n";
31 $conf = $cf->getConfig();
33 // header
34 $ret = '<?php' . $crlf
35 . '/*' . $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;
43 //servers
44 if (! empty($conf['Servers'])) {
45 $ret .= self::getServerPart($cf, $crlf, $conf['Servers']);
46 unset($conf['Servers']);
49 // other settings
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);
66 $ret .= '?' . '>';
68 return $ret;
71 /**
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
78 * @return string
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;
86 $ret = '';
87 if (self::_isZeroBasedArray($var_value)) {
88 $ret = "\$cfg['$var_name'] = "
89 . self::_exportZeroBasedArray($var_value, $crlf)
90 . ';' . $crlf;
91 } else {
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;
99 return $ret;
103 * Check whether $array is a continuous 0-based array
105 * @param array $array Array to check
107 * @return boolean
109 private static function _isZeroBasedArray(array $array)
111 for ($i = 0, $nb = count($array); $i < $nb; $i++) {
112 if (! isset($array[$i])) {
113 return false;
116 return true;
120 * Exports continuous 0-based array
122 * @param array $array Array to export
123 * @param string $crlf Newline string
125 * @return string
127 private static function _exportZeroBasedArray(array $array, $crlf)
129 $retv = array();
130 foreach ($array as $v) {
131 $retv[] = var_export($v, true);
133 $ret = "array(";
134 if (count($retv) <= 4) {
135 // up to 4 values - one line
136 $ret .= implode(', ', $retv);
137 } else {
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];
144 $ret .= ')';
145 return $ret;
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
155 * @return string
157 protected static function getServerPart(ConfigFile $cf, $crlf, array $servers)
159 if ($cf->getServerCount() === 0) {
160 return null;
163 $ret = "/* Servers configuration */$crlf\$i = 0;" . $crlf . $crlf;
164 foreach ($servers as $id => $server) {
165 $ret .= '/* Server: '
166 . strtr($cf->getServerName($id) . " [$id] ", '*/', '-')
167 . "*/" . $crlf
168 . '$i++;' . $crlf;
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))
175 . ';' . $crlf;
177 $ret .= $crlf;
179 $ret .= '/* End of servers configuration */' . $crlf . $crlf;
180 return $ret;