Translation update done using Pootle.
[phpmyadmin/dkf.git] / libraries / config / ConfigFile.class.php
blob7a15a89c21593e836160cc9e6206a11954d1d88d
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Config file management
6 * @package phpMyAdmin
7 */
9 /**
10 * Config file management class.
11 * Stores its data in $_SESSION
13 * @package phpMyAdmin
15 class ConfigFile
17 /**
18 * Stores default PMA config from config.default.php
19 * @var array
21 private $cfg;
23 /**
24 * Stores allowed values for non-standard fields
25 * @var array
27 private $cfgDb;
29 /**
30 * Keys which will be always written to config file
31 * @var array
33 private $persistKeys = array();
35 /**
36 * Changes keys while updating config in {@link updateWithGlobalConfig()} or reading
37 * by {@link getConfig()} or {@link getConfigArray()}
38 * @var array
40 private $cfgUpdateReadMapping = array();
42 /**
43 * Key filter for {@link set()}
44 * @var array|null
46 private $setFilter;
48 /**
49 * Instance id (key in $_SESSION array, separate for each server - ConfigFile{server id})
50 * @var string
52 private $id;
54 /**
55 * Result for {@link _flattenArray()}
56 * @var array
58 private $_flattenArrayResult;
60 /**
61 * ConfigFile instance
62 * @var ConfigFile
64 private static $_instance;
66 /**
67 * Private constructor, use {@link getInstance()}
69 * @uses PMA_array_write()
71 private function __construct()
73 // load default config values
74 $cfg = &$this->cfg;
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();
95 /**
96 * Returns class instance
98 * @return ConfigFile
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)
112 * @param array $keys
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()}
123 * @return array
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;
140 return;
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()}
149 * @var array
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)
167 * @param array $cfg
169 public function setConfigData(array $cfg)
171 $_SESSION[$this->id] = $cfg;
175 * Sets config value
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])) {
192 return;
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]);
202 return;
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
213 * @param mixed $key
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);
222 } else {
223 $this->_flattenArrayResult[$prefix . $key] = $value;
228 * Returns default config in a flattened array
230 * @return 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;
238 return $flat_cfg;
242 * Updates config with values read from given array
243 * (config will contain differences to defaults from config.defaults.php).
245 * @param array $cfg
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;
255 // save values
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
272 * @return mixed
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
287 * @return mixed
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
301 * @return mixed
303 public function getValue($path, $default = null)
305 $v = PMA_array_read($path, $_SESSION[$this->id], null);
306 if ($v !== null) {
307 return $v;
309 $path = $this->getCanonicalPath($path);
310 return $this->getDefault($path, $default);
314 * Returns canonical path
316 * @param string $path
317 * @return string
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
329 * @return mixed
331 public function getDbEntry($path, $default = null)
333 return PMA_array_read($path, $this->cfgDb, $default);
337 * Returns server count
339 * @return int
341 public function getServerCount()
343 return isset($_SESSION[$this->id]['Servers'])
344 ? count($_SESSION[$this->id]['Servers'])
345 : 0;
349 * Returns server list
351 * @return array|null
353 public function getServers()
355 return isset($_SESSION[$this->id]['Servers'])
356 ? $_SESSION[$this->id]['Servers']
357 : null;
361 * Returns DSN of given server
363 * @param integer $server
364 * @return string
366 function getServerDSN($server)
368 if (!isset($_SESSION[$this->id]['Servers'][$server])) {
369 return '';
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")) {
377 $dsn .= ':***';
379 $dsn .= '@';
381 if ($this->getValue("$path/connect_type") == 'tcp') {
382 $dsn .= $this->getValue("$path/host");
383 $port = $this->getValue("$path/port");
384 if ($port) {
385 $dsn .= ':' . $port;
387 } else {
388 $dsn .= $this->getValue("$path/socket");
390 return $dsn;
394 * Returns server name
396 * @param int $id
397 * @return string
399 public function getServerName($id)
401 if (!isset($_SESSION[$this->id]['Servers'][$id])) {
402 return '';
404 $verbose = $this->get("Servers/$id/verbose");
405 if (!empty($verbose)) {
406 return $verbose;
408 $host = $this->get("Servers/$id/host");
409 return empty($host) ? 'localhost' : $host;
413 * Removes server
415 * @param int $server
417 public function removeServer($server)
419 if (!isset($_SESSION[$this->id]['Servers'][$server])) {
420 return;
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
438 * @return string
440 public function getFilePath()
442 // Load paths
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)
453 * @return array
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);
462 return $c;
466 * Returns configuration array (flat format)
468 * @return array
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])) {
484 continue;
486 $c[$map_to] = $c[$map_from];
487 unset($c[$map_from]);
489 return $c;