2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * User preferences management page
8 declare(strict_types
=1);
10 use PhpMyAdmin\Config\ConfigFile
;
11 use PhpMyAdmin\Config\Forms\User\UserFormList
;
14 use PhpMyAdmin\Message
;
15 use PhpMyAdmin\Response
;
16 use PhpMyAdmin\Sanitize
;
17 use PhpMyAdmin\ThemeManager
;
19 use PhpMyAdmin\UserPreferences
;
22 if (! defined('ROOT_PATH')) {
23 define('ROOT_PATH', __DIR__
. DIRECTORY_SEPARATOR
);
27 * Gets some core libraries and displays a top message if required
29 require_once ROOT_PATH
. 'libraries/common.inc.php';
31 $userPreferences = new UserPreferences();
33 $cf = new ConfigFile($GLOBALS['PMA_Config']->base_settings
);
34 $userPreferences->pageInit($cf);
35 $response = Response
::getInstance();
38 if (isset($_POST['submit_export'])
39 && isset($_POST['export_type'])
40 && $_POST['export_type'] == 'text_file'
42 // export to JSON file
44 $filename = 'phpMyAdmin-config-' . urlencode(Core
::getenv('HTTP_HOST')) . '.json';
45 Core
::downloadHeader($filename, 'application/json');
46 $settings = $userPreferences->load();
47 echo json_encode($settings['config_data'], JSON_PRETTY_PRINT
);
49 } elseif (isset($_POST['submit_export'])
50 && isset($_POST['export_type'])
51 && $_POST['export_type'] == 'php_file'
53 // export to JSON file
55 $filename = 'phpMyAdmin-config-' . urlencode(Core
::getenv('HTTP_HOST')) . '.php';
56 Core
::downloadHeader($filename, 'application/php');
57 $settings = $userPreferences->load();
58 echo '/* ' . __('phpMyAdmin configuration snippet') . " */\n\n";
59 echo '/* ' . __('Paste it to your config.inc.php') . " */\n\n";
60 foreach ($settings['config_data'] as $key => $val) {
61 echo '$cfg[\'' . str_replace('/', '\'][\'', $key) . '\'] = ';
62 echo var_export($val, true) . ";\n";
65 } elseif (isset($_POST['submit_get_json'])) {
66 $settings = $userPreferences->load();
67 $response->addJSON('prefs', json_encode($settings['config_data']));
68 $response->addJSON('mtime', $settings['mtime']);
70 } elseif (isset($_POST['submit_import'])) {
71 // load from JSON file
73 if (isset($_POST['import_type'])
74 && $_POST['import_type'] == 'text_file'
75 && isset($_FILES['import_file'])
76 && $_FILES['import_file']['error'] == UPLOAD_ERR_OK
77 && is_uploaded_file($_FILES['import_file']['tmp_name'])
79 $import_handle = new File($_FILES['import_file']['tmp_name']);
80 $import_handle->checkUploadedFile();
81 if ($import_handle->isError()) {
82 $error = $import_handle->getError();
84 // read JSON from uploaded file
85 $json = $import_handle->getRawContent();
88 // read from POST value (json)
89 $json = isset($_POST['json']) ?
$_POST['json'] : null;
92 // hide header message
93 $_SESSION['userprefs_autoload'] = true;
95 $config = json_decode($json, true);
96 $return_url = isset($_POST['return_url']) ?
$_POST['return_url'] : null;
97 if (! is_array($config)) {
98 if (! isset($error)) {
99 $error = __('Could not import configuration');
102 // sanitize input values: treat them as though
103 // they came from HTTP POST request
104 $form_display = new UserFormList($cf);
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);
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();
119 if (! $all_ok && isset($_POST['fix_errors'])) {
120 $form_display->fixErrors();
124 // mimic original form and post json in a hidden field
125 include ROOT_PATH
. 'libraries/user_preferences.inc.php';
126 $msg = Message
::error(
127 __('Configuration contains incorrect data for some fields.')
130 echo '<div class="config-form">';
131 echo $form_display->displayErrors();
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">';
142 echo '<input type="hidden" name="return_url" value="'
143 , htmlspecialchars($return_url) , '">';
146 echo __('Do you want to import remaining settings?');
148 echo '<input class="btn btn-secondary" type="submit" name="submit_import" value="'
150 echo '<input class="btn btn-secondary" type="submit" name="submit_ignore" value="'
156 // check for ThemeDefault
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['lang'])
167 && $config['lang'] != $GLOBALS['lang']
169 $params['lang'] = $config['lang'];
173 $result = $userPreferences->save($cf->getConfigArray());
174 if ($result === true) {
176 $query = PhpMyAdmin\Util
::splitURLQuery($return_url);
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);
185 $params[$k] = mb_substr($q, $pos +
1);
188 $return_url = 'prefs_manage.php';
191 $GLOBALS['PMA_Config']->loadUserPreferences();
192 $userPreferences->redirect($return_url, $params);
198 } elseif (isset($_POST['submit_clear'])) {
199 $result = $userPreferences->save([]);
200 if ($result === true) {
202 $GLOBALS['PMA_Config']->removeCookie('pma_collaction_connection');
203 $GLOBALS['PMA_Config']->removeCookie('pma_lang');
204 $userPreferences->redirect('prefs_manage.php', $params);
212 $response = Response
::getInstance();
213 $header = $response->getHeader();
214 $scripts = $header->getScripts();
215 $scripts->addFile('config.js');
217 require ROOT_PATH
. 'libraries/user_preferences.inc.php';
219 if (! $error instanceof Message
) {
220 $error = Message
::error($error);
225 <script type
="text/javascript">
227 Sanitize
::printJsValue("PMA_messages['strSavedOn']", __('Saved on: @DATE@'));
230 <div id
="maincontainer">
231 <div id
="main_pane_left">
234 echo '<h2>' , __('Import') , '</h2>'
235 , '<form class="group-cnt prefs-form disableAjax" name="prefs_import"'
236 , ' action="prefs_manage.php" method="post" enctype="multipart/form-data">'
237 , Util
::generateHiddenMaxFileSize($GLOBALS['max_upload_size'])
238 , Url
::getHiddenInputs()
239 , '<input type="hidden" name="json" value="">'
240 , '<input type="radio" id="import_text_file" name="import_type"'
241 , ' value="text_file" checked="checked">'
242 , '<label for="import_text_file">' . __('Import from file') . '</label>'
243 , '<div id="opts_import_text_file" class="prefsmanage_opts">'
244 , '<label for="input_import_file">' , __('Browse your computer:') , '</label>'
245 , '<input type="file" name="import_file" id="input_import_file">'
247 , '<input type="radio" id="import_local_storage" name="import_type"'
248 , ' value="local_storage" disabled="disabled">'
249 , '<label for="import_local_storage">'
250 , __('Import from browser\'s storage') , '</label>'
251 , '<div id="opts_import_local_storage" class="prefsmanage_opts disabled">'
252 , '<div class="localStorage-supported">'
253 , __('Settings will be imported from your browser\'s local storage.')
255 , '<div class="localStorage-exists">'
256 , __('Saved on: @DATE@')
258 , '<div class="localStorage-empty">';
259 Message
::notice(__('You have no saved settings!'))->display();
262 , '<div class="localStorage-unsupported">';
264 __('This feature is not supported by your web browser')
268 , '<input type="checkbox" id="import_merge" name="import_merge">'
269 , '<label for="import_merge">'
270 , __('Merge with current configuration') . '</label>'
272 , '<input class="btn btn-primary" type="submit" name="submit_import" value="'
276 if (@file_exists
(ROOT_PATH
. 'setup/index.php') && ! @file_exists
(CONFIG_FILE
)) {
277 // show only if setup script is available, allows to disable this message
278 // by simply removing setup directory
279 // Also do not show in config exists (and setup would refuse to work)
282 <h2
><?php
echo __('More settings') ?
></h2
>
283 <div
class="group-cnt">
287 'You can set more settings by modifying config.inc.php, eg. '
288 . 'by using %sSetup script%s.'
290 '<a href="setup/index.php" target="_blank">',
292 ) , PhpMyAdmin\Util
::showDocu('setup', 'setup-script');
300 <div id
="main_pane_right">
302 <h2
><?php
echo __('Export'); ?
></h2
>
303 <div
class="click-hide-message group-cnt hide">
306 __('Configuration has been saved.')
310 <form
class="group-cnt prefs-form disableAjax" name
="prefs_export"
311 action
="prefs_manage.php" method
="post">
312 <?php
echo Url
::getHiddenInputs(); ?
>
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'); ?
>
319 <input type
="radio" id
="export_php_file" name
="export_type"
321 <label
for="export_php_file">
322 <?php
echo __('Save as PHP file'); ?
>
324 <input type
="radio" id
="export_local_storage" name
="export_type"
325 value
="local_storage" disabled
="disabled">
326 <label
for="export_local_storage">
327 <?php
echo __('Save to browser\'s storage'); ?
></label
>
329 <div id
="opts_export_local_storage"
330 class="prefsmanage_opts disabled">
331 <span
class="localStorage-supported">
334 'Settings will be saved in your browser\'s local '
338 <div
class="localStorage-exists">
342 'Existing settings will be overwritten!'
348 <div
class="localStorage-unsupported">
351 __('This feature is not supported by your web browser')
358 echo '<input class="btn btn-primary" type="submit" name="submit_export" value="' , __(
365 <h2
><?php
echo __('Reset'); ?
></h2
>
366 <form
class="group-cnt prefs-form disableAjax" name
="prefs_reset"
367 action
="prefs_manage.php" method
="post">
369 echo Url
::getHiddenInputs() , __(
370 'You can reset all your settings and restore them to default '
375 <input
class="btn btn-secondary" type
="submit" name
="submit_clear"
376 value
="<?php echo __('Reset'); ?>">
380 <br
class="clearfloat">
384 if ($response->isAjax()) {
385 $response->addJSON('_disableNaviSettings', true);
387 define('PMA_DISABLE_NAVI_SETTINGS', true);