Merge pull request #17763 from MauricioFauth/setup-page-refactor

Refactor setup entry points to use controllers
This commit is contained in:
Maurício Meneghini Fauth 2022-10-10 05:09:06 -03:00 committed by GitHub
commit 2fda553382
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 202 additions and 141 deletions

View File

@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers\Setup;
use PhpMyAdmin\Core;
use PhpMyAdmin\Header;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\Template;
use PhpMyAdmin\Url;
use function __;
use function file_exists;
use function header;
use function in_array;
final class MainController
{
public function __invoke(ServerRequest $request): void
{
if (@file_exists(CONFIG_FILE) && ! $GLOBALS['cfg']['DBG']['demo']) {
Core::fatalError(__('Configuration already exists, setup is disabled!'));
}
$params = $request->getQueryParams();
$page = 'index';
if (isset($params['page']) && in_array($params['page'], ['form', 'config', 'servers'], true)) {
$page = $params['page'];
}
Core::noCacheHeader();
// Sent security-related headers
(new Header())->sendHttpHeaders();
if ($page === 'form') {
echo (new FormController($GLOBALS['ConfigFile'], new Template()))([
'formset' => $params['formset'] ?? null,
]);
return;
}
if ($page === 'config') {
echo (new ConfigController($GLOBALS['ConfigFile'], new Template()))([
'formset' => $params['formset'] ?? null,
'eol' => $params['eol'] ?? null,
]);
return;
}
if ($page === 'servers') {
$controller = new ServersController($GLOBALS['ConfigFile'], new Template());
if (isset($params['mode']) && $params['mode'] === 'remove' && $request->isPost()) {
$controller->destroy([
'id' => $params['id'] ?? null,
]);
header('Location: index.php' . Url::getCommonRaw());
return;
}
echo $controller->index([
'formset' => $params['formset'] ?? null,
'mode' => $params['mode'] ?? null,
'id' => $params['id'] ?? null,
]);
return;
}
echo (new HomeController($GLOBALS['ConfigFile'], new Template()))([
'formset' => $params['formset'] ?? null,
'version_check' => $params['version_check'] ?? null,
]);
}
}

View File

@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers\Setup;
use PhpMyAdmin\Config\Forms\Setup\ConfigForm;
use PhpMyAdmin\Core;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Setup\ConfigGenerator;
use PhpMyAdmin\Url;
use function is_string;
final class ShowConfigController
{
public function __invoke(ServerRequest $request): void
{
$form_display = new ConfigForm($GLOBALS['ConfigFile']);
$form_display->save('Config');
$response = ResponseRenderer::getInstance();
$response->disable();
/** @var mixed $eol */
$eol = $request->getParsedBodyParam('eol');
if ($eol !== null) {
$_SESSION['eol'] = $eol === 'unix' ? 'unix' : 'win';
}
/** @var mixed $submitClear */
$submitClear = $request->getParsedBodyParam('submit_clear');
if (is_string($submitClear) && $submitClear !== '') {
// Clear current config and return to main page
$GLOBALS['ConfigFile']->resetConfigData();
// drop post data
$response->generateHeader303('index.php' . Url::getCommonRaw());
return;
}
/** @var mixed $submitDownload */
$submitDownload = $request->getParsedBodyParam('submit_download');
if (is_string($submitDownload) && $submitDownload !== '') {
// Output generated config file
Core::downloadHeader('config.inc.php', 'text/plain');
$response->disable();
echo ConfigGenerator::getConfigFile($GLOBALS['ConfigFile']);
return;
}
// Show generated config file in a <textarea>
$response->generateHeader303('index.php' . Url::getCommonRaw(['page' => 'config']));
}
}

View File

