Merge branch 'QA_4_7'
[phpmyadmin.git] / prefs_manage.php
bloba961720110c893544cc339d230702a79511f2eff
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\Forms\User\UserFormList;
10 use PhpMyAdmin\Core;
11 use PhpMyAdmin\File;
12 use PhpMyAdmin\Message;
13 use PhpMyAdmin\Response;
14 use PhpMyAdmin\Sanitize;
15 use PhpMyAdmin\ThemeManager;
16 use PhpMyAdmin\Url;
17 use PhpMyAdmin\UserPreferences;
18 use PhpMyAdmin\Util;
20 /**
21 * Gets some core libraries and displays a top message if required
23 require_once 'libraries/common.inc.php';
25 $cf = new ConfigFile($GLOBALS['PMA_Config']->base_settings);
26 UserPreferences::pageInit($cf);
27 $response = Response::getInstance();
29 $error = '';
30 if (isset($_POST['submit_export'])
31 && isset($_POST['export_type'])
32 && $_POST['export_type'] == 'text_file'
33 ) {
34 // export to JSON file
35 $response->disable();
36 $filename = 'phpMyAdmin-config-' . urlencode(Core::getenv('HTTP_HOST')) . '.json';
37 Core::downloadHeader($filename, 'application/json');
38 $settings = UserPreferences::load();
39 echo json_encode($settings['config_data'], JSON_PRETTY_PRINT);
40 exit;
41 } elseif (isset($_POST['submit_export'])
42 && isset($_POST['export_type'])
43 && $_POST['export_type'] == 'php_file'
44 ) {
45 // export to JSON file
46 $response->disable();
47 $filename = 'phpMyAdmin-config-' . urlencode(Core::getenv('HTTP_HOST')) . '.php';
48 Core::downloadHeader($filename, 'application/php');
49 $settings = UserPreferences::load();
50 echo '/* ' . __('phpMyAdmin configuration snippet') . " */\n\n";
51 echo '/* ' . __('Paste it to your config.inc.php') . " */\n\n";
52 foreach ($settings['config_data'] as $key => $val) {
53 echo '$cfg[\'' . str_replace('/', '\'][\'', $key) . '\'] = ';
54 echo var_export($val, true) . ";\n";
56 exit;
57 } elseif (isset($_POST['submit_get_json'])) {
58 $settings = UserPreferences::load();
59 $response->addJSON('prefs', json_encode($settings['config_data']));
60 $response->addJSON('mtime', $settings['mtime']);
61 exit;
62 } elseif (isset($_POST['submit_import'])) {
63 // load from JSON file
64 $json = '';
65 if (isset($_POST['import_type'])
66 && $_POST['import_type'] == 'text_file'
67 && isset($_FILES['import_file'])
68 && $_FILES['import_file']['error'] == UPLOAD_ERR_OK
69 && is_uploaded_file($_FILES['import_file']['tmp_name'])
70 ) {
71 $import_handle = new File($_FILES['import_file']['tmp_name']);
72 $import_handle->checkUploadedFile();
73 if ($import_handle->isError()) {
74 $error = $import_handle->getError();
75 } else {
76 // read JSON from uploaded file
77 $json = $import_handle->getRawContent();
79 } else {
80 // read from POST value (json)
81 $json = isset($_POST['json']) ? $_POST['json'] : null;
84 // hide header message
85 $_SESSION['userprefs_autoload'] = true;
87 $config = json_decode($json, true);
88 $return_url = isset($_POST['return_url']) ? $_POST['return_url'] : null;
89 if (! is_array($config)) {
90 if (! isset($error)) {
91 $error = __('Could not import configuration');
93 } else {
94 // sanitize input values: treat them as though
95 // they came from HTTP POST request
96 $form_display = new UserFormList($cf);
97 $new_config = $cf->getFlatDefaultConfig();
98 if (!empty($_POST['import_merge'])) {
99 $new_config = array_merge($new_config, $cf->getConfigArray());
101 $new_config = array_merge($new_config, $config);
102 $_POST_bak = $_POST;
103 foreach ($new_config as $k => $v) {
104 $_POST[str_replace('/', '-', $k)] = $v;
106 $cf->resetConfigData();
107 $all_ok = $form_display->process(true, false);
108 $all_ok = $all_ok && !$form_display->hasErrors();
109 $_POST = $_POST_bak;
111 if (!$all_ok && isset($_POST['fix_errors'])) {
112 $form_display->fixErrors();
113 $all_ok = true;
115 if (!$all_ok) {
116 // mimic original form and post json in a hidden field
117 include 'libraries/user_preferences.inc.php';
118 $msg = Message::error(
119 __('Configuration contains incorrect data for some fields.')
121 $msg->display();
122 echo '<div class="config-form">';
123 echo $form_display->displayErrors();
124 echo '</div>';
125 echo '<form action="prefs_manage.php" method="post">';
126 echo Url::getHiddenInputs() , "\n";
127 echo '<input type="hidden" name="json" value="'
128 , htmlspecialchars($json) , '" />';
129 echo '<input type="hidden" name="fix_errors" value="1" />';
130 if (! empty($_POST['import_merge'])) {
131 echo '<input type="hidden" name="import_merge" value="1" />';
133 if ($return_url) {
134 echo '<input type="hidden" name="return_url" value="'
135 , htmlspecialchars($return_url) , '" />';
137 echo '<p>';
138 echo __('Do you want to import remaining settings?');
139 echo '</p>';
140 echo '<input type="submit" name="submit_import" value="'
141 , __('Yes') , '" />';
142 echo '<input type="submit" name="submit_ignore" value="'
143 , __('No') , '" />';
144 echo '</form>';
145 exit;
148 // check for ThemeDefault
149 $params = array();
150 $tmanager = ThemeManager::getInstance();
151 if (isset($config['ThemeDefault'])
152 && $tmanager->theme->getId() != $config['ThemeDefault']
153 && $tmanager->checkTheme($config['ThemeDefault'])
155 $tmanager->setActiveTheme($config['ThemeDefault']);
156 $tmanager->setThemeCookie();
158 if (isset($config['lang'])
159 && $config['lang'] != $GLOBALS['lang']
161 $params['lang'] = $config['lang'];
164 // save settings
165 $result = UserPreferences::save($cf->getConfigArray());
166 if ($result === true) {
167 if ($return_url) {
168 $query = PhpMyAdmin\Util::splitURLQuery($return_url);
169 $return_url = parse_url($return_url, PHP_URL_PATH);
171 foreach ($query as $q) {
172 $pos = mb_strpos($q, '=');
173 $k = mb_substr($q, 0, $pos);
174 if ($k == 'token') {
175 continue;
177 $params[$k] = mb_substr($q, $pos + 1);
179 } else {
180 $return_url = 'prefs_manage.php';
182 // reload config
183 $GLOBALS['PMA_Config']->loadUserPreferences();
184 UserPreferences::redirect($return_url, $params);
185 exit;
186 } else {
187 $error = $result;
190 } elseif (isset($_POST['submit_clear'])) {
191 $result = UserPreferences::save(array());
192 if ($result === true) {
193 $params = array();
194 $GLOBALS['PMA_Config']->removeCookie('pma_collaction_connection');
195 $GLOBALS['PMA_Config']->removeCookie('pma_lang');
196 UserPreferences::redirect('prefs_manage.php', $params);
197 exit;
198 } else {
199 $error = $result;
201 exit;
204 $response = Response::getInstance();
205 $header = $response->getHeader();
206 $scripts = $header->getScripts();
207 $scripts->addFile('config.js');
209 require 'libraries/user_preferences.inc.php';
210 if ($error) {
211 if (!$error instanceof Message) {
212 $error = Message::error($error);
214 $error->display();
217 <script type="text/javascript">
218 <?php
219 Sanitize::printJsValue("PMA_messages['strSavedOn']", __('Saved on: @DATE@'));
221 </script>
222 <div id="maincontainer">
223 <div id="main_pane_left">
224 <div class="group">
225 <?php
226 echo '<h2>' , __('Import') , '</h2>'
227 , '<form class="group-cnt prefs-form disableAjax" name="prefs_import"'
228 , ' action="prefs_manage.php" method="post" enctype="multipart/form-data">'
229 , Util::generateHiddenMaxFileSize($GLOBALS['max_upload_size'])
230 , Url::getHiddenInputs()
231 , '<input type="hidden" name="json" value="" />'
232 , '<input type="radio" id="import_text_file" name="import_type"'
233 , ' value="text_file" checked="checked" />'
234 , '<label for="import_text_file">' . __('Import from file') . '</label>'
235 , '<div id="opts_import_text_file" class="prefsmanage_opts">'
236 , '<label for="input_import_file">' , __('Browse your computer:') , '</label>'
237 , '<input type="file" name="import_file" id="input_import_file" />'
238 , '</div>'
239 , '<input type="radio" id="import_local_storage" name="import_type"'
240 , ' value="local_storage" disabled="disabled" />'
241 , '<label for="import_local_storage">'
242 , __('Import from browser\'s storage') , '</label>'
243 , '<div id="opts_import_local_storage" class="prefsmanage_opts disabled">'
244 , '<div class="localStorage-supported">'
245 , __('Settings will be imported from your browser\'s local storage.')
246 , '<br />'
247 , '<div class="localStorage-exists">'
248 , __('Saved on: @DATE@')
249 , '</div>'
250 , '<div class="localStorage-empty">';
251 Message::notice(__('You have no saved settings!'))->display();
252 echo '</div>'
253 , '</div>'
254 , '<div class="localStorage-unsupported">';
255 Message::notice(
256 __('This feature is not supported by your web browser')
257 )->display();
258 echo '</div>'
259 , '</div>'
260 , '<input type="checkbox" id="import_merge" name="import_merge" />'
261 , '<label for="import_merge">'
262 , __('Merge with current configuration') . '</label>'
263 , '<br /><br />'
264 , '<input type="submit" name="submit_import" value="'
265 , __('Go') . '" />'
266 , '</form>'
267 , '</div>';
268 if (@file_exists('setup/index.php') && ! @file_exists(CONFIG_FILE)) {
269 // show only if setup script is available, allows to disable this message
270 // by simply removing setup directory
271 // Also do not show in config exists (and setup would refuse to work)
273 <div class="group">
274 <h2><?php echo __('More settings') ?></h2>
275 <div class="group-cnt">
276 <?php
277 echo sprintf(
279 'You can set more settings by modifying config.inc.php, eg. '
280 . 'by using %sSetup script%s.'
281 ), '<a href="setup/index.php" target="_blank">', '</a>'
282 ) , PhpMyAdmin\Util::showDocu('setup', 'setup-script');
284 </div>
285 </div>
286 <?php
289 </div>
290 <div id="main_pane_right">
291 <div class="group">
292 <h2><?php echo __('Export'); ?></h2>
293 <div class="click-hide-message group-cnt hide">
294 <?php
295 Message::rawSuccess(
296 __('Configuration has been saved.')
297 )->display();
299 </div>
300 <form class="group-cnt prefs-form disableAjax" name="prefs_export"
301 action="prefs_manage.php" method="post">
302 <?php echo Url::getHiddenInputs(); ?>
303 <div style="padding-bottom:0.5em">
304 <input type="radio" id="export_text_file" name="export_type"
305 value="text_file" checked="checked" />
306 <label for="export_text_file">
307 <?php echo __('Save as file'); ?>
308 </label><br />
309 <input type="radio" id="export_php_file" name="export_type"
310 value="php_file" />
311 <label for="export_php_file">
312 <?php echo __('Save as PHP file'); ?>
313 </label><br />
314 <input type="radio" id="export_local_storage" name="export_type"
315 value="local_storage" disabled="disabled" />
316 <label for="export_local_storage">
317 <?php echo __('Save to browser\'s storage'); ?></label>
318 </div>
319 <div id="opts_export_local_storage"
320 class="prefsmanage_opts disabled">
321 <span class="localStorage-supported">
322 <?php
323 echo __(
324 'Settings will be saved in your browser\'s local '
325 . 'storage.'
328 <div class="localStorage-exists">
330 <?php
331 echo __(
332 'Existing settings will be overwritten!'
335 </b>
336 </div>
337 </span>
338 <div class="localStorage-unsupported">
339 <?php
340 Message::notice(
341 __('This feature is not supported by your web browser')
342 )->display();
344 </div>
345 </div>
346 <br />
347 <?php
348 echo '<input type="submit" name="submit_export" value="' , __(
349 'Go'
350 ) , '" />';
352 </form>
353 </div>
354 <div class="group">
355 <h2><?php echo __('Reset'); ?></h2>
356 <form class="group-cnt prefs-form disableAjax" name="prefs_reset"
357 action="prefs_manage.php" method="post">
358 <?php
359 echo Url::getHiddenInputs() , __(
360 'You can reset all your settings and restore them to default '
361 . 'values.'
364 <br /><br />
365 <input type="submit" name="submit_clear"
366 value="<?php echo __('Reset'); ?>"/>
367 </form>
368 </div>
369 </div>
370 <br class="clearfloat" />
371 </div>
373 <?php
374 if ($response->isAjax()) {
375 $response->addJSON('_disableNaviSettings', true);
376 } else {
377 define('PMA_DISABLE_NAVI_SETTINGS', true);