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