Skip to content

Commit ad5e2ab

Browse files
committed
Throw ConfigException for some Config errors
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 8228122 commit ad5e2ab

3 files changed

Lines changed: 71 additions & 53 deletions

File tree

libraries/classes/Common.php

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpMyAdmin\ConfigStorage\Relation;
99
use PhpMyAdmin\Dbal\DatabaseName;
1010
use PhpMyAdmin\Dbal\TableName;
11+
use PhpMyAdmin\Exceptions\ConfigException;
1112
use PhpMyAdmin\Exceptions\MissingExtensionException;
1213
use PhpMyAdmin\Http\Factory\ServerRequestFactory;
1314
use PhpMyAdmin\Http\ServerRequest;
@@ -106,11 +107,7 @@ public static function run(bool $isSetupPage = false): void
106107
try {
107108
self::checkRequiredPhpExtensions();
108109
} catch (MissingExtensionException $exception) {
109-
echo (new Template())->render('error/generic', [
110-
'lang' => $GLOBALS['lang'] ?? 'en',
111-
'dir' => $GLOBALS['text_dir'] ?? 'ltr',
112-
'error_message' => $exception->getMessage(),
113-
]);
110+
echo self::getGenericError($exception->getMessage());
114111

115112
return;
116113
}
@@ -127,9 +124,16 @@ public static function run(bool $isSetupPage = false): void
127124

128125
/** @var Config $config */
129126
$config = $GLOBALS['containerBuilder']->get('config');
130-
$config->loadAndCheck(CONFIG_FILE);
131127
$GLOBALS['config'] = $config;
132128

129+
try {
130+
$config->loadAndCheck(CONFIG_FILE);
131+
} catch (ConfigException $exception) {
132+
echo self::getGenericError($exception->getMessage());
133+
134+
return;
135+
}
136+
133137
if ($route !== '/messages') {
134138
// Include session handling after the globals, to prevent overwriting.
135139
Session::setUp($config, $errorHandler);
@@ -166,22 +170,24 @@ public static function run(bool $isSetupPage = false): void
166170
$language = LanguageManager::getInstance()->selectLanguage();
167171
$language->activate();
168172

169-
/**
170-
* check for errors occurred while loading configuration
171-
* this check is done here after loading language files to present errors in locale
172-
*/
173-
$config->checkPermissions();
174-
$config->checkErrors();
173+
try {
174+
/**
175+
* check for errors occurred while loading configuration
176+
* this check is done here after loading language files to present errors in locale
177+
*/
178+
$config->checkPermissions();
179+
$config->checkErrors();
180+
} catch (ConfigException $exception) {
181+
echo self::getGenericError($exception->getMessage());
182+
183+
return;
184+
}
175185

176186
try {
177187
self::checkServerConfiguration();
178188
self::checkRequest();
179189
} catch (RuntimeException $exception) {
180-
echo (new Template())->render('error/generic', [
181-
'lang' => $GLOBALS['lang'] ?? 'en',
182-
'dir' => $GLOBALS['text_dir'] ?? 'ltr',
183-
'error_message' => $exception->getMessage(),
184-
]);
190+
echo self::getGenericError($exception->getMessage());
185191

186192
return;
187193
}
@@ -252,15 +258,11 @@ public static function run(bool $isSetupPage = false): void
252258
Logging::logUser($GLOBALS['cfg']['Server']['user']);
253259

254260
if ($GLOBALS['dbi']->getVersion() < $GLOBALS['cfg']['MysqlMinVersion']['internal']) {
255-
echo (new Template())->render('error/generic', [
256-
'lang' => $GLOBALS['lang'] ?? 'en',
257-
'dir' => $GLOBALS['text_dir'] ?? 'ltr',
258-
'error_message' => sprintf(
259-
__('You should upgrade to %s %s or later.'),
260-
'MySQL',
261-
(string) $GLOBALS['cfg']['MysqlMinVersion']['human']
262-
),
263-
]);
261+
echo self::getGenericError(sprintf(
262+
__('You should upgrade to %s %s or later.'),
263+
'MySQL',
264+
(string) $GLOBALS['cfg']['MysqlMinVersion']['human']
265+
));
264266

265267
return;
266268
}
@@ -689,4 +691,13 @@ private static function setCurrentServerGlobal(Config $config): void
689691
$GLOBALS['containerBuilder']->setParameter('server', $server);
690692
$GLOBALS['containerBuilder']->setParameter('url_params', $GLOBALS['urlParams']);
691693
}
694+
695+
private static function getGenericError(string $message): string
696+
{
697+
return (new Template())->render('error/generic', [
698+
'lang' => $GLOBALS['lang'] ?? 'en',
699+
'dir' => $GLOBALS['text_dir'] ?? 'ltr',
700+
'error_message' => $message,
701+
]);
702+
}
692703
}

