prefs_manage.php 13 KB

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