@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers\Setup;
use PhpMyAdmin\Config\Validator;
use PhpMyAdmin\Core;
use PhpMyAdmin\Http\ServerRequest;
use stdClass;
use function __;
use function explode;
use function implode;
use function is_string;
use function json_decode;
use function json_encode;
use function sprintf;
final class ValidateController
{
public function __invoke(ServerRequest $request): void
{
Core::headerJSON();
/** @var mixed $id */
$id = $request->getParsedBodyParam('id');
$vids = explode(',', is_string($id) ? $id : '');
/** @var mixed $valuesParam */
$valuesParam = $request->getParsedBodyParam('values');
$values = json_decode(is_string($valuesParam) ? $valuesParam : '');
if (! ($values instanceof stdClass)) {
Core::fatalError(__('Wrong data'));
}
$values = (array) $values;
$result = Validator::validate($GLOBALS['ConfigFile'], $vids, $values, true);
if ($result === false) {
$result = sprintf(
__('Wrong data or no validation for %s'),
implode(',', $vids)
);
}
echo $result !== true ? json_encode($result) : '';
}
}

View File

@ -3143,6 +3143,11 @@
<code>$id</code>
</MixedArgumentTypeCoercion>
</file>
<file src="libraries/classes/Controllers/Setup/ValidateController.php">
<MixedAssignment occurrences="1">
<code>$values</code>
</MixedAssignment>
</file>
<file src="libraries/classes/Controllers/Sql/EnumValuesController.php">
<PossiblyInvalidArgument occurrences="2">
<code>$column</code>
@ -14367,8 +14372,7 @@
<code>mb_strpos($value, '.')</code>
<code>mb_strrpos($columnSpecification, ')')</code>
</PossiblyFalseOperand>
<PossiblyInvalidArgument occurrences="6">
<code>$row</code>
<PossiblyInvalidArgument occurrences="5">
<code>$sep</code>
<code>$sep</code>
<code>$table</code>
@ -14600,15 +14604,6 @@
<code>(string) $GLOBALS['lang']</code>
</RedundantCast>
</file>
<file src="setup/validate.php">
<MixedAssignment occurrences="1">
<code>$values</code>
</MixedAssignment>
<RedundantCast occurrences="2">
<code>(string) $_POST['id']</code>
<code>(string) $_POST['values']</code>
</RedundantCast>
</file>
<file src="test/classes/AbstractNetworkTestCase.php">
<MixedAssignment occurrences="1">
<code>$http_response_code_param</code>
@ -15537,7 +15532,7 @@
<code>$_SESSION['tmpval']['relational_display']</code>
<code>$_SESSION['tmpval']['relational_display']</code>
</MixedArrayAssignment>
<MixedAssignment occurrences="29">
<MixedAssignment occurrences="30">
<code>$actual</code>
<code>$actual</code>
<code>$result</code>
@ -15567,6 +15562,7 @@
<code>$result</code>
<code>$result</code>
<code>$result</code>
<code>$result</code>
</MixedAssignment>
<MixedInferredReturnType occurrences="1">
<code>array</code>

View File

