2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Functions for displaying user preferences pages
8 if (! defined('PHPMYADMIN')) {
13 * Common initialization for user preferences modification pages
17 function PMA_userprefsPageInit()
19 $forms_all_keys = PMA_readUserprefsFieldNames($GLOBALS['forms']);
20 $cf = ConfigFile
::getInstance();
21 $cf->resetConfigData(); // start with a clean instance
22 $cf->setAllowedKeys($forms_all_keys);
23 $cf->setCfgUpdateReadMapping(
25 'Server/hide_db' => 'Servers/1/hide_db',
26 'Server/only_db' => 'Servers/1/only_db'
29 $cf->updateWithGlobalConfig($GLOBALS['cfg']);
33 * Loads user preferences
36 * * config_data - path => value pairs
37 * * mtime - last modification time
38 * * type - 'db' (config read from pmadb) or 'session' (read from user session)
42 function PMA_loadUserprefs()
44 $cfgRelation = PMA_getRelationsParam();
45 if (! $cfgRelation['userconfigwork']) {
46 // no pmadb table, use session storage
47 if (! isset($_SESSION['userconfig'])) {
48 $_SESSION['userconfig'] = array(
53 'config_data' => $_SESSION['userconfig']['db'],
54 'mtime' => $_SESSION['userconfig']['ts'],
57 // load configuration from pmadb
58 $query_table = PMA_Util
::backquote($cfgRelation['db']) . '.'
59 . PMA_Util
::backquote($cfgRelation['userconfig']);
60 $query = 'SELECT `config_data`, UNIX_TIMESTAMP(`timevalue`) ts'
61 . ' FROM ' . $query_table
62 . ' WHERE `username` = \''
63 . PMA_Util
::sqlAddSlashes($cfgRelation['user'])
65 $row = $GLOBALS['dbi']->fetchSingleRow($query, 'ASSOC', $GLOBALS['controllink']);
68 'config_data' => $row ?
(array)json_decode($row['config_data']) : array(),
69 'mtime' => $row ?
$row['ts'] : time(),
74 * Saves user preferences
76 * @param array $config_array configuration array
78 * @return true|PMA_Message
80 function PMA_saveUserprefs(array $config_array)
82 $cfgRelation = PMA_getRelationsParam();
83 $server = isset($GLOBALS['server'])
85 : $GLOBALS['cfg']['ServerDefault'];
86 $cache_key = 'server_' . $server;
87 if (! $cfgRelation['userconfigwork']) {
88 // no pmadb table, use session storage
89 $_SESSION['userconfig'] = array(
90 'db' => $config_array,
92 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
93 unset($_SESSION['cache'][$cache_key]['userprefs']);
98 // save configuration to pmadb
99 $query_table = PMA_Util
::backquote($cfgRelation['db']) . '.'
100 . PMA_Util
::backquote($cfgRelation['userconfig']);
101 $query = 'SELECT `username` FROM ' . $query_table
102 . ' WHERE `username` = \''
103 . PMA_Util
::sqlAddSlashes($cfgRelation['user'])
106 $has_config = $GLOBALS['dbi']->fetchValue(
107 $query, 0, 0, $GLOBALS['controllink']
109 $config_data = json_encode($config_array);
111 $query = 'UPDATE ' . $query_table
112 . ' SET `config_data` = \''
113 . PMA_Util
::sqlAddSlashes($config_data)
115 . ' WHERE `username` = \''
116 . PMA_Util
::sqlAddSlashes($cfgRelation['user'])
119 $query = 'INSERT INTO ' . $query_table . ' (`username`, `config_data`) '
121 . PMA_Util
::sqlAddSlashes($cfgRelation['user']) . '\', \''
122 . PMA_Util
::sqlAddSlashes($config_data) . '\')';
124 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
125 unset($_SESSION['cache'][$cache_key]['userprefs']);
127 if (!$GLOBALS['dbi']->tryQuery($query, $GLOBALS['controllink'])) {
128 $message = PMA_Message
::error(__('Could not save configuration'));
129 $message->addMessage('<br /><br />');
130 $message->addMessage(
131 PMA_Message
::rawError(
132 $GLOBALS['dbi']->getError($GLOBALS['controllink'])
141 * Returns a user preferences array filtered by $cfg['UserprefsDisallow']
142 * (blacklist) and keys from user preferences form (whitelist)
144 * @param array $config_data path => value pairs
148 function PMA_applyUserprefs(array $config_data)
151 $blacklist = array_flip($GLOBALS['cfg']['UserprefsDisallow']);
152 if (!$GLOBALS['cfg']['UserprefsDeveloperTab']) {
153 // disallow everything in the Developers tab
154 $blacklist['Error_Handler/display'] = true;
155 $blacklist['Error_Handler/gather'] = true;
156 $blacklist['DBG/sql'] = true;
158 $whitelist = array_flip(PMA_readUserprefsFieldNames());
159 // whitelist some additional fields which are custom handled
160 $whitelist['ThemeDefault'] = true;
161 $whitelist['fontsize'] = true;
162 $whitelist['lang'] = true;
163 $whitelist['collation_connection'] = true;
164 $whitelist['Server/hide_db'] = true;
165 $whitelist['Server/only_db'] = true;
166 foreach ($config_data as $path => $value) {
167 if (! isset($whitelist[$path]) ||
isset($blacklist[$path])) {
170 PMA_arrayWrite($path, $cfg, $value);
176 * Reads user preferences field names
178 * @param array|null $forms
182 function PMA_readUserprefsFieldNames(array $forms = null)
186 if (defined('TESTSUITE')) {
190 // return cached results
191 if ($names !== null) {
194 if (is_null($forms)) {
196 include 'libraries/config/user_preferences.forms.php';
199 foreach ($forms as $formset) {
200 foreach ($formset as $form) {
201 foreach ($form as $k => $v) {
202 $names[] = is_int($k) ?
$v : $k;
210 * Updates one user preferences option (loads and saves to database).
212 * No validation is done!
214 * @param string $path configuration
215 * @param mixed $value value
216 * @param mixed $default_value default value
220 function PMA_persistOption($path, $value, $default_value)
222 $prefs = PMA_loadUserprefs();
223 if ($value === $default_value) {
224 if (isset($prefs['config_data'][$path])) {
225 unset($prefs['config_data'][$path]);
230 $prefs['config_data'][$path] = $value;
232 PMA_saveUserprefs($prefs['config_data']);
236 * Redirects after saving new user preferences
238 * @param string $file_name
239 * @param array $params
240 * @param string $hash
244 function PMA_userprefsRedirect($file_name,
245 $params = null, $hash = null
248 $url_params = array('saved' => 1);
249 if (is_array($params)) {
250 $url_params = array_merge($params, $url_params);
253 $hash = '#' . urlencode($hash);
255 PMA_sendHeaderLocation(
256 $GLOBALS['cfg']['PmaAbsoluteUri'] . $file_name
257 . PMA_URL_getCommon($url_params, '&') . $hash
262 * Shows form which allows to quickly load
263 * settings stored in browser's local storage
267 function PMA_userprefsAutoloadGetHeader()
271 if (isset($_REQUEST['prefs_autoload'])
272 && $_REQUEST['prefs_autoload'] == 'hide'
274 $_SESSION['userprefs_autoload'] = true;
276 $script_name = basename(basename($GLOBALS['PMA_PHP_SELF']));
277 $return_url = htmlspecialchars(
278 $script_name . '?' . http_build_query($_GET, '', '&')
281 $retval .= '<div id="prefs_autoload" class="notice" style="display:none">';
282 $retval .= '<form action="prefs_manage.php" method="post">';
283 $retval .= PMA_URL_getHiddenInputs();
284 $retval .= '<input type="hidden" name="json" value="" />';
285 $retval .= '<input type="hidden" name="submit_import" value="1" />';
286 $retval .= '<input type="hidden" name="return_url" value="'
287 . $return_url . '" />';
289 'Your browser has phpMyAdmin configuration for this domain. '
290 . 'Would you like to import it for current session?'
293 $retval .= '<a href="#yes">' . __('Yes') . '</a>';
295 $retval .= '<a href="#no">' . __('No') . '</a>';
296 $retval .= '</form>';