Translation update done using Pootle.
[phpmyadmin/madhuracj.git] / libraries / user_preferences.lib.php
blob84ca5c4fd82da09eb31c9eb4e36099e1015bf9da
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(
20 array(
21 'Server/hide_db' => 'Servers/1/hide_db',
22 'Server/only_db' => 'Servers/1/only_db'
25 $cf->updateWithGlobalConfig($GLOBALS['cfg']);
28 /**
29 * Loads user preferences
31 * Returns an array:
32 * * config_data - path => value pairs
33 * * mtime - last modification time
34 * * type - 'db' (config read from pmadb) or 'session' (read from user session)
36 * @return array
38 function PMA_load_userprefs()
40 $cfgRelation = PMA_getRelationsParam();
41 if (! $cfgRelation['userconfigwork']) {
42 // no pmadb table, use session storage
43 if (! isset($_SESSION['userconfig'])) {
44 $_SESSION['userconfig'] = array(
45 'db' => array(),
46 'ts' => time());
48 return array(
49 'config_data' => $_SESSION['userconfig']['db'],
50 'mtime' => $_SESSION['userconfig']['ts'],
51 'type' => 'session');
53 // load configuration from pmadb
54 $query_table = PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['userconfig']);
55 $query = '
56 SELECT `config_data`, UNIX_TIMESTAMP(`timevalue`) ts
57 FROM ' . $query_table . '
58 WHERE `username` = \'' . PMA_sqlAddSlashes($cfgRelation['user']) . '\'';
59 $row = PMA_DBI_fetch_single_row($query, 'ASSOC', $GLOBALS['controllink']);
61 return array(
62 'config_data' => $row ? (array)json_decode($row['config_data']) : array(),
63 'mtime' => $row ? $row['ts'] : time(),
64 'type' => 'db');
67 /**
68 * Saves user preferences
70 * @param array $config_array configuration array
72 * @return true|PMA_Message
74 function PMA_save_userprefs(array $config_array)
76 $cfgRelation = PMA_getRelationsParam();
77 $server = isset($GLOBALS['server'])
78 ? $GLOBALS['server']
79 : $GLOBALS['cfg']['ServerDefault'];
80 $cache_key = 'server_' . $server;
81 if (! $cfgRelation['userconfigwork']) {
82 // no pmadb table, use session storage
83 $_SESSION['userconfig'] = array(
84 'db' => $config_array,
85 'ts' => time());
86 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
87 unset($_SESSION['cache'][$cache_key]['userprefs']);
89 return true;
92 // save configuration to pmadb
93 $query_table = PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['userconfig']);
94 $query = '
95 SELECT `username`
96 FROM ' . $query_table . '
97 WHERE `username` = \'' . PMA_sqlAddSlashes($cfgRelation['user']) . '\'';
99 $has_config = PMA_DBI_fetch_value($query, 0, 0, $GLOBALS['controllink']);
100 $config_data = json_encode($config_array);
101 if ($has_config) {
102 $query = '
103 UPDATE ' . $query_table . '
104 SET `config_data` = \'' . PMA_sqlAddSlashes($config_data) . '\'
105 WHERE `username` = \'' . PMA_sqlAddSlashes($cfgRelation['user']) . '\'';
106 } else {
107 $query = '
108 INSERT INTO ' . $query_table . ' (`username`, `config_data`)
109 VALUES (\'' . PMA_sqlAddSlashes($cfgRelation['user']) . '\',
110 \'' . PMA_sqlAddSlashes($config_data) . '\')';
112 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
113 unset($_SESSION['cache'][$cache_key]['userprefs']);
115 if (!PMA_DBI_try_query($query, $GLOBALS['controllink'])) {
116 $message = PMA_Message::error(__('Could not save configuration'));
117 $message->addMessage('<br /><br />');
118 $message->addMessage(PMA_Message::rawError(PMA_DBI_getError($GLOBALS['controllink'])));
119 return $message;
121 return true;
125 * Returns a user preferences array filtered by $cfg['UserprefsDisallow']
126 * (blacklist) and keys from user preferences form (whitelist)
128 * @param array $config_data path => value pairs
130 * @return array
132 function PMA_apply_userprefs(array $config_data)
134 $cfg = array();
135 $blacklist = array_flip($GLOBALS['cfg']['UserprefsDisallow']);
136 if (!$GLOBALS['cfg']['UserprefsDeveloperTab']) {
137 // disallow everything in the Developers tab
138 $blacklist['Error_Handler/display'] = true;
139 $blacklist['Error_Handler/gather'] = true;
140 $blacklist['DBG/sql'] = true;
142 $whitelist = array_flip(PMA_read_userprefs_fieldnames());
143 // whitelist some additional fields which are custom handled
144 $whitelist['ThemeDefault'] = true;
145 $whitelist['fontsize'] = true;
146 $whitelist['lang'] = true;
147 $whitelist['collation_connection'] = true;
148 $whitelist['Server/hide_db'] = true;
149 $whitelist['Server/only_db'] = true;
150 foreach ($config_data as $path => $value) {
151 if (! isset($whitelist[$path]) || isset($blacklist[$path])) {
152 continue;
154 PMA_array_write($path, $cfg, $value);
156 return $cfg;
160 * Reads user preferences field names
162 * @param array|null $forms
164 * @return array
166 function PMA_read_userprefs_fieldnames(array $forms = null)
168 static $names;
170 // return cached results
171 if ($names !== null) {
172 return $names;
174 if (is_null($forms)) {
175 $forms = array();
176 include 'libraries/config/user_preferences.forms.php';
178 $names = array();
179 foreach ($forms as $formset) {
180 foreach ($formset as $form) {
181 foreach ($form as $k => $v) {
182 $names[] = is_int($k) ? $v : $k;
186 return $names;
190 * Updates one user preferences option (loads and saves to database).
192 * No validation is done!
194 * @param string $path configuration
195 * @param mixed $value value
196 * @param mixed $default_value default value
198 * @return void
200 function PMA_persist_option($path, $value, $default_value)
202 $prefs = PMA_load_userprefs();
203 if ($value === $default_value) {
204 if (isset($prefs['config_data'][$path])) {
205 unset($prefs['config_data'][$path]);
206 } else {
207 return;
209 } else {
210 $prefs['config_data'][$path] = $value;
212 PMA_save_userprefs($prefs['config_data']);
216 * Redirects after saving new user preferences
218 * @param array $forms
219 * @param array $old_settings
220 * @param string $file_name
221 * @param array $params
222 * @param string $hash
224 function PMA_userprefs_redirect(array $forms, array $old_settings, $file_name, $params = null, $hash = null)
226 $reload_left_frame = isset($params['reload_left_frame']) && $params['reload_left_frame'];
227 if (!$reload_left_frame) {
228 // compute differences and check whether left frame should be refreshed
229 $old_settings = isset($old_settings['config_data'])
230 ? $old_settings['config_data']
231 : array();
232 $new_settings = ConfigFile::getInstance()->getConfigArray();
233 $diff_keys = array_keys(
234 array_diff_assoc($old_settings, $new_settings)
235 + array_diff_assoc($new_settings, $old_settings)
237 $check_keys = array('NaturalOrder', 'MainPageIconic', 'DefaultTabDatabase',
238 'Server/hide_db', 'Server/only_db');
239 $check_keys = array_merge(
240 $check_keys, $forms['Left_frame']['Left_frame'],
241 $forms['Left_frame']['Left_databases']
243 $diff = array_intersect($check_keys, $diff_keys);
244 $reload_left_frame = !empty($diff);
247 // redirect
248 $url_params = array(
249 'saved' => 1,
250 'reload_left_frame' => $reload_left_frame);
251 if (is_array($params)) {
252 $url_params = array_merge($params, $url_params);
254 if ($hash) {
255 $hash = '#' . urlencode($hash);
257 PMA_sendHeaderLocation(
258 $GLOBALS['cfg']['PmaAbsoluteUri'] . $file_name
259 . PMA_generate_common_url($url_params, '&') . $hash
264 * Shows form which allows to quickly load settings stored in browser's local storage
267 function PMA_userprefs_autoload_header()
269 if (isset($_REQUEST['prefs_autoload']) && $_REQUEST['prefs_autoload'] == 'hide') {
270 $_SESSION['userprefs_autoload'] = true;
271 exit;
273 $script_name = basename(basename($GLOBALS['PMA_PHP_SELF']));
274 $return_url = $script_name . '?' . http_build_query($_GET, '', '&');
276 <div id="prefs_autoload" class="notice" style="display:none">
277 <form action="prefs_manage.php" method="post">
278 <?php echo PMA_generate_common_hidden_inputs() . "\n"; ?>
279 <input type="hidden" name="json" value="" />
280 <input type="hidden" name="submit_import" value="1" />
281 <input type="hidden" name="return_url" value="<?php echo htmlspecialchars($return_url) ?>" />
282 <?php echo __('Your browser has phpMyAdmin configuration for this domain. Would you like to import it for current session?') ?>
283 <br />
284 <a href="#yes"><?php echo __('Yes') ?></a> / <a href="#no"><?php echo __('No') ?></a>
285 </form>
286 </div>
287 <?php