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
12 * @uses ConfigFile::getInstance()
13 * @uses ConfigFile::resetConfigData()
14 * @uses ConfigFile::setAllowedKeys()
15 * @uses ConfigFile::setCfgUpdateReadMapping()
16 * @uses ConfigFile::updateWithGlobalConfig()
17 * @uses PMA_read_userprefs_fieldnames()
19 function PMA_userprefs_pageinit()
21 $forms_all_keys = PMA_read_userprefs_fieldnames($GLOBALS['forms']);
22 $cf = ConfigFile
::getInstance();
23 $cf->resetConfigData(); // start with a clean instance
24 $cf->setAllowedKeys($forms_all_keys);
25 $cf->setCfgUpdateReadMapping(array(
26 'Server/hide_db' => 'Servers/1/hide_db',
27 'Server/only_db' => 'Servers/1/only_db'));
28 $cf->updateWithGlobalConfig($GLOBALS['cfg']);
32 * Loads user preferences
35 * * config_data - path => value pairs
36 * * mtime - last modification time
37 * * type - 'db' (config read from pmadb) or 'session' (read from user session)
39 * @uses $_SESSION['userconfig']
40 * @uses $GLOBALS['controllink']
41 * @uses PMA_array_merge_recursive
42 * @uses PMA_backquote()
43 * @uses PMA_DBI_fetch_single_row()
44 * @uses PMA_getRelationsParam()
45 * @uses PMA_sqlAddslashes()
46 * @uses $GLOBALS['controllink']
49 function PMA_load_userprefs()
51 $cfgRelation = PMA_getRelationsParam();
52 if (!$cfgRelation['userconfigwork']) {
53 // no pmadb table, use session storage
54 if (!isset($_SESSION['userconfig'])) {
55 $_SESSION['userconfig'] = array(
60 'config_data' => $_SESSION['userconfig']['db'],
61 'mtime' => $_SESSION['userconfig']['ts'],
64 // load configuration from pmadb
65 $query_table = PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['userconfig']);
67 SELECT `config_data`, UNIX_TIMESTAMP(`timevalue`) ts
68 FROM ' . $query_table . '
69 WHERE `username` = \'' . PMA_sqlAddslashes($cfgRelation['user']) . '\'';
70 $row = PMA_DBI_fetch_single_row($query, 'ASSOC', $GLOBALS['controllink']);
72 'config_data' => $row ?
unserialize($row['config_data']) : array(),
73 'mtime' => $row ?
$row['ts'] : time(),
78 * Saves user preferences
80 * @uses $_SESSION['cache'][...]['userprefs']
81 * @uses $_SESSION['userconfig']
82 * @uses $GLOBALS['cfg']['ServerDefault']
83 * @uses $GLOBALS['controllink']
84 * @uses $GLOBALS['server']
85 * @uses ConfigFile::getConfigArray()
86 * @uses ConfigFile::getInstance()
87 * @uses PMA_backquote()
88 * @uses PMA_DBI_fetch_value
89 * @uses PMA_DBI_getError()
90 * @uses PMA_DBI_try_query()
91 * @uses PMA_Message::addMessage()
92 * @uses PMA_Message::error()
93 * @uses PMA_Message::rawError()
94 * @uses PMA_sqlAddslashes()
95 * @uses PMA_getRelationsParam()
96 * @param array $config_data
97 * @return true|PMA_Message
99 function PMA_save_userprefs(array $config_array)
101 $cfgRelation = PMA_getRelationsParam();
102 $server = isset($GLOBALS['server'])
104 : $GLOBALS['cfg']['ServerDefault'];
105 $cache_key = 'server_' . $server;
106 if (!$cfgRelation['userconfigwork']) {
107 // no pmadb table, use session storage
108 $_SESSION['userconfig'] = array(
109 'db' => $config_array,
111 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
112 unset($_SESSION['cache'][$cache_key]['userprefs']);
117 // save configuration to pmadb
118 $query_table = PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['userconfig']);
121 FROM ' . $query_table . '
122 WHERE `username` = \'' . PMA_sqlAddslashes($cfgRelation['user']) . '\'';
124 $has_config = PMA_DBI_fetch_value($query, 0, 0, $GLOBALS['controllink']);
125 $config_data = serialize($config_array);
128 UPDATE ' . $query_table . '
129 SET `config_data` = \'' . PMA_sqlAddslashes($config_data) . '\'
130 WHERE `username` = \'' . PMA_sqlAddslashes($cfgRelation['user']) . '\'';
133 INSERT INTO ' . $query_table . ' (`username`, `config_data`)
134 VALUES (\'' . PMA_sqlAddslashes($cfgRelation['user']) . '\',
135 \'' . PMA_sqlAddslashes($config_data) . '\')';
137 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
138 unset($_SESSION['cache'][$cache_key]['userprefs']);
140 if (!PMA_DBI_try_query($query, $GLOBALS['controllink'])) {
141 $message = PMA_Message
::error(__('Could not save configuration'));
142 $message->addMessage('<br /><br />');
143 $message->addMessage(PMA_Message
::rawError(PMA_DBI_getError($GLOBALS['controllink'])));
150 * Returns a user preferences array filtered by $cfg['UserprefsDisallow']
151 * (blacklist) and keys from user preferences form (whitelist)
153 * @uses PMA_array_write()
154 * @uses PMA_read_userprefs_fieldnames()
155 * @param array $config_data path => value pairs
158 function PMA_apply_userprefs(array $config_data)
161 $blacklist = array_flip($GLOBALS['cfg']['UserprefsDisallow']);
162 if (!$GLOBALS['cfg']['UserprefsDeveloperTab']) {
163 // disallow everything in the Developers tab
164 $blacklist['Error_Handler/display'] = true;
165 $blacklist['Error_Handler/gather'] = true;
166 $blacklist['DBG/sql'] = true;
168 $whitelist = array_flip(PMA_read_userprefs_fieldnames());
169 // whitelist some additional fields which are custom handled
170 $whitelist['ThemeDefault'] = true;
171 $whitelist['fontsize'] = true;
172 $whitelist['lang'] = true;
173 $whitelist['collation_connection'] = true;
174 $whitelist['Server/hide_db'] = true;
175 $whitelist['Server/only_db'] = true;
176 foreach ($config_data as $path => $value) {
177 if (!isset($whitelist[$path]) ||
isset($blacklist[$path])) {
180 PMA_array_write($path, $cfg, $value);
186 * Reads user preferences field names
188 * @param array|null $forms
191 function PMA_read_userprefs_fieldnames(array $forms = null)
195 // return cached results
196 if ($names !== null) {
199 if (is_null($forms)) {
201 include 'libraries/config/user_preferences.forms.php';
204 foreach ($forms as $formset) {
205 foreach ($formset as $form) {
206 foreach ($form as $k => $v) {
207 $names[] = is_int($k) ?
$v : $k;
215 * Updates one user preferences option (loads and saves to database).
217 * No validation is done!
219 * @uses PMA_load_userprefs()
220 * @uses PMA_save_userprefs()
221 * @param string $cfg_name
222 * @param mixed $value
225 function PMA_persist_option($path, $value, $default_value)
227 $prefs = PMA_load_userprefs();
228 if ($value === $default_value) {
229 if (isset($prefs['config_data'][$path])) {
230 unset($prefs['config_data'][$path]);
235 $prefs['config_data'][$path] = $value;
237 PMA_save_userprefs($prefs['config_data']);
241 * Redirects after saving new user preferences
243 * @uses ConfigFile::getConfigArray()
244 * @uses ConfigFile::getInstance()
245 * @uses PMA_generate_common_url()
246 * @uses PMA_sendHeaderLocation()
247 * @param array $forms
248 * @param array $old_settings
249 * @param string $file_name
250 * @param array $params
251 * @param string $hash
253 function PMA_userprefs_redirect(array $forms, array $old_settings, $file_name, $params = null, $hash = null)
255 $reload_left_frame = isset($params['reload_left_frame']) && $params['reload_left_frame'];
256 if (!$reload_left_frame) {
257 // compute differences and check whether left frame should be refreshed
258 $old_settings = isset($old_settings['config_data'])
259 ?
$old_settings['config_data']
261 $new_settings = ConfigFile
::getInstance()->getConfigArray();
262 $diff_keys = array_keys(array_diff_assoc($old_settings, $new_settings)
263 +
array_diff_assoc($new_settings, $old_settings));
264 $check_keys = array('NaturalOrder', 'MainPageIconic', 'DefaultTabDatabase',
265 'Server/hide_db', 'Server/only_db');
266 $check_keys = array_merge($check_keys, $forms['Left_frame']['Left_frame'],
267 $forms['Left_frame']['Left_databases']);
268 $diff = array_intersect($check_keys, $diff_keys);
269 $reload_left_frame = !empty($diff);
275 'reload_left_frame' => $reload_left_frame);
276 if (is_array($params)) {
277 $url_params = array_merge($params, $url_params);
280 $hash = '#' . urlencode($hash);
282 PMA_sendHeaderLocation($GLOBALS['cfg']['PmaAbsoluteUri'] . $file_name
283 . PMA_generate_common_url($url_params, '&') . $hash);
287 * Shows form which allows to quickly load settings stored in browser's local storage
289 * @uses $_REQUEST['prefs_autoload']
290 * @uses $_SESSION['userprefs_autoload']
291 * @uses PMA_generate_common_hidden_inputs()
293 function PMA_userprefs_autoload_header()
295 if (isset($_REQUEST['prefs_autoload']) && $_REQUEST['prefs_autoload'] == 'hide') {
296 $_SESSION['userprefs_autoload'] = true;
299 $script_name = basename(basename($GLOBALS['PMA_PHP_SELF']));
300 $return_url = $script_name . '?' . http_build_query($_GET, '', '&');
302 <div id
="prefs_autoload" class="notice" style
="display:none">
303 <form action
="prefs_manage.php" method
="post">
304 <?php
echo PMA_generate_common_hidden_inputs() . "\n"; ?
>
305 <input type
="hidden" name
="json" value
="" />
306 <input type
="hidden" name
="submit_import" value
="1" />
307 <input type
="hidden" name
="return_url" value
="<?php echo htmlspecialchars($return_url) ?>" />
308 <?php
echo __('Your browser has phpMyAdmin configuration for this domain. Would you like to import it for current session?') ?
>
310 <a href
="#yes"><?php
echo __('Yes') ?
></a
> / <a href
="#no"><?php
echo __('No') ?
></a
>