2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Functions for displaying user preferences pages
10 * Common initialization for user preferences modification pages
13 function PMA_userprefs_pageinit()
15 $forms_all_keys = PMA_read_userprefs_fieldnames($GLOBALS['forms']);
16 $cf = ConfigFile
::getInstance();
17 $cf->resetConfigData(); // start with a clean instance
18 $cf->setAllowedKeys($forms_all_keys);
19 $cf->setCfgUpdateReadMapping(array(
20 'Server/hide_db' => 'Servers/1/hide_db',
21 'Server/only_db' => 'Servers/1/only_db'));
22 $cf->updateWithGlobalConfig($GLOBALS['cfg']);
26 * Loads user preferences
29 * * config_data - path => value pairs
30 * * mtime - last modification time
31 * * type - 'db' (config read from pmadb) or 'session' (read from user session)
35 function PMA_load_userprefs()
37 $cfgRelation = PMA_getRelationsParam();
38 if (! $cfgRelation['userconfigwork']) {
39 // no pmadb table, use session storage
40 if (! isset($_SESSION['userconfig'])) {
41 $_SESSION['userconfig'] = array(
46 'config_data' => $_SESSION['userconfig']['db'],
47 'mtime' => $_SESSION['userconfig']['ts'],
50 // load configuration from pmadb
51 $query_table = PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['userconfig']);
53 SELECT `config_data`, UNIX_TIMESTAMP(`timevalue`) ts
54 FROM ' . $query_table . '
55 WHERE `username` = \'' . PMA_sqlAddSlashes($cfgRelation['user']) . '\'';
56 $row = PMA_DBI_fetch_single_row($query, 'ASSOC', $GLOBALS['controllink']);
59 'config_data' => $row ?
(array)json_decode($row['config_data']) : array(),
60 'mtime' => $row ?
$row['ts'] : time(),
65 * Saves user preferences
67 * @param array $config_data
68 * @return true|PMA_Message
70 function PMA_save_userprefs(array $config_array)
72 $cfgRelation = PMA_getRelationsParam();
73 $server = isset($GLOBALS['server'])
75 : $GLOBALS['cfg']['ServerDefault'];
76 $cache_key = 'server_' . $server;
77 if (! $cfgRelation['userconfigwork']) {
78 // no pmadb table, use session storage
79 $_SESSION['userconfig'] = array(
80 'db' => $config_array,
82 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
83 unset($_SESSION['cache'][$cache_key]['userprefs']);
88 // save configuration to pmadb
89 $query_table = PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['userconfig']);
92 FROM ' . $query_table . '
93 WHERE `username` = \'' . PMA_sqlAddSlashes($cfgRelation['user']) . '\'';
95 $has_config = PMA_DBI_fetch_value($query, 0, 0, $GLOBALS['controllink']);
96 $config_data = json_encode($config_array);
99 UPDATE ' . $query_table . '
100 SET `config_data` = \'' . PMA_sqlAddSlashes($config_data) . '\'
101 WHERE `username` = \'' . PMA_sqlAddSlashes($cfgRelation['user']) . '\'';
104 INSERT INTO ' . $query_table . ' (`username`, `config_data`)
105 VALUES (\'' . PMA_sqlAddSlashes($cfgRelation['user']) . '\',
106 \'' . PMA_sqlAddSlashes($config_data) . '\')';
108 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
109 unset($_SESSION['cache'][$cache_key]['userprefs']);
111 if (!PMA_DBI_try_query($query, $GLOBALS['controllink'])) {
112 $message = PMA_Message
::error(__('Could not save configuration'));
113 $message->addMessage('<br /><br />');
114 $message->addMessage(PMA_Message
::rawError(PMA_DBI_getError($GLOBALS['controllink'])));
121 * Returns a user preferences array filtered by $cfg['UserprefsDisallow']
122 * (blacklist) and keys from user preferences form (whitelist)
124 * @param array $config_data path => value pairs
127 function PMA_apply_userprefs(array $config_data)
130 $blacklist = array_flip($GLOBALS['cfg']['UserprefsDisallow']);
131 if (!$GLOBALS['cfg']['UserprefsDeveloperTab']) {
132 // disallow everything in the Developers tab
133 $blacklist['Error_Handler/display'] = true;
134 $blacklist['Error_Handler/gather'] = true;
135 $blacklist['DBG/sql'] = true;
137 $whitelist = array_flip(PMA_read_userprefs_fieldnames());
138 // whitelist some additional fields which are custom handled
139 $whitelist['ThemeDefault'] = true;
140 $whitelist['fontsize'] = true;
141 $whitelist['lang'] = true;
142 $whitelist['collation_connection'] = true;
143 $whitelist['Server/hide_db'] = true;
144 $whitelist['Server/only_db'] = true;
145 foreach ($config_data as $path => $value) {
146 if (! isset($whitelist[$path]) ||
isset($blacklist[$path])) {
149 PMA_array_write($path, $cfg, $value);
155 * Reads user preferences field names
157 * @param array|null $forms
160 function PMA_read_userprefs_fieldnames(array $forms = null)
164 // return cached results
165 if ($names !== null) {
168 if (is_null($forms)) {
170 include 'libraries/config/user_preferences.forms.php';
173 foreach ($forms as $formset) {
174 foreach ($formset as $form) {
175 foreach ($form as $k => $v) {
176 $names[] = is_int($k) ?
$v : $k;
184 * Updates one user preferences option (loads and saves to database).
186 * No validation is done!
188 * @param string $cfg_name
189 * @param mixed $value
192 function PMA_persist_option($path, $value, $default_value)
194 $prefs = PMA_load_userprefs();
195 if ($value === $default_value) {
196 if (isset($prefs['config_data'][$path])) {
197 unset($prefs['config_data'][$path]);
202 $prefs['config_data'][$path] = $value;
204 PMA_save_userprefs($prefs['config_data']);
208 * Redirects after saving new user preferences
210 * @param array $forms
211 * @param array $old_settings
212 * @param string $file_name
213 * @param array $params
214 * @param string $hash
216 function PMA_userprefs_redirect(array $forms, array $old_settings, $file_name, $params = null, $hash = null)
218 $reload_left_frame = isset($params['reload_left_frame']) && $params['reload_left_frame'];
219 if (!$reload_left_frame) {
220 // compute differences and check whether left frame should be refreshed
221 $old_settings = isset($old_settings['config_data'])
222 ?
$old_settings['config_data']
224 $new_settings = ConfigFile
::getInstance()->getConfigArray();
225 $diff_keys = array_keys(array_diff_assoc($old_settings, $new_settings)
226 +
array_diff_assoc($new_settings, $old_settings));
227 $check_keys = array('NaturalOrder', 'MainPageIconic', 'DefaultTabDatabase',
228 'Server/hide_db', 'Server/only_db');
229 $check_keys = array_merge($check_keys, $forms['Left_frame']['Left_frame'],
230 $forms['Left_frame']['Left_databases']);
231 $diff = array_intersect($check_keys, $diff_keys);
232 $reload_left_frame = !empty($diff);
238 'reload_left_frame' => $reload_left_frame);
239 if (is_array($params)) {
240 $url_params = array_merge($params, $url_params);
243 $hash = '#' . urlencode($hash);
245 PMA_sendHeaderLocation($GLOBALS['cfg']['PmaAbsoluteUri'] . $file_name
246 . PMA_generate_common_url($url_params, '&') . $hash);
250 * Shows form which allows to quickly load settings stored in browser's local storage
253 function PMA_userprefs_autoload_header()
255 if (isset($_REQUEST['prefs_autoload']) && $_REQUEST['prefs_autoload'] == 'hide') {
256 $_SESSION['userprefs_autoload'] = true;
259 $script_name = basename(basename($GLOBALS['PMA_PHP_SELF']));
260 $return_url = $script_name . '?' . http_build_query($_GET, '', '&');
262 <div id
="prefs_autoload" class="notice" style
="display:none">
263 <form action
="prefs_manage.php" method
="post">
264 <?php
echo PMA_generate_common_hidden_inputs() . "\n"; ?
>
265 <input type
="hidden" name
="json" value
="" />
266 <input type
="hidden" name
="submit_import" value
="1" />
267 <input type
="hidden" name
="return_url" value
="<?php echo htmlspecialchars($return_url) ?>" />
268 <?php
echo __('Your browser has phpMyAdmin configuration for this domain. Would you like to import it for current session?') ?
>
270 <a href
="#yes"><?php
echo __('Yes') ?
></a
> / <a href
="#no"><?php
echo __('No') ?
></a
>