2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Functions for displaying user preferences pages
8 use PMA\libraries\config\ConfigFile
;
9 use PMA\libraries\Message
;
10 use PMA\libraries\URL
;
12 if (! defined('PHPMYADMIN')) {
17 * Common initialization for user preferences modification pages
19 * @param ConfigFile $cf Config file instance
23 function PMA_userprefsPageInit(ConfigFile
$cf)
25 $forms_all_keys = PMA_readUserprefsFieldNames($GLOBALS['forms']);
26 $cf->resetConfigData(); // start with a clean instance
27 $cf->setAllowedKeys($forms_all_keys);
28 $cf->setCfgUpdateReadMapping(
30 'Server/hide_db' => 'Servers/1/hide_db',
31 'Server/only_db' => 'Servers/1/only_db'
34 $cf->updateWithGlobalConfig($GLOBALS['cfg']);
38 * Loads user preferences
41 * * config_data - path => value pairs
42 * * mtime - last modification time
43 * * type - 'db' (config read from pmadb) or 'session' (read from user session)
47 function PMA_loadUserprefs()
49 $cfgRelation = PMA_getRelationsParam();
50 if (! $cfgRelation['userconfigwork']) {
51 // no pmadb table, use session storage
52 if (! isset($_SESSION['userconfig'])) {
53 $_SESSION['userconfig'] = array(
58 'config_data' => $_SESSION['userconfig']['db'],
59 'mtime' => $_SESSION['userconfig']['ts'],
62 // load configuration from pmadb
63 $query_table = PMA\libraries\Util
::backquote($cfgRelation['db']) . '.'
64 . PMA\libraries\Util
::backquote($cfgRelation['userconfig']);
65 $query = 'SELECT `config_data`, UNIX_TIMESTAMP(`timevalue`) ts'
66 . ' FROM ' . $query_table
67 . ' WHERE `username` = \''
68 . $GLOBALS['dbi']->escapeString($cfgRelation['user'])
70 $row = $GLOBALS['dbi']->fetchSingleRow($query, 'ASSOC', $GLOBALS['controllink']);
73 'config_data' => $row ?
(array)json_decode($row['config_data']) : array(),
74 'mtime' => $row ?
$row['ts'] : time(),
79 * Saves user preferences
81 * @param array $config_array configuration array
83 * @return true|PMA\libraries\Message
85 function PMA_saveUserprefs(array $config_array)
87 $cfgRelation = PMA_getRelationsParam();
88 $server = isset($GLOBALS['server'])
90 : $GLOBALS['cfg']['ServerDefault'];
91 $cache_key = 'server_' . $server;
92 if (! $cfgRelation['userconfigwork']) {
93 // no pmadb table, use session storage
94 $_SESSION['userconfig'] = array(
95 'db' => $config_array,
97 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
98 unset($_SESSION['cache'][$cache_key]['userprefs']);
103 // save configuration to pmadb
104 $query_table = PMA\libraries\Util
::backquote($cfgRelation['db']) . '.'
105 . PMA\libraries\Util
::backquote($cfgRelation['userconfig']);
106 $query = 'SELECT `username` FROM ' . $query_table
107 . ' WHERE `username` = \''
108 . $GLOBALS['dbi']->escapeString($cfgRelation['user'])
111 $has_config = $GLOBALS['dbi']->fetchValue(
112 $query, 0, 0, $GLOBALS['controllink']
114 $config_data = json_encode($config_array);
116 $query = 'UPDATE ' . $query_table
117 . ' SET `timevalue` = NOW(), `config_data` = \''
118 . $GLOBALS['dbi']->escapeString($config_data)
120 . ' WHERE `username` = \''
121 . $GLOBALS['dbi']->escapeString($cfgRelation['user'])
124 $query = 'INSERT INTO ' . $query_table
125 . ' (`username`, `timevalue`,`config_data`) '
127 . $GLOBALS['dbi']->escapeString($cfgRelation['user']) . '\', NOW(), '
128 . '\'' . $GLOBALS['dbi']->escapeString($config_data) . '\')';
130 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
131 unset($_SESSION['cache'][$cache_key]['userprefs']);
133 if (!$GLOBALS['dbi']->tryQuery($query, $GLOBALS['controllink'])) {
134 $message = Message
::error(__('Could not save configuration'));
135 $message->addMessage(
137 $GLOBALS['dbi']->getError($GLOBALS['controllink'])
147 * Returns a user preferences array filtered by $cfg['UserprefsDisallow']
148 * (blacklist) and keys from user preferences form (whitelist)
150 * @param array $config_data path => value pairs
154 function PMA_applyUserprefs(array $config_data)
157 $blacklist = array_flip($GLOBALS['cfg']['UserprefsDisallow']);
158 if (!$GLOBALS['cfg']['UserprefsDeveloperTab']) {
159 // disallow everything in the Developers tab
160 $blacklist['DBG/sql'] = true;
162 $whitelist = array_flip(PMA_readUserprefsFieldNames());
163 // whitelist some additional fields which are custom handled
164 $whitelist['ThemeDefault'] = true;
165 $whitelist['fontsize'] = true;
166 $whitelist['lang'] = true;
167 $whitelist['collation_connection'] = true;
168 $whitelist['Server/hide_db'] = true;
169 $whitelist['Server/only_db'] = true;
170 foreach ($config_data as $path => $value) {
171 if (! isset($whitelist[$path]) ||
isset($blacklist[$path])) {
174 PMA_arrayWrite($path, $cfg, $value);
180 * Reads user preferences field names
182 * @param array|null $forms Forms
186 function PMA_readUserprefsFieldNames(array $forms = null)
190 if (defined('TESTSUITE')) {
194 // return cached results
195 if ($names !== null) {
198 if (is_null($forms)) {
200 include 'libraries/config/user_preferences.forms.php';
203 foreach ($forms as $formset) {
204 foreach ($formset as $form) {
205 foreach ($form as $k => $v) {
206 $names[] = is_int($k) ?
$v : $k;
214 * Updates one user preferences option (loads and saves to database).
216 * No validation is done!
218 * @param string $path configuration
219 * @param mixed $value value
220 * @param mixed $default_value default value
224 function PMA_persistOption($path, $value, $default_value)
226 $prefs = PMA_loadUserprefs();
227 if ($value === $default_value) {
228 if (isset($prefs['config_data'][$path])) {
229 unset($prefs['config_data'][$path]);
234 $prefs['config_data'][$path] = $value;
236 PMA_saveUserprefs($prefs['config_data']);
240 * Redirects after saving new user preferences
242 * @param string $file_name Filename
243 * @param array $params URL parameters
244 * @param string $hash Hash value
248 function PMA_userprefsRedirect($file_name,
249 $params = null, $hash = null
252 $url_params = array('saved' => 1);
253 if (is_array($params)) {
254 $url_params = array_merge($params, $url_params);
257 $hash = '#' . urlencode($hash);
259 PMA_sendHeaderLocation('./' . $file_name
260 . URL
::getCommonRaw($url_params) . $hash
265 * Shows form which allows to quickly load
266 * settings stored in browser's local storage
270 function PMA_userprefsAutoloadGetHeader()
272 if (isset($_REQUEST['prefs_autoload'])
273 && $_REQUEST['prefs_autoload'] == 'hide'
275 $_SESSION['userprefs_autoload'] = true;
279 $script_name = basename(basename($GLOBALS['PMA_PHP_SELF']));
280 $return_url = htmlspecialchars(
281 $script_name . '?' . http_build_query($_GET, '', '&')
284 return PMA\libraries\Template
::get('prefs_autoload')
287 'hiddenInputs' => URL
::getHiddenInputs(),
288 'return_url' => $return_url,