Translation update done using Pootle.
[phpmyadmin/dkf.git] / libraries / user_preferences.lib.php
blobbf84b25dc189644e9bb4e4e1f3023af58698e418
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']);
71 return array(
72 'config_data' => $row ? unserialize($row['config_data']) : array(),
73 'mtime' => $row ? $row['ts'] : time(),
74 'type' => 'db');
77 /**
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'])
103 ? $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,
110 'ts' => time());
111 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
112 unset($_SESSION['cache'][$cache_key]['userprefs']);
114 return true;
117 // save configuration to pmadb
118 $query_table = PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['userconfig']);
119 $query = '
120 SELECT `username`
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);
126 if ($has_config) {
127 $query = '
128 UPDATE ' . $query_table . '
129 SET `config_data` = \'' . PMA_sqlAddslashes($config_data) . '\'
130 WHERE `username` = \'' . PMA_sqlAddslashes($cfgRelation['user']) . '\'';
131 } else {
132 $query = '
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'])));
144 return $message;
146 return true;
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
156 * @return array
158 function PMA_apply_userprefs(array $config_data)
160 $cfg = array();
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])) {
178 continue;
180 PMA_array_write($path, $cfg, $value);
182 return $cfg;
186 * Reads user preferences field names
188 * @param array|null $forms
189 * @return array
191 function PMA_read_userprefs_fieldnames(array $forms = null)
193 static $names;
195 // return cached results
196 if ($names !== null) {
197 return $names;
199 if (is_null($forms)) {
200 $forms = array();
201 include 'libraries/config/user_preferences.forms.php';
203 $names = array();
204 foreach ($forms as $formset) {
205 foreach ($formset as $form) {
206 foreach ($form as $k => $v) {
207 $names[] = is_int($k) ? $v : $k;
211 return $names;
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
223 * @return void
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]);
231 } else {
232 return;
234 } else {
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']
260 : array();
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);
272 // redirect
273 $url_params = array(
274 'saved' => 1,
275 'reload_left_frame' => $reload_left_frame);
276 if (is_array($params)) {
277 $url_params = array_merge($params, $url_params);
279 if ($hash) {
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;
297 exit;
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?') ?>
309 <br />
310 <a href="#yes"><?php echo __('Yes') ?></a> / <a href="#no"><?php echo __('No') ?></a>
311 </form>
312 </div>
313 <?php