-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathFormController.php
More file actions
135 lines (110 loc) · 4.45 KB
/
FormController.php
File metadata and controls
135 lines (110 loc) · 4.45 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers\Setup;
use Fig\Http\Message\StatusCodeInterface;
use PhpMyAdmin\Config;
use PhpMyAdmin\Config\Forms\Setup\SetupFormList;
use PhpMyAdmin\Controllers\InvocableController;
use PhpMyAdmin\Current;
use PhpMyAdmin\Http\Factory\ResponseFactory;
use PhpMyAdmin\Http\Response;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Setup\SetupHelper;
use PhpMyAdmin\Template;
use PhpMyAdmin\Url;
use function __;
use function file_exists;
use function in_array;
use function is_numeric;
use function is_string;
use const CONFIG_FILE;
final readonly class FormController implements InvocableController
{
public function __construct(
private ResponseFactory $responseFactory,
private ResponseRenderer $responseRenderer,
private Template $template,
private Config $config,
) {
}
public function __invoke(ServerRequest $request): Response
{
if (@file_exists(CONFIG_FILE) && ! $this->config->config->debug->demo) {
$response = $this->responseFactory->createResponse(StatusCodeInterface::STATUS_NOT_FOUND);
return $response->write($this->template->render('error/generic', [
'lang' => Current::$lang,
'error_message' => __('Configuration already exists, setup is disabled!'),
]));
}
$response = $this->responseFactory->createResponse();
foreach ($this->responseRenderer->getHeader()->getHttpHeaders() as $name => $value) {
$response = $response->withHeader($name, $value);
}
$pages = SetupHelper::getPages();
$formSet = $this->getFormSetParam($request->getQueryParam('formset'));
$formClass = SetupFormList::get($formSet);
if ($formClass === null) {
return $response->write($this->template->render('error/generic', [
'lang' => Current::$lang,
'error_message' => __('Incorrect form specified!'),
]));
}
$configFile = SetupHelper::createConfigFile();
$formDisplay = new $formClass($configFile);
if ($request->getQueryParam('mode') === 'revert') {
// revert erroneous fields to their default values
$formDisplay->fixErrors();
return $response->withStatus(StatusCodeInterface::STATUS_FOUND)
->withHeader('Location', '../setup/index.php' . Url::getCommonRaw(['route' => '/setup']));
}
if (! $formDisplay->process(false)) {
// handle form view and failed POST
return $response->write($this->template->render('setup/form/index', [
'formset' => $formSet,
'pages' => $pages,
'name' => $formDisplay::getName(),
'page' => $formDisplay->getDisplay(),
]));
}
// check for form errors
if (! $formDisplay->hasErrors()) {
return $response->withStatus(StatusCodeInterface::STATUS_FOUND)
->withHeader('Location', '../setup/index.php' . Url::getCommonRaw(['route' => '/setup']));
}
$page = $this->getPageParam($request->getQueryParam('page'));
$id = $this->getIdParam($request->getQueryParam('id'));
if ($id === 0 && $page === 'servers') {
// we've just added a new server, get its id
$id = $formDisplay->getConfigFile()->getServerCount();
}
$errors = $this->template->render('setup/error', [
'url_params' => ['page' => $page, 'formset' => $formSet, 'id' => $id],
'errors' => $formDisplay->displayErrors(),
]);
return $response->write($this->template->render('setup/form/index', [
'formset' => $formSet,
'pages' => $pages,
'name' => $formDisplay::getName(),
'page' => $errors,
]));
}
private function getFormSetParam(mixed $formSetParam): string
{
return is_string($formSetParam) ? $formSetParam : '';
}
/** @psalm-return 'form'|'config'|'servers'|'index' */
private function getPageParam(mixed $pageParam): string
{
return in_array($pageParam, ['form', 'config', 'servers'], true) ? $pageParam : 'index';
}
/** @psalm-return int<0, max> */
private function getIdParam(mixed $idParam): int
{
if (! is_numeric($idParam)) {
return 0;
}
$id = (int) $idParam;
return $id >= 1 ? $id : 0;
}
}