libraries/classes/Config.php

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpMyAdmin;
66

77
use PhpMyAdmin\Config\Settings;
8+
use PhpMyAdmin\Exceptions\ConfigException;
89

910
use function __;
1011
use function array_filter;
@@ -105,13 +106,14 @@ class Config
105106

106107
/**
107108
* @param string|null $source source to read config from
109+
*
110+
* @throws ConfigException
108111
*/
109112
public function loadAndCheck(?string $source = null): void
110113
{
111114
$this->settings = ['is_setup' => false];
112115

113-
// functions need to refresh in case of config file changed goes in
114-
// PhpMyAdmin\Config::load()
116+
// functions need to refresh in case of config file changed goes in PhpMyAdmin\Config::load()
115117
$this->load($source);
116118

117119
// other settings, independent of config file, comes in
@@ -340,7 +342,9 @@ public function loadDefaults(): void
340342
* loads configuration from $source, usually the config file
341343
* should be called on object creation
342344
*
343-
* @param string $source config file
345+
* @param string|null $source config file
346+
*
347+
* @throws ConfigException
344348
*/
345349
public function load(?string $source = null): bool
346350
{
@@ -608,7 +612,7 @@ public function setSource(string $source): void
608612
}
609613

610614
/**
611-
* check config source
615+
* @throws ConfigException
612616
*/
613617
public function checkConfigSource(): bool
614618
{
@@ -636,20 +640,13 @@ public function checkConfigSource(): bool
636640

637641
if ($contents === false) {
638642
$this->sourceMtime = 0;
639-
echo (new Template())->render('error/generic', [
640-
'lang' => $GLOBALS['lang'] ?? 'en',
641-
'dir' => $GLOBALS['text_dir'] ?? 'ltr',
642-
'error_message' => sprintf(
643-
function_exists('__')
644-
? __('Existing configuration file (%s) is not readable.')
645-
: 'Existing configuration file (%s) is not readable.',
646-
$this->getSource()
647-
),
648-
]);
649-
650-
if (! defined('TESTSUITE')) {
651-
exit;
652-
}
643+
644+
throw new ConfigException(sprintf(
645+
function_exists('__')
646+
? __('Existing configuration file (%s) is not readable.')
647+
: 'Existing configuration file (%s) is not readable.',
648+
$this->getSource()
649+
));
653650
}
654651
}
655652

@@ -659,6 +656,8 @@ function_exists('__')
659656
/**
660657
* verifies the permissions on config file (if asked by configuration)
661658
* (must be called after config.inc.php has been merged)
659+
*
660+
* @throws ConfigException
662661
*/
663662
public function checkPermissions(): void
664663
{
@@ -679,18 +678,14 @@ public function checkPermissions(): void
679678
}
680679

681680
$this->sourceMtime = 0;
682-
echo (new Template())->render('error/generic', [
683-
'lang' => $GLOBALS['lang'] ?? 'en',
684-
'dir' => $GLOBALS['text_dir'] ?? 'ltr',
685-
'error_message' => __('Wrong permissions on configuration file, should not be world writable!'),
686-
]);
687681

688-
exit;
682+
throw new ConfigException(__('Wrong permissions on configuration file, should not be world writable!'));
689683
}
690684

691685
/**
692-
* Checks for errors
693-
* (must be called after config.inc.php has been merged)
686+
* Checks for errors (must be called after config.inc.php has been merged)
687+
*
688+
* @throws ConfigException
694689
*/
695690
public function checkErrors(): void
696691
{
@@ -703,7 +698,8 @@ public function checkErrors(): void
703698
. __('This usually means there is a syntax error in it, please check any errors shown below.')
704699
. '[br][br]'
705700
. '[conferr]';
706-
trigger_error($error, E_USER_ERROR);
701+
702+
throw new ConfigException(Sanitize::sanitizeMessage($error));
707703
}
708704

709705
/**
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\Exceptions;
6+
7+
use Exception;
8+
9+
class ConfigException extends Exception
10+
{
11+
}

0 commit comments

Comments
 (0)