Skip to content

Commit e89ebe1

Browse files
Merge pull request #17825 from MauricioFauth/common-refactor
Improve `index.php` and `setup/index.php`
2 parents 93ed4b1 + 334bd84 commit e89ebe1

File tree

6 files changed

+99
-114
lines changed

6 files changed

+99
-114
lines changed

index.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,21 @@
33
declare(strict_types=1);
44

55
use PhpMyAdmin\Common;
6-
use PhpMyAdmin\Core;
7-
use PhpMyAdmin\Routing;
86

7+
// phpcs:disable PSR1.Files.SideEffects
98
if (! defined('ROOT_PATH')) {
10-
// phpcs:disable PSR1.Files.SideEffects
119
define('ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR);
12-
// phpcs:enable
1310
}
1411

12+
define('PHPMYADMIN', true);
13+
// phpcs:enable
14+
1515
if (PHP_VERSION_ID < 70205) {
1616
die('<p>PHP 7.2.5+ is required.</p><p>Currently installed version is: ' . PHP_VERSION . '</p>');
1717
}
1818

19-
// phpcs:disable PSR1.Files.SideEffects
20-
define('PHPMYADMIN', true);
21-
// phpcs:enable
22-
2319
require_once ROOT_PATH . 'libraries/constants.php';
2420

25-
/**
26-
* Activate autoloader
27-
*/
2821
if (! @is_readable(AUTOLOAD_FILE)) {
2922
die(
3023
'<p>File <samp>' . AUTOLOAD_FILE . '</samp> missing or not readable.</p>'
@@ -37,5 +30,3 @@
3730
require AUTOLOAD_FILE;
3831

3932
Common::run();
40-
41-
Routing::callControllerForRoute(Common::getRequest(), Routing::getDispatcher(), Core::getContainerBuilder());

libraries/classes/Common.php

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

55
namespace PhpMyAdmin;
66

7+
use PhpMyAdmin\Config\ConfigFile;
78
use PhpMyAdmin\ConfigStorage\Relation;
89
use PhpMyAdmin\Dbal\DatabaseName;
910
use PhpMyAdmin\Dbal\TableName;
@@ -38,7 +39,9 @@
3839
use function mb_strpos;
3940
use function mb_strrpos;
4041
use function mb_substr;
42+
use function ob_start;
4143
use function register_shutdown_function;
44+
use function restore_error_handler;
4245
use function session_id;
4346
use function strlen;
4447
use function time;
@@ -219,6 +222,15 @@ public static function run(bool $isSetupPage = false): void
219222
UrlRedirector::redirect($_GET['url'] ?? '');
220223
}
221224

225+
if ($isSetupPage) {
226+
self::setupPageBootstrap($config);
227+
Routing::callSetupController($request);
228+
229+
return;
230+
}
231+
232+
Routing::callControllerForRoute($request, Routing::getDispatcher(), $GLOBALS['containerBuilder']);
233+
222234
return;
223235
}
224236

@@ -292,7 +304,8 @@ public static function run(bool $isSetupPage = false): void
292304
'message',
293305
Message::error(__('Error: Token mismatch'))
294306
);
295-
exit;
307+
308+
return;
296309
}
297310

298311
Profiling::check($GLOBALS['dbi'], $response);
@@ -307,13 +320,13 @@ public static function run(bool $isSetupPage = false): void
307320
/* Tell tracker that it can actually work */
308321
Tracker::enable();
309322

310-
if (empty($GLOBALS['server']) || ! isset($GLOBALS['cfg']['ZeroConf']) || $GLOBALS['cfg']['ZeroConf'] !== true) {
311-
return;
323+
if (! empty($GLOBALS['server']) && isset($GLOBALS['cfg']['ZeroConf']) && $GLOBALS['cfg']['ZeroConf']) {
324+
/** @var Relation $relation */
325+
$relation = $GLOBALS['containerBuilder']->get('relation');
326+
$GLOBALS['dbi']->postConnectControl($relation);
312327
}
313328

314-
/** @var Relation $relation */
315-
$relation = $GLOBALS['containerBuilder']->get('relation');
316-
$GLOBALS['dbi']->postConnectControl($relation);
329+
Routing::callControllerForRoute($request, Routing::getDispatcher(), $GLOBALS['containerBuilder']);
317330
}
318331

