Merge remote-tracking branch 'origin/master'
[phpmyadmin.git] / prefs_manage.php
bloba6b2bcdd9b525c3a70d254eb49ff3846ccaa2dff
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * User preferences management page
6 * @package PhpMyAdmin
7 */
9 /**
10 * Gets some core libraries and displays a top message if required
12 require_once 'libraries/common.inc.php';
13 require_once 'libraries/user_preferences.lib.php';
14 require_once 'libraries/config/config_functions.lib.php';
15 require_once 'libraries/config/messages.inc.php';
16 require_once 'libraries/config/ConfigFile.class.php';
17 require_once 'libraries/config/Form.class.php';
18 require_once 'libraries/config/FormDisplay.class.php';
19 require 'libraries/config/user_preferences.forms.php';
21 $cf = new ConfigFile($GLOBALS['PMA_Config']->base_settings);
22 PMA_userprefsPageInit($cf);
24 $error = '';
25 if (isset($_POST['submit_export'])
26 && isset($_POST['export_type'])
27 && $_POST['export_type'] == 'text_file'
28 ) {
29 // export to JSON file
30 PMA_Response::getInstance()->disable();
31 $filename = 'phpMyAdmin-config-' . urlencode(PMA_getenv('HTTP_HOST')) . '.json';
32 PMA_downloadHeader($filename, 'application/json');
33 $settings = PMA_loadUserprefs();
34 echo json_encode($settings['config_data'], JSON_PRETTY_PRINT);
35 exit;
36 } else if (isset($_POST['submit_get_json'])) {
37 $settings = PMA_loadUserprefs();
38 $response = PMA_Response::getInstance();
39 $response->addJSON('prefs', json_encode($settings['config_data']));
40 $response->addJSON('mtime', $settings['mtime']);
41 exit;
42 } else if (isset($_POST['submit_import'])) {
43 // load from JSON file
44 $json = '';
45 if (isset($_POST['import_type'])
46 && $_POST['import_type'] == 'text_file'
47 && isset($_FILES['import_file'])
48 && $_FILES['import_file']['error'] == UPLOAD_ERR_OK
49 && is_uploaded_file($_FILES['import_file']['tmp_name'])
50 ) {
51 // read JSON from uploaded file
52 $open_basedir = @ini_get('open_basedir');
53 $file_to_unlink = '';
54 $import_file = $_FILES['import_file']['tmp_name'];
56 // If we are on a server with open_basedir, we must move the file
57 // before opening it. The doc explains how to create the "./tmp"
58 // directory
59 if (!empty($open_basedir)) {
60 $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : 'tmp/');
61 if (is_writable($tmp_subdir)) {
62 $import_file_new = tempnam($tmp_subdir, 'prefs');
63 if (move_uploaded_file($import_file, $import_file_new)) {
64 $import_file = $import_file_new;
65 $file_to_unlink = $import_file_new;
69 $json = file_get_contents($import_file);
70 if ($file_to_unlink) {
71 unlink($file_to_unlink);
73 } else {
74 // read from POST value (json)
75 $json = isset($_POST['json']) ? $_POST['json'] : null;
78 // hide header message
79 $_SESSION['userprefs_autoload'] = true;
81 $config = json_decode($json, true);
82 $return_url = isset($_POST['return_url']) ? $_POST['return_url'] : null;
83 if (! is_array($config)) {
84 $error = __('Could not import configuration');
85 } else {
86 // sanitize input values: treat them as though
87 // they came from HTTP POST request
88 $form_display = new FormDisplay($cf);
89 foreach ($forms as $formset_id => $formset) {
90 foreach ($formset as $form_name => $form) {
91 $form_display->registerForm($formset_id . ': ' . $form_name, $form);
94 $new_config = $cf->getFlatDefaultConfig();
95 if (!empty($_POST['import_merge'])) {
96 $new_config = array_merge($new_config, $cf->getConfigArray());
98 $new_config = array_merge($new_config, $config);
99 $_POST_bak = $_POST;
100 foreach ($new_config as $k => $v) {
101 $_POST[str_replace('/', '-', $k)] = $v;
103 $cf->resetConfigData();
104 $all_ok = $form_display->process(true, false);
105 $all_ok = $all_ok && !$form_display->hasErrors();
106 $_POST = $_POST_bak;
108 if (!$all_ok && isset($_POST['fix_errors'])) {
109 $form_display->fixErrors();
110 $all_ok = true;
112 if (!$all_ok) {
113 // mimic original form and post json in a hidden field
114 include 'libraries/user_preferences.inc.php';
115 $msg = PMA_Message::error(
116 __('Configuration contains incorrect data for some fields.')
118 $msg->display();
119 echo '<div class="config-form">';
120 echo $form_display->displayErrors();
121 echo '</div>';
122 echo '<form action="prefs_manage.php" method="post">';
123 echo PMA_URL_getHiddenInputs() . "\n";
124 echo '<input type="hidden" name="json" value="'
125 . htmlspecialchars($json) . '" />';
126 echo '<input type="hidden" name="fix_errors" value="1" />';
127 if (! empty($_POST['import_merge'])) {
128 echo '<input type="hidden" name="import_merge" value="1" />';
130 if ($return_url) {
131 echo '<input type="hidden" name="return_url" value="'
132 . htmlspecialchars($return_url) . '" />';
134 echo '<p>';
135 echo __('Do you want to import remaining settings?');
136 echo '</p>';
137 echo '<input type="submit" name="submit_import" value="'
138 . __('Yes') . '" />';
139 echo '<input type="submit" name="submit_ignore" value="'
140 . __('No') . '" />';
141 echo '</form>';
142 exit;
145 // check for ThemeDefault and fontsize
146 $params = array();
147 if (isset($config['ThemeDefault'])
148 && $_SESSION['PMA_Theme_Manager']->theme->getId() != $config['ThemeDefault']
149 && $_SESSION['PMA_Theme_Manager']->checkTheme($config['ThemeDefault'])
151 $_SESSION['PMA_Theme_Manager']->setActiveTheme($config['ThemeDefault']);
152 $_SESSION['PMA_Theme_Manager']->setThemeCookie();
154 if (isset($config['fontsize'])
155 && $config['fontsize'] != $GLOBALS['PMA_Config']->get('fontsize')
157 $params['set_fontsize'] = $config['fontsize'];
159 if (isset($config['lang'])
160 && $config['lang'] != $GLOBALS['lang']
162 $params['lang'] = $config['lang'];
164 if (isset($config['collation_connection'])
165 && $config['collation_connection'] != $GLOBALS['collation_connection']
167 $params['collation_connection'] = $config['collation_connection'];
170 // save settings
171 $result = PMA_saveUserprefs($cf->getConfigArray());
172 if ($result === true) {
173 if ($return_url) {
174 $query = explode('&', parse_url($return_url, PHP_URL_QUERY));
175 $return_url = parse_url($return_url, PHP_URL_PATH);
177 /** @var PMA_String $pmaString */
178 $pmaString = $GLOBALS['PMA_String'];
180 foreach ($query as $q) {
181 $pos = /*overload*/mb_strpos($q, '=');
182 $k = /*overload*/mb_substr($q, 0, $pos);
183 if ($k == 'token') {
184 continue;
186 $params[$k] = /*overload*/mb_substr($q, $pos + 1);
188 } else {
189 $return_url = 'prefs_manage.php';
191 // reload config
192 $GLOBALS['PMA_Config']->loadUserPreferences();
193 PMA_userprefsRedirect($return_url, $params);
194 exit;
195 } else {
196 $error = $result;
199 } else if (isset($_POST['submit_clear'])) {
200 $result = PMA_saveUserprefs(array());
201 if ($result === true) {
202 $params = array();
203 if ($GLOBALS['PMA_Config']->get('fontsize') != '82%') {
204 $GLOBALS['PMA_Config']->removeCookie('pma_fontsize');
206 $GLOBALS['PMA_Config']->removeCookie('pma_collaction_connection');
207 $GLOBALS['PMA_Config']->removeCookie('pma_lang');
208 PMA_userprefsRedirect('prefs_manage.php', $params);
209 exit;
210 } else {
211 $error = $result;
213 exit;
216 $response = PMA_Response::getInstance();
217 $header = $response->getHeader();
218 $scripts = $header->getScripts();
219 $scripts->addFile('config.js');
221 require 'libraries/user_preferences.inc.php';
222 if ($error) {
223 if (!$error instanceof PMA_Message) {
224 $error = PMA_Message::error($error);
226 $error->display();
229 <script type="text/javascript">
230 <?php
231 PMA_printJsValue("PMA_messages['strSavedOn']", __('Saved on: @DATE@'));
233 </script>
234 <div id="maincontainer">
235 <div id="main_pane_left">
236 <div class="group">
237 <?php
238 echo '<h2>' . __('Import') . '</h2>'
239 . '<form class="group-cnt prefs-form disableAjax" name="prefs_import"'
240 . ' action="prefs_manage.php" method="post" enctype="multipart/form-data">'
241 . PMA_Util::generateHiddenMaxFileSize($GLOBALS['max_upload_size'])
242 . PMA_URL_getHiddenInputs()
243 . '<input type="hidden" name="json" value="" />'
244 . '<input type="radio" id="import_text_file" name="import_type"'
245 . ' value="text_file" checked="checked" />'
246 . '<label for="import_text_file">' . __('Import from file') . '</label>'
247 . '<div id="opts_import_text_file" class="prefsmanage_opts">'
248 . '<label for="input_import_file">' . __('Browse your computer:') . '</label>'
249 . '<input type="file" name="import_file" id="input_import_file" />'
250 . '</div>'
251 . '<input type="radio" id="import_local_storage" name="import_type"'
252 . ' value="local_storage" disabled="disabled" />'
253 . '<label for="import_local_storage">'
254 . __('Import from browser\'s storage') . '</label>'
255 . '<div id="opts_import_local_storage" class="prefsmanage_opts disabled">'
256 . '<div class="localStorage-supported">'
257 . __('Settings will be imported from your browser\'s local storage.')
258 . '<br />'
259 . '<div class="localStorage-exists">'
260 . __('Saved on: @DATE@')
261 . '</div>'
262 . '<div class="localStorage-empty">';
263 PMA_Message::notice(__('You have no saved settings!'))->display();
264 echo '</div>'
265 . '</div>'
266 . '<div class="localStorage-unsupported">';
267 PMA_Message::notice(
268 __('This feature is not supported by your web browser')
269 )->display();
270 echo '</div>'
271 . '</div>'
272 . '<input type="checkbox" id="import_merge" name="import_merge" />'
273 . '<label for="import_merge">'
274 . __('Merge with current configuration') . '</label>'
275 . '<br /><br />'
276 . '<input type="submit" name="submit_import" value="'
277 . __('Go') . '" />'
278 . '</form>'
279 . '</div>';
280 if (file_exists('setup/index.php')) {
281 // show only if setup script is available, allows to disable this message
282 // by simply removing setup directory
284 <div class="group">
285 <h2><?php echo __('More settings') ?></h2>
286 <div class="group-cnt">
287 <?php
288 echo sprintf(
290 'You can set more settings by modifying config.inc.php, eg. '
291 . 'by using %sSetup script%s.'
292 ), '<a href="setup/index.php" target="_blank">', '</a>'
293 ) . PMA_Util::showDocu('setup', 'setup-script');
295 </div>
296 </div>
297 <?php
300 </div>
301 <div id="main_pane_right">
302 <div class="group">
303 <h2><?php echo __('Export'); ?></h2>
304 <div class="click-hide-message group-cnt" style="display:none">
305 <?php
306 PMA_Message::rawSuccess(
307 __('Configuration has been saved.')
308 )->display();
310 </div>
311 <form class="group-cnt prefs-form disableAjax" name="prefs_export"
312 action="prefs_manage.php" method="post">
313 <?php echo PMA_URL_getHiddenInputs(); ?>
314 <div style="padding-bottom:0.5em">
315 <input type="radio" id="export_text_file" name="export_type"
316 value="text_file" checked="checked" />
317 <label for="export_text_file">
318 <?php echo __('Save as file'); ?>
319 </label><br />
320 <input type="radio" id="export_local_storage" name="export_type"
321 value="local_storage" disabled="disabled" />
322 <label for="export_local_storage">
323 <?php echo __('Save to browser\'s storage'); ?></label>
324 </div>
325 <div id="opts_export_local_storage"
326 class="prefsmanage_opts disabled">
327 <span class="localStorage-supported">
328 <?php
329 echo __(
330 'Settings will be saved in your browser\'s local '
331 . 'storage.'
334 <div class="localStorage-exists">
336 <?php
337 echo __(
338 'Existing settings will be overwritten!'
341 </b>
342 </div>
343 </span>
344 <div class="localStorage-unsupported">
345 <?php
346 PMA_Message::notice(
347 __('This feature is not supported by your web browser')
348 )->display();
350 </div>
351 </div>
352 <br />
353 <?php
354 echo '<input type="submit" name="submit_export" value="' . __(
355 'Go'
356 ) . '" />';
358 </form>
359 </div>
360 <div class="group">
361 <h2><?php echo __('Reset'); ?></h2>
362 <form class="group-cnt prefs-form disableAjax" name="prefs_reset"
363 action="prefs_manage.php" method="post">
364 <?php
365 echo PMA_URL_getHiddenInputs() . __(
366 'You can reset all your settings and restore them to default '
367 . 'values.'
370 <br /><br />
371 <input type="submit" name="submit_clear"
372 value="<?php echo __('Reset'); ?>"/>
373 </form>
374 </div>
375 </div>
376 <br class="clearfloat" />
377 </div>
379 <?php
380 if ($response->isAjax()) {
381 $response->addJSON('_disableNaviSettings', true);
382 } else {
383 define('PMA_DISABLE_NAVI_SETTINGS', true);