2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Config file management
10 * Config file management class.
11 * Stores its data in $_SESSION
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
33 private $persistKeys = array();
36 * Changes keys while updating config in {@link updateWithGlobalConfig()} or reading
37 * by {@link getConfig()} or {@link getConfigArray()}
40 private $cfgUpdateReadMapping = array();
43 * Key filter for {@link set()}
49 * Instance id (key in $_SESSION array, separate for each server - ConfigFile{server id})
55 * Result for {@link _flattenArray()}
58 private $_flattenArrayResult;
64 private static $_instance;
67 * Private constructor, use {@link getInstance()}
69 * @uses PMA_array_write()
71 private function __construct()
73 // load default config values
75 require './libraries/config.default.php';
76 $cfg['fontsize'] = '82%';
78 // load additional config information
79 $cfg_db = &$this->cfgDb
;
80 require './libraries/config.values.php';
82 // apply default values overrides
83 if (count($cfg_db['_overrides'])) {
84 foreach ($cfg_db['_overrides'] as $path => $value) {
85 PMA_array_write($path, $cfg, $value);
89 $this->id
= 'ConfigFile' . $GLOBALS['server'];
90 if (!isset($_SESSION[$this->id
])) {
91 $_SESSION[$this->id
] = array();
96 * Returns class instance
100 public static function getInstance()
102 if (is_null(self
::$_instance)) {
103 self
::$_instance = new ConfigFile();
105 return self
::$_instance;
109 * Sets names of config options which will be placed in config file even if they are set
110 * to their default values (use only full paths)
114 public function setPersistKeys($keys)
116 // checking key presence is much faster than searching so move values to keys
117 $this->persistKeys
= array_flip($keys);
121 * Returns flipped array set by {@link setPersistKeys()}
125 public function getPersistKeysMap()
127 return $this->persistKeys
;
131 * By default ConfigFile allows setting of all configuration keys, use this method
132 * to set up a filter on {@link set()} method
134 * @param array|null $keys array of allowed keys or null to remove filter
136 public function setAllowedKeys($keys)
138 if ($keys === null) {
139 $this->setFilter
= null;
142 // checking key presence is much faster than searching so move values to keys
143 $this->setFilter
= array_flip($keys);
147 * Sets path mapping for updating config in {@link updateWithGlobalConfig()} or reading
148 * by {@link getConfig()} or {@link getConfigArray()}
151 public function setCfgUpdateReadMapping(array $mapping)
153 $this->cfgUpdateReadMapping
= $mapping;
157 * Resets configuration data
159 public function resetConfigData()
161 $_SESSION[$this->id
] = array();
165 * Sets configuration data (overrides old data)
169 public function setConfigData(array $cfg)
171 $_SESSION[$this->id
] = $cfg;
177 * @uses $GLOBALS['cfg']
178 * @uses PMA_array_read()
179 * @uses PMA_array_remove()
180 * @uses PMA_array_write()
181 * @param string $path
182 * @param mixed $value
183 * @param string $canonical_path
185 public function set($path, $value, $canonical_path = null)
187 if ($canonical_path === null) {
188 $canonical_path = $this->getCanonicalPath($path);
190 // apply key whitelist
191 if ($this->setFilter
!== null && !isset($this->setFilter
[$canonical_path])) {
194 // remove if the path isn't protected and it's empty or has a default value
195 if (!isset($this->persistKeys
[$canonical_path])) {
196 $default_value = $this->getDefault($canonical_path);
197 // check $GLOBALS['cfg'] to allow overwriting options set in config.inc.php with default value
198 $current_global = PMA_array_read($canonical_path, $GLOBALS['cfg']);
199 if (($value === $default_value && (defined('PMA_SETUP') ||
$current_global === $default_value))
200 ||
(empty($value) && empty($default_value) && (defined('PMA_SETUP') ||
empty($current_global)))) {
201 PMA_array_remove($path, $_SESSION[$this->id
]);
205 PMA_array_write($path, $_SESSION[$this->id
], $value);
209 * Flattens multidimensional array, changes indices to paths (eg. 'key/subkey').
210 * Used as array_walk() callback.
212 * @param mixed $value
214 * @param mixed $prefix
216 private function _flattenArray($value, $key, $prefix)
218 // no recursion for numeric arrays
219 if (is_array($value) && !isset($value[0])) {
220 $prefix .= $key . '/';
221 array_walk($value, array($this, '_flattenArray'), $prefix);
223 $this->_flattenArrayResult
[$prefix . $key] = $value;
228 * Returns default config in a flattened array
232 public function getFlatDefaultConfig()
234 $this->_flattenArrayResult
= array();
235 array_walk($this->cfg
, array($this, '_flattenArray'), '');
236 $flat_cfg = $this->_flattenArrayResult
;
237 $this->_flattenArrayResult
= null;
242 * Updates config with values read from given array
243 * (config will contain differences to defaults from config.defaults.php).
247 public function updateWithGlobalConfig(array $cfg)
249 // load config array and flatten it
250 $this->_flattenArrayResult
= array();
251 array_walk($cfg, array($this, '_flattenArray'), '');
252 $flat_cfg = $this->_flattenArrayResult
;
253 $this->_flattenArrayResult
= null;
256 // map for translating a few user preferences paths, should be complemented
257 // by code reading from generated config to perform inverse mapping
258 foreach ($flat_cfg as $path => $value) {
259 if (isset($this->cfgUpdateReadMapping
[$path])) {
260 $path = $this->cfgUpdateReadMapping
[$path];
262 $this->set($path, $value, $path);
267 * Returns config value or $default if it's not set
269 * @uses PMA_array_read()
270 * @param string $path
271 * @param mixed $default
274 public function get($path, $default = null)
276 return PMA_array_read($path, $_SESSION[$this->id
], $default);
280 * Returns default config value or $default it it's not set ie. it doesn't
281 * exist in config.default.php ($cfg) and config.values.php
282 * ($_cfg_db['_overrides'])
284 * @uses PMA_array_read()
285 * @param string $canonical_path
286 * @param mixed $default
289 public function getDefault($canonical_path, $default = null)
291 return PMA_array_read($canonical_path, $this->cfg
, $default);
295 * Returns config value, if it's not set uses the default one; returns
296 * $default if the path isn't set and doesn't contain a default value
298 * @uses PMA_array_read()
299 * @param string $path
300 * @param mixed $default
303 public function getValue($path, $default = null)
305 $v = PMA_array_read($path, $_SESSION[$this->id
], null);
309 $path = $this->getCanonicalPath($path);
310 return $this->getDefault($path, $default);
314 * Returns canonical path
316 * @param string $path
319 public function getCanonicalPath($path) {
320 return preg_replace('#^Servers/([\d]+)/#', 'Servers/1/', $path);
324 * Returns config database entry for $path ($cfg_db in config_info.php)
326 * @uses PMA_array_read()
327 * @param string $path
328 * @param mixed $default
331 public function getDbEntry($path, $default = null)
333 return PMA_array_read($path, $this->cfgDb
, $default);
337 * Returns server count
341 public function getServerCount()
343 return isset($_SESSION[$this->id
]['Servers'])
344 ?
count($_SESSION[$this->id
]['Servers'])
349 * Returns server list
353 public function getServers()
355 return isset($_SESSION[$this->id
]['Servers'])
356 ?
$_SESSION[$this->id
]['Servers']
361 * Returns DSN of given server
363 * @param integer $server
366 function getServerDSN($server)
368 if (!isset($_SESSION[$this->id
]['Servers'][$server])) {
372 $path = 'Servers/' . $server;
373 $dsn = $this->getValue("$path/extension") . '://';
374 if ($this->getValue("$path/auth_type") == 'config') {
375 $dsn .= $this->getValue("$path/user");
376 if (!$this->getValue("$path/nopassword")) {
381 if ($this->getValue("$path/connect_type") == 'tcp') {
382 $dsn .= $this->getValue("$path/host");
383 $port = $this->getValue("$path/port");
388 $dsn .= $this->getValue("$path/socket");
394 * Returns server name
399 public function getServerName($id)
401 if (!isset($_SESSION[$this->id
]['Servers'][$id])) {
404 $verbose = $this->get("Servers/$id/verbose");
405 if (!empty($verbose)) {
408 $host = $this->get("Servers/$id/host");
409 return empty($host) ?
'localhost' : $host;
417 public function removeServer($server)
419 if (!isset($_SESSION[$this->id
]['Servers'][$server])) {
422 $last_server = $this->getServerCount();
424 for ($i = $server; $i < $last_server; $i++
) {
425 $_SESSION[$this->id
]['Servers'][$i] = $_SESSION[$this->id
]['Servers'][$i+
1];
427 unset($_SESSION[$this->id
]['Servers'][$last_server]);
429 if (isset($_SESSION[$this->id
]['ServerDefault'])
430 && $_SESSION[$this->id
]['ServerDefault'] >= 0) {
431 unset($_SESSION[$this->id
]['ServerDefault']);
436 * Returns config file path, relative to phpMyAdmin's root path
440 public function getFilePath()
443 if (!defined('SETUP_CONFIG_FILE')) {
444 require_once './libraries/vendor_config.php';
447 return SETUP_CONFIG_FILE
;
451 * Returns configuration array (full, multidimensional format)
455 public function getConfig()
457 $c = $_SESSION[$this->id
];
458 foreach ($this->cfgUpdateReadMapping
as $map_to => $map_from) {
459 PMA_array_write($map_to, $c, PMA_array_read($map_from, $c));
460 PMA_array_remove($map_from, $c);
466 * Returns configuration array (flat format)
470 public function getConfigArray()
472 $this->_flattenArrayResult
= array();
473 array_walk($_SESSION[$this->id
], array($this, '_flattenArray'), '');
474 $c = $this->_flattenArrayResult
;
475 $this->_flattenArrayResult
= null;
477 $persistKeys = array_diff(array_keys($this->persistKeys
), array_keys($c));
478 foreach ($persistKeys as $k) {
479 $c[$k] = $this->getDefault($k);
482 foreach ($this->cfgUpdateReadMapping
as $map_to => $map_from) {
483 if (!isset($c[$map_from])) {
486 $c[$map_to] = $c[$map_from];
487 unset($c[$map_from]);