Merge pull request #17825 from MauricioFauth/common-refactor

Improve `index.php` and `setup/index.php`
This commit is contained in:
Maurício Meneghini Fauth 2022-10-22 10:13:23 -03:00 committed by GitHub
commit e89ebe179c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 102 additions and 117 deletions

View File

@ -3,28 +3,21 @@
declare(strict_types=1);
use PhpMyAdmin\Common;
use PhpMyAdmin\Core;
use PhpMyAdmin\Routing;
// phpcs:disable PSR1.Files.SideEffects
if (! defined('ROOT_PATH')) {
// phpcs:disable PSR1.Files.SideEffects
define('ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR);
// phpcs:enable
}
define('PHPMYADMIN', true);
// phpcs:enable
if (PHP_VERSION_ID < 70205) {
die('<p>PHP 7.2.5+ is required.</p><p>Currently installed version is: ' . PHP_VERSION . '</p>');
}
// phpcs:disable PSR1.Files.SideEffects
define('PHPMYADMIN', true);
// phpcs:enable
require_once ROOT_PATH . 'libraries/constants.php';
/**
* Activate autoloader
*/
if (! @is_readable(AUTOLOAD_FILE)) {
die(
'<p>File <samp>' . AUTOLOAD_FILE . '</samp> missing or not readable.</p>'
@ -37,5 +30,3 @@ if (! @is_readable(AUTOLOAD_FILE)) {
require AUTOLOAD_FILE;
Common::run();
Routing::callControllerForRoute(Common::getRequest(), Routing::getDispatcher(), Core::getContainerBuilder());

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace PhpMyAdmin;
use PhpMyAdmin\Config\ConfigFile;
use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\Dbal\DatabaseName;
use PhpMyAdmin\Dbal\TableName;
@ -38,7 +39,9 @@ use function mb_strlen;
use function mb_strpos;
use function mb_strrpos;
use function mb_substr;
use function ob_start;
use function register_shutdown_function;
use function restore_error_handler;
use function session_id;
use function strlen;
use function time;
@ -219,6 +222,15 @@ final class Common
UrlRedirector::redirect($_GET['url'] ?? '');
}
if ($isSetupPage) {
self::setupPageBootstrap($config);
Routing::callSetupController($request);
return;
}
Routing::callControllerForRoute($request, Routing::getDispatcher(), $GLOBALS['containerBuilder']);
return;
}
@ -292,7 +304,8 @@ final class Common
'message',
Message::error(__('Error: Token mismatch'))
);
exit;
return;
}
Profiling::check($GLOBALS['dbi'], $response);
@ -307,13 +320,13 @@ final class Common
/* Tell tracker that it can actually work */
Tracker::enable();
if (empty($GLOBALS['server']) || ! isset($GLOBALS['cfg']['ZeroConf']) || $GLOBALS['cfg']['ZeroConf'] !== true) {
return;
if (! empty($GLOBALS['server']) && isset($GLOBALS['cfg']['ZeroConf']) && $GLOBALS['cfg']['ZeroConf']) {
/** @var Relation $relation */
$relation = $GLOBALS['containerBuilder']->get('relation');
$GLOBALS['dbi']->postConnectControl($relation);
}
/** @var Relation $relation */
$relation = $GLOBALS['containerBuilder']->get('relation');
$GLOBALS['dbi']->postConnectControl($relation);
Routing::callControllerForRoute($request, Routing::getDispatcher(), $GLOBALS['containerBuilder']);
}
/**
@ -629,4 +642,34 @@ final class Common
return self::$request;
}
private static function setupPageBootstrap(Config $config): void
{
// use default error handler
restore_error_handler();
// Save current language in a cookie, since it was not set in Common::run().
$config->setCookie('pma_lang', (string) $GLOBALS['lang']);
$config->set('is_setup', true);
$GLOBALS['ConfigFile'] = new ConfigFile();
$GLOBALS['ConfigFile']->setPersistKeys([
'DefaultLang',
'ServerDefault',
'UploadDir',
'SaveDir',
'Servers/1/verbose',
'Servers/1/host',
'Servers/1/port',
'Servers/1/socket',
'Servers/1/auth_type',
'Servers/1/user',
'Servers/1/password',
]);
$GLOBALS['dbi'] = DatabaseInterface::load();
// allows for redirection even after sending some data
ob_start();
}
}

View File

@ -10,6 +10,9 @@ use FastRoute\Dispatcher\GroupCountBased as DispatcherGroupCountBased;
use FastRoute\RouteCollector;
use FastRoute\RouteParser\Std as RouteParserStd;
use PhpMyAdmin\Controllers\HomeController;
use PhpMyAdmin\Controllers\Setup\MainController;
use PhpMyAdmin\Controllers\Setup\ShowConfigController;
use PhpMyAdmin\Controllers\Setup\ValidateController;
use PhpMyAdmin\Http\ServerRequest;
use Psr\Container\ContainerInterface;
@ -178,4 +181,31 @@ class Routing
&& isset($dispatchData[0]['GET']['/']) && is_string($dispatchData[0]['GET']['/'])
&& $dispatchData[0]['GET']['/'] === HomeController::class;
}
public static function callSetupController(ServerRequest $request): void
{
$route = $request->getRoute();
if ($route === '/setup' || $route === '/') {
(new MainController())($request);
return;
}
if ($route === '/setup/show-config') {
(new ShowConfigController())($request);
return;
}
if ($route === '/setup/validate') {
(new ValidateController())($request);
return;
}
Core::fatalError(sprintf(
__('Error 404! The page %s was not found.'),
'[code]' . htmlspecialchars($route) . '[/code]'
));
}
}

View File

@ -239,7 +239,8 @@
<code>$_REQUEST['back']</code>
<code>$_REQUEST['goto']</code>
</PossiblyInvalidCast>
<RedundantCast occurrences="2">
<RedundantCast occurrences="3">
<code>(string) $GLOBALS['lang']</code>
<code>(string) $_POST['token']</code>
<code>(string) $_POST['token']</code>
</RedundantCast>
@ -14438,11 +14439,6 @@
<code>$serviceName</code>
</MixedAssignment>
</file>
<file src="setup/lib/common.inc.php">
<RedundantCast occurrences="1">
<code>(string) $GLOBALS['lang']</code>
</RedundantCast>
</file>
<file src="test/classes/AbstractNetworkTestCase.php">
<MixedAssignment occurrences="1">
<code>$http_response_code_param</code>

View File

@ -3,41 +3,30 @@
declare(strict_types=1);
use PhpMyAdmin\Common;
use PhpMyAdmin\Controllers\Setup\MainController;
use PhpMyAdmin\Controllers\Setup\ShowConfigController;
use PhpMyAdmin\Controllers\Setup\ValidateController;
use PhpMyAdmin\Core;
if (! defined('ROOT_PATH')) {
// phpcs:disable PSR1.Files.SideEffects
define('ROOT_PATH', dirname(__DIR__) . DIRECTORY_SEPARATOR);
// phpcs:enable
}
// phpcs:disable PSR1.Files.SideEffects
if (! defined('ROOT_PATH')) {
define('ROOT_PATH', dirname(__DIR__) . DIRECTORY_SEPARATOR);
}
define('PHPMYADMIN', true);
// phpcs:enable
require ROOT_PATH . 'setup/lib/common.inc.php';
$request = Common::getRequest();
$route = $request->getRoute();
if ($route === '/setup' || $route === '/') {
(new MainController())($request);
exit;
if (PHP_VERSION_ID < 70205) {
die('<p>PHP 7.2.5+ is required.</p><p>Currently installed version is: ' . PHP_VERSION . '</p>');
}
if ($route === '/setup/show-config') {
(new ShowConfigController())($request);
exit;
require_once ROOT_PATH . 'libraries/constants.php';
if (! @is_readable(AUTOLOAD_FILE)) {
die(
'<p>File <samp>' . AUTOLOAD_FILE . '</samp> missing or not readable.</p>'
. '<p>Most likely you did not run Composer to '
. '<a href="https://docs.phpmyadmin.net/en/latest/setup.html#installing-from-git">'
. 'install library files</a>.</p>'
);
}
if ($route === '/setup/validate') {
(new ValidateController())($request);
exit;
}
require AUTOLOAD_FILE;
Core::fatalError(sprintf(
__('Error 404! The page %s was not found.'),
'[code]' . htmlspecialchars($route) . '[/code]'
));
Common::run(true);

View File

@ -1,64 +0,0 @@
<?php
declare(strict_types=1);
use PhpMyAdmin\Common;
use PhpMyAdmin\Config\ConfigFile;
use PhpMyAdmin\DatabaseInterface;
if (PHP_VERSION_ID < 70205) {
die('<p>PHP 7.2.5+ is required.</p><p>Currently installed version is: ' . PHP_VERSION . '</p>');
}
if (! defined('PHPMYADMIN')) {
exit;
}
require_once ROOT_PATH . 'libraries/constants.php';
/**
* Activate autoloader
*/
if (! @is_readable(AUTOLOAD_FILE)) {
die(
'<p>File <samp>' . AUTOLOAD_FILE . '</samp> missing or not readable.</p>'
. '<p>Most likely you did not run Composer to '
. '<a href="https://docs.phpmyadmin.net/en/latest/setup.html#installing-from-git">'
. 'install library files</a>.</p>'
);
}
require AUTOLOAD_FILE;
chdir('..');
Common::run(true);
// use default error handler
restore_error_handler();
// Save current language in a cookie, since it was not set in Common::run().
$GLOBALS['config']->setCookie('pma_lang', (string) $GLOBALS['lang']);
$GLOBALS['config']->set('is_setup', true);
$GLOBALS['ConfigFile'] = new ConfigFile();
$GLOBALS['ConfigFile']->setPersistKeys(
[
'DefaultLang',
'ServerDefault',
'UploadDir',
'SaveDir',
'Servers/1/verbose',
'Servers/1/host',
'Servers/1/port',
'Servers/1/socket',
'Servers/1/auth_type',
'Servers/1/user',
'Servers/1/password',
]
);
$GLOBALS['dbi'] = DatabaseInterface::load();
// allows for redirection even after sending some data
ob_start();