319332
/**
@@ -629,4 +642,34 @@ public static function getRequest(): ServerRequest
629642

630643
return self::$request;
631644
}
645+
646+
private static function setupPageBootstrap(Config $config): void
647+
{
648+
// use default error handler
649+
restore_error_handler();
650+
651+
// Save current language in a cookie, since it was not set in Common::run().
652+
$config->setCookie('pma_lang', (string) $GLOBALS['lang']);
653+
$config->set('is_setup', true);
654+
655+
$GLOBALS['ConfigFile'] = new ConfigFile();
656+
$GLOBALS['ConfigFile']->setPersistKeys([
657+
'DefaultLang',
658+
'ServerDefault',
659+
'UploadDir',
660+
'SaveDir',
661+
'Servers/1/verbose',
662+
'Servers/1/host',
663+
'Servers/1/port',
664+
'Servers/1/socket',
665+
'Servers/1/auth_type',
666+
'Servers/1/user',
667+
'Servers/1/password',
668+
]);
669+
670+
$GLOBALS['dbi'] = DatabaseInterface::load();
671+
672+
// allows for redirection even after sending some data
673+
ob_start();
674+
}
632675
}

libraries/classes/Routing.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use FastRoute\RouteCollector;
1111
use FastRoute\RouteParser\Std as RouteParserStd;
1212
use PhpMyAdmin\Controllers\HomeController;
13+
use PhpMyAdmin\Controllers\Setup\MainController;
14+
use PhpMyAdmin\Controllers\Setup\ShowConfigController;
15+
use PhpMyAdmin\Controllers\Setup\ValidateController;
1316
use PhpMyAdmin\Http\ServerRequest;
1417
use Psr\Container\ContainerInterface;
1518

@@ -178,4 +181,31 @@ private static function isRoutesCacheFileValid($dispatchData): bool
178181
&& isset($dispatchData[0]['GET']['/']) && is_string($dispatchData[0]['GET']['/'])
179182
&& $dispatchData[0]['GET']['/'] === HomeController::class;
180183
}
184+
185+
public static function callSetupController(ServerRequest $request): void
186+
{
187+
$route = $request->getRoute();
188+
if ($route === '/setup' || $route === '/') {
189+
(new MainController())($request);
190+
191+
return;
192+
}
193+
194+
if ($route === '/setup/show-config') {
195+
(new ShowConfigController())($request);
196+
197+
return;
198+
}
199+
200+
if ($route === '/setup/validate') {
201+
(new ValidateController())($request);
202+
203+
return;
204+
}
205+
206+
Core::fatalError(sprintf(
207+
__('Error 404! The page %s was not found.'),
208+
'[code]' . htmlspecialchars($route) . '[/code]'
209+
));
210+
}
181211
}

psalm-baseline.xml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@
239239
<code>$_REQUEST['back']</code>
240240
<code>$_REQUEST['goto']</code>
241241
</PossiblyInvalidCast>
242-
<RedundantCast occurrences="2">
242+
<RedundantCast occurrences="3">
243+
<code>(string) $GLOBALS['lang']</code>
243244
<code>(string) $_POST['token']</code>
244245
<code>(string) $_POST['token']</code>
245246
</RedundantCast>
@@ -14438,11 +14439,6 @@
1443814439
<code>$serviceName</code>
1443914440
</MixedAssignment>
1444014441
</file>
14441-
<file src="setup/lib/common.inc.php">
14442-
<RedundantCast occurrences="1">
14443-
<code>(string) $GLOBALS['lang']</code>
14444-
</RedundantCast>
14445-
</file>
1444614442
<file src="test/classes/AbstractNetworkTestCase.php">
1444714443
<MixedAssignment occurrences="1">
1444814444
<code>$http_response_code_param</code>

setup/index.php

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,30 @@
33
declare(strict_types=1);
44

55
use PhpMyAdmin\Common;
6-
use PhpMyAdmin\Controllers\Setup\MainController;
7-
use PhpMyAdmin\Controllers\Setup\ShowConfigController;
8-
use PhpMyAdmin\Controllers\Setup\ValidateController;
9-
use PhpMyAdmin\Core;
106

7+
// phpcs:disable PSR1.Files.SideEffects
118
if (! defined('ROOT_PATH')) {
12-
// phpcs:disable PSR1.Files.SideEffects
139
define('ROOT_PATH', dirname(__DIR__) . DIRECTORY_SEPARATOR);
14-
// phpcs:enable
1510
}
1611

17-
// phpcs:disable PSR1.Files.SideEffects
1812
define('PHPMYADMIN', true);
1913
// phpcs:enable
2014

21-
require ROOT_PATH . 'setup/lib/common.inc.php';
22-
23-
$request = Common::getRequest();
24-
$route = $request->getRoute();
25-
if ($route === '/setup' || $route === '/') {
26-
(new MainController())($request);
27-
exit;
15+
if (PHP_VERSION_ID < 70205) {
16+
die('<p>PHP 7.2.5+ is required.</p><p>Currently installed version is: ' . PHP_VERSION . '</p>');
2817
}
2918

30-
if ($route === '/setup/show-config') {
31-
(new ShowConfigController())($request);
32-
exit;
33-
}
19+
require_once ROOT_PATH . 'libraries/constants.php';
3420

35-
if ($route === '/setup/validate') {
36-
(new ValidateController())($request);
37-
exit;
21+
if (! @is_readable(AUTOLOAD_FILE)) {
22+
die(
23+
'<p>File <samp>' . AUTOLOAD_FILE . '</samp> missing or not readable.</p>'
24+
. '<p>Most likely you did not run Composer to '
25+
. '<a href="https://docs.phpmyadmin.net/en/latest/setup.html#installing-from-git">'
26+
. 'install library files</a>.</p>'
27+
);
3828
}
3929

40-
Core::fatalError(sprintf(
41-
__('Error 404! The page %s was not found.'),
42-
'[code]' . htmlspecialchars($route) . '[/code]'
43-
));
30+
require AUTOLOAD_FILE;
31+
32+
Common::run(true);

setup/lib/common.inc.php

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)