Prepare for version 5.0.0-rc1
[phpmyadmin.git] / prefs_manage.php
blobca7573846dafc2d6ba3deb7338a351eb903f49cb
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'])
44 && isset($_POST['export_type'])
45 && $_POST['export_type'] == 'text_file'
46 ) {
47 // export to JSON file
48 $response->disable();
49 $filename = 'phpMyAdmin-config-' . urlencode(Core::getenv('HTTP_HOST')) . '.json';
50 Core::downloadHeader($filename, 'application/json');
51 $settings = $userPreferences->load();
52 echo json_encode($settings['config_data'], JSON_PRETTY_PRINT);
53 exit;
54 } elseif (isset($_POST['submit_export'])
55 && isset($_POST['export_type'])
56 && $_POST['export_type'] == 'php_file'
57 ) {
58 // export to JSON file
59 $response->disable();
60 $filename = 'phpMyAdmin-config-' . urlencode(Core::getenv('HTTP_HOST')) . '.php';
61 Core::downloadHeader($filename, 'application/php');
62 $settings = $userPreferences->load();
63 echo '/* ' . __('phpMyAdmin configuration snippet') . " */\n\n";
64 echo '/* ' . __('Paste it to your config.inc.php') . " */\n\n";
65 foreach ($settings['config_data'] as $key => $val) {
66 echo '$cfg[\'' . str_replace('/', '\'][\'', $key) . '\'] = ';
67 echo var_export($val, true) . ";\n";
69 exit;
70 } elseif (isset($_POST['submit_get_json'])) {
71 $settings = $userPreferences->load();
72 $response->addJSON('prefs', json_encode($settings['config_data']));
73 $response->addJSON('mtime', $settings['mtime']);
74 exit;
75 } elseif (isset($_POST['submit_import'])) {
76 // load from JSON file
77 $json = '';
78 if (isset($_POST['import_type'])
79 && $_POST['import_type'] == 'text_file'
80 && isset($_FILES['import_file'])
81 && $_FILES['import_file']['error'] == UPLOAD_ERR_OK
82 && is_uploaded_file($_FILES['import_file']['tmp_name'])
83 ) {
84 $import_handle = new File($_FILES['import_file']['tmp_name']);
85 $import_handle->checkUploadedFile();
86 if ($import_handle->isError()) {
87 $error = $import_handle->getError();
88 } else {
89 // read JSON from uploaded file
90 $json = $import_handle->getRawContent();
92 } else {
93 // read from POST value (json)
94 $json = isset($_POST['json']) ? $_POST['json'] : null;
97 // hide header message
98 $_SESSION['userprefs_autoload'] = true;
100 $config = json_decode($json, true);
101 $return_url = isset($_POST['return_url']) ? $_POST['return_url'] : null;
102 if (! is_array($config)) {
103 if (! isset($error)) {
104 $error = __('Could not import configuration');
106 } else {
107 // sanitize input values: treat them as though
108 // they came from HTTP POST request
109 $form_display = new UserFormList($cf);
110 $new_config = $cf->getFlatDefaultConfig();
111 if (! empty($_POST['import_merge'])) {
112 $new_config = array_merge($new_config, $cf->getConfigArray());
114 $new_config = array_merge($new_config, $config);
115 $_POST_bak = $_POST;
116 foreach ($new_config as $k => $v) {
117 $_POST[str_replace('/', '-', $k)] = $v;
119 $cf->resetConfigData();
120 $all_ok = $form_display->process(true, false);
121 $all_ok = $all_ok && ! $form_display->hasErrors();
122 $_POST = $_POST_bak;
124 if (! $all_ok && isset($_POST['fix_errors'])) {
125 $form_display->fixErrors();
126 $all_ok = true;
128 if (! $all_ok) {
129 // mimic original form and post json in a hidden field
130 echo UserPreferencesHeader::getContent($template, $relation);
132 echo $template->render('preferences/manage/error', [
133 'form_errors' => $form_display->displayErrors(),
134 'json' => $json,
135 'import_merge' => isset($_POST['import_merge']) ? $_POST['import_merge'] : null,
136 'return_url' => $return_url,
138 exit;
141 // check for ThemeDefault
142 $params = [];
143 $tmanager = ThemeManager::getInstance();
144 if (isset($config['ThemeDefault'])
145 && $tmanager->theme->getId() != $config['ThemeDefault']
146 && $tmanager->checkTheme($config['ThemeDefault'])
148 $tmanager->setActiveTheme($config['ThemeDefault']);
149 $tmanager->setThemeCookie();
151 if (isset($config['lang'])
152 && $config['lang'] != $GLOBALS['lang']
154 $params['lang'] = $config['lang'];
157 // save settings
158 $result = $userPreferences->save($cf->getConfigArray());
159 if ($result === true) {
160 if ($return_url) {
161 $query = PhpMyAdmin\Util::splitURLQuery($return_url);
162 $return_url = parse_url($return_url, PHP_URL_PATH);
164 foreach ($query as $q) {
165 $pos = mb_strpos($q, '=');
166 $k = mb_substr($q, 0, $pos);
167 if ($k == 'token') {
168 continue;
170 $params[$k] = mb_substr($q, $pos + 1);
172 } else {
173 $return_url = 'prefs_manage.php';
175 // reload config
176 $GLOBALS['PMA_Config']->loadUserPreferences();
177 $userPreferences->redirect($return_url, $params);
178 exit;
179 } else {
180 $error = $result;
183 } elseif (isset($_POST['submit_clear'])) {
184 $result = $userPreferences->save([]);
185 if ($result === true) {
186 $params = [];
187 $GLOBALS['PMA_Config']->removeCookie('pma_collaction_connection');
188 $GLOBALS['PMA_Config']->removeCookie('pma_lang');
189 $userPreferences->redirect('prefs_manage.php', $params);
190 exit;
191 } else {
192 $error = $result;
194 exit;
197 $response = Response::getInstance();
198 $header = $response->getHeader();
199 $scripts = $header->getScripts();
200 $scripts->addFile('config.js');
202 echo UserPreferencesHeader::getContent($template, $relation);
203 if ($error) {
204 if (! $error instanceof Message) {
205 $error = Message::error($error);
207 $error->getDisplay();
210 echo $template->render('preferences/manage/main', [
211 'error' => $error,
212 'max_upload_size' => $GLOBALS['max_upload_size'],
213 'exists_setup_and_not_exists_config' => @file_exists(ROOT_PATH . 'setup/index.php') && ! @file_exists(CONFIG_FILE),
216 if ($response->isAjax()) {
217 $response->addJSON('disableNaviSettings', true);
218 } else {
219 define('PMA_DISABLE_NAVI_SETTINGS', true);