Translated using Weblate (Estonian)
[phpmyadmin.git] / libraries / user_preferences.lib.php
blob5c2209dea1ee126b9df0a00292597e70db3b0414
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 PhpMyAdmin\Config\ConfigFile;
9 use PhpMyAdmin\Config\Forms\User\UserFormList;
10 use PhpMyAdmin\Core;
11 use PhpMyAdmin\Message;
12 use PhpMyAdmin\Relation;
13 use PhpMyAdmin\Url;
15 if (! defined('PHPMYADMIN')) {
16 exit;
19 /**
20 * Common initialization for user preferences modification pages
22 * @param ConfigFile $cf Config file instance
24 * @return void
26 function PMA_userprefsPageInit(ConfigFile $cf)
28 $forms_all_keys = UserFormList::getFields();
29 $cf->resetConfigData(); // start with a clean instance
30 $cf->setAllowedKeys($forms_all_keys);
31 $cf->setCfgUpdateReadMapping(
32 array(
33 'Server/hide_db' => 'Servers/1/hide_db',
34 'Server/only_db' => 'Servers/1/only_db'
37 $cf->updateWithGlobalConfig($GLOBALS['cfg']);
40 /**
41 * Loads user preferences
43 * Returns an array:
44 * * config_data - path => value pairs
45 * * mtime - last modification time
46 * * type - 'db' (config read from pmadb) or 'session' (read from user session)
48 * @return array
50 function PMA_loadUserprefs()
52 $cfgRelation = Relation::getRelationsParam();
53 if (! $cfgRelation['userconfigwork']) {
54 // no pmadb table, use session storage
55 if (! isset($_SESSION['userconfig'])) {
56 $_SESSION['userconfig'] = array(
57 'db' => array(),
58 'ts' => time());
60 return array(
61 'config_data' => $_SESSION['userconfig']['db'],
62 'mtime' => $_SESSION['userconfig']['ts'],
63 'type' => 'session');
65 // load configuration from pmadb
66 $query_table = PhpMyAdmin\Util::backquote($cfgRelation['db']) . '.'
67 . PhpMyAdmin\Util::backquote($cfgRelation['userconfig']);
68 $query = 'SELECT `config_data`, UNIX_TIMESTAMP(`timevalue`) ts'
69 . ' FROM ' . $query_table
70 . ' WHERE `username` = \''
71 . $GLOBALS['dbi']->escapeString($cfgRelation['user'])
72 . '\'';
73 $row = $GLOBALS['dbi']->fetchSingleRow($query, 'ASSOC', $GLOBALS['controllink']);
75 return array(
76 'config_data' => $row ? (array)json_decode($row['config_data']) : array(),
77 'mtime' => $row ? $row['ts'] : time(),
78 'type' => 'db');
81 /**
82 * Saves user preferences
84 * @param array $config_array configuration array
86 * @return true|PhpMyAdmin\Message
88 function PMA_saveUserprefs(array $config_array)
90 $cfgRelation = Relation::getRelationsParam();
91 $server = isset($GLOBALS['server'])
92 ? $GLOBALS['server']
93 : $GLOBALS['cfg']['ServerDefault'];
94 $cache_key = 'server_' . $server;
95 if (! $cfgRelation['userconfigwork']) {
96 // no pmadb table, use session storage
97 $_SESSION['userconfig'] = array(
98 'db' => $config_array,
99 'ts' => time());
100 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
101 unset($_SESSION['cache'][$cache_key]['userprefs']);
103 return true;
106 // save configuration to pmadb
107 $query_table = PhpMyAdmin\Util::backquote($cfgRelation['db']) . '.'
108 . PhpMyAdmin\Util::backquote($cfgRelation['userconfig']);
109 $query = 'SELECT `username` FROM ' . $query_table
110 . ' WHERE `username` = \''
111 . $GLOBALS['dbi']->escapeString($cfgRelation['user'])
112 . '\'';
114 $has_config = $GLOBALS['dbi']->fetchValue(
115 $query, 0, 0, $GLOBALS['controllink']
117 $config_data = json_encode($config_array);
118 if ($has_config) {
119 $query = 'UPDATE ' . $query_table
120 . ' SET `timevalue` = NOW(), `config_data` = \''
121 . $GLOBALS['dbi']->escapeString($config_data)
122 . '\''
123 . ' WHERE `username` = \''
124 . $GLOBALS['dbi']->escapeString($cfgRelation['user'])
125 . '\'';
126 } else {
127 $query = 'INSERT INTO ' . $query_table
128 . ' (`username`, `timevalue`,`config_data`) '
129 . 'VALUES (\''
130 . $GLOBALS['dbi']->escapeString($cfgRelation['user']) . '\', NOW(), '
131 . '\'' . $GLOBALS['dbi']->escapeString($config_data) . '\')';
133 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
134 unset($_SESSION['cache'][$cache_key]['userprefs']);
136 if (!$GLOBALS['dbi']->tryQuery($query, $GLOBALS['controllink'])) {
137 $message = Message::error(__('Could not save configuration'));
138 $message->addMessage(
139 Message::rawError(
140 $GLOBALS['dbi']->getError($GLOBALS['controllink'])
142 '<br /><br />'
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 * @param array $config_data path => value pairs
155 * @return array
157 function PMA_applyUserprefs(array $config_data)
159 $cfg = array();
160 $blacklist = array_flip($GLOBALS['cfg']['UserprefsDisallow']);
161 $whitelist = array_flip(UserFormList::getFields());
162 // whitelist some additional fields which are custom handled
163 $whitelist['ThemeDefault'] = true;
164 $whitelist['fontsize'] = true;
165 $whitelist['lang'] = true;
166 $whitelist['collation_connection'] = true;
167 $whitelist['Server/hide_db'] = true;
168 $whitelist['Server/only_db'] = true;
169 foreach ($config_data as $path => $value) {
170 if (! isset($whitelist[$path]) || isset($blacklist[$path])) {
171 continue;
173 Core::arrayWrite($path, $cfg, $value);
175 return $cfg;
179 * Updates one user preferences option (loads and saves to database).
181 * No validation is done!
183 * @param string $path configuration
184 * @param mixed $value value
185 * @param mixed $default_value default value
187 * @return void
189 function PMA_persistOption($path, $value, $default_value)
191 $prefs = PMA_loadUserprefs();
192 if ($value === $default_value) {
193 if (isset($prefs['config_data'][$path])) {
194 unset($prefs['config_data'][$path]);
195 } else {
196 return;
198 } else {
199 $prefs['config_data'][$path] = $value;
201 PMA_saveUserprefs($prefs['config_data']);
205 * Redirects after saving new user preferences
207 * @param string $file_name Filename
208 * @param array $params URL parameters
209 * @param string $hash Hash value
211 * @return void
213 function PMA_userprefsRedirect($file_name,
214 $params = null, $hash = null
216 // redirect
217 $url_params = array('saved' => 1);
218 if (is_array($params)) {
219 $url_params = array_merge($params, $url_params);
221 if ($hash) {
222 $hash = '#' . urlencode($hash);
224 Core::sendHeaderLocation('./' . $file_name
225 . Url::getCommonRaw($url_params) . $hash
230 * Shows form which allows to quickly load
231 * settings stored in browser's local storage
233 * @return string
235 function PMA_userprefsAutoloadGetHeader()
237 if (isset($_REQUEST['prefs_autoload'])
238 && $_REQUEST['prefs_autoload'] == 'hide'
240 $_SESSION['userprefs_autoload'] = true;
241 return '';
244 $script_name = basename(basename($GLOBALS['PMA_PHP_SELF']));
245 $return_url = $script_name . '?' . http_build_query($_GET, '', '&');
247 return PhpMyAdmin\Template::get('prefs_autoload')
248 ->render(
249 array(
250 'hidden_inputs' => Url::getHiddenInputs(),
251 'return_url' => $return_url,