Translated using Weblate (Danish)
[phpmyadmin.git] / libraries / user_preferences.lib.php
blob5e83251198ea49cae8330c81434952fbe6a88ed9
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Functions for displaying user preferences pages
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * Common initialization for user preferences modification pages
15 * @return void
17 function PMA_userprefsPageInit()
19 $forms_all_keys = PMA_readUserprefsFieldNames($GLOBALS['forms']);
20 $cf = ConfigFile::getInstance();
21 $cf->resetConfigData(); // start with a clean instance
22 $cf->setAllowedKeys($forms_all_keys);
23 $cf->setCfgUpdateReadMapping(
24 array(
25 'Server/hide_db' => 'Servers/1/hide_db',
26 'Server/only_db' => 'Servers/1/only_db'
29 $cf->updateWithGlobalConfig($GLOBALS['cfg']);
32 /**
33 * Loads user preferences
35 * Returns an array:
36 * * config_data - path => value pairs
37 * * mtime - last modification time
38 * * type - 'db' (config read from pmadb) or 'session' (read from user session)
40 * @return array
42 function PMA_loadUserprefs()
44 $cfgRelation = PMA_getRelationsParam();
45 if (! $cfgRelation['userconfigwork']) {
46 // no pmadb table, use session storage
47 if (! isset($_SESSION['userconfig'])) {
48 $_SESSION['userconfig'] = array(
49 'db' => array(),
50 'ts' => time());
52 return array(
53 'config_data' => $_SESSION['userconfig']['db'],
54 'mtime' => $_SESSION['userconfig']['ts'],
55 'type' => 'session');
57 // load configuration from pmadb
58 $query_table = PMA_Util::backquote($cfgRelation['db']) . '.'
59 . PMA_Util::backquote($cfgRelation['userconfig']);
60 $query = 'SELECT `config_data`, UNIX_TIMESTAMP(`timevalue`) ts'
61 . ' FROM ' . $query_table
62 . ' WHERE `username` = \''
63 . PMA_Util::sqlAddSlashes($cfgRelation['user'])
64 . '\'';
65 $row = $GLOBALS['dbi']->fetchSingleRow($query, 'ASSOC', $GLOBALS['controllink']);
67 return array(
68 'config_data' => $row ? (array)json_decode($row['config_data']) : array(),
69 'mtime' => $row ? $row['ts'] : time(),
70 'type' => 'db');
73 /**
74 * Saves user preferences
76 * @param array $config_array configuration array
78 * @return true|PMA_Message
80 function PMA_saveUserprefs(array $config_array)
82 $cfgRelation = PMA_getRelationsParam();
83 $server = isset($GLOBALS['server'])
84 ? $GLOBALS['server']
85 : $GLOBALS['cfg']['ServerDefault'];
86 $cache_key = 'server_' . $server;
87 if (! $cfgRelation['userconfigwork']) {
88 // no pmadb table, use session storage
89 $_SESSION['userconfig'] = array(
90 'db' => $config_array,
91 'ts' => time());
92 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
93 unset($_SESSION['cache'][$cache_key]['userprefs']);
95 return true;
98 // save configuration to pmadb
99 $query_table = PMA_Util::backquote($cfgRelation['db']) . '.'
100 . PMA_Util::backquote($cfgRelation['userconfig']);
101 $query = 'SELECT `username` FROM ' . $query_table
102 . ' WHERE `username` = \''
103 . PMA_Util::sqlAddSlashes($cfgRelation['user'])
104 . '\'';
106 $has_config = $GLOBALS['dbi']->fetchValue(
107 $query, 0, 0, $GLOBALS['controllink']
109 $config_data = json_encode($config_array);
110 if ($has_config) {
111 $query = 'UPDATE ' . $query_table
112 . ' SET `config_data` = \''
113 . PMA_Util::sqlAddSlashes($config_data)
114 . '\''
115 . ' WHERE `username` = \''
116 . PMA_Util::sqlAddSlashes($cfgRelation['user'])
117 . '\'';
118 } else {
119 $query = 'INSERT INTO ' . $query_table . ' (`username`, `config_data`) '
120 . 'VALUES (\''
121 . PMA_Util::sqlAddSlashes($cfgRelation['user']) . '\', \''
122 . PMA_Util::sqlAddSlashes($config_data) . '\')';
124 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
125 unset($_SESSION['cache'][$cache_key]['userprefs']);
127 if (!$GLOBALS['dbi']->tryQuery($query, $GLOBALS['controllink'])) {
128 $message = PMA_Message::error(__('Could not save configuration'));
129 $message->addMessage('<br /><br />');
130 $message->addMessage(
131 PMA_Message::rawError(
132 $GLOBALS['dbi']->getError($GLOBALS['controllink'])
135 return $message;
137 return true;
141 * Returns a user preferences array filtered by $cfg['UserprefsDisallow']
142 * (blacklist) and keys from user preferences form (whitelist)
144 * @param array $config_data path => value pairs
146 * @return array
148 function PMA_applyUserprefs(array $config_data)
150 $cfg = array();
151 $blacklist = array_flip($GLOBALS['cfg']['UserprefsDisallow']);
152 if (!$GLOBALS['cfg']['UserprefsDeveloperTab']) {
153 // disallow everything in the Developers tab
154 $blacklist['Error_Handler/display'] = true;
155 $blacklist['Error_Handler/gather'] = true;
156 $blacklist['DBG/sql'] = true;
158 $whitelist = array_flip(PMA_readUserprefsFieldNames());
159 // whitelist some additional fields which are custom handled
160 $whitelist['ThemeDefault'] = true;
161 $whitelist['fontsize'] = true;
162 $whitelist['lang'] = true;
163 $whitelist['collation_connection'] = true;
164 $whitelist['Server/hide_db'] = true;
165 $whitelist['Server/only_db'] = true;
166 foreach ($config_data as $path => $value) {
167 if (! isset($whitelist[$path]) || isset($blacklist[$path])) {
168 continue;
170 PMA_arrayWrite($path, $cfg, $value);
172 return $cfg;
176 * Reads user preferences field names
178 * @param array|null $forms
180 * @return array
182 function PMA_readUserprefsFieldNames(array $forms = null)
184 static $names;
186 if (defined('TESTSUITE')) {
187 $names = null;
190 // return cached results
191 if ($names !== null) {
192 return $names;
194 if (is_null($forms)) {
195 $forms = array();
196 include 'libraries/config/user_preferences.forms.php';
198 $names = array();
199 foreach ($forms as $formset) {
200 foreach ($formset as $form) {
201 foreach ($form as $k => $v) {
202 $names[] = is_int($k) ? $v : $k;
206 return $names;
210 * Updates one user preferences option (loads and saves to database).
212 * No validation is done!
214 * @param string $path configuration
215 * @param mixed $value value
216 * @param mixed $default_value default value
218 * @return void
220 function PMA_persistOption($path, $value, $default_value)
222 $prefs = PMA_loadUserprefs();
223 if ($value === $default_value) {
224 if (isset($prefs['config_data'][$path])) {
225 unset($prefs['config_data'][$path]);
226 } else {
227 return;
229 } else {
230 $prefs['config_data'][$path] = $value;
232 PMA_saveUserprefs($prefs['config_data']);
236 * Redirects after saving new user preferences
238 * @param string $file_name
239 * @param array $params
240 * @param string $hash
242 * @return void
244 function PMA_userprefsRedirect($file_name,
245 $params = null, $hash = null
247 // redirect
248 $url_params = array('saved' => 1);
249 if (is_array($params)) {
250 $url_params = array_merge($params, $url_params);
252 if ($hash) {
253 $hash = '#' . urlencode($hash);
255 PMA_sendHeaderLocation(
256 $GLOBALS['cfg']['PmaAbsoluteUri'] . $file_name
257 . PMA_URL_getCommon($url_params, '&') . $hash
262 * Shows form which allows to quickly load
263 * settings stored in browser's local storage
265 * @return string
267 function PMA_userprefsAutoloadGetHeader()
269 $retval = '';
271 if (isset($_REQUEST['prefs_autoload'])
272 && $_REQUEST['prefs_autoload'] == 'hide'
274 $_SESSION['userprefs_autoload'] = true;
275 } else {
276 $script_name = basename(basename($GLOBALS['PMA_PHP_SELF']));
277 $return_url = htmlspecialchars(
278 $script_name . '?' . http_build_query($_GET, '', '&')
281 $retval .= '<div id="prefs_autoload" class="notice" style="display:none">';
282 $retval .= '<form action="prefs_manage.php" method="post">';
283 $retval .= PMA_URL_getHiddenInputs();
284 $retval .= '<input type="hidden" name="json" value="" />';
285 $retval .= '<input type="hidden" name="submit_import" value="1" />';
286 $retval .= '<input type="hidden" name="return_url" value="'
287 . $return_url . '" />';
288 $retval .= __(
289 'Your browser has phpMyAdmin configuration for this domain. '
290 . 'Would you like to import it for current session?'
292 $retval .= '<br />';
293 $retval .= '<a href="#yes">' . __('Yes') . '</a>';
294 $retval .= ' / ';
295 $retval .= '<a href="#no">' . __('No') . '</a>';
296 $retval .= '</form>';
297 $retval .= '</div>';
299 return $retval;