From 39270211d93b960cc3efaddc70c29f3cbbce3ccc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Fri, 21 Oct 2022 15:52:20 -0300 Subject: [PATCH 1/4] Remove the setup/lib/common.inc.php file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Inline the file contents into setup/index.php file. Signed-off-by: Maurício Meneghini Fauth --- index.php | 13 +++----- psalm-baseline.xml | 2 +- setup/index.php | 60 ++++++++++++++++++++++++++++++++----- setup/lib/common.inc.php | 64 ---------------------------------------- 4 files changed, 58 insertions(+), 81 deletions(-) delete mode 100644 setup/lib/common.inc.php diff --git a/index.php b/index.php index 4ed75379e1..ad73078bf7 100644 --- a/index.php +++ b/index.php @@ -6,25 +6,20 @@ 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.

' diff --git a/psalm-baseline.xml b/psalm-baseline.xml index e091397ee0..20240c86cd 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -14438,7 +14438,7 @@ $serviceName - + (string) $GLOBALS['lang'] diff --git a/setup/index.php b/setup/index.php index e7ca83ef41..1b1077c372 100644 --- a/setup/index.php +++ b/setup/index.php @@ -3,22 +3,68 @@ declare(strict_types=1); use PhpMyAdmin\Common; +use PhpMyAdmin\Config\ConfigFile; 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 -} +use PhpMyAdmin\DatabaseInterface; // 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'; +if (PHP_VERSION_ID < 70205) { + die('

PHP 7.2.5+ is required.

Currently installed version is: ' . PHP_VERSION . '

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

' + ); +} + +require AUTOLOAD_FILE; + +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(); $request = Common::getRequest(); $route = $request->getRoute(); 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(); From dbb9d68236f020065dd4aa77941be347afa46bd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Fri, 21 Oct 2022 17:24:10 -0300 Subject: [PATCH 2/4] Extract setup page bootstrap to the Common class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- libraries/classes/Common.php | 37 ++++++++++++++++++++++++++++++++++++ psalm-baseline.xml | 8 ++------ setup/index.php | 31 ------------------------------ 3 files changed, 39 insertions(+), 37 deletions(-) diff --git a/libraries/classes/Common.php b/libraries/classes/Common.php index 0ea25f1b81..22e2a0f2f3 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,10 @@ final class Common UrlRedirector::redirect($_GET['url'] ?? ''); } + if ($isSetupPage) { + self::setupPageBootstrap($config); + } + return; } @@ -629,4 +636,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/psalm-baseline.xml b/psalm-baseline.xml index 20240c86cd..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 1b1077c372..dc4d1ffe20 100644 --- a/setup/index.php +++ b/setup/index.php @@ -3,12 +3,10 @@ declare(strict_types=1); use PhpMyAdmin\Common; -use PhpMyAdmin\Config\ConfigFile; use PhpMyAdmin\Controllers\Setup\MainController; use PhpMyAdmin\Controllers\Setup\ShowConfigController; use PhpMyAdmin\Controllers\Setup\ValidateController; use PhpMyAdmin\Core; -use PhpMyAdmin\DatabaseInterface; // phpcs:disable PSR1.Files.SideEffects if (! defined('ROOT_PATH')) { @@ -37,35 +35,6 @@ require AUTOLOAD_FILE; 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(); - $request = Common::getRequest(); $route = $request->getRoute(); if ($route === '/setup' || $route === '/') { From 49e7d82c0fc3e632eadea27d78f93bd871eace34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Fri, 21 Oct 2022 18:17:16 -0300 Subject: [PATCH 3/4] Extract setup routing logic to the Routing class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- libraries/classes/Common.php | 1 + libraries/classes/Routing.php | 30 ++++++++++++++++++++++++++++++ setup/index.php | 26 -------------------------- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/libraries/classes/Common.php b/libraries/classes/Common.php index 22e2a0f2f3..1ed3e21fd9 100644 --- a/libraries/classes/Common.php +++ b/libraries/classes/Common.php @@ -224,6 +224,7 @@ final class Common if ($isSetupPage) { self::setupPageBootstrap($config); + Routing::callSetupController($request); } return; 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/setup/index.php b/setup/index.php index dc4d1ffe20..a8b328d62e 100644 --- a/setup/index.php +++ b/setup/index.php @@ -3,10 +3,6 @@ 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; // phpcs:disable PSR1.Files.SideEffects if (! defined('ROOT_PATH')) { @@ -34,25 +30,3 @@ if (! @is_readable(AUTOLOAD_FILE)) { require AUTOLOAD_FILE; Common::run(true); - -$request = Common::getRequest(); -$route = $request->getRoute(); -if ($route === '/setup' || $route === '/') { - (new MainController())($request); - exit; -} - -if ($route === '/setup/show-config') { - (new ShowConfigController())($request); - exit; -} - -if ($route === '/setup/validate') { - (new ValidateController())($request); - exit; -} - -Core::fatalError(sprintf( - __('Error 404! The page %s was not found.'), - '[code]' . htmlspecialchars($route) . '[/code]' -)); From 334bd8400280de10323d3caffcac364efb21b8b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Fri, 21 Oct 2022 22:09:24 -0300 Subject: [PATCH 4/4] Move Routing::callControllerForRoute call to Common class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- index.php | 4 ---- libraries/classes/Common.php | 17 +++++++++++------ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/index.php b/index.php index ad73078bf7..7c0f885ea7 100644 --- a/index.php +++ b/index.php @@ -3,8 +3,6 @@ declare(strict_types=1); use PhpMyAdmin\Common; -use PhpMyAdmin\Core; -use PhpMyAdmin\Routing; // phpcs:disable PSR1.Files.SideEffects if (! defined('ROOT_PATH')) { @@ -32,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 1ed3e21fd9..c1f8433a31 100644 --- a/libraries/classes/Common.php +++ b/libraries/classes/Common.php @@ -225,8 +225,12 @@ final class Common if ($isSetupPage) { self::setupPageBootstrap($config); Routing::callSetupController($request); + + return; } + Routing::callControllerForRoute($request, Routing::getDispatcher(), $GLOBALS['containerBuilder']); + return; } @@ -300,7 +304,8 @@ final class Common 'message', Message::error(__('Error: Token mismatch')) ); - exit; + + return; } Profiling::check($GLOBALS['dbi'], $response); @@ -315,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']); } /**