Skip to content

Commit 1b7fbb9

Browse files
Merge pull request #18496 from MauricioFauth/common-class
Rename the Common class to Application
2 parents 78e446e + 951c965 commit 1b7fbb9

15 files changed

Lines changed: 201 additions & 142 deletions

File tree

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,18 @@
4848
use const CONFIG_FILE;
4949
use const E_USER_ERROR;
5050

51-
final class Common
51+
final class Application
5252
{
5353
private static ServerRequest|null $request = null;
5454

55+
public static function init(): self
56+
{
57+
/** @var Application $application */
58+
$application = Core::getContainerBuilder()->get(self::class);
59+
60+
return $application;
61+
}
62+
5563
/**
5664
* Misc stuff and REQUIRED by ALL the scripts.
5765
* MUST be included by every script
@@ -80,33 +88,28 @@ final class Common
8088
* - db connection
8189
* - authentication work
8290
*/
83-
public static function run(bool $isSetupPage = false): void
91+
public function run(bool $isSetupPage = false): void
8492
{
85-
$GLOBALS['lang'] ??= null;
86-
$GLOBALS['theme'] ??= null;
87-
$GLOBALS['urlParams'] ??= null;
88-
$GLOBALS['token_mismatch'] ??= null;
93+
$container = Core::getContainerBuilder();
8994

9095
$request = self::getRequest();
9196
$route = $request->getRoute();
9297

9398
$isMinimumCommon = $isSetupPage || $route === '/import-status' || $route === '/url' || $route === '/messages';
9499

95-
$container = Core::getContainerBuilder();
96-
97100
/** @var ErrorHandler $errorHandler */
98101
$errorHandler = $container->get('error_handler');
99102
$GLOBALS['errorHandler'] = $errorHandler;
100103

101104
try {
102-
self::checkRequiredPhpExtensions();
105+
$this->checkRequiredPhpExtensions();
103106
} catch (MissingExtensionException $exception) {
104-
echo self::getGenericError($exception->getMessage());
107+
echo $this->getGenericError($exception->getMessage());
105108

106109
return;
107110
}
108111

109-
self::configurePhpSettings();
112+
$this->configurePhpSettings();
110113

111114
/** @var Config $config */
112115
$config = $container->get('config');
@@ -115,19 +118,19 @@ public static function run(bool $isSetupPage = false): void
115118
try {
116119
$config->loadAndCheck(CONFIG_FILE);
117120
} catch (ConfigException $exception) {
118-
echo self::getGenericError($exception->getMessage());
121+
echo $this->getGenericError($exception->getMessage());
119122

120123
return;
121124
}
122125

123-
$request = self::updateUriScheme($config, $request);
126+
$request = $this->updateUriScheme($config, $request);
124127

125128
if ($route !== '/messages') {
126129
try {
127130
// Include session handling after the globals, to prevent overwriting.
128131
Session::setUp($config, $errorHandler);
129132
} catch (SessionHandlerException $exception) {
130-
echo self::getGenericError($exception->getMessage());
133+
echo $this->getGenericError($exception->getMessage());
131134

132135
return;
133136
}
@@ -147,10 +150,10 @@ public static function run(bool $isSetupPage = false): void
147150
$GLOBALS['urlParams'] = [];
148151
$container->setParameter('url_params', $GLOBALS['urlParams']);
149152

150-
self::setGotoAndBackGlobals($container, $config);
151-
self::checkTokenRequestParam();
152-
self::setDatabaseAndTableFromRequest($container, $request);
153-
self::setSQLQueryGlobalFromRequest($container, $request);
153+
$this->setGotoAndBackGlobals($container, $config);
154+
$this->checkTokenRequestParam();
155+
$this->setDatabaseAndTableFromRequest($container, $request);
156+
$this->setSQLQueryGlobalFromRequest($container, $request);
154157

155158
//$_REQUEST['set_theme'] // checked later in this file LABEL_theme_setup
156159
//$_REQUEST['server']; // checked later in this file
@@ -172,21 +175,21 @@ public static function run(bool $isSetupPage = false): void
172175
$config->checkPermissions();
173176
$config->checkErrors();
174177
} catch (ConfigException $exception) {
175-
echo self::getGenericError($exception->getMessage());
178+
echo $this->getGenericError($exception->getMessage());
176179

177180
return;
178181
}
179182

180183
try {
181-
self::checkServerConfiguration();
182-
self::checkRequest();
184+
$this->checkServerConfiguration();
185+
$this->checkRequest();
183186
} catch (RuntimeException $exception) {
184-
echo self::getGenericError($exception->getMessage());
187+
echo $this->getGenericError($exception->getMessage());
185188

186189
return;
187190
}
188191

189-
self::setCurrentServerGlobal($container, $config, $request->getParam('server'));
192+
$this->setCurrentServerGlobal($container, $config, $request->getParam('server'));
190193

191194
$GLOBALS['cfg'] = $config->settings;
192195
$settings = $config->getSettings();
@@ -206,7 +209,7 @@ public static function run(bool $isSetupPage = false): void
206209
}
207210

208211
if ($isSetupPage) {
209-
self::setupPageBootstrap($config);
212+
$this->setupPageBootstrap($config);
210213
Routing::callSetupController($request);
211214

212215
return;
@@ -237,7 +240,7 @@ public static function run(bool $isSetupPage = false): void
237240
try {
238241
$authPlugin = $authPluginFactory->create();
239242
} catch (AuthenticationPluginException $exception) {
240-
echo self::getGenericError($exception->getMessage());
243+
echo $this->getGenericError($exception->getMessage());
241244

242245
return;
243246
}
@@ -253,15 +256,15 @@ public static function run(bool $isSetupPage = false): void
253256
// phpcs:enable
254257
}
255258

256-
self::connectToDatabaseServer($GLOBALS['dbi'], $authPlugin, $currentServer);
259+
$this->connectToDatabaseServer($GLOBALS['dbi'], $authPlugin, $currentServer);
257260
$authPlugin->rememberCredentials();
258261
$authPlugin->checkTwoFactor();
259262

260263
/* Log success */
261264
Logging::logUser($config, $currentServer->user);
262265

263266
if ($GLOBALS['dbi']->getVersion() < $settings->mysqlMinVersion['internal']) {
264-
echo self::getGenericError(sprintf(
267+
echo $this->getGenericError(sprintf(
265268
__('You should upgrade to %s %s or later.'),
266269
'MySQL',
267270
$settings->mysqlMinVersion['human'],
@@ -322,7 +325,7 @@ public static function run(bool $isSetupPage = false): void
322325
/**
323326
* Checks that required PHP extensions are there.
324327
*/
325-
private static function checkRequiredPhpExtensions(): void
328+
private function checkRequiredPhpExtensions(): void
326329
{
327330
/**
328331
* Warning about mbstring.
@@ -374,7 +377,7 @@ private static function checkRequiredPhpExtensions(): void
374377
/**
375378
* Applies changes to PHP configuration.
376379
*/
377-
private static function configurePhpSettings(): void
380+
private function configurePhpSettings(): void
378381
{
379382
/**
380383
* Set utf-8 encoding for PHP
@@ -397,7 +400,7 @@ private static function configurePhpSettings(): void
397400
date_default_timezone_set(@date_default_timezone_get());
398401
}
399402

400-
private static function setGotoAndBackGlobals(ContainerInterface $container, Config $config): void
403+
private function setGotoAndBackGlobals(ContainerInterface $container, Config $config): void
401404
{
402405
$GLOBALS['back'] ??= null;
403406
$GLOBALS['urlParams'] ??= null;
@@ -442,7 +445,7 @@ private static function setGotoAndBackGlobals(ContainerInterface $container, Con
442445
* GET Requests would never have token and therefore checking
443446
* mis-match does not make sense.
444447
*/
445-
public static function checkTokenRequestParam(): void
448+
public function checkTokenRequestParam(): void
446449
{
447450
$GLOBALS['token_mismatch'] = true;
448451
$GLOBALS['token_provided'] = false;
@@ -478,7 +481,7 @@ public static function checkTokenRequestParam(): void
478481
Sanitize::removeRequestVars($allowList);
479482
}
480483

481-
private static function setDatabaseAndTableFromRequest(ContainerInterface $container, ServerRequest $request): void
484+
private function setDatabaseAndTableFromRequest(ContainerInterface $container, ServerRequest $request): void
482485
{
483486
$GLOBALS['urlParams'] ??= null;
484487

@@ -500,7 +503,7 @@ private static function setDatabaseAndTableFromRequest(ContainerInterface $conta
500503
/**
501504
* Check whether PHP configuration matches our needs.
502505
*/
503-
private static function checkServerConfiguration(): void
506+
private function checkServerConfiguration(): void
504507
{
505508
/**
506509
* As we try to handle charsets by ourself, mbstring overloads just
@@ -533,7 +536,7 @@ private static function checkServerConfiguration(): void
533536
/**
534537
* Checks request and fails with fatal error if something problematic is found
535538
*/
536-
private static function checkRequest(): void
539+
private function checkRequest(): void
537540
{
538541
if (isset($_REQUEST['GLOBALS']) || isset($_FILES['GLOBALS'])) {
539542
throw new RuntimeException(__('GLOBALS overwrite attempt'));
@@ -549,7 +552,7 @@ private static function checkRequest(): void
549552
throw new RuntimeException(__('possible exploit'));
550553
}
551554

552-
private static function connectToDatabaseServer(
555+
private function connectToDatabaseServer(
553556
DatabaseInterface $dbi,
554557
AuthenticationPlugin $auth,
555558
Server $currentServer,
@@ -589,13 +592,13 @@ public static function getRequest(): ServerRequest
589592
return self::$request;
590593
}
591594

592-
private static function setupPageBootstrap(Config $config): void
595+
private function setupPageBootstrap(Config $config): void
593596
{
594597
// use default error handler
595598
restore_error_handler();
596599

597600
// Save current language in a cookie, since it was not set in Common::run().
598-
$config->setCookie('pma_lang', (string) $GLOBALS['lang']);
601+
$config->setCookie('pma_lang', $GLOBALS['lang']);
599602
$config->set('is_setup', true);
600603

601604
$GLOBALS['ConfigFile'] = new ConfigFile();
@@ -619,7 +622,7 @@ private static function setupPageBootstrap(Config $config): void
619622
ob_start();
620623
}
621624

622-
private static function setSQLQueryGlobalFromRequest(ContainerInterface $container, ServerRequest $request): void
625+
private function setSQLQueryGlobalFromRequest(ContainerInterface $container, ServerRequest $request): void
623626
{
624627
$sqlQuery = '';
625628
if ($request->isPost()) {
@@ -634,7 +637,7 @@ private static function setSQLQueryGlobalFromRequest(ContainerInterface $contain
634637
$container->setParameter('sql_query', $sqlQuery);
635638
}
636639

637-
private static function setCurrentServerGlobal(
640+
private function setCurrentServerGlobal(
638641
ContainerInterface $container,
639642
Config $config,
640643
mixed $serverParamFromRequest,
@@ -646,7 +649,7 @@ private static function setCurrentServerGlobal(
646649
$container->setParameter('url_params', $GLOBALS['urlParams']);
647650
}
648651

649-
private static function getGenericError(string $message): string
652+
private function getGenericError(string $message): string
650653
{
651654
return (new Template())->render('error/generic', [
652655
'lang' => $GLOBALS['lang'] ?? 'en',
@@ -655,7 +658,7 @@ private static function getGenericError(string $message): string
655658
]);
656659
}
657660

658-
private static function updateUriScheme(Config $config, ServerRequest $request): ServerRequest
661+
private function updateUriScheme(Config $config, ServerRequest $request): ServerRequest
659662
{
660663
$uriScheme = $config->isHttps() ? 'https' : 'http';
661664
$uri = $request->getUri();

libraries/classes/DatabaseInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public function tryQuery(
225225
sprintf(
226226
'SQL[%s?route=%s]: %0.3f(W:%d,C:%s,L:0x%02X) > %s',
227227
basename($_SERVER['SCRIPT_NAME']),
228-
Common::getRequest()->getRoute(),
228+
Application::getRequest()->getRoute(),
229229
$this->lastQueryExecutionTime,
230230
$this->getWarningCount($connectionType),
231231
$cacheAffectedRows ? 'y' : 'n',

libraries/classes/DbTableExists.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private static function checkTable(string $db, string $table, bool $isTransforma
108108

109109
/** @var SqlController $controller */
110110
$controller = Core::getContainerBuilder()->get(SqlController::class);
111-
$controller(Common::getRequest());
111+
$controller(Application::getRequest());
112112

113113
ResponseRenderer::getInstance()->callExit();
114114
}

libraries/classes/Export.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ public function exportTable(
10671067
public function showPage(string $exportType): void
10681068
{
10691069
$GLOBALS['active_page'] ??= null;
1070-
$request = Common::getRequest();
1070+
$request = Application::getRequest();
10711071
$container = Core::getContainerBuilder();
10721072
if ($exportType === 'server') {
10731073
$GLOBALS['active_page'] = Url::getFromRoute('/server/export');

libraries/classes/Footer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function getSelfUrl(): string
129129
$GLOBALS['server'] ??= null;
130130

131131
$params = [];
132-
$params['route'] = Common::getRequest()->getRoute();
132+
$params['route'] = Application::getRequest()->getRoute();
133133

134134
if (isset($GLOBALS['db']) && strlen($GLOBALS['db']) > 0) {
135135
$params['db'] = $GLOBALS['db'];

libraries/classes/Menu.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ private function getBreadcrumbs(): string
207207
*/
208208
private function getTableTabs(): array
209209
{
210-
$route = Common::getRequest()->getRoute();
210+
$route = Application::getRequest()->getRoute();
211211

212212
$isSystemSchema = Utilities::isSystemSchema($this->db);
213213
$tableIsView = $this->dbi->getTable($this->db, $this->table)
@@ -319,7 +319,7 @@ private function getTableTabs(): array
319319
*/
320320
private function getDbTabs(): array
321321
{
322-
$route = Common::getRequest()->getRoute();
322+
$route = Application::getRequest()->getRoute();
323323

324324
$isSystemSchema = Utilities::isSystemSchema($this->db);
325325
$numTables = count($this->dbi->getTables($this->db));
@@ -434,7 +434,7 @@ private function getDbTabs(): array
434434
*/
435435
private function getServerTabs(): array
436436
{
437-
$route = Common::getRequest()->getRoute();
437+
$route = Application::getRequest()->getRoute();
438438

439439
$isSuperUser = $this->dbi->isSuperUser();
440440
$isCreateOrGrantUser = $this->dbi->isGrantUser() || $this->dbi->isCreateUser();

libraries/classes/Plugins/Auth/AuthenticationCookie.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace PhpMyAdmin\Plugins\Auth;
99

10-
use PhpMyAdmin\Common;
10+
use PhpMyAdmin\Application;
1111
use PhpMyAdmin\Config;
1212
use PhpMyAdmin\Core;
1313
use PhpMyAdmin\Exceptions\SessionHandlerException;
@@ -142,7 +142,7 @@ public function showLoginForm(): never
142142
}
143143

144144
$formParams = [];
145-
$formParams['route'] = Common::getRequest()->getRoute();
145+
$formParams['route'] = Application::getRequest()->getRoute();
146146

147147
if (strlen($GLOBALS['db'])) {
148148
$formParams['db'] = $GLOBALS['db'];
@@ -461,7 +461,7 @@ public function rememberCredentials(): void
461461

462462
// any parameters to pass?
463463
$urlParams = [];
464-
$urlParams['route'] = Common::getRequest()->getRoute();
464+
$urlParams['route'] = Application::getRequest()->getRoute();
465465

466466
if (strlen($GLOBALS['db']) > 0) {
467467
$urlParams['db'] = $GLOBALS['db'];

libraries/classes/Plugins/TwoFactor/WebAuthn.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace PhpMyAdmin\Plugins\TwoFactor;
66

7-
use PhpMyAdmin\Common;
7+
use PhpMyAdmin\Application;
88
use PhpMyAdmin\Http\ServerRequest;
99
use PhpMyAdmin\Plugins\TwoFactorPlugin;
1010
use PhpMyAdmin\ResponseRenderer;
@@ -75,7 +75,7 @@ public function setServer(Server $server): void
7575
private function getRequest(): ServerRequest
7676
{
7777
if ($this->serverRequest === null) {
78-
$this->serverRequest = Common::getRequest();
78+
$this->serverRequest = Application::getRequest();
7979
}
8080

8181
return $this->serverRequest;

0 commit comments

Comments
 (0)