Translated using Weblate (Estonian)
[phpmyadmin.git] / prefs_manage.php
blobc332614ca6d7d7f45f9967180ac08fb91563ae9e
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * User preferences management page
6 * @package PhpMyAdmin
7 */
8 declare(strict_types=1);
10 use PhpMyAdmin\Config\ConfigFile;
11 use PhpMyAdmin\Config\Forms\User\UserFormList;
12 use PhpMyAdmin\Core;
13 use PhpMyAdmin\File;
14 use PhpMyAdmin\Message;
15 use PhpMyAdmin\Response;
16 use PhpMyAdmin\Sanitize;
17 use PhpMyAdmin\ThemeManager;
18 use PhpMyAdmin\Url;
19 use PhpMyAdmin\UserPreferences;
20 use PhpMyAdmin\Util;
22 if (! defined('ROOT_PATH')) {
23 define('ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR);
26 /**
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();
37 $error = '';
38 if (isset($_POST['submit_export'])
39 && isset($_POST['export_type'])
40 && $_POST['export_type'] == 'text_file'
41 ) {
42 // export to JSON file
43 $response->disable();
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);
48 exit;
49 } elseif (isset($_POST['submit_export'])
50 && isset($_POST['export_type'])
51 && $_POST['export_type'] == 'php_file'
52 ) {
53 // export to JSON file
54 $response->disable();
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";
64 exit;
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']);
69 exit;
70 } elseif (isset($_POST['submit_import'])) {
71 // load from JSON file
72 $json = '';
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'])
78 ) {
79 $import_handle = new File($_FILES['import_file']['tmp_name']);
80 $import_handle->checkUploadedFile();
81 if ($import_handle->isError()) {
82 $error = $import_handle->getError();
83 } else {
84 // read JSON from uploaded file
85 $json = $import_handle->getRawContent();
87 } else {
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');
101 } else {
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);
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 ROOT_PATH . '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 class="btn btn-secondary" type="submit" name="submit_import" value="'
149 , __('Yes') , '">';
150 echo '<input class="btn btn-secondary" type="submit" name="submit_ignore" value="'
151 , __('No') , '">';
152 echo '</form>';
153 exit;
156 // check for ThemeDefault
157 $params = [];
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'];
172 // save settings
173 $result = $userPreferences->save($cf->getConfigArray());
174 if ($result === true) {
175 if ($return_url) {
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);
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 $userPreferences->redirect($return_url, $params);
193 exit;
194 } else {
195 $error = $result;
198 } elseif (isset($_POST['submit_clear'])) {
199 $result = $userPreferences->save([]);
200 if ($result === true) {
201 $params = [];
202 $GLOBALS['PMA_Config']->removeCookie('pma_collaction_connection');
203 $GLOBALS['PMA_Config']->removeCookie('pma_lang');
204 $userPreferences->redirect('prefs_manage.php', $params);
205 exit;
206 } else {
207 $error = $result;
209 exit;
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';
218 if ($error) {
219 if (! $error instanceof Message) {
220 $error = Message::error($error);
222 $error->display();
225 <script type="text/javascript">
226 <?php
227 Sanitize::printJsValue("PMA_messages['strSavedOn']", __('Saved on: @DATE@'));
229 </script>
230 <div id="maincontainer">
231 <div id="main_pane_left">
232 <div class="group">
233 <?php
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">'
246 , '</div>'
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.')
254 , '<br>'
255 , '<div class="localStorage-exists">'
256 , __('Saved on: @DATE@')
257 , '</div>'
258 , '<div class="localStorage-empty">';
259 Message::notice(__('You have no saved settings!'))->display();
260 echo '</div>'
261 , '</div>'
262 , '<div class="localStorage-unsupported">';
263 Message::notice(
264 __('This feature is not supported by your web browser')
265 )->display();
266 echo '</div>'
267 , '</div>'
268 , '<input type="checkbox" id="import_merge" name="import_merge">'
269 , '<label for="import_merge">'
270 , __('Merge with current configuration') . '</label>'
271 , '<br><br>'
272 , '<input class="btn btn-primary" type="submit" name="submit_import" value="'
273 , __('Go') . '">'
274 , '</form>'
275 , '</div>';
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)
281 <div class="group">
282 <h2><?php echo __('More settings') ?></h2>
283 <div class="group-cnt">
284 <?php
285 echo sprintf(
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">',
291 '</a>'
292 ) , PhpMyAdmin\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 hide">
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 Url::getHiddenInputs(); ?>
313 <div>
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_php_file" name="export_type"
320 value="php_file">
321 <label for="export_php_file">
322 <?php echo __('Save as PHP file'); ?>
323 </label><br>
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>
328 </div>
329 <div id="opts_export_local_storage"
330 class="prefsmanage_opts disabled">
331 <span class="localStorage-supported">
332 <?php
333 echo __(
334 'Settings will be saved in your browser\'s local '
335 . 'storage.'
338 <div class="localStorage-exists">
340 <?php
341 echo __(
342 'Existing settings will be overwritten!'
345 </b>
346 </div>
347 </span>
348 <div class="localStorage-unsupported">
349 <?php
350 Message::notice(
351 __('This feature is not supported by your web browser')
352 )->display();
354 </div>
355 </div>
356 <br>
357 <?php
358 echo '<input class="btn btn-primary" type="submit" name="submit_export" value="' , __(
359 'Go'
360 ) , '">';
362 </form>
363 </div>
364 <div class="group">
365 <h2><?php echo __('Reset'); ?></h2>
366 <form class="group-cnt prefs-form disableAjax" name="prefs_reset"
367 action="prefs_manage.php" method="post">
368 <?php
369 echo Url::getHiddenInputs() , __(
370 'You can reset all your settings and restore them to default '
371 . 'values.'
374 <br><br>
375 <input class="btn btn-secondary" type="submit" name="submit_clear"
376 value="<?php echo __('Reset'); ?>">
377 </form>
378 </div>
379 </div>
380 <br class="clearfloat">
381 </div>
383 <?php
384 if ($response->isAjax()) {
385 $response->addJSON('_disableNaviSettings', true);
386 } else {
387 define('PMA_DISABLE_NAVI_SETTINGS', true);