/
ImportController.php
106 lines (87 loc) · 3.5 KB
/
ImportController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers\Preferences;
use PhpMyAdmin\Config;
use PhpMyAdmin\Config\ConfigFile;
use PhpMyAdmin\Config\Forms\User\ImportForm;
use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\Controllers\InvocableController;
use PhpMyAdmin\Current;
use PhpMyAdmin\Http\Response;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\Message;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Theme\ThemeManager;
use PhpMyAdmin\TwoFactor;
use PhpMyAdmin\Url;
use PhpMyAdmin\UserPreferences;
use function define;
use function ltrim;
final class ImportController implements InvocableController
{
public function __construct(
private readonly ResponseRenderer $response,
private readonly UserPreferences $userPreferences,
private readonly Relation $relation,
private readonly Config $config,
private readonly ThemeManager $themeManager,
) {
}
public function __invoke(ServerRequest $request): Response|null
{
$GLOBALS['cf'] ??= null;
$GLOBALS['error'] ??= null;
$GLOBALS['tabHash'] ??= null;
$GLOBALS['hash'] ??= null;
$GLOBALS['cf'] = new ConfigFile($this->config->baseSettings);
$this->userPreferences->pageInit($GLOBALS['cf']);
$formDisplay = new ImportForm($GLOBALS['cf'], 1);
if ($request->hasBodyParam('revert')) {
// revert erroneous fields to their default values
$formDisplay->fixErrors();
$this->response->redirectToRoute('/preferences/import', []);
return null;
}
$GLOBALS['error'] = null;
if ($formDisplay->process(false) && ! $formDisplay->hasErrors()) {
// Load 2FA settings
$twoFactor = new TwoFactor(Config::getInstance()->selectedServer['user']);
// save settings
$result = $this->userPreferences->save($GLOBALS['cf']->getConfigArray());
// save back the 2FA setting only
$twoFactor->save();
if ($result === true) {
// reload config
$this->config->loadUserPreferences($this->themeManager);
$GLOBALS['tabHash'] = $request->getParsedBodyParam('tab_hash');
$GLOBALS['hash'] = ltrim($GLOBALS['tabHash'], '#');
$this->userPreferences->redirect('index.php?route=/preferences/import', null, $GLOBALS['hash']);
return null;
}
$GLOBALS['error'] = $result;
}
$relationParameters = $this->relation->getRelationParameters();
$this->response->render('preferences/header', [
'route' => $request->getRoute(),
'is_saved' => $request->hasQueryParam('saved'),
'has_config_storage' => $relationParameters->userPreferencesFeature !== null,
]);
$formErrors = $formDisplay->displayErrors();
$this->response->render('preferences/forms/main', [
'error' => $GLOBALS['error'] instanceof Message ? $GLOBALS['error']->getDisplay() : '',
'has_errors' => $formDisplay->hasErrors(),
'errors' => $formErrors,
'form' => $formDisplay->getDisplay(
true,
Url::getFromRoute('/preferences/import'),
['server' => Current::$server],
),
]);
if ($request->isAjax()) {
$this->response->addJSON('disableNaviSettings', true);
} else {
define('PMA_DISABLE_NAVI_SETTINGS', true);
}
return null;
}
}