Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / phpmyadmin / libraries / user_preferences.lib.php
blob94699ad7fa30230effa2d3ef2a7c13f1e5bd6fb5
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 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * Common initialization for user preferences modification pages
15 * @return void
17 function PMA_userprefsPageInit()
19 $forms_all_keys = PMA_readUserprefsFieldNames($GLOBALS['forms']);
20 $cf = ConfigFile::getInstance();
21 $cf->resetConfigData(); // start with a clean instance
22 $cf->setAllowedKeys($forms_all_keys);
23 $cf->setCfgUpdateReadMapping(
24 array(
25 'Server/hide_db' => 'Servers/1/hide_db',
26 'Server/only_db' => 'Servers/1/only_db'
29 $cf->updateWithGlobalConfig($GLOBALS['cfg']);
32 /**
33 * Loads user preferences
35 * Returns an array:
36 * * config_data - path => value pairs
37 * * mtime - last modification time
38 * * type - 'db' (config read from pmadb) or 'session' (read from user session)
40 * @return array
42 function PMA_loadUserprefs()
44 $cfgRelation = PMA_getRelationsParam();
45 if (! $cfgRelation['userconfigwork']) {
46 // no pmadb table, use session storage
47 if (! isset($_SESSION['userconfig'])) {
48 $_SESSION['userconfig'] = array(
49 'db' => array(),
50 'ts' => time());
52 return array(
53 'config_data' => $_SESSION['userconfig']['db'],
54 'mtime' => $_SESSION['userconfig']['ts'],
55 'type' => 'session');
57 // load configuration from pmadb
58 $query_table = PMA_Util::backquote($cfgRelation['db']) . '.'
59 . PMA_Util::backquote($cfgRelation['userconfig']);
60 $query = '
61 SELECT `config_data`, UNIX_TIMESTAMP(`timevalue`) ts
62 FROM ' . $query_table . '
63 WHERE `username` = \'' . PMA_Util::sqlAddSlashes($cfgRelation['user']) . '\'';
64 $row = PMA_DBI_fetch_single_row($query, 'ASSOC', $GLOBALS['controllink']);
66 return array(
67 'config_data' => $row ? (array)json_decode($row['config_data']) : array(),
68 'mtime' => $row ? $row['ts'] : time(),
69 'type' => 'db');
72 /**
73 * Saves user preferences
75 * @param array $config_array configuration array
77 * @return true|PMA_Message
79 function PMA_saveUserprefs(array $config_array)
81 $cfgRelation = PMA_getRelationsParam();
82 $server = isset($GLOBALS['server'])
83 ? $GLOBALS['server']
84 : $GLOBALS['cfg']['ServerDefault'];
85 $cache_key = 'server_' . $server;
86 if (! $cfgRelation['userconfigwork']) {
87 // no pmadb table, use session storage
88 $_SESSION['userconfig'] = array(
89 'db' => $config_array,
90 'ts' => time());
91 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
92 unset($_SESSION['cache'][$cache_key]['userprefs']);
94 return true;
97 // save configuration to pmadb
98 $query_table = PMA_Util::backquote($cfgRelation['db']) . '.'
99 . PMA_Util::backquote($cfgRelation['userconfig']);
100 $query = '
101 SELECT `username`
102 FROM ' . $query_table . '
103 WHERE `username` = \'' . PMA_Util::sqlAddSlashes($cfgRelation['user']) . '\'';
105 $has_config = PMA_DBI_fetch_value($query, 0, 0, $GLOBALS['controllink']);
106 $config_data = json_encode($config_array);
107 if ($has_config) {
108 $query = '
109 UPDATE ' . $query_table . '
110 SET `config_data` = \'' . PMA_Util::sqlAddSlashes($config_data) . '\'
111 WHERE `username` = \'' . PMA_Util::sqlAddSlashes($cfgRelation['user']) . '\'';
112 } else {
113 $query = '
114 INSERT INTO ' . $query_table . ' (`username`, `config_data`)
115 VALUES (\'' . PMA_Util::sqlAddSlashes($cfgRelation['user']) . '\',
116 \'' . PMA_Util::sqlAddSlashes($config_data) . '\')';
118 if (isset($_SESSION['cache'][$cache_key]['userprefs'])) {
119 unset($_SESSION['cache'][$cache_key]['userprefs']);
121 if (!PMA_DBI_try_query($query, $GLOBALS['controllink'])) {
122 $message = PMA_Message::error(__('Could not save configuration'));
123 $message->addMessage('<br /><br />');
124 $message->addMessage(
125 PMA_Message::rawError(PMA_DBI_getError($GLOBALS['controllink']))
127 return $message;
129 return true;
133 * Returns a user preferences array filtered by $cfg['UserprefsDisallow']
134 * (blacklist) and keys from user preferences form (whitelist)
136 * @param array $config_data path => value pairs
138 * @return array
140 function PMA_applyUserprefs(array $config_data)
142 $cfg = array();
143 $blacklist = array_flip($GLOBALS['cfg']['UserprefsDisallow']);
144 if (!$GLOBALS['cfg']['UserprefsDeveloperTab']) {
145 // disallow everything in the Developers tab
146 $blacklist['Error_Handler/display'] = true;
147 $blacklist['Error_Handler/gather'] = true;
148 $blacklist['DBG/sql'] = true;
150 $whitelist = array_flip(PMA_readUserprefsFieldNames());
151 // whitelist some additional fields which are custom handled
152 $whitelist['ThemeDefault'] = true;
153 $whitelist['fontsize'] = true;
154 $whitelist['lang'] = true;
155 $whitelist['collation_connection'] = true;
156 $whitelist['Server/hide_db'] = true;
157 $whitelist['Server/only_db'] = true;
158 foreach ($config_data as $path => $value) {
159 if (! isset($whitelist[$path]) || isset($blacklist[$path])) {
160 continue;
162 PMA_arrayWrite($path, $cfg, $value);
164 return $cfg;
168 * Reads user preferences field names
170 * @param array|null $forms
172 * @return array
174 function PMA_readUserprefsFieldNames(array $forms = null)
176 static $names;
178 // return cached results
179 if ($names !== null) {
180 return $names;
182 if (is_null($forms)) {
183 $forms = array();
184 include 'libraries/config/user_preferences.forms.php';
186 $names = array();
187 foreach ($forms as $formset) {
188 foreach ($formset as $form) {
189 foreach ($form as $k => $v) {
190 $names[] = is_int($k) ? $v : $k;
194 return $names;
198 * Updates one user preferences option (loads and saves to database).
200 * No validation is done!
202 * @param string $path configuration
203 * @param mixed $value value
204 * @param mixed $default_value default value
206 * @return void
208 function PMA_persistOption($path, $value, $default_value)
210 $prefs = PMA_loadUserprefs();
211 if ($value === $default_value) {
212 if (isset($prefs['config_data'][$path])) {
213 unset($prefs['config_data'][$path]);
214 } else {
215 return;
217 } else {
218 $prefs['config_data'][$path] = $value;
220 PMA_saveUserprefs($prefs['config_data']);
224 * Redirects after saving new user preferences
226 * @param string $file_name
227 * @param array $params
228 * @param string $hash
230 * @return void
232 function PMA_userprefsRedirect($file_name,
233 $params = null, $hash = null
235 // redirect
236 $url_params = array('saved' => 1);
237 if (is_array($params)) {
238 $url_params = array_merge($params, $url_params);
240 if ($hash) {
241 $hash = '#' . urlencode($hash);
243 PMA_sendHeaderLocation(
244 $GLOBALS['cfg']['PmaAbsoluteUri'] . $file_name
245 . PMA_generate_common_url($url_params, '&') . $hash
250 * Shows form which allows to quickly load
251 * settings stored in browser's local storage
253 * @return string
255 function PMA_userprefsAutoloadGetHeader()
257 $retval = '';
259 if (isset($_REQUEST['prefs_autoload'])
260 && $_REQUEST['prefs_autoload'] == 'hide'
262 $_SESSION['userprefs_autoload'] = true;
263 } else {
264 $script_name = basename(basename($GLOBALS['PMA_PHP_SELF']));
265 $return_url = htmlspecialchars(
266 $script_name . '?' . http_build_query($_GET, '', '&')
269 $retval .= '<div id="prefs_autoload" class="notice" style="display:none">';
270 $retval .= '<form action="prefs_manage.php" method="post">';
271 $retval .= PMA_generate_common_hidden_inputs();
272 $retval .= '<input type="hidden" name="json" value="" />';
273 $retval .= '<input type="hidden" name="submit_import" value="1" />';
274 $retval .= '<input type="hidden" name="return_url" value="' . $return_url . '" />';
275 $retval .= __(
276 'Your browser has phpMyAdmin configuration for this domain. '
277 . 'Would you like to import it for current session?'
279 $retval .= '<br />';
280 $retval .= '<a href="#yes">' . __('Yes') . '</a>';
281 $retval .= ' / ';
282 $retval .= '<a href="#no">' . __('No') . '</a>';
283 $retval .= '</form>';
284 $retval .= '</div>';
286 return $retval;