Translation update done using Pootle.
[phpmyadmin/ammaryasirr.git] / libraries / user_preferences.lib.php
blob2246a4df8fcf0ea591f17953c763314a922de34a
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
13 function PMA_userprefs_pageinit()
15 $forms_all_keys = PMA_read_userprefs_fieldnames($GLOBALS['forms']);
16 $cf = ConfigFile::getInstance();
17 $cf->resetConfigData(); // start with a clean instance
18 $cf->setAllowedKeys($forms_all_keys);
19 $cf->setCfgUpdateReadMapping(array(
20 'Server/hide_db' => 'Servers/1/hide_db',
21 'Server/only_db' => 'Servers/1/only_db'));
22 $cf->updateWithGlobalConfig($GLOBALS['cfg']);
25 /**
26 * Loads user preferences
28 * Returns an array:
29 * * config_data - path => value pairs
30 * * mtime - last modification time
31 * * type - 'db' (config read from pmadb) or 'session' (read from user session)
33 * @return array
35 function PMA_load_userprefs()
37 $cfgRelation = PMA_getRelationsParam();
38 if (! $cfgRelation['userconfigwork']) {
39 // no pmadb table, use session storage
40 if (! isset($_SESSION['userconfig'])) {
41 $_SESSION['userconfig'] = array(
42 'db' => array(),
43 'ts' => time());
45 return array(
46 'config_data' => $_SESSION['userconfig']['db'],
47 'mtime' => $_SESSION['userconfig']['ts'],
48 'type' => 'session');
50 // load configuration from pmadb
51 $query_table = PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['userconfig']);
52 $query = '
53 SELECT `config_data`, UNIX_TIMESTAMP(`timevalue`) ts
54 FROM ' . $query_table . '
55 WHERE `username` = \'' . PMA_sqlAddSlashes($cfgRelation['user']) . '\'';
56 $row = PMA_DBI_fetch_single_row($query, 'ASSOC', $GLOBALS['controllink']);
58 return array(
59 'config_data' => $row ? (array)json_decode($row['config_data']) : array(),
60 'mtime' => $row ? $row['ts'] : time(),
61 'type' => 'db');
64 /**
65 * Saves user preferences
67 * @param array $config_data
68 * @return true|PMA_Message
70 function PMA_save_userprefs(array $config_array)
72 $cfgRelation = PMA_getRelationsParam();
73 $server = isset($GLOBALS['server'])
74 ? $GLOBALS['server']
75 : $GLOBALS['cfg']['ServerDefault'];
76 $cache_key = 'server_' . $server;
77 if (! $cfgRelation['userconfigwork']) {
78 // no pmadb table, use session storage
79 $_SESSION['userconfig'] = array(
80 'db' => $config_array,
81 'ts' => time());
82 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
83 unset($_SESSION['cache'][$cache_key]['userprefs']);
85 return true;
88 // save configuration to pmadb
89 $query_table = PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['userconfig']);
90 $query = '
91 SELECT `username`
92 FROM ' . $query_table . '
93 WHERE `username` = \'' . PMA_sqlAddSlashes($cfgRelation['user']) . '\'';
95 $has_config = PMA_DBI_fetch_value($query, 0, 0, $GLOBALS['controllink']);
96 $config_data = json_encode($config_array);
97 if ($has_config) {
98 $query = '
99 UPDATE ' . $query_table . '
100 SET `config_data` = \'' . PMA_sqlAddSlashes($config_data) . '\'
101 WHERE `username` = \'' . PMA_sqlAddSlashes($cfgRelation['user']) . '\'';
102 } else {
103 $query = '
104 INSERT INTO ' . $query_table . ' (`username`, `config_data`)
105 VALUES (\'' . PMA_sqlAddSlashes($cfgRelation['user']) . '\',
106 \'' . PMA_sqlAddSlashes($config_data) . '\')';
108 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
109 unset($_SESSION['cache'][$cache_key]['userprefs']);
111 if (!PMA_DBI_try_query($query, $GLOBALS['controllink'])) {
112 $message = PMA_Message::error(__('Could not save configuration'));
113 $message->addMessage('<br /><br />');
114 $message->addMessage(PMA_Message::rawError(PMA_DBI_getError($GLOBALS['controllink'])));
115 return $message;
117 return true;
121 * Returns a user preferences array filtered by $cfg['UserprefsDisallow']
122 * (blacklist) and keys from user preferences form (whitelist)
124 * @param array $config_data path => value pairs
125 * @return array
127 function PMA_apply_userprefs(array $config_data)
129 $cfg = array();
130 $blacklist = array_flip($GLOBALS['cfg']['UserprefsDisallow']);
131 if (!$GLOBALS['cfg']['UserprefsDeveloperTab']) {
132 // disallow everything in the Developers tab
133 $blacklist['Error_Handler/display'] = true;
134 $blacklist['Error_Handler/gather'] = true;
135 $blacklist['DBG/sql'] = true;
137 $whitelist = array_flip(PMA_read_userprefs_fieldnames());
138 // whitelist some additional fields which are custom handled
139 $whitelist['ThemeDefault'] = true;
140 $whitelist['fontsize'] = true;
141 $whitelist['lang'] = true;
142 $whitelist['collation_connection'] = true;
143 $whitelist['Server/hide_db'] = true;
144 $whitelist['Server/only_db'] = true;
145 foreach ($config_data as $path => $value) {
146 if (! isset($whitelist[$path]) || isset($blacklist[$path])) {
147 continue;
149 PMA_array_write($path, $cfg, $value);
151 return $cfg;
155 * Reads user preferences field names
157 * @param array|null $forms
158 * @return array
160 function PMA_read_userprefs_fieldnames(array $forms = null)
162 static $names;
164 // return cached results
165 if ($names !== null) {
166 return $names;
168 if (is_null($forms)) {
169 $forms = array();
170 include 'libraries/config/user_preferences.forms.php';
172 $names = array();
173 foreach ($forms as $formset) {
174 foreach ($formset as $form) {
175 foreach ($form as $k => $v) {
176 $names[] = is_int($k) ? $v : $k;
180 return $names;
184 * Updates one user preferences option (loads and saves to database).
186 * No validation is done!
188 * @param string $cfg_name
189 * @param mixed $value
190 * @return void
192 function PMA_persist_option($path, $value, $default_value)
194 $prefs = PMA_load_userprefs();
195 if ($value === $default_value) {
196 if (isset($prefs['config_data'][$path])) {
197 unset($prefs['config_data'][$path]);
198 } else {
199 return;
201 } else {
202 $prefs['config_data'][$path] = $value;
204 PMA_save_userprefs($prefs['config_data']);
208 * Redirects after saving new user preferences
210 * @param array $forms
211 * @param array $old_settings
212 * @param string $file_name
213 * @param array $params
214 * @param string $hash
216 function PMA_userprefs_redirect(array $forms, array $old_settings, $file_name, $params = null, $hash = null)
218 $reload_left_frame = isset($params['reload_left_frame']) && $params['reload_left_frame'];
219 if (!$reload_left_frame) {
220 // compute differences and check whether left frame should be refreshed
221 $old_settings = isset($old_settings['config_data'])
222 ? $old_settings['config_data']
223 : array();
224 $new_settings = ConfigFile::getInstance()->getConfigArray();
225 $diff_keys = array_keys(array_diff_assoc($old_settings, $new_settings)
226 + array_diff_assoc($new_settings, $old_settings));
227 $check_keys = array('NaturalOrder', 'MainPageIconic', 'DefaultTabDatabase',
228 'Server/hide_db', 'Server/only_db');
229 $check_keys = array_merge($check_keys, $forms['Left_frame']['Left_frame'],
230 $forms['Left_frame']['Left_databases']);
231 $diff = array_intersect($check_keys, $diff_keys);
232 $reload_left_frame = !empty($diff);
235 // redirect
236 $url_params = array(
237 'saved' => 1,
238 'reload_left_frame' => $reload_left_frame);
239 if (is_array($params)) {
240 $url_params = array_merge($params, $url_params);
242 if ($hash) {
243 $hash = '#' . urlencode($hash);
245 PMA_sendHeaderLocation($GLOBALS['cfg']['PmaAbsoluteUri'] . $file_name
246 . PMA_generate_common_url($url_params, '&') . $hash);
250 * Shows form which allows to quickly load settings stored in browser's local storage
253 function PMA_userprefs_autoload_header()
255 if (isset($_REQUEST['prefs_autoload']) && $_REQUEST['prefs_autoload'] == 'hide') {
256 $_SESSION['userprefs_autoload'] = true;
257 exit;
259 $script_name = basename(basename($GLOBALS['PMA_PHP_SELF']));
260 $return_url = $script_name . '?' . http_build_query($_GET, '', '&');
262 <div id="prefs_autoload" class="notice" style="display:none">
263 <form action="prefs_manage.php" method="post">
264 <?php echo PMA_generate_common_hidden_inputs() . "\n"; ?>
265 <input type="hidden" name="json" value="" />
266 <input type="hidden" name="submit_import" value="1" />
267 <input type="hidden" name="return_url" value="<?php echo htmlspecialchars($return_url) ?>" />
268 <?php echo __('Your browser has phpMyAdmin configuration for this domain. Would you like to import it for current session?') ?>
269 <br />
270 <a href="#yes"><?php echo __('Yes') ?></a> / <a href="#no"><?php echo __('No') ?></a>
271 </form>
272 </div>
273 <?php