Translated using Weblate (Estonian)
[phpmyadmin.git] / prefs_manage.php
blob79de2b4c7752cc4121a1b14606d6a2dd44070d88
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\Relation;
16 use PhpMyAdmin\Response;
17 use PhpMyAdmin\Template;
18 use PhpMyAdmin\ThemeManager;
19 use PhpMyAdmin\UserPreferences;
20 use PhpMyAdmin\UserPreferencesHeader;
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 /** @var Template $template */
32 $template = $containerBuilder->get('template');
33 /** @var Relation $relation */
34 $relation = $containerBuilder->get('relation');
36 $userPreferences = new UserPreferences();
38 $cf = new ConfigFile($GLOBALS['PMA_Config']->base_settings);
39 $userPreferences->pageInit($cf);
40 $response = Response::getInstance();
42 $error = '';
43 if (isset($_POST['submit_export'], $_POST['export_type']) && $_POST['export_type'] == 'text_file') {
44 // export to JSON file
45 $response->disable();
46 $filename = 'phpMyAdmin-config-' . urlencode(Core::getenv('HTTP_HOST')) . '.json';
47 Core::downloadHeader($filename, 'application/json');
48 $settings = $userPreferences->load();
49 echo json_encode($settings['config_data'], JSON_PRETTY_PRINT);
50 exit;
51 } elseif (isset($_POST['submit_export'], $_POST['export_type']) && $_POST['export_type'] == 'php_file') {
52 // export to JSON file
53 $response->disable();
54 $filename = 'phpMyAdmin-config-' . urlencode(Core::getenv('HTTP_HOST')) . '.php';
55 Core::downloadHeader($filename, 'application/php');
56 $settings = $userPreferences->load();
57 echo '/* ' . __('phpMyAdmin configuration snippet') . " */\n\n";
58 echo '/* ' . __('Paste it to your config.inc.php') . " */\n\n";
59 foreach ($settings['config_data'] as $key => $val) {
60 echo '$cfg[\'' . str_replace('/', '\'][\'', $key) . '\'] = ';
61 echo var_export($val, true) . ";\n";
63 exit;
64 } elseif (isset($_POST['submit_get_json'])) {
65 $settings = $userPreferences->load();
66 $response->addJSON('prefs', json_encode($settings['config_data']));
67 $response->addJSON('mtime', $settings['mtime']);
68 exit;
69 } elseif (isset($_POST['submit_import'])) {
70 // load from JSON file
71 $json = '';
72 if (isset($_POST['import_type'], $_FILES['import_file'])
73 && $_POST['import_type'] == 'text_file'
74 && $_FILES['import_file']['error'] == UPLOAD_ERR_OK
75 && is_uploaded_file($_FILES['import_file']['tmp_name'])
76 ) {
77 $import_handle = new File($_FILES['import_file']['tmp_name']);
78 $import_handle->checkUploadedFile();
79 if ($import_handle->isError()) {
80 $error = $import_handle->getError();
81 } else {
82 // read JSON from uploaded file
83 $json = $import_handle->getRawContent();
85 } else {
86 // read from POST value (json)
87 $json = isset($_POST['json']) ? $_POST['json'] : null;
90 // hide header message
91 $_SESSION['userprefs_autoload'] = true;
93 $config = json_decode($json, true);
94 $return_url = isset($_POST['return_url']) ? $_POST['return_url'] : null;
95 if (! is_array($config)) {
96 if (! isset($error)) {
97 $error = __('Could not import configuration');
99 } else {
100 // sanitize input values: treat them as though
101 // they came from HTTP POST request
102 $form_display = new UserFormList($cf);
103 $new_config = $cf->getFlatDefaultConfig();
104 if (! empty($_POST['import_merge'])) {
105 $new_config = array_merge($new_config, $cf->getConfigArray());
107 $new_config = array_merge($new_config, $config);
108 $_POST_bak = $_POST;
109 foreach ($new_config as $k => $v) {
110 $_POST[str_replace('/', '-', $k)] = $v;
112 $cf->resetConfigData();
113 $all_ok = $form_display->process(true, false);
114 $all_ok = $all_ok && ! $form_display->hasErrors();
115 $_POST = $_POST_bak;
117 if (! $all_ok && isset($_POST['fix_errors'])) {
118 $form_display->fixErrors();
119 $all_ok = true;
121 if (! $all_ok) {
122 // mimic original form and post json in a hidden field
123 echo UserPreferencesHeader::getContent($template, $relation);
125 echo $template->render('preferences/manage/error', [
126 'form_errors' => $form_display->displayErrors(),
127 'json' => $json,
128 'import_merge' => isset($_POST['import_merge']) ? $_POST['import_merge'] : null,
129 'return_url' => $return_url,
131 exit;
134 // check for ThemeDefault
135 $params = [];
136 $tmanager = ThemeManager::getInstance();
137 if (isset($config['ThemeDefault'])
138 && $tmanager->theme->getId() != $config['ThemeDefault']
139 && $tmanager->checkTheme($config['ThemeDefault'])
141 $tmanager->setActiveTheme($config['ThemeDefault']);
142 $tmanager->setThemeCookie();
144 if (isset($config['lang'])
145 && $config['lang'] != $GLOBALS['lang']
147 $params['lang'] = $config['lang'];
150 // save settings
151 $result = $userPreferences->save($cf->getConfigArray());
152 if ($result === true) {
153 if ($return_url) {
154 $query = PhpMyAdmin\Util::splitURLQuery($return_url);
155 $return_url = parse_url($return_url, PHP_URL_PATH);
157 foreach ($query as $q) {
158 $pos = mb_strpos($q, '=');
159 $k = mb_substr($q, 0, $pos);
160 if ($k == 'token') {
161 continue;
163 $params[$k] = mb_substr($q, $pos + 1);
165 } else {
166 $return_url = 'prefs_manage.php';
168 // reload config
169 $GLOBALS['PMA_Config']->loadUserPreferences();
170 $userPreferences->redirect($return_url, $params);
171 exit;
172 } else {
173 $error = $result;
176 } elseif (isset($_POST['submit_clear'])) {
177 $result = $userPreferences->save([]);
178 if ($result === true) {
179 $params = [];
180 $GLOBALS['PMA_Config']->removeCookie('pma_collaction_connection');
181 $GLOBALS['PMA_Config']->removeCookie('pma_lang');
182 $userPreferences->redirect('prefs_manage.php', $params);
183 exit;
184 } else {
185 $error = $result;
187 exit;
190 $response = Response::getInstance();
191 $header = $response->getHeader();
192 $scripts = $header->getScripts();
193 $scripts->addFile('config.js');
195 echo UserPreferencesHeader::getContent($template, $relation);
196 if ($error) {
197 if (! $error instanceof Message) {
198 $error = Message::error($error);
200 $error->getDisplay();
203 echo $template->render('preferences/manage/main', [
204 'error' => $error,
205 'max_upload_size' => $GLOBALS['max_upload_size'],
206 'exists_setup_and_not_exists_config' => @file_exists(ROOT_PATH . 'setup/index.php') && ! @file_exists(CONFIG_FILE),
209 if ($response->isAjax()) {
210 $response->addJSON('disableNaviSettings', true);
211 } else {
212 define('PMA_DISABLE_NAVI_SETTINGS', true);