Skip to content

Commit

Permalink
allow users to set only_db and hide_db
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrp committed Aug 1, 2010
1 parent 30413de commit 9a73307
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 9 deletions.
45 changes: 40 additions & 5 deletions libraries/config/ConfigFile.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ class ConfigFile
*/
private $persistKeys = array();

/**
* Changes keys while updating config in {@link updateWithGlobalConfig()} or reading
* by {@link getConfig()} or {@link getConfigArray()}
* @var array
*/
private $cfgUpdateReadMapping = array();

/**
* Key filter for {@link set()}
* @var array|null
Expand Down Expand Up @@ -136,6 +143,16 @@ public function setAllowedKeys($keys)
$this->setFilter = array_flip($keys);
}

/**
* Sets path mapping for updating config in {@link updateWithGlobalConfig()} or reading
* by {@link getConfig()} or {@link getConfigArray()}
* @var array
*/
public function setCfgUpdateReadMapping(array $mapping)
{
$this->cfgUpdateReadMapping = $mapping;
}

/**
* Resets configuration data
*/
Expand Down Expand Up @@ -216,21 +233,26 @@ public function getFlatDefaultConfig()
}

/**
* Updates config with values read from PMA_Config class
* Updates config with values read from given array
* (config will contain differences to defaults from config.defaults.php).
*
* @param PMA_Config $PMA_Config
* @param array $cfg
*/
public function updateWithGlobalConfig(PMA_Config $PMA_Config)
public function updateWithGlobalConfig(array $cfg)
{
// load config array and flatten it
$this->_flattenArrayResult = array();
array_walk($PMA_Config->settings, array($this, '_flattenArray'), '');
array_walk($cfg, array($this, '_flattenArray'), '');
$flat_cfg = $this->_flattenArrayResult;
$this->_flattenArrayResult = null;

// save values
// map for translating a few user preferences paths, should be complemented
// by code reading from generated config to perform inverse mapping
foreach ($flat_cfg as $path => $value) {
if (isset($this->cfgUpdateReadMapping[$path])) {
$path = $this->cfgUpdateReadMapping[$path];
}
$this->set($path, $value, $path);
}
}
Expand Down Expand Up @@ -426,7 +448,12 @@ public function getFilePath()
*/
public function getConfig()
{
return $_SESSION[$this->id];
$c = $_SESSION[$this->id];
foreach ($this->cfgUpdateReadMapping as $map_to => $map_from) {
PMA_array_write($map_to, $c, PMA_array_read($map_from, $c));
PMA_array_remove($map_from, $c);
}
return $c;
}

/**
Expand All @@ -445,6 +472,14 @@ public function getConfigArray()
foreach ($persistKeys as $k) {
$c[$k] = $this->getDefault($k);
}

foreach ($this->cfgUpdateReadMapping as $map_to => $map_from) {
if (!isset($c[$map_from])) {
continue;
}
$c[$map_to] = $c[$map_from];
unset($c[$map_from]);
}
return $c;
}
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/config/messages.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@
$strConfigServers_LogoutURL_name = __('Logout URL');
$strConfigServers_nopassword_desc = __('Try to connect without password');
$strConfigServers_nopassword_name = __('Connect without password');
$strConfigServers_only_db_desc = __('You can use MySQL wildcard characters (% and _), escape them if you want to use their literal instances, i.e. use \'my\_db\' and not \'my_db\'');
$strConfigServers_only_db_desc = __('You can use MySQL wildcard characters (% and _), escape them if you want to use their literal instances, i.e. use [kbd]\'my\_db\'[/kbd] and not [kbd]\'my_db\'[/kbd]. Using this option you can sort database list, just enter their names in order and use [kbd]*[/kbd] at the end to show the rest in alphabetical order.');
$strConfigServers_only_db_name = __('Show only listed databases');
$strConfigServers_password_desc = __('Leave empty if not using config auth');
$strConfigServers_password_name = __('Password for config auth');
Expand Down
2 changes: 2 additions & 0 deletions libraries/config/user_preferences.forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
'InitialSlidersState',
'ErrorIconic',
'ReplaceHelpImg',
'Servers/1/only_db', // saves to Server/only_db
'Servers/1/hide_db', // saves to Server/hide_db
'SkipLockedTables',
'MaxDbList',
'MaxTableList');
Expand Down
5 changes: 4 additions & 1 deletion libraries/user_preferences.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@
$cf = ConfigFile::getInstance();
$cf->resetConfigData(); // start with a clean instance
$cf->setAllowedKeys($forms_all_keys);
$cf->updateWithGlobalConfig($GLOBALS['PMA_Config']);
$cf->setCfgUpdateReadMapping(array(
'Server/hide_db' => 'Servers/1/hide_db',
'Server/only_db' => 'Servers/1/only_db'));
$cf->updateWithGlobalConfig($GLOBALS['cfg']);

// todo: debug - remove
$arr = $cf->getConfigArray();
Expand Down
5 changes: 4 additions & 1 deletion libraries/user_preferences.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ function PMA_apply_userprefs(array $config_data)
$whitelist['fontsize'] = true;
$whitelist['lang'] = true;
$whitelist['collation_connection'] = true;
$whitelist['Server/hide_db'] = true;
$whitelist['Server/only_db'] = true;
foreach ($config_data as $path => $value) {
if (!isset($whitelist[$path]) || isset($blacklist[$path])) {
continue;
Expand Down Expand Up @@ -230,7 +232,8 @@ function PMA_userprefs_redirect(array $forms, array $old_settings, $file_name, $
$new_settings = ConfigFile::getInstance()->getConfigArray();
$diff_keys = array_keys(array_diff_assoc($old_settings, $new_settings)
+ array_diff_assoc($new_settings, $old_settings));
$check_keys = array('NaturalOrder', 'MainPageIconic', 'DefaultTabDatabase');
$check_keys = array('NaturalOrder', 'MainPageIconic', 'DefaultTabDatabase',
'Server/hide_db', 'Server/only_db');
$check_keys = array_merge($check_keys, $forms['Left_frame']['Left_frame'],
$forms['Left_frame']['Left_databases']);
$diff = array_intersect($check_keys, $diff_keys);
Expand Down
2 changes: 1 addition & 1 deletion prefs_forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
if ($form_name == 'Developer' && !$GLOBALS['cfg']['UserprefsDeveloperTab']) {
continue;
}
$form_display->registerForm($form_name, $form);
$form_display->registerForm($form_name, $form, 1);
}

if (isset($_POST['revert'])) {
Expand Down

0 comments on commit 9a73307

Please sign in to comment.