From 9a733070032a5f00f368c85450e8000fa5e02aca Mon Sep 17 00:00:00 2001 From: Crack Date: Sun, 1 Aug 2010 21:55:04 +0200 Subject: [PATCH] allow users to set only_db and hide_db --- libraries/config/ConfigFile.class.php | 45 ++++++++++++++++++--- libraries/config/messages.inc.php | 2 +- libraries/config/user_preferences.forms.php | 2 + libraries/user_preferences.inc.php | 5 ++- libraries/user_preferences.lib.php | 5 ++- prefs_forms.php | 2 +- 6 files changed, 52 insertions(+), 9 deletions(-) diff --git a/libraries/config/ConfigFile.class.php b/libraries/config/ConfigFile.class.php index ebb520dfc..3b9d6f231 100644 --- a/libraries/config/ConfigFile.class.php +++ b/libraries/config/ConfigFile.class.php @@ -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 @@ -136,6 +143,16 @@ class ConfigFile $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 */ @@ -216,21 +233,26 @@ class ConfigFile } /** - * 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); } } @@ -426,7 +448,12 @@ class ConfigFile */ 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; } /** @@ -445,6 +472,14 @@ class ConfigFile 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; } } diff --git a/libraries/config/messages.inc.php b/libraries/config/messages.inc.php index bdaec1fd7..a4d731212 100644 --- a/libraries/config/messages.inc.php +++ b/libraries/config/messages.inc.php @@ -390,7 +390,7 @@ $strConfigServers_host_name = __('Server hostname'); $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'); diff --git a/libraries/config/user_preferences.forms.php b/libraries/config/user_preferences.forms.php index 7d60e1056..0e913b9d6 100644 --- a/libraries/config/user_preferences.forms.php +++ b/libraries/config/user_preferences.forms.php @@ -18,6 +18,8 @@ $forms['Features']['General'] = array( 'InitialSlidersState', 'ErrorIconic', 'ReplaceHelpImg', + 'Servers/1/only_db', // saves to Server/only_db + 'Servers/1/hide_db', // saves to Server/hide_db 'SkipLockedTables', 'MaxDbList', 'MaxTableList'); diff --git a/libraries/user_preferences.inc.php b/libraries/user_preferences.inc.php index e1dee2b66..00bc89747 100644 --- a/libraries/user_preferences.inc.php +++ b/libraries/user_preferences.inc.php @@ -51,7 +51,10 @@ $forms_all_keys = PMA_read_userprefs_fieldnames($forms); $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(); diff --git a/libraries/user_preferences.lib.php b/libraries/user_preferences.lib.php index 0f10b5816..6616963e8 100644 --- a/libraries/user_preferences.lib.php +++ b/libraries/user_preferences.lib.php @@ -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; @@ -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); diff --git a/prefs_forms.php b/prefs_forms.php index 2e170bf5a..280083237 100644 --- a/prefs_forms.php +++ b/prefs_forms.php @@ -30,7 +30,7 @@ foreach ($forms[$form_param] as $form_name => $form) { 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'])) {