bug#3212720 Show error message on error.
[phpmyadmin/ayax.git] / libraries / user_preferences.lib.php
blob6c48116d6feb29eb5a791a61bbe92064bd2c3039
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Functions for displaying user preferences pages
6 * @package phpMyAdmin
7 */
9 /**
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']);
31 /**
32 * Loads user preferences
34 * Returns an array:
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']
47 * @return array
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(
56 'db' => array(),
57 'ts' => time());
59 return array(
60 'config_data' => $_SESSION['userconfig']['db'],
61 'mtime' => $_SESSION['userconfig']['ts'],
62 'type' => 'session');
64 // load configuration from pmadb
65 $query_table = PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['userconfig']);
66 $query = '
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 return array(
73 'config_data' => $row ? (array)json_decode($row['config_data']) : array(),
74 'mtime' => $row ? $row['ts'] : time(),
75 'type' => 'db');
78 /**
79 * Saves user preferences
81 * @uses $_SESSION['cache'][...]['userprefs']
82 * @uses $_SESSION['userconfig']
83 * @uses $GLOBALS['cfg']['ServerDefault']
84 * @uses $GLOBALS['controllink']
85 * @uses $GLOBALS['server']
86 * @uses ConfigFile::getConfigArray()
87 * @uses ConfigFile::getInstance()
88 * @uses PMA_backquote()
89 * @uses PMA_DBI_fetch_value
90 * @uses PMA_DBI_getError()
91 * @uses PMA_DBI_try_query()
92 * @uses PMA_Message::addMessage()
93 * @uses PMA_Message::error()
94 * @uses PMA_Message::rawError()
95 * @uses PMA_sqlAddslashes()
96 * @uses PMA_getRelationsParam()
97 * @param array $config_data
98 * @return true|PMA_Message
100 function PMA_save_userprefs(array $config_array)
102 $cfgRelation = PMA_getRelationsParam();
103 $server = isset($GLOBALS['server'])
104 ? $GLOBALS['server']
105 : $GLOBALS['cfg']['ServerDefault'];
106 $cache_key = 'server_' . $server;
107 if (!$cfgRelation['userconfigwork']) {
108 // no pmadb table, use session storage
109 $_SESSION['userconfig'] = array(
110 'db' => $config_array,
111 'ts' => time());
112 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
113 unset($_SESSION['cache'][$cache_key]['userprefs']);
115 return true;
118 // save configuration to pmadb
119 $query_table = PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['userconfig']);
120 $query = '
121 SELECT `username`
122 FROM ' . $query_table . '
123 WHERE `username` = \'' . PMA_sqlAddslashes($cfgRelation['user']) . '\'';
125 $has_config = PMA_DBI_fetch_value($query, 0, 0, $GLOBALS['controllink']);
126 $config_data = json_encode($config_array);
127 if ($has_config) {
128 $query = '
129 UPDATE ' . $query_table . '
130 SET `config_data` = \'' . PMA_sqlAddslashes($config_data) . '\'
131 WHERE `username` = \'' . PMA_sqlAddslashes($cfgRelation['user']) . '\'';
132 } else {
133 $query = '
134 INSERT INTO ' . $query_table . ' (`username`, `config_data`)
135 VALUES (\'' . PMA_sqlAddslashes($cfgRelation['user']) . '\',
136 \'' . PMA_sqlAddslashes($config_data) . '\')';
138 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
139 unset($_SESSION['cache'][$cache_key]['userprefs']);
141 if (!PMA_DBI_try_query($query, $GLOBALS['controllink'])) {
142 $message = PMA_Message::error(__('Could not save configuration'));
143 $message->addMessage('<br /><br />');
144 $message->addMessage(PMA_Message::rawError(PMA_DBI_getError($GLOBALS['controllink'])));
145 return $message;
147 return true;
151 * Returns a user preferences array filtered by $cfg['UserprefsDisallow']
152 * (blacklist) and keys from user preferences form (whitelist)
154 * @uses PMA_array_write()
155 * @uses PMA_read_userprefs_fieldnames()
156 * @param array $config_data path => value pairs
157 * @return array
159 function PMA_apply_userprefs(array $config_data)
161 $cfg = array();
162 $blacklist = array_flip($GLOBALS['cfg']['UserprefsDisallow']);
163 if (!$GLOBALS['cfg']['UserprefsDeveloperTab']) {
164 // disallow everything in the Developers tab
165 $blacklist['Error_Handler/display'] = true;
166 $blacklist['Error_Handler/gather'] = true;
167 $blacklist['DBG/sql'] = true;
169 $whitelist = array_flip(PMA_read_userprefs_fieldnames());
170 // whitelist some additional fields which are custom handled
171 $whitelist['ThemeDefault'] = true;
172 $whitelist['fontsize'] = true;
173 $whitelist['lang'] = true;
174 $whitelist['collation_connection'] = true;
175 $whitelist['Server/hide_db'] = true;
176 $whitelist['Server/only_db'] = true;
177 foreach ($config_data as $path => $value) {
178 if (!isset($whitelist[$path]) || isset($blacklist[$path])) {
179 continue;
181 PMA_array_write($path, $cfg, $value);
183 return $cfg;
187 * Reads user preferences field names
189 * @param array|null $forms
190 * @return array
192 function PMA_read_userprefs_fieldnames(array $forms = null)
194 static $names;
196 // return cached results
197 if ($names !== null) {
198 return $names;
200 if (is_null($forms)) {
201 $forms = array();
202 include 'libraries/config/user_preferences.forms.php';
204 $names = array();
205 foreach ($forms as $formset) {
206 foreach ($formset as $form) {
207 foreach ($form as $k => $v) {
208 $names[] = is_int($k) ? $v : $k;
212 return $names;
216 * Updates one user preferences option (loads and saves to database).
218 * No validation is done!
220 * @uses PMA_load_userprefs()
221 * @uses PMA_save_userprefs()
222 * @param string $cfg_name
223 * @param mixed $value
224 * @return void
226 function PMA_persist_option($path, $value, $default_value)
228 $prefs = PMA_load_userprefs();
229 if ($value === $default_value) {
230 if (isset($prefs['config_data'][$path])) {
231 unset($prefs['config_data'][$path]);
232 } else {
233 return;
235 } else {
236 $prefs['config_data'][$path] = $value;
238 PMA_save_userprefs($prefs['config_data']);
242 * Redirects after saving new user preferences
244 * @uses ConfigFile::getConfigArray()
245 * @uses ConfigFile::getInstance()
246 * @uses PMA_generate_common_url()
247 * @uses PMA_sendHeaderLocation()
248 * @param array $forms
249 * @param array $old_settings
250 * @param string $file_name
251 * @param array $params
252 * @param string $hash
254 function PMA_userprefs_redirect(array $forms, array $old_settings, $file_name, $params = null, $hash = null)
256 $reload_left_frame = isset($params['reload_left_frame']) && $params['reload_left_frame'];
257 if (!$reload_left_frame) {
258 // compute differences and check whether left frame should be refreshed
259 $old_settings = isset($old_settings['config_data'])
260 ? $old_settings['config_data']
261 : array();
262 $new_settings = ConfigFile::getInstance()->getConfigArray();
263 $diff_keys = array_keys(array_diff_assoc($old_settings, $new_settings)
264 + array_diff_assoc($new_settings, $old_settings));
265 $check_keys = array('NaturalOrder', 'MainPageIconic', 'DefaultTabDatabase',
266 'Server/hide_db', 'Server/only_db');
267 $check_keys = array_merge($check_keys, $forms['Left_frame']['Left_frame'],
268 $forms['Left_frame']['Left_databases']);
269 $diff = array_intersect($check_keys, $diff_keys);
270 $reload_left_frame = !empty($diff);
273 // redirect
274 $url_params = array(
275 'saved' => 1,
276 'reload_left_frame' => $reload_left_frame);
277 if (is_array($params)) {
278 $url_params = array_merge($params, $url_params);
280 if ($hash) {
281 $hash = '#' . urlencode($hash);
283 PMA_sendHeaderLocation($GLOBALS['cfg']['PmaAbsoluteUri'] . $file_name
284 . PMA_generate_common_url($url_params, '&') . $hash);
288 * Shows form which allows to quickly load settings stored in browser's local storage
290 * @uses $_REQUEST['prefs_autoload']
291 * @uses $_SESSION['userprefs_autoload']
292 * @uses PMA_generate_common_hidden_inputs()
294 function PMA_userprefs_autoload_header()
296 if (isset($_REQUEST['prefs_autoload']) && $_REQUEST['prefs_autoload'] == 'hide') {
297 $_SESSION['userprefs_autoload'] = true;
298 exit;
300 $script_name = basename(basename($GLOBALS['PMA_PHP_SELF']));
301 $return_url = $script_name . '?' . http_build_query($_GET, '', '&');
303 <div id="prefs_autoload" class="notice" style="display:none">
304 <form action="prefs_manage.php" method="post">
305 <?php echo PMA_generate_common_hidden_inputs() . "\n"; ?>
306 <input type="hidden" name="json" value="" />
307 <input type="hidden" name="submit_import" value="1" />
308 <input type="hidden" name="return_url" value="<?php echo htmlspecialchars($return_url) ?>" />
309 <?php echo __('Your browser has phpMyAdmin configuration for this domain. Would you like to import it for current session?') ?>
310 <br />
311 <a href="#yes"><?php echo __('Yes') ?></a> / <a href="#no"><?php echo __('No') ?></a>
312 </form>
313 </div>
314 <?php