From d253986eb554f76c6dcecb397f6a52f41528b85f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Tue, 7 Jan 2025 19:01:14 -0300 Subject: [PATCH] Remove hostname global variable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- phpstan-baseline.neon | 36 ------------------- psalm.xml | 1 - .../Server/PrivilegesController.php | 32 ++++++++--------- src/Controllers/UserPasswordController.php | 8 +---- src/Server/Privileges.php | 10 +++--- tests/unit/Server/PrivilegesTest.php | 6 ++-- 6 files changed, 24 insertions(+), 69 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 84b2f1d2fb..e06476e3c9 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -3510,24 +3510,12 @@ parameters: count: 1 path: src/Controllers/Server/PrivilegesController.php - - - message: '#^Parameter \#2 \$hostname of method PhpMyAdmin\\Server\\Privileges\:\:getHtmlForRoutineSpecificPrivileges\(\) expects string, mixed given\.$#' - identifier: argument.type - count: 1 - path: src/Controllers/Server/PrivilegesController.php - - message: '#^Parameter \#2 \$initial of method PhpMyAdmin\\Server\\Privileges\:\:getHtmlForUserOverview\(\) expects string\|null, mixed given\.$#' identifier: argument.type count: 1 path: src/Controllers/Server/PrivilegesController.php - - - message: '#^Parameter \#4 \$hostname of method PhpMyAdmin\\Server\\Privileges\:\:getHtmlForUserProperties\(\) expects string, mixed given\.$#' - identifier: argument.type - count: 1 - path: src/Controllers/Server/PrivilegesController.php - - message: '#^Only booleans are allowed in an if condition, mixed given\.$#' identifier: if.condNotBoolean @@ -5370,12 +5358,6 @@ parameters: count: 1 path: src/Controllers/UserPasswordController.php - - - message: '#^Parameter \#2 \$hostname of method PhpMyAdmin\\UserPassword\:\:getFormForChangePassword\(\) expects string\|null, mixed given\.$#' - identifier: argument.type - count: 1 - path: src/Controllers/UserPasswordController.php - - message: '#^Loose comparison via "\!\=" is not allowed\.$#' identifier: notEqual.notAllowed @@ -15006,12 +14988,6 @@ parameters: count: 2 path: src/Server/Privileges.php - - - message: '#^Parameter \#1 \$string of function mb_strtolower expects string, mixed given\.$#' - identifier: argument.type - count: 1 - path: src/Server/Privileges.php - - message: '#^Parameter \#1 \$string of function mb_substr expects string, string\|null given\.$#' identifier: argument.type @@ -15024,12 +15000,6 @@ parameters: count: 1 path: src/Server/Privileges.php - - - message: '#^Parameter \#2 \$hostname of method PhpMyAdmin\\Server\\Privileges\:\:getUserHostCondition\(\) expects string, mixed given\.$#' - identifier: argument.type - count: 1 - path: src/Server/Privileges.php - - message: '#^Parameter \#2 \$string of function explode expects string, mixed given\.$#' identifier: argument.type @@ -15060,12 +15030,6 @@ parameters: count: 2 path: src/Server/Privileges.php - - - message: '#^Parameter \#4 \$hostname of method PhpMyAdmin\\Server\\Privileges\:\:getSqlQueryForDisplayPrivTable\(\) expects string, mixed given\.$#' - identifier: argument.type - count: 1 - path: src/Server/Privileges.php - - message: '#^Parameter \#5 \$tablename of method PhpMyAdmin\\Server\\Privileges\:\:getUserLink\(\) expects string, mixed given\.$#' identifier: argument.type diff --git a/psalm.xml b/psalm.xml index b22c8423d6..3a1904736d 100644 --- a/psalm.xml +++ b/psalm.xml @@ -50,7 +50,6 @@ errno: int, file_handle: resource|null, from_cookie: bool, - hostname: string, import_text: string, knjenc: string, maxsize: string, diff --git a/src/Controllers/Server/PrivilegesController.php b/src/Controllers/Server/PrivilegesController.php index bf96845fd6..f67943d3d5 100644 --- a/src/Controllers/Server/PrivilegesController.php +++ b/src/Controllers/Server/PrivilegesController.php @@ -46,8 +46,6 @@ final class PrivilegesController implements InvocableController public function __invoke(ServerRequest $request): Response { - $GLOBALS['hostname'] ??= null; - $userPrivileges = $this->userPrivilegesFactory->getPrivileges(); $relationParameters = $this->relation->getRelationParameters(); @@ -80,7 +78,7 @@ final class PrivilegesController implements InvocableController } $serverPrivileges->username = $serverPrivileges->getUsernameParam($request); - $GLOBALS['hostname'] = $serverPrivileges->getHostnameParam($request); + $serverPrivileges->hostname = $serverPrivileges->getHostnameParam($request); $databaseName = $serverPrivileges->getDbname($request); $tablename = $serverPrivileges->getTablename($request); $routinename = $serverPrivileges->getRoutinename($request); @@ -115,7 +113,7 @@ final class PrivilegesController implements InvocableController if ( $request->hasBodyParam('change_copy') && $serverPrivileges->username === $request->getParsedBodyParam('old_username') - && $GLOBALS['hostname'] === $request->getParsedBodyParam('old_hostname') + && $serverPrivileges->hostname === $request->getParsedBodyParam('old_hostname') ) { $this->response->addHTML( Message::error( @@ -150,7 +148,7 @@ final class PrivilegesController implements InvocableController if ($request->hasBodyParam('adduser_submit') || $request->hasBodyParam('change_copy')) { $hostname = $serverPrivileges->getHostname( $request->getParsedBodyParamAsString('pred_hostname', ''), - $GLOBALS['hostname'] ?? '', + $serverPrivileges->hostname ?? '', ); [ $retMessage, @@ -178,12 +176,12 @@ final class PrivilegesController implements InvocableController if ( $request->hasBodyParam('change_copy') && $serverPrivileges->username !== null - && $GLOBALS['hostname'] !== null + && $serverPrivileges->hostname !== null ) { $queries = $serverPrivileges->getDbSpecificPrivsQueriesForChangeOrCopyUser( $queries, $serverPrivileges->username, - $GLOBALS['hostname'], + $serverPrivileges->hostname, $request->getParsedBodyParamAsString('old_username'), $request->getParsedBodyParamAsString('old_hostname'), ); @@ -203,7 +201,7 @@ final class PrivilegesController implements InvocableController foreach ($databaseName as $key => $dbName) { [$statements[$key], Current::$message] = $serverPrivileges->updatePrivileges( $serverPrivileges->username ?? '', - $GLOBALS['hostname'] ?? '', + $serverPrivileges->hostname ?? '', $tablename ?? $routinename ?? '', $dbName, $itemType, @@ -214,7 +212,7 @@ final class PrivilegesController implements InvocableController } else { [Current::$sqlQuery, Current::$message] = $serverPrivileges->updatePrivileges( $serverPrivileges->username ?? '', - $GLOBALS['hostname'] ?? '', + $serverPrivileges->hostname ?? '', $tablename ?? $routinename ?? '', $databaseName ?? '', $itemType, @@ -244,7 +242,7 @@ final class PrivilegesController implements InvocableController is_string($databaseName) ? $databaseName : '', $tablename ?? $routinename ?? '', $serverPrivileges->username ?? '', - $GLOBALS['hostname'] ?? '', + $serverPrivileges->hostname ?? '', $itemType, ); } @@ -256,7 +254,7 @@ final class PrivilegesController implements InvocableController Current::$message = $serverPrivileges->updatePassword( $errorUrl, $serverPrivileges->username ?? '', - $GLOBALS['hostname'] ?? '', + $serverPrivileges->hostname ?? '', ); } @@ -308,7 +306,7 @@ final class PrivilegesController implements InvocableController $extraData = $serverPrivileges->getExtraDataForAjaxBehavior( $password ?? '', Current::$sqlQuery, - $GLOBALS['hostname'] ?? '', + $serverPrivileges->hostname ?? '', $serverPrivileges->username ?? '', ! is_array($databaseName) ? $databaseName : null, ); @@ -337,18 +335,18 @@ final class PrivilegesController implements InvocableController $title = $this->getExportPageTitle( $serverPrivileges->username ?? '', - $GLOBALS['hostname'] ?? '', + $serverPrivileges->hostname ?? '', $selectedUsers, ); $export = $serverPrivileges->getExportUserDefinitionTextarea( $serverPrivileges->username ?? '', - $GLOBALS['hostname'] ?? '', + $serverPrivileges->hostname ?? '', $selectedUsers, ); $serverPrivileges->username = null; - unset($GLOBALS['hostname']); + $serverPrivileges->hostname = null; if ($request->isAjax()) { $this->response->addJSON('message', $export); @@ -387,7 +385,7 @@ final class PrivilegesController implements InvocableController $this->response->addHTML( $serverPrivileges->getHtmlForRoutineSpecificPrivileges( $serverPrivileges->username, - $GLOBALS['hostname'] ?? '', + $serverPrivileges->hostname ?? '', is_string($databaseName) ? $databaseName : '', $routinename, $serverPrivileges->escapeGrantWildcards($urlDbname ?? ''), @@ -405,7 +403,7 @@ final class PrivilegesController implements InvocableController $dbnameIsWildcard, $serverPrivileges->escapeGrantWildcards($urlDbname ?? ''), $serverPrivileges->username, - $GLOBALS['hostname'] ?? '', + $serverPrivileges->hostname ?? '', $databaseName ?? '', $tablename ?? '', $request->getRoute(), diff --git a/src/Controllers/UserPasswordController.php b/src/Controllers/UserPasswordController.php index 4dfb6ff9b0..cb483663de 100644 --- a/src/Controllers/UserPasswordController.php +++ b/src/Controllers/UserPasswordController.php @@ -30,8 +30,6 @@ final class UserPasswordController implements InvocableController public function __invoke(ServerRequest $request): Response { - $GLOBALS['hostname'] ??= null; - $this->response->addScriptFiles(['server/privileges.js', 'vendor/zxcvbn-ts.js']); $config = Config::getInstance(); @@ -103,11 +101,7 @@ final class UserPasswordController implements InvocableController $this->response->addHTML($message->getDisplay()); } - $this->response->addHTML($this->userPassword->getFormForChangePassword( - '', - $GLOBALS['hostname'], - $request->getRoute(), - )); + $this->response->addHTML($this->userPassword->getFormForChangePassword('', '', $request->getRoute())); return $this->response->response(); } diff --git a/src/Server/Privileges.php b/src/Server/Privileges.php index 530114fd16..de7e2a565e 100644 --- a/src/Server/Privileges.php +++ b/src/Server/Privileges.php @@ -71,6 +71,7 @@ use function uksort; class Privileges { public string|null $username = null; + public string|null $hostname = null; private string|null $sslType = null; private string|null $sslCipher = null; private string|null $x509Issuer = null; @@ -473,7 +474,7 @@ class Privileges $row = []; if ($this->username !== null) { $username = $this->username; - $hostname = $GLOBALS['hostname']; + $hostname = $this->hostname ?? ''; $sqlQuery = $this->getSqlQueryForDisplayPrivTable($db, $table, $username, $hostname); $row = $this->dbi->fetchSingleRow($sqlQuery); } @@ -596,7 +597,6 @@ class Privileges ): string { $GLOBALS['pred_username'] ??= null; $GLOBALS['pred_hostname'] ??= null; - $GLOBALS['hostname'] ??= null; $GLOBALS['new_username'] ??= null; [$usernameLength, $hostnameLength] = $this->getUsernameAndHostnameLength(); @@ -618,8 +618,8 @@ class Privileges ); } - if (! isset($GLOBALS['pred_hostname']) && isset($GLOBALS['hostname'])) { - $GLOBALS['pred_hostname'] = match (mb_strtolower($GLOBALS['hostname'])) { + if (! isset($GLOBALS['pred_hostname']) && $this->hostname !== null) { + $GLOBALS['pred_hostname'] = match (mb_strtolower($this->hostname)) { 'localhost', '127.0.0.1' => 'localhost', '%' => 'any', default => 'userdefined', @@ -651,7 +651,7 @@ class Privileges 'hostname_length' => $hostnameLength, 'username' => $this->username, 'new_username' => $GLOBALS['new_username'] ?? null, - 'hostname' => $GLOBALS['hostname'] ?? null, + 'hostname' => $this->hostname, 'this_host' => $thisHost, 'is_change' => $user !== null && $host !== null, 'auth_plugin' => $authPlugin, diff --git a/tests/unit/Server/PrivilegesTest.php b/tests/unit/Server/PrivilegesTest.php index 7b59b5bbeb..78d604e56d 100644 --- a/tests/unit/Server/PrivilegesTest.php +++ b/tests/unit/Server/PrivilegesTest.php @@ -599,11 +599,11 @@ class PrivilegesTest extends AbstractTestCase public function testGetHtmlToDisplayPrivilegesTable(): void { - $GLOBALS['hostname'] = 'hostname'; $dbi = $this->createDatabaseInterface(); $serverPrivileges = $this->getPrivileges($dbi); $serverPrivileges->username = 'username'; + $serverPrivileges->hostname = 'hostname'; $html = $serverPrivileges->getHtmlToDisplayPrivilegesTable(); //validate 2: button @@ -877,7 +877,7 @@ class PrivilegesTest extends AbstractTestCase $serverPrivileges->dbi = $dbi; $serverPrivileges->username = 'PMA_username'; - $GLOBALS['hostname'] = 'PMA_hostname'; + $serverPrivileges->hostname = 'PMA_hostname'; $html = $serverPrivileges->getHtmlToDisplayPrivilegesTable('PMA_db', 'PMA_table'); self::assertStringContainsString('checkbox_Update_priv_none', $html); @@ -1473,7 +1473,7 @@ class PrivilegesTest extends AbstractTestCase $serverPrivileges = $this->getPrivileges($this->dbi); $serverPrivileges->username = 'user'; - $GLOBALS['hostname'] = 'host'; + $serverPrivileges->hostname = 'host'; $actual = $serverPrivileges->getHtmlForUserProperties( false,