FeaturesController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Controllers\Preferences;
  4. use PhpMyAdmin\Config\ConfigFile;
  5. use PhpMyAdmin\Config\Forms\User\FeaturesForm;
  6. use PhpMyAdmin\Controllers\AbstractController;
  7. use PhpMyAdmin\Core;
  8. use PhpMyAdmin\Relation;
  9. use PhpMyAdmin\Response;
  10. use PhpMyAdmin\Template;
  11. use PhpMyAdmin\TwoFactor;
  12. use PhpMyAdmin\Url;
  13. use PhpMyAdmin\UserPreferences;
  14. use function define;
  15. use function ltrim;
  16. class FeaturesController extends AbstractController
  17. {
  18. /** @var UserPreferences */
  19. private $userPreferences;
  20. /** @var Relation */
  21. private $relation;
  22. /**
  23. * @param Response $response
  24. */
  25. public function __construct(
  26. $response,
  27. Template $template,
  28. UserPreferences $userPreferences,
  29. Relation $relation
  30. ) {
  31. parent::__construct($response, $template);
  32. $this->userPreferences = $userPreferences;
  33. $this->relation = $relation;
  34. }
  35. public function index(): void
  36. {
  37. global $cfg, $cf, $error, $tabHash, $hash;
  38. global $server, $PMA_Config, $route;
  39. $cf = new ConfigFile($PMA_Config->baseSettings);
  40. $this->userPreferences->pageInit($cf);
  41. $formDisplay = new FeaturesForm($cf, 1);
  42. if (isset($_POST['revert'])) {
  43. // revert erroneous fields to their default values
  44. $formDisplay->fixErrors();
  45. Core::sendHeaderLocation('./index.php?route=/preferences/features');
  46. return;
  47. }
  48. $error = null;
  49. if ($formDisplay->process(false) && ! $formDisplay->hasErrors()) {
  50. // Load 2FA settings
  51. $twoFactor = new TwoFactor($cfg['Server']['user']);
  52. // save settings
  53. $result = $this->userPreferences->save($cf->getConfigArray());
  54. // save back the 2FA setting only
  55. $twoFactor->save();
  56. if ($result === true) {
  57. // reload config
  58. $PMA_Config->loadUserPreferences();
  59. $tabHash = $_POST['tab_hash'] ?? null;
  60. $hash = ltrim($tabHash, '#');
  61. $this->userPreferences->redirect(
  62. 'index.php?route=/preferences/features',
  63. null,
  64. $hash
  65. );
  66. return;
  67. }
  68. $error = $result;
  69. }
  70. $this->addScriptFiles(['config.js']);
  71. $cfgRelation = $this->relation->getRelationsParam();
  72. $this->render('preferences/header', [
  73. 'route' => $route,
  74. 'is_saved' => ! empty($_GET['saved']),
  75. 'has_config_storage' => $cfgRelation['userconfigwork'],
  76. ]);
  77. if ($formDisplay->hasErrors()) {
  78. $formErrors = $formDisplay->displayErrors();
  79. }
  80. $this->render('preferences/forms/main', [
  81. 'error' => $error ? $error->getDisplay() : '',
  82. 'has_errors' => $formDisplay->hasErrors(),
  83. 'errors' => $formErrors ?? null,
  84. 'form' => $formDisplay->getDisplay(
  85. true,
  86. true,
  87. true,
  88. Url::getFromRoute('/preferences/features'),
  89. ['server' => $server]
  90. ),
  91. ]);
  92. if ($this->response->isAjax()) {
  93. $this->response->addJSON('disableNaviSettings', true);
  94. } else {
  95. define('PMA_DISABLE_NAVI_SETTINGS', true);
  96. }
  97. }
  98. }