3 * Config file management and generation
5 * @author Piotr Przybylski <piotrprz@gmail.com>
6 * @license http://www.gnu.org/licenses/gpl.html GNU GPL 2.0
8 * @package phpMyAdmin-setup
12 * Config file management and generation class
14 * @package phpMyAdmin-setup
19 * Stores default PMA config from config.default.php
25 * Stores allowed values for non-standard fields
31 * Keys which will be always written to config file
40 private static $_instance;
43 * Private constructor, use {@link getInstance()}
45 private function __construct()
47 // load default config values
49 require './libraries/config.default.php';
51 // load additionsl config information
52 $cfg_db = &$this->cfgDb
;
53 $persist_keys = array();
54 require './setup/lib/config_info.inc.php';
56 // apply default values overrides
57 if (count($cfg_db['_overrides'])) {
58 foreach ($cfg_db['_overrides'] as $path => $value) {
59 array_write($path, $cfg, $value);
63 // checking key presence is much faster than searching so move values to keys
64 $this->persistKeys
= array_flip($persist_keys);
68 * Returns class instance
72 public static function getInstance()
74 if (is_null(self
::$_instance)) {
75 self
::$_instance = new ConfigFile();
77 return self
::$_instance;
85 * @param string $canonical_path
87 public function set($path, $value, $canonical_path = null)
89 if ($canonical_path === null) {
90 $canonical_path = $this->getCanonicalPath($path);
92 // remove if the path isn't protected and it's empty or has a default value
93 $default_value = $this->getDefault($canonical_path);
94 if (!isset($this->persistKeys
[$canonical_path])
95 && (($value == $default_value) ||
(empty($value) && empty($default_value)))) {
96 array_remove($path, $_SESSION['ConfigFile']);
98 array_write($path, $_SESSION['ConfigFile'], $value);
103 * Returns config value or $default if it's not set
105 * @param string $path
106 * @param mixed $default
109 public function get($path, $default = null)
111 return array_read($path, $_SESSION['ConfigFile'], $default);
115 * Returns default config value or $default it it's not set ie. it doesn't
116 * exist in config.default.php ($cfg) and config_info.inc.php
117 * ($_cfg_db['_overrides'])
119 * @param string $canonical_path
120 * @param mixed $default
123 public function getDefault($canonical_path, $default = null)
125 return array_read($canonical_path, $this->cfg
, $default);
129 * Returns config value, if it's not set uses the default one; returns
130 * $default if the path isn't set and doesn't contain a default value
132 * @param string $path
133 * @param mixed $default
136 public function getValue($path, $default = null)
138 $v = array_read($path, $_SESSION['ConfigFile'], null);
142 $path = $this->getCanonicalPath($path);
143 return $this->getDefault($path, $default);
147 * Returns canonical path
149 * @param string $path
152 public function getCanonicalPath($path) {
153 return preg_replace('#^Servers/([\d]+)/#', 'Servers/1/', $path);
157 * Returns config database entry for $path ($cfg_db in config_info.php)
159 * @param string $path
160 * @param mixed $default
163 public function getDbEntry($path, $default = null)
165 return array_read($path, $this->cfgDb
, $default);
169 * Returns server count
173 public function getServerCount()
175 return isset($_SESSION['ConfigFile']['Servers'])
176 ?
count($_SESSION['ConfigFile']['Servers'])
181 * Returns DSN of given server
183 * @param integer $server
186 function getServerDSN($server)
188 if (!isset($_SESSION['ConfigFile']['Servers'][$server])) {
192 $path = 'Servers/' . $server;
193 $dsn = $this->getValue("$path/extension") . '://';
194 if ($this->getValue("$path/auth_type") == 'config') {
195 $dsn .= $this->getValue("$path/user");
196 if (!$this->getValue("$path/nopassword")) {
201 if ($this->getValue("$path/connect_type") == 'tcp') {
202 $dsn .= $this->getValue("$path/host");
203 $port = $this->getValue("$path/port");
208 $dsn .= $this->getValue("$path/socket");
214 * Returns server name
219 public function getServerName($id)
221 if (!isset($_SESSION['ConfigFile']['Servers'][$id])) {
224 $verbose = $this->get("Servers/$id/verbose");
225 if (!empty($verbose)) {
228 $host = $this->get("Servers/$id/host");
229 return empty($host) ?
'localhost' : $host;
237 public function removeServer($server)
239 if (!isset($_SESSION['ConfigFile']['Servers'][$server])) {
242 $last_server = $this->getServerCount();
244 for ($i = $server; $i < $last_server; $i++
) {
245 $_SESSION['ConfigFile']['Servers'][$i] = $_SESSION['ConfigFile']['Servers'][$i+
1];
247 unset($_SESSION['ConfigFile']['Servers'][$last_server]);
249 if (isset($_SESSION['ConfigFile']['ServerDefault'])
250 && $_SESSION['ConfigFile']['ServerDefault'] >= 0) {
251 unset($_SESSION['ConfigFile']['ServerDefault']);
256 * Returns config file path
260 public function getFilePath()
262 return $this->getDbEntry('_config_file_path');
266 * Creates config file
270 public function getConfigFile()
272 $crlf = (isset($_SESSION['eol']) && $_SESSION['eol'] == 'win') ?
"\r\n" : "\n";
273 $c = $_SESSION['ConfigFile'];
276 $ret = '<?php' . $crlf
278 . ' * Generated configuration file' . $crlf
279 . ' * Generated by: phpMyAdmin '
280 . $_SESSION['PMA_Config']->get('PMA_VERSION')
281 . ' setup script by Piotr Przybylski <piotrprz@gmail.com>' . $crlf
282 . ' * Date: ' . date(DATE_RFC1123
) . $crlf
283 . ' */' . $crlf . $crlf;
286 if ($this->getServerCount() > 0) {
287 $ret .= "/* Servers configuration */$crlf\$i = 0;" . $crlf . $crlf;
288 foreach ($c['Servers'] as $id => $server) {
289 $ret .= '/* Server: ' . strtr($this->getServerName($id), '*/', '-') . " [$id] */" . $crlf
291 foreach ($server as $k => $v) {
292 $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
293 $ret .= "\$cfg['Servers'][\$i]['$k'] = "
294 . var_export($v, true) . ';' . $crlf;
298 $ret .= '/* End of servers configuration */' . $crlf . $crlf;
300 unset($c['Servers']);
303 $persistKeys = $this->persistKeys
;
304 foreach ($c as $k => $v) {
305 $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
306 $ret .= "\$cfg['$k'] = " . var_export($v, true) . ';' . $crlf;
307 if (isset($persistKeys[$k])) {
308 unset($persistKeys[$k]);
311 // keep 1d array keys which are present in $persist_keys (config_info.inc.php)
312 foreach (array_keys($persistKeys) as $k) {
313 if (strpos($k, '/') === false) {
314 $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
315 $ret .= "\$cfg['$k'] = " . var_export($this->getDefault($k), true) . ';' . $crlf;