Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / config / ConfigFile.class.php
blob238f0b0817464701a99f26d645d5da1a2038a183
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 original PMA_Config object, not modified by user preferences
25 * @var PMA_Config
27 private $_orgCfgObject;
29 /**
30 * Stores allowed values for non-standard fields
31 * @var array
33 private $_cfgDb;
35 /**
36 * Keys which will be always written to config file
37 * @var array
39 private $_persistKeys = array();
41 /**
42 * Changes keys while updating config in {@link updateWithGlobalConfig()}
43 * or reading by {@link getConfig()} or {@link getConfigArray()}
44 * @var array
46 private $_cfgUpdateReadMapping = array();
48 /**
49 * Key filter for {@link set()}
50 * @var array|null
52 private $_setFilter;
54 /**
55 * Instance id (key in $_SESSION array, separate for each server -
56 * ConfigFile{server id})
57 * @var string
59 private $_id;
61 /**
62 * Result for {@link _flattenArray()}
63 * @var array
65 private $_flattenArrayResult;
67 /**
68 * ConfigFile instance
69 * @var ConfigFile
71 private static $_instance;
73 /**
74 * Private constructor, use {@link getInstance()}
77 private function __construct()
79 // load default config values
80 $cfg = &$this->_cfg;
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
107 * @return ConfigFile
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
120 * @return PMA_Config
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)
131 * @param array $keys
133 * @return void
135 public function setPersistKeys($keys)
137 // checking key presence is much faster than searching so move values
138 // to keys
139 $this->_persistKeys = array_flip($keys);
143 * Returns flipped array set by {@link setPersistKeys()}
145 * @return array
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
158 * @return void
160 public function setAllowedKeys($keys)
162 if ($keys === null) {
163 $this->_setFilter = null;
164 return;
166 // checking key presence is much faster than searching so move values
167 // to keys
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
178 * @return void
180 public function setCfgUpdateReadMapping(array $mapping)
182 $this->_cfgUpdateReadMapping = $mapping;
186 * Resets configuration data
188 * @return void
190 public function resetConfigData()
192 $_SESSION[$this->_id] = array();
196 * Sets configuration data (overrides old data)
198 * @param array $cfg
200 * @return void
202 public function setConfigData(array $cfg)
204 $_SESSION[$this->_id] = $cfg;
208 * Sets config value
210 * @param string $path
211 * @param mixed $value
212 * @param string $canonical_path
214 * @return void
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])
225 return;
227 // remove if the path isn't protected and it's empty or has a default
228 // value
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(
235 $canonical_path,
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]);
243 return;
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
255 * @param mixed $key
256 * @param mixed $prefix
258 * @return void
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);
266 } else {
267 $this->_flattenArrayResult[$prefix . $key] = $value;
272 * Returns default config in a flattened array
274 * @return 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;
282 return $flat_cfg;
286 * Updates config with values read from given array
287 * (config will contain differences to defaults from config.defaults.php).
289 * @param array $cfg
291 * @return void
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
318 * @return mixed
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
333 * @return mixed
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
347 * @return mixed
349 public function getValue($path, $default = null)
351 $v = PMA_arrayRead($path, $_SESSION[$this->_id], null);
352 if ($v !== null) {
353 return $v;
355 $path = $this->getCanonicalPath($path);
356 return $this->getDefault($path, $default);
360 * Returns canonical path
362 * @param string $path
364 * @return string
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
377 * @return mixed
379 public function getDbEntry($path, $default = null)
381 return PMA_arrayRead($path, $this->_cfgDb, $default);
385 * Returns server count
387 * @return int
389 public function getServerCount()
391 return isset($_SESSION[$this->_id]['Servers'])
392 ? count($_SESSION[$this->_id]['Servers'])
393 : 0;
397 * Returns server list
399 * @return array|null
401 public function getServers()
403 return isset($_SESSION[$this->_id]['Servers'])
404 ? $_SESSION[$this->_id]['Servers']
405 : null;
409 * Returns DSN of given server
411 * @param integer $server
413 * @return string
415 function getServerDSN($server)
417 if (!isset($_SESSION[$this->_id]['Servers'][$server])) {
418 return '';
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")) {
426 $dsn .= ':***';
428 $dsn .= '@';
430 if ($this->getValue("$path/connect_type") == 'tcp') {
431 $dsn .= $this->getValue("$path/host");
432 $port = $this->getValue("$path/port");
433 if ($port) {
434 $dsn .= ':' . $port;
436 } else {
437 $dsn .= $this->getValue("$path/socket");
439 return $dsn;
443 * Returns server name
445 * @param int $id
447 * @return string
449 public function getServerName($id)
451 if (!isset($_SESSION[$this->_id]['Servers'][$id])) {
452 return '';
454 $verbose = $this->get("Servers/$id/verbose");
455 if (!empty($verbose)) {
456 return $verbose;
458 $host = $this->get("Servers/$id/host");
459 return empty($host) ? 'localhost' : $host;
463 * Removes server
465 * @param int $server
467 * @return void
469 public function removeServer($server)
471 if (!isset($_SESSION[$this->_id]['Servers'][$server])) {
472 return;
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
492 * @return string
494 public function getFilePath()
496 // Load paths
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)
507 * @return array
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);
516 return $c;
520 * Returns configuration array (flat format)
522 * @return array
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),
533 array_keys($c)
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])) {
541 continue;
543 $c[$map_to] = $c[$map_from];
544 unset($c[$map_from]);
546 return $c;