Replace `global` keyword with `$GLOBALS`
[phpmyadmin.git] / libraries / classes / Controllers / Preferences / MainPanelController.php
bloba4de9a852de03c499f25858974eef0a0e356533f
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Controllers\Preferences;
7 use PhpMyAdmin\Config;
8 use PhpMyAdmin\Config\ConfigFile;
9 use PhpMyAdmin\Config\Forms\User\MainForm;
10 use PhpMyAdmin\ConfigStorage\Relation;
11 use PhpMyAdmin\Controllers\AbstractController;
12 use PhpMyAdmin\ResponseRenderer;
13 use PhpMyAdmin\Routing;
14 use PhpMyAdmin\Template;
15 use PhpMyAdmin\TwoFactor;
16 use PhpMyAdmin\Url;
17 use PhpMyAdmin\UserPreferences;
19 use function define;
20 use function ltrim;
22 class MainPanelController extends AbstractController
24 /** @var UserPreferences */
25 private $userPreferences;
27 /** @var Relation */
28 private $relation;
30 /** @var Config */
31 private $config;
33 public function __construct(
34 ResponseRenderer $response,
35 Template $template,
36 UserPreferences $userPreferences,
37 Relation $relation,
38 Config $config
39 ) {
40 parent::__construct($response, $template);
41 $this->userPreferences = $userPreferences;
42 $this->relation = $relation;
43 $this->config = $config;
46 public function __invoke(): void
48 $route = Routing::getCurrentRoute();
50 $GLOBALS['cf'] = new ConfigFile($this->config->baseSettings);
51 $this->userPreferences->pageInit($GLOBALS['cf']);
53 $formDisplay = new MainForm($GLOBALS['cf'], 1);
55 if (isset($_POST['revert'])) {
56 // revert erroneous fields to their default values
57 $formDisplay->fixErrors();
58 $this->redirect('/preferences/main-panel');
60 return;
63 $GLOBALS['error'] = null;
64 if ($formDisplay->process(false) && ! $formDisplay->hasErrors()) {
65 // Load 2FA settings
66 $twoFactor = new TwoFactor($GLOBALS['cfg']['Server']['user']);
67 // save settings
68 $result = $this->userPreferences->save($GLOBALS['cf']->getConfigArray());
69 // save back the 2FA setting only
70 $twoFactor->save();
71 if ($result === true) {
72 // reload config
73 $this->config->loadUserPreferences();
74 $GLOBALS['tabHash'] = $_POST['tab_hash'] ?? null;
75 $GLOBALS['hash'] = ltrim($GLOBALS['tabHash'], '#');
76 $this->userPreferences->redirect('index.php?route=/preferences/main-panel', null, $GLOBALS['hash']);
78 return;
81 $GLOBALS['error'] = $result;
84 $this->addScriptFiles(['config.js']);
86 $relationParameters = $this->relation->getRelationParameters();
88 $this->render('preferences/header', [
89 'route' => $route,
90 'is_saved' => ! empty($_GET['saved']),
91 'has_config_storage' => $relationParameters->userPreferencesFeature !== null,
92 ]);
94 if ($formDisplay->hasErrors()) {
95 $formErrors = $formDisplay->displayErrors();
98 $this->render('preferences/forms/main', [
99 'error' => $GLOBALS['error'] ? $GLOBALS['error']->getDisplay() : '',
100 'has_errors' => $formDisplay->hasErrors(),
101 'errors' => $formErrors ?? null,
102 'form' => $formDisplay->getDisplay(
103 true,
104 Url::getFromRoute('/preferences/main-panel'),
105 ['server' => $GLOBALS['server']]
109 if ($this->response->isAjax()) {
110 $this->response->addJSON('disableNaviSettings', true);
111 } else {
112 define('PMA_DISABLE_NAVI_SETTINGS', true);