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 original PMA_Config object, not modified by user preferences
27 private $_orgCfgObject;
30 * Stores allowed values for non-standard fields
36 * Keys which will be always written to config file
39 private $_persistKeys = array();
42 * Changes keys while updating config in {@link updateWithGlobalConfig()}
43 * or reading by {@link getConfig()} or {@link getConfigArray()}
46 private $_cfgUpdateReadMapping = array();
49 * Key filter for {@link set()}
55 * Instance id (key in $_SESSION array, separate for each server -
56 * ConfigFile{server id})
62 * Result for {@link _flattenArray()}
65 private $_flattenArrayResult;
71 private static $_instance;
74 * Private constructor, use {@link getInstance()}
77 private function __construct()
79 // load default config values
81 include './libraries/config.default.php';
82 $cfg['fontsize'] = '82%';
84 // create PMA_Config to read config.inc.php values
85 $this->_orgCfgObject
= new PMA_Config(CONFIG_FILE
);
87 // load additional config information
88 $cfg_db = &$this->_cfgDb
;
89 include './libraries/config.values.php';
91 // apply default values overrides
92 if (count($cfg_db['_overrides'])) {
93 foreach ($cfg_db['_overrides'] as $path => $value) {
94 PMA_arrayWrite($path, $cfg, $value);
98 $this->_id
= 'ConfigFile' . $GLOBALS['server'];
99 if (!isset($_SESSION[$this->_id
])) {
100 $_SESSION[$this->_id
] = array();
105 * Returns class instance
109 public static function getInstance()
111 if (is_null(self
::$_instance)) {
112 self
::$_instance = new ConfigFile();
114 return self
::$_instance;
118 * Returns PMA_Config without user preferences applied
122 public function getOrgConfigObj()
124 return $this->_orgCfgObject
;
128 * Sets names of config options which will be placed in config file even if
129 * they are set to their default values (use only full paths)
135 public function setPersistKeys($keys)
137 // checking key presence is much faster than searching so move values
139 $this->_persistKeys
= array_flip($keys);
143 * Returns flipped array set by {@link setPersistKeys()}
147 public function getPersistKeysMap()
149 return $this->_persistKeys
;
153 * By default ConfigFile allows setting of all configuration keys, use
154 * this method to set up a filter on {@link set()} method
156 * @param array|null $keys array of allowed keys or null to remove filter
160 public function setAllowedKeys($keys)
162 if ($keys === null) {
163 $this->_setFilter
= null;
166 // checking key presence is much faster than searching so move values
168 $this->_setFilter
= array_flip($keys);
172 * Sets path mapping for updating config in
173 * {@link updateWithGlobalConfig()} or reading
174 * by {@link getConfig()} or {@link getConfigArray()}
176 * @param array $mapping
180 public function setCfgUpdateReadMapping(array $mapping)
182 $this->_cfgUpdateReadMapping
= $mapping;
186 * Resets configuration data
190 public function resetConfigData()
192 $_SESSION[$this->_id
] = array();
196 * Sets configuration data (overrides old data)
202 public function setConfigData(array $cfg)
204 $_SESSION[$this->_id
] = $cfg;
210 * @param string $path
211 * @param mixed $value
212 * @param string $canonical_path
216 public function set($path, $value, $canonical_path = null)
218 if ($canonical_path === null) {
219 $canonical_path = $this->getCanonicalPath($path);
221 // apply key whitelist
222 if ($this->_setFilter
!== null
223 && ! isset($this->_setFilter
[$canonical_path])
227 // remove if the path isn't protected and it's empty or has a default
229 if (!isset($this->_persistKeys
[$canonical_path])) {
230 $default_value = $this->getDefault($canonical_path);
231 // we need original config values not overwritten by user
232 // preferences to allow for overwriting options set in
233 // config.inc.php with default values
234 $instance_default_value = PMA_arrayRead(
236 $this->_orgCfgObject
->settings
238 if (($value === $default_value && (defined('PMA_SETUP')
239 ||
$instance_default_value === $default_value))
240 ||
(empty($value) && empty($default_value) && (defined('PMA_SETUP')))
242 PMA_arrayRemove($path, $_SESSION[$this->_id
]);
246 PMA_arrayWrite($path, $_SESSION[$this->_id
], $value);
250 * Flattens multidimensional array, changes indices to paths
251 * (eg. 'key/subkey').
252 * Used as array_walk() callback.
254 * @param mixed $value
256 * @param mixed $prefix
260 private function _flattenArray($value, $key, $prefix)
262 // no recursion for numeric arrays
263 if (is_array($value) && !isset($value[0])) {
264 $prefix .= $key . '/';
265 array_walk($value, array($this, '_flattenArray'), $prefix);
267 $this->_flattenArrayResult
[$prefix . $key] = $value;
272 * Returns default config in a flattened array
276 public function getFlatDefaultConfig()
278 $this->_flattenArrayResult
= array();
279 array_walk($this->_cfg
, array($this, '_flattenArray'), '');
280 $flat_cfg = $this->_flattenArrayResult
;
281 $this->_flattenArrayResult
= null;
286 * Updates config with values read from given array
287 * (config will contain differences to defaults from config.defaults.php).
293 public function updateWithGlobalConfig(array $cfg)
295 // load config array and flatten it
296 $this->_flattenArrayResult
= array();
297 array_walk($cfg, array($this, '_flattenArray'), '');
298 $flat_cfg = $this->_flattenArrayResult
;
299 $this->_flattenArrayResult
= null;
301 // save values map for translating a few user preferences paths,
302 // should be complemented by code reading from generated config
303 // to perform inverse mapping
304 foreach ($flat_cfg as $path => $value) {
305 if (isset($this->_cfgUpdateReadMapping
[$path])) {
306 $path = $this->_cfgUpdateReadMapping
[$path];
308 $this->set($path, $value, $path);
313 * Returns config value or $default if it's not set
315 * @param string $path
316 * @param mixed $default
320 public function get($path, $default = null)
322 return PMA_arrayRead($path, $_SESSION[$this->_id
], $default);
326 * Returns default config value or $default it it's not set ie. it doesn't
327 * exist in config.default.php ($cfg) and config.values.php
328 * ($_cfg_db['_overrides'])
330 * @param string $canonical_path
331 * @param mixed $default
335 public function getDefault($canonical_path, $default = null)
337 return PMA_arrayRead($canonical_path, $this->_cfg
, $default);
341 * Returns config value, if it's not set uses the default one; returns
342 * $default if the path isn't set and doesn't contain a default value
344 * @param string $path
345 * @param mixed $default
349 public function getValue($path, $default = null)
351 $v = PMA_arrayRead($path, $_SESSION[$this->_id
], null);
355 $path = $this->getCanonicalPath($path);
356 return $this->getDefault($path, $default);
360 * Returns canonical path
362 * @param string $path
366 public function getCanonicalPath($path)
368 return preg_replace('#^Servers/([\d]+)/#', 'Servers/1/', $path);
372 * Returns config database entry for $path ($cfg_db in config_info.php)
374 * @param string $path
375 * @param mixed $default
379 public function getDbEntry($path, $default = null)
381 return PMA_arrayRead($path, $this->_cfgDb
, $default);
385 * Returns server count
389 public function getServerCount()
391 return isset($_SESSION[$this->_id
]['Servers'])
392 ?
count($_SESSION[$this->_id
]['Servers'])
397 * Returns server list
401 public function getServers()
403 return isset($_SESSION[$this->_id
]['Servers'])
404 ?
$_SESSION[$this->_id
]['Servers']
409 * Returns DSN of given server
411 * @param integer $server
415 function getServerDSN($server)
417 if (!isset($_SESSION[$this->_id
]['Servers'][$server])) {
421 $path = 'Servers/' . $server;
422 $dsn = $this->getValue("$path/extension") . '://';
423 if ($this->getValue("$path/auth_type") == 'config') {
424 $dsn .= $this->getValue("$path/user");
425 if (!$this->getValue("$path/nopassword")) {
430 if ($this->getValue("$path/connect_type") == 'tcp') {
431 $dsn .= $this->getValue("$path/host");
432 $port = $this->getValue("$path/port");
437 $dsn .= $this->getValue("$path/socket");
443 * Returns server name
449 public function getServerName($id)
451 if (!isset($_SESSION[$this->_id
]['Servers'][$id])) {
454 $verbose = $this->get("Servers/$id/verbose");
455 if (!empty($verbose)) {
458 $host = $this->get("Servers/$id/host");
459 return empty($host) ?
'localhost' : $host;
469 public function removeServer($server)
471 if (!isset($_SESSION[$this->_id
]['Servers'][$server])) {
474 $last_server = $this->getServerCount();
476 for ($i = $server; $i < $last_server; $i++
) {
477 $_SESSION[$this->_id
]['Servers'][$i]
478 = $_SESSION[$this->_id
]['Servers'][$i +
1];
480 unset($_SESSION[$this->_id
]['Servers'][$last_server]);
482 if (isset($_SESSION[$this->_id
]['ServerDefault'])
483 && $_SESSION[$this->_id
]['ServerDefault'] >= 0
485 unset($_SESSION[$this->_id
]['ServerDefault']);
490 * Returns config file path, relative to phpMyAdmin's root path
494 public function getFilePath()
497 if (!defined('SETUP_CONFIG_FILE')) {
498 include_once './libraries/vendor_config.php';
501 return SETUP_CONFIG_FILE
;
505 * Returns configuration array (full, multidimensional format)
509 public function getConfig()
511 $c = $_SESSION[$this->_id
];
512 foreach ($this->_cfgUpdateReadMapping
as $map_to => $map_from) {
513 PMA_arrayWrite($map_to, $c, PMA_arrayRead($map_from, $c));
514 PMA_arrayRemove($map_from, $c);
520 * Returns configuration array (flat format)
524 public function getConfigArray()
526 $this->_flattenArrayResult
= array();
527 array_walk($_SESSION[$this->_id
], array($this, '_flattenArray'), '');
528 $c = $this->_flattenArrayResult
;
529 $this->_flattenArrayResult
= null;
531 $persistKeys = array_diff(
532 array_keys($this->_persistKeys
),
535 foreach ($persistKeys as $k) {
536 $c[$k] = $this->getDefault($k);
539 foreach ($this->_cfgUpdateReadMapping
as $map_to => $map_from) {
540 if (!isset($c[$map_from])) {
543 $c[$map_to] = $c[$map_from];
544 unset($c[$map_from]);