Remove broken link
[phpmyadmin.git] / libraries / user_preferences.lib.php
blob2c747bdfb486eb2961956b3b0a8c12b513e208a8
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 use PMA\libraries\config\ConfigFile;
9 use PMA\libraries\Message;
10 use PMA\libraries\URL;
12 if (! defined('PHPMYADMIN')) {
13 exit;
16 /**
17 * Common initialization for user preferences modification pages
19 * @param ConfigFile $cf Config file instance
21 * @return void
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(
29 array(
30 'Server/hide_db' => 'Servers/1/hide_db',
31 'Server/only_db' => 'Servers/1/only_db'
34 $cf->updateWithGlobalConfig($GLOBALS['cfg']);
37 /**
38 * Loads user preferences
40 * Returns an array:
41 * * config_data - path => value pairs
42 * * mtime - last modification time
43 * * type - 'db' (config read from pmadb) or 'session' (read from user session)
45 * @return array
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(
54 'db' => array(),
55 'ts' => time());
57 return array(
58 'config_data' => $_SESSION['userconfig']['db'],
59 'mtime' => $_SESSION['userconfig']['ts'],
60 'type' => 'session');
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'])
69 . '\'';
70 $row = $GLOBALS['dbi']->fetchSingleRow($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 * @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'])
89 ? $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,
96 'ts' => time());
97 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
98 unset($_SESSION['cache'][$cache_key]['userprefs']);
100 return true;
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'])
109 . '\'';
111 $has_config = $GLOBALS['dbi']->fetchValue(
112 $query, 0, 0, $GLOBALS['controllink']
114 $config_data = json_encode($config_array);
115 if ($has_config) {
116 $query = 'UPDATE ' . $query_table
117 . ' SET `timevalue` = NOW(), `config_data` = \''
118 . $GLOBALS['dbi']->escapeString($config_data)
119 . '\''
120 . ' WHERE `username` = \''
121 . $GLOBALS['dbi']->escapeString($cfgRelation['user'])
122 . '\'';
123 } else {
124 $query = 'INSERT INTO ' . $query_table
125 . ' (`username`, `timevalue`,`config_data`) '
126 . 'VALUES (\''
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(
136 Message::rawError(
137 $GLOBALS['dbi']->getError($GLOBALS['controllink'])
139 '<br /><br />'
141 return $message;
143 return true;
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
152 * @return array
154 function PMA_applyUserprefs(array $config_data)
156 $cfg = array();
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])) {
172 continue;
174 PMA_arrayWrite($path, $cfg, $value);
176 return $cfg;
180 * Reads user preferences field names
182 * @param array|null $forms Forms
184 * @return array
186 function PMA_readUserprefsFieldNames(array $forms = null)
188 static $names;
190 if (defined('TESTSUITE')) {
191 $names = null;
194 // return cached results
195 if ($names !== null) {
196 return $names;
198 if (is_null($forms)) {
199 $forms = array();
200 include 'libraries/config/user_preferences.forms.php';
202 $names = array();
203 foreach ($forms as $formset) {
204 foreach ($formset as $form) {
205 foreach ($form as $k => $v) {
206 $names[] = is_int($k) ? $v : $k;
210 return $names;
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
222 * @return void
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]);
230 } else {
231 return;
233 } else {
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
246 * @return void
248 function PMA_userprefsRedirect($file_name,
249 $params = null, $hash = null
251 // redirect
252 $url_params = array('saved' => 1);
253 if (is_array($params)) {
254 $url_params = array_merge($params, $url_params);
256 if ($hash) {
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
268 * @return string
270 function PMA_userprefsAutoloadGetHeader()
272 if (isset($_REQUEST['prefs_autoload'])
273 && $_REQUEST['prefs_autoload'] == 'hide'
275 $_SESSION['userprefs_autoload'] = true;
276 return '';
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')
285 ->render(
286 array(
287 'hiddenInputs' => URL::getHiddenInputs(),
288 'return_url' => $return_url,