prefs_manage.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * User preferences management page
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. /**
  9. * Gets some core libraries and displays a top message if required
  10. */
  11. require_once 'libraries/common.inc.php';
  12. require_once 'libraries/user_preferences.lib.php';
  13. require_once 'libraries/config/config_functions.lib.php';
  14. require_once 'libraries/config/messages.inc.php';
  15. require_once 'libraries/config/ConfigFile.class.php';
  16. require_once 'libraries/config/Form.class.php';
  17. require_once 'libraries/config/FormDisplay.class.php';
  18. require 'libraries/config/user_preferences.forms.php';
  19. PMA_userprefsPageInit();
  20. $error = '';
  21. if (isset($_POST['submit_export'])
  22. && filter_input(INPUT_POST, 'export_type') == 'text_file'
  23. ) {
  24. // export to JSON file
  25. PMA_Response::getInstance()->disable();
  26. $filename = 'phpMyAdmin-config-' . urlencode(PMA_getenv('HTTP_HOST')) . '.json';
  27. PMA_downloadHeader($filename, 'application/json');
  28. $settings = PMA_loadUserprefs();
  29. echo json_encode($settings['config_data']);
  30. exit;
  31. } else if (isset($_POST['submit_get_json'])) {
  32. $settings = PMA_loadUserprefs();
  33. $response = PMA_Response::getInstance();
  34. $response->addJSON('prefs', json_encode($settings['config_data']));
  35. $response->addJSON('mtime', $settings['mtime']);
  36. exit;
  37. } else if (isset($_POST['submit_import'])) {
  38. // load from JSON file
  39. $json = '';
  40. if (filter_input(INPUT_POST, 'import_type') == 'text_file'
  41. && isset($_FILES['import_file'])
  42. && $_FILES['import_file']['error'] == UPLOAD_ERR_OK
  43. && is_uploaded_file($_FILES['import_file']['tmp_name'])
  44. ) {
  45. // read JSON from uploaded file
  46. $open_basedir = @ini_get('open_basedir');
  47. $file_to_unlink = '';
  48. $import_file = $_FILES['import_file']['tmp_name'];
  49. // If we are on a server with open_basedir, we must move the file
  50. // before opening it. The doc explains how to create the "./tmp"
  51. // directory
  52. if (!empty($open_basedir)) {
  53. $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : 'tmp/');
  54. if (is_writable($tmp_subdir)) {
  55. $import_file_new = tempnam($tmp_subdir, 'prefs');
  56. if (move_uploaded_file($import_file, $import_file_new)) {
  57. $import_file = $import_file_new;
  58. $file_to_unlink = $import_file_new;
  59. }
  60. }
  61. }
  62. $json = file_get_contents($import_file);
  63. if ($file_to_unlink) {
  64. unlink($file_to_unlink);
  65. }
  66. } else {
  67. // read from POST value (json)
  68. $json = filter_input(INPUT_POST, 'json');
  69. }
  70. // hide header message
  71. $_SESSION['userprefs_autoload'] = true;
  72. $config = json_decode($json, true);
  73. $return_url = filter_input(INPUT_POST, 'return_url');
  74. if (! is_array($config)) {
  75. $error = __('Could not import configuration');
  76. } else {
  77. // sanitize input values: treat them as though
  78. // they came from HTTP POST request
  79. $form_display = new FormDisplay();
  80. foreach ($forms as $formset_id => $formset) {
  81. foreach ($formset as $form_name => $form) {
  82. $form_display->registerForm($formset_id . ': ' . $form_name, $form);
  83. }
  84. }
  85. $cf = ConfigFile::getInstance();
  86. $new_config = $cf->getFlatDefaultConfig();
  87. if (!empty($_POST['import_merge'])) {
  88. $new_config = array_merge($new_config, $cf->getConfigArray());
  89. }
  90. $new_config = array_merge($new_config, $config);
  91. $_POST_bak = $_POST;
  92. foreach ($new_config as $k => $v) {
  93. $_POST[str_replace('/', '-', $k)] = $v;
  94. }
  95. $cf->resetConfigData();
  96. $all_ok = $form_display->process(true, false);
  97. $all_ok = $all_ok && !$form_display->hasErrors();
  98. $_POST = $_POST_bak;
  99. if (!$all_ok && isset($_POST['fix_errors'])) {
  100. $form_display->fixErrors();
  101. $all_ok = true;
  102. }
  103. if (!$all_ok) {
  104. // mimic original form and post json in a hidden field
  105. include 'libraries/user_preferences.inc.php';
  106. $msg = PMA_Message::error(__('Configuration contains incorrect data for some fields.'));
  107. $msg->display();
  108. echo '<div class="config-form">';
  109. $form_display->displayErrors();
  110. echo '</div>';
  111. ?>
  112. <form action="prefs_manage.php" method="post">
  113. <?php echo PMA_generate_common_hidden_inputs() . "\n"; ?>
  114. <input type="hidden" name="json" value="<?php echo htmlspecialchars($json) ?>" />
  115. <input type="hidden" name="fix_errors" value="1" />
  116. <?php if (! empty($_POST['import_merge'])) { ?>
  117. <input type="hidden" name="import_merge" value="1" />
  118. <?php } ?>
  119. <?php if ($return_url) { ?>
  120. <input type="hidden" name="return_url" value="<?php echo htmlspecialchars($return_url) ?>" />
  121. <?php } ?>
  122. <p><?php echo __('Do you want to import remaining settings?') ?></p>
  123. <input type="submit" name="submit_import" value="<?php echo __('Yes') ?>" />
  124. <input type="submit" name="submit_ignore" value="<?php echo __('No') ?>" />
  125. </form>
  126. <?php
  127. exit;
  128. }
  129. // check for ThemeDefault and fontsize
  130. $params = array();
  131. if (isset($config['ThemeDefault'])
  132. && $_SESSION['PMA_Theme_Manager']->theme->getId() != $config['ThemeDefault']
  133. && $_SESSION['PMA_Theme_Manager']->checkTheme($config['ThemeDefault'])
  134. ) {
  135. $_SESSION['PMA_Theme_Manager']->setActiveTheme($config['ThemeDefault']);
  136. $_SESSION['PMA_Theme_Manager']->setThemeCookie();
  137. }
  138. if (isset($config['fontsize'])
  139. && $config['fontsize'] != $GLOBALS['PMA_Config']->get('fontsize')
  140. ) {
  141. $params['set_fontsize'] = $config['fontsize'];
  142. }
  143. if (isset($config['lang'])
  144. && $config['lang'] != $GLOBALS['lang']
  145. ) {
  146. $params['lang'] = $config['lang'];
  147. }
  148. if (isset($config['collation_connection'])
  149. && $config['collation_connection'] != $GLOBALS['collation_connection']
  150. ) {
  151. $params['collation_connection'] = $config['collation_connection'];
  152. }
  153. // save settings
  154. $result = PMA_saveUserprefs($cf->getConfigArray());
  155. if ($result === true) {
  156. if ($return_url) {
  157. $query = PMA_Util::splitURLQuery($return_url);
  158. $return_url = parse_url($return_url, PHP_URL_PATH);
  159. foreach ($query as $q) {
  160. $pos = strpos($q, '=');
  161. $k = substr($q, 0, $pos);
  162. if ($k == 'token') {
  163. continue;
  164. }
  165. $params[$k] = substr($q, $pos+1);
  166. }
  167. } else {
  168. $return_url = 'prefs_manage.php';
  169. }
  170. // reload config
  171. $GLOBALS['PMA_Config']->loadUserPreferences();
  172. PMA_userprefsRedirect($return_url, $params);
  173. exit;
  174. } else {
  175. $error = $result;
  176. }
  177. }
  178. } else if (isset($_POST['submit_clear'])) {
  179. $result = PMA_saveUserprefs(array());
  180. if ($result === true) {
  181. $params = array();
  182. if ($_SESSION['PMA_Theme_Manager']->theme->getId() != 'original') {
  183. $GLOBALS['PMA_Config']->removeCookie(
  184. $_SESSION['PMA_Theme_Manager']->getThemeCookieName()
  185. );
  186. unset($_SESSION['PMA_Theme_Manager']);
  187. unset($_SESSION['PMA_Theme']);
  188. }
  189. if ($GLOBALS['PMA_Config']->get('fontsize') != '82%') {
  190. $GLOBALS['PMA_Config']->removeCookie('pma_fontsize');
  191. }
  192. $GLOBALS['PMA_Config']->removeCookie('pma_collaction_connection');
  193. $GLOBALS['PMA_Config']->removeCookie('pma_lang');
  194. PMA_userprefsRedirect('prefs_manage.php', $params);
  195. exit;
  196. } else {
  197. $error = $result;
  198. }
  199. exit;
  200. }
  201. $response = PMA_Response::getInstance();
  202. $header = $response->getHeader();
  203. $scripts = $header->getScripts();
  204. $scripts->addFile('config.js');
  205. require 'libraries/user_preferences.inc.php';
  206. if ($error) {
  207. if (!$error instanceof PMA_Message) {
  208. $error = PMA_Message::error($error);
  209. }
  210. $error->display();
  211. }
  212. ?>
  213. <script type="text/javascript">
  214. <?php
  215. PMA_printJsValue("PMA_messages['strSavedOn']", __('Saved on: @DATE@'));
  216. ?>
  217. </script>
  218. <div id="maincontainer">
  219. <div id="main_pane_left">
  220. <div class="group">
  221. <h2><?php echo __('Import') ?></h2>
  222. <form class="group-cnt prefs-form disableAjax" name="prefs_import" action="prefs_manage.php" method="post" enctype="multipart/form-data">
  223. <?php
  224. echo PMA_Util::generateHiddenMaxFileSize($max_upload_size) . "\n";
  225. echo PMA_generate_common_hidden_inputs() . "\n";
  226. ?>
  227. <input type="hidden" name="json" value="" />
  228. <input type="radio" id="import_text_file" name="import_type" value="text_file" checked="checked" />
  229. <label for="import_text_file"><?php echo __('Import from file') ?></label>
  230. <div id="opts_import_text_file" class="prefsmanage_opts">
  231. <label for="input_import_file"><?php echo __('Browse your computer:'); ?></label>
  232. <input type="file" name="import_file" id="input_import_file" />
  233. </div>
  234. <input type="radio" id="import_local_storage" name="import_type" value="local_storage" disabled="disabled" />
  235. <label for="import_local_storage"><?php echo __('Import from browser\'s storage') ?></label>
  236. <div id="opts_import_local_storage" class="prefsmanage_opts disabled">
  237. <div class="localStorage-supported">
  238. <?php echo __('Settings will be imported from your browser\'s local storage.') ?>
  239. <br />
  240. <div class="localStorage-exists">
  241. <?php echo __('Saved on: @DATE@') ?>
  242. </div>
  243. <div class="localStorage-empty">
  244. <?php PMA_Message::notice(__('You have no saved settings!'))->display() ?>
  245. </div>
  246. </div>
  247. <div class="localStorage-unsupported">
  248. <?php PMA_Message::notice(__('This feature is not supported by your web browser'))->display() ?>
  249. </div>
  250. </div>
  251. <input type="checkbox" id="import_merge" name="import_merge" />
  252. <label for="import_merge"><?php echo __('Merge with current configuration') ?></label>
  253. <br /><br />
  254. <input type="submit" name="submit_import" value="<?php echo __('Go'); ?>" />
  255. </form>
  256. </div>
  257. <?php
  258. if (file_exists('setup/index.php')) {
  259. // show only if setup script is available, allows to disable this message
  260. // by simply removing setup directory
  261. ?>
  262. <div class="group">
  263. <h2><?php echo __('More settings') ?></h2>
  264. <div class="group-cnt">
  265. <?php
  266. echo sprintf(__('You can set more settings by modifying config.inc.php, eg. by using %sSetup script%s.'), '<a href="setup/index.php" target="_blank">', '</a>');
  267. echo PMA_Util::showDocu('setup', 'setup-script');
  268. ?>
  269. </div>
  270. </div>
  271. <?php
  272. }
  273. ?>
  274. </div>
  275. <div id="main_pane_right">
  276. <div class="group">
  277. <h2><?php echo __('Export') ?></h2>
  278. <div class="click-hide-message group-cnt" style="display:none">
  279. <?php
  280. PMA_Message::rawSuccess(__('Configuration has been saved'))->display();
  281. ?>
  282. </div>
  283. <form class="group-cnt prefs-form disableAjax" name="prefs_export" action="prefs_manage.php" method="post">
  284. <?php echo PMA_generate_common_hidden_inputs() . "\n" ?>
  285. <div style="padding-bottom:0.5em">
  286. <input type="radio" id="export_text_file" name="export_type" value="text_file" checked="checked" />
  287. <label for="export_text_file"><?php echo __('Save as file') ?></label>
  288. <br />
  289. <input type="radio" id="export_local_storage" name="export_type" value="local_storage" disabled="disabled" />
  290. <label for="export_local_storage"><?php echo __('Save to browser\'s storage') ?></label>
  291. </div>
  292. <div id="opts_export_local_storage" class="prefsmanage_opts disabled">
  293. <span class="localStorage-supported">
  294. <?php echo __('Settings will be saved in your browser\'s local storage.') ?>
  295. <span class="localStorage-exists">
  296. <br /><b><?php echo __('Existing settings will be overwritten!') ?></b>
  297. </span>
  298. </span>
  299. <div class="localStorage-unsupported">
  300. <?php PMA_Message::notice(__('This feature is not supported by your web browser'))->display() ?>
  301. </div>
  302. </div>
  303. <br />
  304. <input type="submit" name="submit_export" value="<?php echo __('Go'); ?>" />
  305. </form>
  306. </div>
  307. <div class="group">
  308. <h2><?php echo __('Reset') ?></h2>
  309. <form class="group-cnt prefs-form disableAjax" name="prefs_reset" action="prefs_manage.php" method="post">
  310. <?php echo PMA_generate_common_hidden_inputs() . "\n" ?>
  311. <?php echo __('You can reset all your settings and restore them to default values.') ?>
  312. <br /><br />
  313. <input type="submit" name="submit_clear" value="<?php echo __('Reset') ?>" />
  314. </form>
  315. </div>
  316. </div>
  317. <br class="clearfloat" />
  318. </div>