diff --git a/index.php b/index.php index 4ed75379e1..7c0f885ea7 100644 --- a/index.php +++ b/index.php @@ -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('

PHP 7.2.5+ is required.

Currently installed version is: ' . PHP_VERSION . '

'); } -// 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( '

File ' . AUTOLOAD_FILE . ' missing or not readable.

' @@ -37,5 +30,3 @@ if (! @is_readable(AUTOLOAD_FILE)) { require AUTOLOAD_FILE; Common::run(); - -Routing::callControllerForRoute(Common::getRequest(), Routing::getDispatcher(), Core::getContainerBuilder()); diff --git a/libraries/classes/Common.php b/libraries/classes/Common.php index 0ea25f1b81..c1f8433a31 100644 --- a/libraries/classes/Common.php +++ b/libraries/classes/Common.php @@ -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(); + } } diff --git a/libraries/classes/Routing.php b/libraries/classes/Routing.php index 47f160f707..fc0192e12b 100644 --- a/libraries/classes/Routing.php +++ b/libraries/classes/Routing.php @@ -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]' + )); + } } diff --git a/psalm-baseline.xml b/psalm-baseline.xml index e091397ee0..b68cb2d992 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -239,7 +239,8 @@ $_REQUEST['back'] $_REQUEST['goto'] - + + (string) $GLOBALS['lang'] (string) $_POST['token'] (string) $_POST['token'] @@ -14438,11 +14439,6 @@ $serviceName - - - (string) $GLOBALS['lang'] - - $http_response_code_param diff --git a/setup/index.php b/setup/index.php index e7ca83ef41..a8b328d62e 100644 --- a/setup/index.php +++ b/setup/index.php @@ -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('

PHP 7.2.5+ is required.

Currently installed version is: ' . PHP_VERSION . '

'); } -if ($route === '/setup/show-config') { - (new ShowConfigController())($request); - exit; +require_once ROOT_PATH . 'libraries/constants.php'; + +if (! @is_readable(AUTOLOAD_FILE)) { + die( + '

File ' . AUTOLOAD_FILE . ' missing or not readable.

' + . '

Most likely you did not run Composer to ' + . '' + . 'install library files.

' + ); } -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); diff --git a/setup/lib/common.inc.php b/setup/lib/common.inc.php deleted file mode 100644 index f20cf1c8ad..0000000000 --- a/setup/lib/common.inc.php +++ /dev/null @@ -1,64 +0,0 @@ -PHP 7.2.5+ is required.

Currently installed version is: ' . PHP_VERSION . '

'); -} - -if (! defined('PHPMYADMIN')) { - exit; -} - -require_once ROOT_PATH . 'libraries/constants.php'; - -/** - * Activate autoloader - */ -if (! @is_readable(AUTOLOAD_FILE)) { - die( - '

File ' . AUTOLOAD_FILE . ' missing or not readable.

' - . '

Most likely you did not run Composer to ' - . '' - . 'install library files.

' - ); -} - -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();