Merge remote-tracking branch 'origin/master'
[phpmyadmin.git] / prefs_manage.php
blob5eedd881ab674fca83fa5d3b033deddf71e9a2c3
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * User preferences management page
6 * @package PhpMyAdmin
7 */
8 use PhpMyAdmin\Config\ConfigFile;
9 use PhpMyAdmin\Config\FormDisplay;
10 use PhpMyAdmin\Core;
11 use PhpMyAdmin\File;
12 use PhpMyAdmin\Message;
13 use PhpMyAdmin\Response;
14 use PhpMyAdmin\Util;
15 use PhpMyAdmin\Url;
16 use PhpMyAdmin\Sanitize;
17 use PhpMyAdmin\ThemeManager;
19 /**
20 * Gets some core libraries and displays a top message if required
22 require_once 'libraries/common.inc.php';
23 require_once 'libraries/user_preferences.lib.php';
24 require_once 'libraries/config/config_functions.lib.php';
25 require_once 'libraries/config/messages.inc.php';
26 require 'libraries/config/user_preferences.forms.php';
28 $cf = new ConfigFile($GLOBALS['PMA_Config']->base_settings);
29 PMA_userprefsPageInit($cf);
30 $response = Response::getInstance();
32 $error = '';
33 if (isset($_POST['submit_export'])
34 && isset($_POST['export_type'])
35 && $_POST['export_type'] == 'text_file'
36 ) {
37 // export to JSON file
38 $response->disable();
39 $filename = 'phpMyAdmin-config-' . urlencode(Core::getenv('HTTP_HOST')) . '.json';
40 Core::downloadHeader($filename, 'application/json');
41 $settings = PMA_loadUserprefs();
42 echo json_encode($settings['config_data'], JSON_PRETTY_PRINT);
43 exit;
44 } elseif (isset($_POST['submit_export'])
45 && isset($_POST['export_type'])
46 && $_POST['export_type'] == 'php_file'
47 ) {
48 // export to JSON file
49 $response->disable();
50 $filename = 'phpMyAdmin-config-' . urlencode(Core::getenv('HTTP_HOST')) . '.php';
51 Core::downloadHeader($filename, 'application/php');
52 $settings = PMA_loadUserprefs();
53 echo '/* ' . _('phpMyAdmin configuration snippet') . " */\n\n";
54 echo '/* ' . _('Paste it to your config.inc.php') . " */\n\n";
55 foreach ($settings['config_data'] as $key => $val) {
56 echo '$cfg[\'' . str_replace('/', '\'][\'', $key) . '\'] = ';
57 echo var_export($val, true) . ";\n";
59 exit;
60 } else if (isset($_POST['submit_get_json'])) {
61 $settings = PMA_loadUserprefs();
62 $response->addJSON('prefs', json_encode($settings['config_data']));
63 $response->addJSON('mtime', $settings['mtime']);
64 exit;
65 } else if (isset($_POST['submit_import'])) {
66 // load from JSON file
67 $json = '';
68 if (isset($_POST['import_type'])
69 && $_POST['import_type'] == 'text_file'
70 && isset($_FILES['import_file'])
71 && $_FILES['import_file']['error'] == UPLOAD_ERR_OK
72 && is_uploaded_file($_FILES['import_file']['tmp_name'])
73 ) {
74 $import_handle = new File($_FILES['import_file']['tmp_name']);
75 $import_handle->checkUploadedFile();
76 if ($import_handle->isError()) {
77 $error = $import_handle->getError();
78 } else {
79 // read JSON from uploaded file
80 $json = $import_handle->getRawContent();
82 } else {
83 // read from POST value (json)
84 $json = isset($_POST['json']) ? $_POST['json'] : null;
87 // hide header message
88 $_SESSION['userprefs_autoload'] = true;
90 $config = json_decode($json, true);
91 $return_url = isset($_POST['return_url']) ? $_POST['return_url'] : null;
92 if (! is_array($config)) {
93 if (! isset($error)) {
94 $error = __('Could not import configuration');
96 } else {
97 // sanitize input values: treat them as though
98 // they came from HTTP POST request
99 $form_display = new FormDisplay($cf);
100 foreach ($forms as $formset_id => $formset) {
101 foreach ($formset as $form_name => $form) {
102 $form_display->registerForm($formset_id . ': ' . $form_name, $form);
105 $new_config = $cf->getFlatDefaultConfig();
106 if (!empty($_POST['import_merge'])) {
107 $new_config = array_merge($new_config, $cf->getConfigArray());
109 $new_config = array_merge($new_config, $config);
110 $_POST_bak = $_POST;
111 foreach ($new_config as $k => $v) {
112 $_POST[str_replace('/', '-', $k)] = $v;
114 $cf->resetConfigData();
115 $all_ok = $form_display->process(true, false);
116 $all_ok = $all_ok && !$form_display->hasErrors();
117 $_POST = $_POST_bak;
119 if (!$all_ok && isset($_POST['fix_errors'])) {
120 $form_display->fixErrors();
121 $all_ok = true;
123 if (!$all_ok) {
124 // mimic original form and post json in a hidden field
125 include 'libraries/user_preferences.inc.php';
126 $msg = Message::error(
127 __('Configuration contains incorrect data for some fields.')
129 $msg->display();
130 echo '<div class="config-form">';
131 echo $form_display->displayErrors();
132 echo '</div>';
133 echo '<form action="prefs_manage.php" method="post">';
134 echo Url::getHiddenInputs() , "\n";
135 echo '<input type="hidden" name="json" value="'
136 , htmlspecialchars($json) , '" />';
137 echo '<input type="hidden" name="fix_errors" value="1" />';
138 if (! empty($_POST['import_merge'])) {
139 echo '<input type="hidden" name="import_merge" value="1" />';
141 if ($return_url) {
142 echo '<input type="hidden" name="return_url" value="'
143 , htmlspecialchars($return_url) , '" />';
145 echo '<p>';
146 echo __('Do you want to import remaining settings?');
147 echo '</p>';
148 echo '<input type="submit" name="submit_import" value="'
149 , __('Yes') , '" />';
150 echo '<input type="submit" name="submit_ignore" value="'
151 , __('No') , '" />';
152 echo '</form>';
153 exit;
156 // check for ThemeDefault and fontsize
157 $params = array();
158 $tmanager = ThemeManager::getInstance();
159 if (isset($config['ThemeDefault'])
160 && $tmanager->theme->getId() != $config['ThemeDefault']
161 && $tmanager->checkTheme($config['ThemeDefault'])
163 $tmanager->setActiveTheme($config['ThemeDefault']);
164 $tmanager->setThemeCookie();
166 if (isset($config['fontsize'])
167 && $config['fontsize'] != $GLOBALS['PMA_Config']->get('fontsize')
169 $params['set_fontsize'] = $config['fontsize'];
171 if (isset($config['lang'])
172 && $config['lang'] != $GLOBALS['lang']
174 $params['lang'] = $config['lang'];
176 if (isset($config['collation_connection'])
177 && $config['collation_connection'] != $GLOBALS['collation_connection']
179 $params['collation_connection'] = $config['collation_connection'];
182 // save settings
183 $result = PMA_saveUserprefs($cf->getConfigArray());
184 if ($result === true) {
185 if ($return_url) {
186 $query = PhpMyAdmin\Util::splitURLQuery($return_url);
187 $return_url = parse_url($return_url, PHP_URL_PATH);
189 foreach ($query as $q) {
190 $pos = mb_strpos($q, '=');
191 $k = mb_substr($q, 0, $pos);
192 if ($k == 'token') {
193 continue;
195 $params[$k] = mb_substr($q, $pos + 1);
197 } else {
198 $return_url = 'prefs_manage.php';
200 // reload config
201 $GLOBALS['PMA_Config']->loadUserPreferences();
202 PMA_userprefsRedirect($return_url, $params);
203 exit;
204 } else {
205 $error = $result;
208 } else if (isset($_POST['submit_clear'])) {
209 $result = PMA_saveUserprefs(array());
210 if ($result === true) {
211 $params = array();
212 if ($GLOBALS['PMA_Config']->get('fontsize') != '82%') {
213 $GLOBALS['PMA_Config']->removeCookie('pma_fontsize');
215 $GLOBALS['PMA_Config']->removeCookie('pma_collaction_connection');
216 $GLOBALS['PMA_Config']->removeCookie('pma_lang');
217 PMA_userprefsRedirect('prefs_manage.php', $params);
218 exit;
219 } else {
220 $error = $result;
222 exit;
225 $response = Response::getInstance();
226 $header = $response->getHeader();
227 $scripts = $header->getScripts();
228 $scripts->addFile('config.js');
230 require 'libraries/user_preferences.inc.php';
231 if ($error) {
232 if (!$error instanceof Message) {
233 $error = Message::error($error);
235 $error->display();
238 <script type="text/javascript">
239 <?php
240 Sanitize::printJsValue("PMA_messages['strSavedOn']", __('Saved on: @DATE@'));
242 </script>
243 <div id="maincontainer">
244 <div id="main_pane_left">
245 <div class="group">
246 <?php
247 echo '<h2>' , __('Import') , '</h2>'
248 , '<form class="group-cnt prefs-form disableAjax" name="prefs_import"'
249 , ' action="prefs_manage.php" method="post" enctype="multipart/form-data">'
250 , Util::generateHiddenMaxFileSize($GLOBALS['max_upload_size'])
251 , Url::getHiddenInputs()
252 , '<input type="hidden" name="json" value="" />'
253 , '<input type="radio" id="import_text_file" name="import_type"'
254 , ' value="text_file" checked="checked" />'
255 , '<label for="import_text_file">' . __('Import from file') . '</label>'
256 , '<div id="opts_import_text_file" class="prefsmanage_opts">'
257 , '<label for="input_import_file">' , __('Browse your computer:') , '</label>'
258 , '<input type="file" name="import_file" id="input_import_file" />'
259 , '</div>'
260 , '<input type="radio" id="import_local_storage" name="import_type"'
261 , ' value="local_storage" disabled="disabled" />'
262 , '<label for="import_local_storage">'
263 , __('Import from browser\'s storage') , '</label>'
264 , '<div id="opts_import_local_storage" class="prefsmanage_opts disabled">'
265 , '<div class="localStorage-supported">'
266 , __('Settings will be imported from your browser\'s local storage.')
267 , '<br />'
268 , '<div class="localStorage-exists">'
269 , __('Saved on: @DATE@')
270 , '</div>'
271 , '<div class="localStorage-empty">';
272 Message::notice(__('You have no saved settings!'))->display();
273 echo '</div>'
274 , '</div>'
275 , '<div class="localStorage-unsupported">';
276 Message::notice(
277 __('This feature is not supported by your web browser')
278 )->display();
279 echo '</div>'
280 , '</div>'
281 , '<input type="checkbox" id="import_merge" name="import_merge" />'
282 , '<label for="import_merge">'
283 , __('Merge with current configuration') . '</label>'
284 , '<br /><br />'
285 , '<input type="submit" name="submit_import" value="'
286 , __('Go') . '" />'
287 , '</form>'
288 , '</div>';
289 if (@file_exists('setup/index.php') && ! @file_exists(CONFIG_FILE)) {
290 // show only if setup script is available, allows to disable this message
291 // by simply removing setup directory
292 // Also do not show in config exists (and setup would refuse to work)
294 <div class="group">
295 <h2><?php echo __('More settings') ?></h2>
296 <div class="group-cnt">
297 <?php
298 echo sprintf(
300 'You can set more settings by modifying config.inc.php, eg. '
301 . 'by using %sSetup script%s.'
302 ), '<a href="setup/index.php" target="_blank">', '</a>'
303 ) , PhpMyAdmin\Util::showDocu('setup', 'setup-script');
305 </div>
306 </div>
307 <?php
310 </div>
311 <div id="main_pane_right">
312 <div class="group">
313 <h2><?php echo __('Export'); ?></h2>
314 <div class="click-hide-message group-cnt hide">
315 <?php
316 Message::rawSuccess(
317 __('Configuration has been saved.')
318 )->display();
320 </div>
321 <form class="group-cnt prefs-form disableAjax" name="prefs_export"
322 action="prefs_manage.php" method="post">
323 <?php echo Url::getHiddenInputs(); ?>
324 <div style="padding-bottom:0.5em">
325 <input type="radio" id="export_text_file" name="export_type"
326 value="text_file" checked="checked" />
327 <label for="export_text_file">
328 <?php echo __('Save as file'); ?>
329 </label><br />
330 <input type="radio" id="export_php_file" name="export_type"
331 value="php_file" />
332 <label for="export_php_file">
333 <?php echo __('Save as PHP file'); ?>
334 </label><br />
335 <input type="radio" id="export_local_storage" name="export_type"
336 value="local_storage" disabled="disabled" />
337 <label for="export_local_storage">
338 <?php echo __('Save to browser\'s storage'); ?></label>
339 </div>
340 <div id="opts_export_local_storage"
341 class="prefsmanage_opts disabled">
342 <span class="localStorage-supported">
343 <?php
344 echo __(
345 'Settings will be saved in your browser\'s local '
346 . 'storage.'
349 <div class="localStorage-exists">
351 <?php
352 echo __(
353 'Existing settings will be overwritten!'
356 </b>
357 </div>
358 </span>
359 <div class="localStorage-unsupported">
360 <?php
361 Message::notice(
362 __('This feature is not supported by your web browser')
363 )->display();
365 </div>
366 </div>
367 <br />
368 <?php
369 echo '<input type="submit" name="submit_export" value="' , __(
370 'Go'
371 ) , '" />';
373 </form>
374 </div>
375 <div class="group">
376 <h2><?php echo __('Reset'); ?></h2>
377 <form class="group-cnt prefs-form disableAjax" name="prefs_reset"
378 action="prefs_manage.php" method="post">
379 <?php
380 echo Url::getHiddenInputs() , __(
381 'You can reset all your settings and restore them to default '
382 . 'values.'
385 <br /><br />
386 <input type="submit" name="submit_clear"
387 value="<?php echo __('Reset'); ?>"/>
388 </form>
389 </div>
390 </div>
391 <br class="clearfloat" />
392 </div>
394 <?php
395 if ($response->isAjax()) {
396 $response->addJSON('_disableNaviSettings', true);
397 } else {
398 define('PMA_DISABLE_NAVI_SETTINGS', true);