2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Functions for displaying user preferences pages
8 if (! defined('PHPMYADMIN')) {
12 require_once './libraries/Template.class.php';
15 * Common initialization for user preferences modification pages
17 * @param ConfigFile $cf Config file instance
21 function PMA_userprefsPageInit(ConfigFile
$cf)
23 $forms_all_keys = PMA_readUserprefsFieldNames($GLOBALS['forms']);
24 $cf->resetConfigData(); // start with a clean instance
25 $cf->setAllowedKeys($forms_all_keys);
26 $cf->setCfgUpdateReadMapping(
28 'Server/hide_db' => 'Servers/1/hide_db',
29 'Server/only_db' => 'Servers/1/only_db'
32 $cf->updateWithGlobalConfig($GLOBALS['cfg']);
36 * Loads user preferences
39 * * config_data - path => value pairs
40 * * mtime - last modification time
41 * * type - 'db' (config read from pmadb) or 'session' (read from user session)
45 function PMA_loadUserprefs()
47 $cfgRelation = PMA_getRelationsParam();
48 if (! $cfgRelation['userconfigwork']) {
49 // no pmadb table, use session storage
50 if (! isset($_SESSION['userconfig'])) {
51 $_SESSION['userconfig'] = array(
56 'config_data' => $_SESSION['userconfig']['db'],
57 'mtime' => $_SESSION['userconfig']['ts'],
60 // load configuration from pmadb
61 $query_table = PMA_Util
::backquote($cfgRelation['db']) . '.'
62 . PMA_Util
::backquote($cfgRelation['userconfig']);
63 $query = 'SELECT `config_data`, UNIX_TIMESTAMP(`timevalue`) ts'
64 . ' FROM ' . $query_table
65 . ' WHERE `username` = \''
66 . PMA_Util
::sqlAddSlashes($cfgRelation['user'])
68 $row = $GLOBALS['dbi']->fetchSingleRow($query, 'ASSOC', $GLOBALS['controllink']);
71 'config_data' => $row ?
(array)json_decode($row['config_data']) : array(),
72 'mtime' => $row ?
$row['ts'] : time(),
77 * Saves user preferences
79 * @param array $config_array configuration array
81 * @return true|PMA_Message
83 function PMA_saveUserprefs(array $config_array)
85 $cfgRelation = PMA_getRelationsParam();
86 $server = isset($GLOBALS['server'])
88 : $GLOBALS['cfg']['ServerDefault'];
89 $cache_key = 'server_' . $server;
90 if (! $cfgRelation['userconfigwork']) {
91 // no pmadb table, use session storage
92 $_SESSION['userconfig'] = array(
93 'db' => $config_array,
95 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
96 unset($_SESSION['cache'][$cache_key]['userprefs']);
101 // save configuration to pmadb
102 $query_table = PMA_Util
::backquote($cfgRelation['db']) . '.'
103 . PMA_Util
::backquote($cfgRelation['userconfig']);
104 $query = 'SELECT `username` FROM ' . $query_table
105 . ' WHERE `username` = \''
106 . PMA_Util
::sqlAddSlashes($cfgRelation['user'])
109 $has_config = $GLOBALS['dbi']->fetchValue(
110 $query, 0, 0, $GLOBALS['controllink']
112 $config_data = json_encode($config_array);
114 $query = 'UPDATE ' . $query_table
115 . ' SET `timevalue` = NOW(), `config_data` = \''
116 . PMA_Util
::sqlAddSlashes($config_data)
118 . ' WHERE `username` = \''
119 . PMA_Util
::sqlAddSlashes($cfgRelation['user'])
122 $query = 'INSERT INTO ' . $query_table
123 . ' (`username`, `timevalue`,`config_data`) '
125 . PMA_Util
::sqlAddSlashes($cfgRelation['user']) . '\', NOW(), '
126 . '\'' . PMA_Util
::sqlAddSlashes($config_data) . '\')';
128 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
129 unset($_SESSION['cache'][$cache_key]['userprefs']);
131 if (!$GLOBALS['dbi']->tryQuery($query, $GLOBALS['controllink'])) {
132 $message = PMA_Message
::error(__('Could not save configuration'));
133 $message->addMessage('<br /><br />');
134 $message->addMessage(
135 PMA_Message
::rawError(
136 $GLOBALS['dbi']->getError($GLOBALS['controllink'])
145 * Returns a user preferences array filtered by $cfg['UserprefsDisallow']
146 * (blacklist) and keys from user preferences form (whitelist)
148 * @param array $config_data path => value pairs
152 function PMA_applyUserprefs(array $config_data)
155 $blacklist = array_flip($GLOBALS['cfg']['UserprefsDisallow']);
156 if (!$GLOBALS['cfg']['UserprefsDeveloperTab']) {
157 // disallow everything in the Developers tab
158 $blacklist['DBG/sql'] = true;
160 $whitelist = array_flip(PMA_readUserprefsFieldNames());
161 // whitelist some additional fields which are custom handled
162 $whitelist['ThemeDefault'] = true;
163 $whitelist['fontsize'] = true;
164 $whitelist['lang'] = true;
165 $whitelist['collation_connection'] = true;
166 $whitelist['Server/hide_db'] = true;
167 $whitelist['Server/only_db'] = true;
168 foreach ($config_data as $path => $value) {
169 if (! isset($whitelist[$path]) ||
isset($blacklist[$path])) {
172 PMA_arrayWrite($path, $cfg, $value);
178 * Reads user preferences field names
180 * @param array|null $forms Forms
184 function PMA_readUserprefsFieldNames(array $forms = null)
188 if (defined('TESTSUITE')) {
192 // return cached results
193 if ($names !== null) {
196 if (is_null($forms)) {
198 include 'libraries/config/user_preferences.forms.php';
201 foreach ($forms as $formset) {
202 foreach ($formset as $form) {
203 foreach ($form as $k => $v) {
204 $names[] = is_int($k) ?
$v : $k;
212 * Updates one user preferences option (loads and saves to database).
214 * No validation is done!
216 * @param string $path configuration
217 * @param mixed $value value
218 * @param mixed $default_value default value
222 function PMA_persistOption($path, $value, $default_value)
224 $prefs = PMA_loadUserprefs();
225 if ($value === $default_value) {
226 if (isset($prefs['config_data'][$path])) {
227 unset($prefs['config_data'][$path]);
232 $prefs['config_data'][$path] = $value;
234 PMA_saveUserprefs($prefs['config_data']);
238 * Redirects after saving new user preferences
240 * @param string $file_name Filename
241 * @param array $params URL parameters
242 * @param string $hash Hash value
246 function PMA_userprefsRedirect($file_name,
247 $params = null, $hash = null
250 $url_params = array('saved' => 1);
251 if (is_array($params)) {
252 $url_params = array_merge($params, $url_params);
255 $hash = '#' . urlencode($hash);
257 PMA_sendHeaderLocation(
258 $GLOBALS['cfg']['PmaAbsoluteUri'] . $file_name
259 . PMA_URL_getCommon($url_params, '&') . $hash
264 * Shows form which allows to quickly load
265 * settings stored in browser's local storage
269 function PMA_userprefsAutoloadGetHeader()
271 if (isset($_REQUEST['prefs_autoload'])
272 && $_REQUEST['prefs_autoload'] == 'hide'
274 $_SESSION['userprefs_autoload'] = true;
278 $script_name = basename(basename($GLOBALS['PMA_PHP_SELF']));
279 $return_url = htmlspecialchars(
280 $script_name . '?' . http_build_query($_GET, '', '&')
283 return PMA\Template
::get('prefs_autoload')
286 'hiddenInputs' => PMA_URL_getHiddenInputs(),
287 'return_url' => $return_url,