3 * Config file management and generation
5 * @license http://www.gnu.org/licenses/gpl.html GNU GPL 2.0
7 * @package phpMyAdmin-setup
11 * Config file management and generation class
13 * @package phpMyAdmin-setup
18 * Stores default PMA config from config.default.php
24 * Stores allowed values for non-standard fields
30 * Keys which will be always written to config file
39 private static $_instance;
42 * Private constructor, use {@link getInstance()}
44 private function __construct()
46 // load default config values
48 require './libraries/config.default.php';
50 // load additionsl config information
51 $cfg_db = &$this->cfgDb
;
52 $persist_keys = array();
53 require './setup/lib/config_info.inc.php';
55 // apply default values overrides
56 if (count($cfg_db['_overrides'])) {
57 foreach ($cfg_db['_overrides'] as $path => $value) {
58 array_write($path, $cfg, $value);
62 // checking key presence is much faster than searching so move values to keys
63 $this->persistKeys
= array_flip($persist_keys);
67 * Returns class instance
71 public static function getInstance()
73 if (is_null(self
::$_instance)) {
74 self
::$_instance = new ConfigFile();
76 return self
::$_instance;
84 * @param string $canonical_path
86 public function set($path, $value, $canonical_path = null)
88 if ($canonical_path === null) {
89 $canonical_path = $this->getCanonicalPath($path);
91 // remove if the path isn't protected and it's empty or has a default value
92 $default_value = $this->getDefault($canonical_path);
93 if (!isset($this->persistKeys
[$canonical_path])
94 && (($value == $default_value) ||
(empty($value) && empty($default_value)))) {
95 array_remove($path, $_SESSION['ConfigFile']);
97 array_write($path, $_SESSION['ConfigFile'], $value);
102 * Returns config value or $default if it's not set
104 * @param string $path
105 * @param mixed $default
108 public function get($path, $default = null)
110 return array_read($path, $_SESSION['ConfigFile'], $default);
114 * Returns default config value or $default it it's not set ie. it doesn't
115 * exist in config.default.php ($cfg) and config_info.inc.php
116 * ($_cfg_db['_overrides'])
118 * @param string $canonical_path
119 * @param mixed $default
122 public function getDefault($canonical_path, $default = null)
124 return array_read($canonical_path, $this->cfg
, $default);
128 * Returns config value, if it's not set uses the default one; returns
129 * $default if the path isn't set and doesn't contain a default value
131 * @param string $path
132 * @param mixed $default
135 public function getValue($path, $default = null)
137 $v = array_read($path, $_SESSION['ConfigFile'], null);
141 $path = $this->getCanonicalPath($path);
142 return $this->getDefault($path, $default);
146 * Returns canonical path
148 * @param string $path
151 public function getCanonicalPath($path) {
152 return preg_replace('#^Servers/([\d]+)/#', 'Servers/1/', $path);
156 * Returns config database entry for $path ($cfg_db in config_info.php)
158 * @param string $path
159 * @param mixed $default
162 public function getDbEntry($path, $default = null)
164 return array_read($path, $this->cfgDb
, $default);
168 * Returns server count
172 public function getServerCount()
174 return isset($_SESSION['ConfigFile']['Servers'])
175 ?
count($_SESSION['ConfigFile']['Servers'])
180 * Returns DSN of given server
182 * @param integer $server
185 function getServerDSN($server)
187 if (!isset($_SESSION['ConfigFile']['Servers'][$server])) {
191 $path = 'Servers/' . $server;
192 $dsn = $this->getValue("$path/extension") . '://';
193 if ($this->getValue("$path/auth_type") == 'config') {
194 $dsn .= $this->getValue("$path/user");
195 if (!$this->getValue("$path/nopassword")) {
200 if ($this->getValue("$path/connect_type") == 'tcp') {
201 $dsn .= $this->getValue("$path/host");
202 $port = $this->getValue("$path/port");
207 $dsn .= $this->getValue("$path/socket");
213 * Returns server name
218 public function getServerName($id)
220 if (!isset($_SESSION['ConfigFile']['Servers'][$id])) {
223 $verbose = $this->get("Servers/$id/verbose");
224 if (!empty($verbose)) {
227 $host = $this->get("Servers/$id/host");
228 return empty($host) ?
'localhost' : $host;
236 public function removeServer($server)
238 if (!isset($_SESSION['ConfigFile']['Servers'][$server])) {
241 $last_server = $this->getServerCount();
243 for ($i = $server; $i < $last_server; $i++
) {
244 $_SESSION['ConfigFile']['Servers'][$i] = $_SESSION['ConfigFile']['Servers'][$i+
1];
246 unset($_SESSION['ConfigFile']['Servers'][$last_server]);
248 if (isset($_SESSION['ConfigFile']['ServerDefault'])
249 && $_SESSION['ConfigFile']['ServerDefault'] >= 0) {
250 unset($_SESSION['ConfigFile']['ServerDefault']);
255 * Returns config file path
259 public function getFilePath()
261 return $this->getDbEntry('_config_file_path');
265 * Creates config file
269 public function getConfigFile()
271 $crlf = (isset($_SESSION['eol']) && $_SESSION['eol'] == 'win') ?
"\r\n" : "\n";
272 $c = $_SESSION['ConfigFile'];
275 $ret = '<?php' . $crlf
277 . ' * Generated configuration file' . $crlf
278 . ' * Generated by: phpMyAdmin '
279 . $GLOBALS['PMA_Config']->get('PMA_VERSION')
280 . ' setup script by Piotr Przybylski <piotrprz@gmail.com>' . $crlf
281 . ' * Date: ' . date(DATE_RFC1123
) . $crlf
282 . ' */' . $crlf . $crlf;
285 if ($this->getServerCount() > 0) {
286 $ret .= "/* Servers configuration */$crlf\$i = 0;" . $crlf . $crlf;
287 foreach ($c['Servers'] as $id => $server) {
288 $ret .= '/* Server: ' . strtr($this->getServerName($id), '*/', '-') . " [$id] */" . $crlf
290 foreach ($server as $k => $v) {
291 $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
292 $ret .= "\$cfg['Servers'][\$i]['$k'] = "
293 . var_export($v, true) . ';' . $crlf;
297 $ret .= '/* End of servers configuration */' . $crlf . $crlf;
299 unset($c['Servers']);
302 $persistKeys = $this->persistKeys
;
303 foreach ($c as $k => $v) {
304 $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
305 $ret .= "\$cfg['$k'] = " . var_export($v, true) . ';' . $crlf;
306 if (isset($persistKeys[$k])) {
307 unset($persistKeys[$k]);
310 // keep 1d array keys which are present in $persist_keys (config_info.inc.php)
311 foreach (array_keys($persistKeys) as $k) {
312 if (strpos($k, '/') === false) {
313 $k = preg_replace('/[^A-Za-z0-9_]/', '_', $k);
314 $ret .= "\$cfg['$k'] = " . var_export($this->getDefault($k), true) . ';' . $crlf;