prefs_manage.php 14 KB

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