@ -1,15 +1,9 @@
<?php
/**
* Front controller for config view / download and clear
*/
declare(strict_types=1);
use PhpMyAdmin\Config\Forms\Setup\ConfigForm;
use PhpMyAdmin\Core;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Setup\ConfigGenerator;
use PhpMyAdmin\Url;
use PhpMyAdmin\Common;
use PhpMyAdmin\Controllers\Setup\ShowConfigController;
if (! defined('ROOT_PATH')) {
// phpcs:disable PSR1.Files.SideEffects
@ -23,32 +17,4 @@ define('PHPMYADMIN', true);
require ROOT_PATH . 'setup/lib/common.inc.php';
$form_display = new ConfigForm($GLOBALS['ConfigFile']);
$form_display->save('Config');
$response = ResponseRenderer::getInstance();
$response->disable();
if (isset($_POST['eol'])) {
$_SESSION['eol'] = $_POST['eol'] === 'unix' ? 'unix' : 'win';
}
if (isset($_POST['submit_clear']) && is_scalar($_POST['submit_clear']) ? $_POST['submit_clear'] : '') {
// Clear current config and return to main page
$GLOBALS['ConfigFile']->resetConfigData();
// drop post data
$response->generateHeader303('index.php' . Url::getCommonRaw());
exit;
}
if (isset($_POST['submit_download']) && is_scalar($_POST['submit_download']) ? $_POST['submit_download'] : '') {
// Output generated config file
Core::downloadHeader('config.inc.php', 'text/plain');
$response->disable();
echo ConfigGenerator::getConfigFile($GLOBALS['ConfigFile']);
exit;
}
// Show generated config file in a <textarea>
$response->generateHeader303('index.php' . Url::getCommonRaw(['page' => 'config']));
exit;
(new ShowConfigController())(Common::getRequest());

View File

@ -1,18 +1,9 @@
<?php
/**
* Front controller for setup script
*/
declare(strict_types=1);
use PhpMyAdmin\Controllers\Setup\ConfigController;
use PhpMyAdmin\Controllers\Setup\FormController;
use PhpMyAdmin\Controllers\Setup\HomeController;
use PhpMyAdmin\Controllers\Setup\ServersController;
use PhpMyAdmin\Core;
use PhpMyAdmin\Header;
use PhpMyAdmin\Template;
use PhpMyAdmin\Url;
use PhpMyAdmin\Common;
use PhpMyAdmin\Controllers\Setup\MainController;
if (! defined('ROOT_PATH')) {
// phpcs:disable PSR1.Files.SideEffects
@ -26,58 +17,4 @@ define('PHPMYADMIN', true);
require ROOT_PATH . 'setup/lib/common.inc.php';
if (@file_exists(CONFIG_FILE) && ! $GLOBALS['cfg']['DBG']['demo']) {
Core::fatalError(__('Configuration already exists, setup is disabled!'));
}
$page = 'index';
if (isset($_GET['page']) && in_array($_GET['page'], ['form', 'config', 'servers'], true)) {
$page = $_GET['page'];
}
Core::noCacheHeader();
// Sent security-related headers
(new Header())->sendHttpHeaders();
if ($page === 'form') {
echo (new FormController($GLOBALS['ConfigFile'], new Template()))([
'formset' => $_GET['formset'] ?? null,
]);
return;
}
if ($page === 'config') {
echo (new ConfigController($GLOBALS['ConfigFile'], new Template()))([
'formset' => $_GET['formset'] ?? null,
'eol' => $_GET['eol'] ?? null,
]);
return;
}
if ($page === 'servers') {
$controller = new ServersController($GLOBALS['ConfigFile'], new Template());
if (isset($_GET['mode']) && $_GET['mode'] === 'remove' && ($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'POST') {
$controller->destroy([
'id' => $_GET['id'] ?? null,
]);
header('Location: index.php' . Url::getCommonRaw());
return;
}
echo $controller->index([
'formset' => $_GET['formset'] ?? null,
'mode' => $_GET['mode'] ?? null,
'id' => $_GET['id'] ?? null,
]);
return;
}
echo (new HomeController($GLOBALS['ConfigFile'], new Template()))([
'formset' => $_GET['formset'] ?? null,
'version_check' => $_GET['version_check'] ?? null,
]);
(new MainController())(Common::getRequest());

View File

@ -1,12 +1,9 @@
<?php
/**
* Validation callback.
*/
declare(strict_types=1);
use PhpMyAdmin\Config\Validator;
use PhpMyAdmin\Core;
use PhpMyAdmin\Common;
use PhpMyAdmin\Controllers\Setup\ValidateController;
if (! defined('ROOT_PATH')) {
// phpcs:disable PSR1.Files.SideEffects
@ -20,24 +17,4 @@ define('PHPMYADMIN', true);
require ROOT_PATH . 'setup/lib/common.inc.php';
Core::headerJSON();
$ids = isset($_POST['id']) && is_scalar($_POST['id']) ? (string) $_POST['id'] : '';
$vids = explode(',', $ids);
$vals = isset($_POST['values']) && is_scalar($_POST['values']) ? (string) $_POST['values'] : '';
$values = json_decode($vals);
if (! ($values instanceof stdClass)) {
Core::fatalError(__('Wrong data'));
}
$values = (array) $values;
$result = Validator::validate($GLOBALS['ConfigFile'], $vids, $values, true);
if ($result === false) {
$result = sprintf(
__('Wrong data or no validation for %s'),
implode(',', $vids)
);
}
echo $result !== true ? json_encode($result) : '';
(new ValidateController())(Common::getRequest());