diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 4a29209940..24e201be88 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -185,11 +185,6 @@ parameters: count: 1 path: src/Charsets.php - - - message: "#^Cannot access an offset on mixed\\.$#" - count: 1 - path: src/CheckUserPrivileges.php - - message: "#^Only booleans are allowed in a negated boolean, PhpMyAdmin\\\\Dbal\\\\ResultInterface\\|false given\\.$#" count: 1 @@ -215,6 +210,51 @@ parameters: count: 1 path: src/CheckUserPrivileges.php + - + message: "#^Static property PhpMyAdmin\\\\UserPrivileges\\:\\:\\$column \\(bool\\) does not accept mixed\\.$#" + count: 1 + path: src/CheckUserPrivileges.php + + - + message: "#^Static property PhpMyAdmin\\\\UserPrivileges\\:\\:\\$database \\(bool\\) does not accept mixed\\.$#" + count: 1 + path: src/CheckUserPrivileges.php + + - + message: "#^Static property PhpMyAdmin\\\\UserPrivileges\\:\\:\\$databaseToCreate \\(string\\) does not accept mixed\\.$#" + count: 1 + path: src/CheckUserPrivileges.php + + - + message: "#^Static property PhpMyAdmin\\\\UserPrivileges\\:\\:\\$databaseToCreate \\(string\\) does not accept string\\|null\\.$#" + count: 2 + path: src/CheckUserPrivileges.php + + - + message: "#^Static property PhpMyAdmin\\\\UserPrivileges\\:\\:\\$databasesToTest \\(array\\\\|false\\) does not accept mixed\\.$#" + count: 2 + path: src/CheckUserPrivileges.php + + - + message: "#^Static property PhpMyAdmin\\\\UserPrivileges\\:\\:\\$isCreateDatabase \\(bool\\) does not accept mixed\\.$#" + count: 1 + path: src/CheckUserPrivileges.php + + - + message: "#^Static property PhpMyAdmin\\\\UserPrivileges\\:\\:\\$isReload \\(bool\\) does not accept mixed\\.$#" + count: 1 + path: src/CheckUserPrivileges.php + + - + message: "#^Static property PhpMyAdmin\\\\UserPrivileges\\:\\:\\$routines \\(bool\\) does not accept mixed\\.$#" + count: 1 + path: src/CheckUserPrivileges.php + + - + message: "#^Static property PhpMyAdmin\\\\UserPrivileges\\:\\:\\$table \\(bool\\) does not accept mixed\\.$#" + count: 1 + path: src/CheckUserPrivileges.php + - message: "#^Parameter \\#1 \\$source of method PhpMyAdmin\\\\Config\\:\\:loadAndCheck\\(\\) expects string\\|null, mixed given\\.$#" count: 1 @@ -9845,11 +9885,6 @@ parameters: count: 1 path: src/Linter.php - - - message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#" - count: 1 - path: src/ListDatabase.php - - message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" count: 1 @@ -9865,11 +9900,6 @@ parameters: count: 1 path: src/ListDatabase.php - - - message: "#^Parameter \\#1 \\$likeDbName of method PhpMyAdmin\\\\ListDatabase\\:\\:retrieve\\(\\) expects string\\|null, mixed given\\.$#" - count: 1 - path: src/ListDatabase.php - - message: "#^Parameter \\#1 \\$str of function strtr expects string, array\\\\|bool\\|int\\<0, max\\>\\|string given\\.$#" count: 1 @@ -9982,7 +10012,7 @@ parameters: - message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#" - count: 2 + count: 1 path: src/Navigation/NavigationTree.php - @@ -10157,11 +10187,11 @@ parameters: - message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" - count: 5 + count: 4 path: src/Navigation/Nodes/Node.php - - message: "#^Method PhpMyAdmin\\\\Navigation\\\\Nodes\\\\Node\\:\\:getDatabasesToSearch\\(\\) should return array but returns mixed\\.$#" + message: "#^Method PhpMyAdmin\\\\Navigation\\\\Nodes\\\\Node\\:\\:getDatabasesToSearch\\(\\) should return array but returns array\\\\|string\\.$#" count: 1 path: src/Navigation/Nodes/Node.php @@ -10171,7 +10201,7 @@ parameters: path: src/Navigation/Nodes/Node.php - - message: "#^Parameter \\#1 \\$array of function sort expects TArray of array\\, mixed given\\.$#" + message: "#^Parameter \\#1 \\$array of function sort expects TArray of array\\, array\\\\|string given\\.$#" count: 1 path: src/Navigation/Nodes/Node.php diff --git a/psalm-baseline.xml b/psalm-baseline.xml index cc77f41958..a4e9649ff3 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -132,14 +132,14 @@ - - - - - - - - + UserPrivileges::$column + UserPrivileges::$database + UserPrivileges::$databaseToCreate + UserPrivileges::$databasesToTest + UserPrivileges::$isCreateDatabase + UserPrivileges::$isReload + UserPrivileges::$routines + UserPrivileges::$table @@ -2674,7 +2674,6 @@ Config::getInstance() - @@ -2716,7 +2715,6 @@ - $database $key @@ -7246,6 +7244,7 @@ DatabaseInterface::getInstance() + $databases $db $db $db @@ -7253,15 +7252,13 @@ $db + $databases $db $db $db $db $db - - $databases - $arr[0] $arr[0] diff --git a/psalm.xml b/psalm.xml index 85b905fe1a..805a24eeb0 100644 --- a/psalm.xml +++ b/psalm.xml @@ -45,7 +45,6 @@ charset: string, charset_connection: string, codegen_format: 0|1, - col_priv: bool, collation_connection: string, complete_query: string, compression: 'none'|'zip'|'gzip', @@ -56,8 +55,6 @@ csv_escaped: string, csv_separator: string, csv_terminated: string, - db_priv: bool, - dbs_to_test: string[]|false, display_query: string, errno: int, error: bool, @@ -71,8 +68,6 @@ import_file: string, import_file_name: string, import_text: string, - is_create_db_priv: bool, - is_reload_priv: bool, knjenc: string, lang: string, latex_data_caption: string, @@ -97,7 +92,6 @@ plugin_scripts: string[], pma_auth_server: string, PMA_PHP_SELF: string, - proc_priv: bool, querytime: float|int, read_limit: int, save_on_server: bool, @@ -114,7 +108,6 @@ sql_query: string, sql_structure_or_data: 'structure'|'data'|'structure_and_data', sql_type: 'INSERT'|'UPDATE'|'REPLACE', - table_priv: bool, tbl_storage_engine: string, token_mismatch: bool, token_provided: bool, diff --git a/src/CheckUserPrivileges.php b/src/CheckUserPrivileges.php index 61a093abfa..10693ade9a 100644 --- a/src/CheckUserPrivileges.php +++ b/src/CheckUserPrivileges.php @@ -44,13 +44,13 @@ class CheckUserPrivileges } if ($showGrants->dbName === '*' && $showGrants->tableName === '*') { - $GLOBALS['col_priv'] = true; - $GLOBALS['db_priv'] = true; - $GLOBALS['proc_priv'] = true; - $GLOBALS['table_priv'] = true; + UserPrivileges::$column = true; + UserPrivileges::$database = true; + UserPrivileges::$routines = true; + UserPrivileges::$table = true; if ($showGrants->grants === 'ALL PRIVILEGES' || $showGrants->grants === 'ALL') { - $GLOBALS['is_reload_priv'] = true; + UserPrivileges::$isReload = true; } } @@ -62,22 +62,22 @@ class CheckUserPrivileges switch ($showGrants->tableName) { case 'columns_priv': - $GLOBALS['col_priv'] = true; + UserPrivileges::$column = true; break; case 'db': - $GLOBALS['db_priv'] = true; + UserPrivileges::$database = true; break; case 'procs_priv': - $GLOBALS['proc_priv'] = true; + UserPrivileges::$routines = true; break; case 'tables_priv': - $GLOBALS['table_priv'] = true; + UserPrivileges::$table = true; break; case '*': - $GLOBALS['col_priv'] = true; - $GLOBALS['db_priv'] = true; - $GLOBALS['proc_priv'] = true; - $GLOBALS['table_priv'] = true; + UserPrivileges::$column = true; + UserPrivileges::$database = true; + UserPrivileges::$routines = true; + UserPrivileges::$table = true; break; default: } @@ -102,28 +102,28 @@ class CheckUserPrivileges private function analyseShowGrant(): void { if (SessionCache::has('is_create_db_priv')) { - $GLOBALS['is_create_db_priv'] = SessionCache::get('is_create_db_priv'); - $GLOBALS['is_reload_priv'] = SessionCache::get('is_reload_priv'); - $GLOBALS['db_to_create'] = SessionCache::get('db_to_create'); - $GLOBALS['dbs_to_test'] = SessionCache::get('dbs_to_test'); + UserPrivileges::$isCreateDatabase = SessionCache::get('is_create_db_priv'); + UserPrivileges::$isReload = SessionCache::get('is_reload_priv'); + UserPrivileges::$databaseToCreate = SessionCache::get('db_to_create'); + UserPrivileges::$databasesToTest = SessionCache::get('dbs_to_test'); - $GLOBALS['db_priv'] = SessionCache::get('db_priv'); - $GLOBALS['col_priv'] = SessionCache::get('col_priv'); - $GLOBALS['table_priv'] = SessionCache::get('table_priv'); - $GLOBALS['proc_priv'] = SessionCache::get('proc_priv'); + UserPrivileges::$database = SessionCache::get('db_priv'); + UserPrivileges::$column = SessionCache::get('col_priv'); + UserPrivileges::$table = SessionCache::get('table_priv'); + UserPrivileges::$routines = SessionCache::get('proc_priv'); return; } // defaults - $GLOBALS['is_create_db_priv'] = false; - $GLOBALS['is_reload_priv'] = false; - $GLOBALS['db_to_create'] = ''; - $GLOBALS['dbs_to_test'] = Utilities::getSystemSchemas(); - $GLOBALS['proc_priv'] = false; - $GLOBALS['db_priv'] = false; - $GLOBALS['col_priv'] = false; - $GLOBALS['table_priv'] = false; + UserPrivileges::$isCreateDatabase = false; + UserPrivileges::$isReload = false; + UserPrivileges::$databaseToCreate = ''; + UserPrivileges::$databasesToTest = Utilities::getSystemSchemas(); + UserPrivileges::$routines = false; + UserPrivileges::$database = false; + UserPrivileges::$column = false; + UserPrivileges::$table = false; $showGrantsResult = $this->dbi->tryQuery('SHOW GRANTS'); @@ -139,14 +139,14 @@ class CheckUserPrivileges if ($showGrants->dbName === '*') { if ($showGrants->grants !== 'USAGE') { - $GLOBALS['dbs_to_test'] = false; + UserPrivileges::$databasesToTest = false; } - } elseif ($GLOBALS['dbs_to_test'] !== false) { - $GLOBALS['dbs_to_test'][] = $showGrants->dbName; + } elseif (UserPrivileges::$databasesToTest !== false) { + UserPrivileges::$databasesToTest[] = $showGrants->dbName; } if (str_contains($showGrants->grants, 'RELOAD')) { - $GLOBALS['is_reload_priv'] = true; + UserPrivileges::$isReload = true; } // check for the required privileges for adjust @@ -167,9 +167,9 @@ class CheckUserPrivileges if ($showGrants->dbName === '*') { // a global CREATE privilege - $GLOBALS['is_create_db_priv'] = true; - $GLOBALS['is_reload_priv'] = true; - $GLOBALS['db_to_create'] = ''; + UserPrivileges::$isCreateDatabase = true; + UserPrivileges::$isReload = true; + UserPrivileges::$databaseToCreate = ''; // @todo we should not break here, cause GRANT ALL *.* // could be revoked by a later rule like GRANT SELECT ON db.* break; @@ -177,7 +177,7 @@ class CheckUserPrivileges $dbNameToTest = Util::backquote($showGrants->dbName); - if ($GLOBALS['is_create_db_priv']) { + if (UserPrivileges::$isCreateDatabase) { // no need for any more tests if we already know this continue; } @@ -202,12 +202,16 @@ class CheckUserPrivileges * Do not handle the underscore wildcard * (this case must be rare anyway) */ - $GLOBALS['db_to_create'] = preg_replace('/' . $re0 . '%/', '\\1', $showGrants->dbName); - $GLOBALS['db_to_create'] = preg_replace('/' . $re1 . '(%|_)/', '\\1\\3', $GLOBALS['db_to_create']); - $GLOBALS['is_create_db_priv'] = true; + UserPrivileges::$databaseToCreate = preg_replace('/' . $re0 . '%/', '\\1', $showGrants->dbName); + UserPrivileges::$databaseToCreate = preg_replace( + '/' . $re1 . '(%|_)/', + '\\1\\3', + UserPrivileges::$databaseToCreate, + ); + UserPrivileges::$isCreateDatabase = true; /** - * @todo collect $GLOBALS['db_to_create'] into an array, + * @todo collect \PhpMyAdmin\UserPrivileges::$dbToCreate into an array, * to display a drop-down in the "Create database" dialog */ // we don't break, we want all possible databases @@ -216,15 +220,15 @@ class CheckUserPrivileges // must also cacheUnset() them in // PhpMyAdmin\Plugins\Auth\AuthenticationCookie - SessionCache::set('is_create_db_priv', $GLOBALS['is_create_db_priv']); - SessionCache::set('is_reload_priv', $GLOBALS['is_reload_priv']); - SessionCache::set('db_to_create', $GLOBALS['db_to_create']); - SessionCache::set('dbs_to_test', $GLOBALS['dbs_to_test']); + SessionCache::set('is_create_db_priv', UserPrivileges::$isCreateDatabase); + SessionCache::set('is_reload_priv', UserPrivileges::$isReload); + SessionCache::set('db_to_create', UserPrivileges::$databaseToCreate); + SessionCache::set('dbs_to_test', UserPrivileges::$databasesToTest); - SessionCache::set('proc_priv', $GLOBALS['proc_priv']); - SessionCache::set('table_priv', $GLOBALS['table_priv']); - SessionCache::set('col_priv', $GLOBALS['col_priv']); - SessionCache::set('db_priv', $GLOBALS['db_priv']); + SessionCache::set('proc_priv', UserPrivileges::$routines); + SessionCache::set('table_priv', UserPrivileges::$table); + SessionCache::set('col_priv', UserPrivileges::$column); + SessionCache::set('db_priv', UserPrivileges::$database); } /** @@ -241,14 +245,14 @@ class CheckUserPrivileges // If MySQL is started with --skip-grant-tables if ($username === '') { - $GLOBALS['is_create_db_priv'] = true; - $GLOBALS['is_reload_priv'] = true; - $GLOBALS['db_to_create'] = ''; - $GLOBALS['dbs_to_test'] = false; - $GLOBALS['db_priv'] = true; - $GLOBALS['col_priv'] = true; - $GLOBALS['table_priv'] = true; - $GLOBALS['proc_priv'] = true; + UserPrivileges::$isCreateDatabase = true; + UserPrivileges::$isReload = true; + UserPrivileges::$databaseToCreate = ''; + UserPrivileges::$databasesToTest = false; + UserPrivileges::$database = true; + UserPrivileges::$column = true; + UserPrivileges::$table = true; + UserPrivileges::$routines = true; return; } diff --git a/src/Controllers/Database/RoutinesController.php b/src/Controllers/Database/RoutinesController.php index 19ac2f0e04..f67927ec16 100644 --- a/src/Controllers/Database/RoutinesController.php +++ b/src/Controllers/Database/RoutinesController.php @@ -19,6 +19,7 @@ use PhpMyAdmin\Message; use PhpMyAdmin\ResponseRenderer; use PhpMyAdmin\Template; use PhpMyAdmin\Url; +use PhpMyAdmin\UserPrivileges; use PhpMyAdmin\Util; use function __; @@ -263,7 +264,7 @@ class RoutinesController extends AbstractController 'parameter_rows' => $parameterRows, 'charsets' => $charsets, 'numeric_options' => $this->routines->numericOptions, - 'has_privileges' => $GLOBALS['proc_priv'] && $GLOBALS['is_reload_priv'], + 'has_privileges' => UserPrivileges::$routines && UserPrivileges::$isReload, 'sql_data_access' => $this->routines->sqlDataAccess, ]); diff --git a/src/Controllers/Operations/DatabaseController.php b/src/Controllers/Operations/DatabaseController.php index c8c3283707..09666573ce 100644 --- a/src/Controllers/Operations/DatabaseController.php +++ b/src/Controllers/Operations/DatabaseController.php @@ -24,6 +24,7 @@ use PhpMyAdmin\Query\Utilities; use PhpMyAdmin\ResponseRenderer; use PhpMyAdmin\Template; use PhpMyAdmin\Url; +use PhpMyAdmin\UserPrivileges; use PhpMyAdmin\Util; use function __; @@ -259,8 +260,8 @@ class DatabaseController extends AbstractController $databaseComment = $this->relation->getDbComment(Current::$database); } - $hasAdjustPrivileges = $GLOBALS['db_priv'] && $GLOBALS['table_priv'] - && $GLOBALS['col_priv'] && $GLOBALS['proc_priv'] && $GLOBALS['is_reload_priv']; + $hasAdjustPrivileges = UserPrivileges::$database && UserPrivileges::$table + && UserPrivileges::$column && UserPrivileges::$routines && UserPrivileges::$isReload; $isDropDatabaseAllowed = ($this->dbi->isSuperUser() || $config->settings['AllowUserDropDatabase']) && Current::$database !== 'mysql'; diff --git a/src/Controllers/Operations/TableController.php b/src/Controllers/Operations/TableController.php index 7ae9aef9dc..0b14e0187e 100644 --- a/src/Controllers/Operations/TableController.php +++ b/src/Controllers/Operations/TableController.php @@ -26,6 +26,7 @@ use PhpMyAdmin\ResponseRenderer; use PhpMyAdmin\StorageEngine; use PhpMyAdmin\Template; use PhpMyAdmin\Url; +use PhpMyAdmin\UserPrivileges; use PhpMyAdmin\Util; use function __; @@ -469,7 +470,7 @@ class TableController extends AbstractController } $hasForeignKeys = $this->relation->getForeigners(Current::$database, Current::$table, '', 'foreign') !== []; - $hasPrivileges = $GLOBALS['table_priv'] && $GLOBALS['col_priv'] && $GLOBALS['is_reload_priv']; + $hasPrivileges = UserPrivileges::$table && UserPrivileges::$column && UserPrivileges::$isReload; $switchToNew = isset($_SESSION['pma_switch_to_new']) && $_SESSION['pma_switch_to_new']; $partitions = []; diff --git a/src/Controllers/Server/DatabasesController.php b/src/Controllers/Server/DatabasesController.php index 676a0ad978..fdaf623eaf 100644 --- a/src/Controllers/Server/DatabasesController.php +++ b/src/Controllers/Server/DatabasesController.php @@ -18,6 +18,7 @@ use PhpMyAdmin\Replication\ReplicationInfo; use PhpMyAdmin\ResponseRenderer; use PhpMyAdmin\Template; use PhpMyAdmin\Url; +use PhpMyAdmin\UserPrivileges; use PhpMyAdmin\Util; use function __; @@ -61,8 +62,6 @@ class DatabasesController extends AbstractController public function __invoke(ServerRequest $request): void { - $GLOBALS['is_create_db_priv'] ??= null; - $GLOBALS['db_to_create'] ??= null; $GLOBALS['errorUrl'] ??= null; $params = [ @@ -116,7 +115,7 @@ class DatabasesController extends AbstractController $charsetsList = []; $config = Config::getInstance(); - if ($config->settings['ShowCreateDb'] && $GLOBALS['is_create_db_priv']) { + if ($config->settings['ShowCreateDb'] && UserPrivileges::$isCreateDatabase) { $charsets = Charsets::getCharsets($this->dbi, $config->selectedServer['DisableIS']); $collations = Charsets::getCollations($this->dbi, $config->selectedServer['DisableIS']); $serverCollation = $this->dbi->getServerCollation(); @@ -142,9 +141,9 @@ class DatabasesController extends AbstractController $this->render('server/databases/index', [ 'is_create_database_shown' => $config->settings['ShowCreateDb'], - 'has_create_database_privileges' => $GLOBALS['is_create_db_priv'], + 'has_create_database_privileges' => UserPrivileges::$isCreateDatabase, 'has_statistics' => $this->hasStatistics, - 'database_to_create' => $GLOBALS['db_to_create'], + 'database_to_create' => UserPrivileges::$databaseToCreate, 'databases' => $databases['databases'], 'total_statistics' => $databases['total_statistics'], 'header_statistics' => $headerStatistics, diff --git a/src/Controllers/Table/Structure/SaveController.php b/src/Controllers/Table/Structure/SaveController.php index c3ef125c39..ec1d27d81f 100644 --- a/src/Controllers/Table/Structure/SaveController.php +++ b/src/Controllers/Table/Structure/SaveController.php @@ -20,6 +20,7 @@ use PhpMyAdmin\Table\Table; use PhpMyAdmin\Template; use PhpMyAdmin\Transformations; use PhpMyAdmin\Url; +use PhpMyAdmin\UserPrivileges; use PhpMyAdmin\Util; use function __; @@ -363,7 +364,7 @@ final class SaveController extends AbstractController { $changed = false; - if (($GLOBALS['col_priv'] ?? false) && ($GLOBALS['is_reload_priv'] ?? false)) { + if (UserPrivileges::$column && UserPrivileges::$isReload) { $this->dbi->selectDb('mysql'); // For Column specific privileges diff --git a/src/Database/Routines.php b/src/Database/Routines.php index 353ac47843..9fa7fd64f3 100644 --- a/src/Database/Routines.php +++ b/src/Database/Routines.php @@ -15,6 +15,7 @@ use PhpMyAdmin\SqlParser\Parser; use PhpMyAdmin\SqlParser\Statements\CreateStatement; use PhpMyAdmin\SqlParser\TokensList; use PhpMyAdmin\SqlParser\Utils\Routine; +use PhpMyAdmin\UserPrivileges; use PhpMyAdmin\Util; use function __; @@ -163,7 +164,7 @@ class Routines */ public function backupPrivileges(): array { - if (! $GLOBALS['proc_priv'] || ! $GLOBALS['is_reload_priv']) { + if (! UserPrivileges::$routines || ! UserPrivileges::$isReload) { return []; } @@ -219,7 +220,7 @@ class Routines // Default value $resultAdjust = false; - if ($GLOBALS['proc_priv'] && $GLOBALS['is_reload_priv']) { + if (UserPrivileges::$routines && UserPrivileges::$isReload) { // Insert all the previous privileges // but with the new name and the new type foreach ($privilegesBackup as $priv) { diff --git a/src/ListDatabase.php b/src/ListDatabase.php index 6c5eed8750..f8f58cfa64 100644 --- a/src/ListDatabase.php +++ b/src/ListDatabase.php @@ -90,13 +90,13 @@ class ListDatabase extends ListAbstract if ($likeDbName !== null) { $command .= " WHERE `SCHEMA_NAME` LIKE '" . $likeDbName . "'"; } - } elseif ($GLOBALS['dbs_to_test'] === false || $likeDbName !== null) { + } elseif (UserPrivileges::$databasesToTest === false || $likeDbName !== null) { $command .= 'SHOW DATABASES'; if ($likeDbName !== null) { $command .= " LIKE '" . $likeDbName . "'"; } } else { - foreach ($GLOBALS['dbs_to_test'] as $db) { + foreach (UserPrivileges::$databasesToTest as $db) { $databaseList = array_merge( $databaseList, $this->retrieve($db), diff --git a/src/Navigation/NavigationTree.php b/src/Navigation/NavigationTree.php index 6403d6c152..f7693fb5c6 100644 --- a/src/Navigation/NavigationTree.php +++ b/src/Navigation/NavigationTree.php @@ -38,6 +38,7 @@ use PhpMyAdmin\Navigation\Nodes\NodeViewContainer; use PhpMyAdmin\ResponseRenderer; use PhpMyAdmin\Template; use PhpMyAdmin\Url; +use PhpMyAdmin\UserPrivileges; use PhpMyAdmin\Util; use function __; @@ -249,7 +250,7 @@ class NavigationTree } $prefixMap = []; - if ($GLOBALS['dbs_to_test'] === false) { + if (UserPrivileges::$databasesToTest === false) { $handle = $this->dbi->tryQuery('SHOW DATABASES'); if ($handle !== false) { while ($database = $handle->fetchValue()) { @@ -267,7 +268,7 @@ class NavigationTree } } else { $databases = []; - foreach ($GLOBALS['dbs_to_test'] as $db) { + foreach (UserPrivileges::$databasesToTest as $db) { $query = "SHOW DATABASES LIKE '" . $db . "'"; $handle = $this->dbi->tryQuery($query); if ($handle === false) { diff --git a/src/Navigation/Nodes/Node.php b/src/Navigation/Nodes/Node.php index b42bed8760..986926e293 100644 --- a/src/Navigation/Nodes/Node.php +++ b/src/Navigation/Nodes/Node.php @@ -14,6 +14,7 @@ use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\Dbal\Connection; use PhpMyAdmin\Html\Generator; use PhpMyAdmin\Navigation\NodeType; +use PhpMyAdmin\UserPrivileges; use PhpMyAdmin\Util; use function __; @@ -371,7 +372,7 @@ class Node return $this->getDataFromInfoSchema($pos, $searchClause); } - if ($GLOBALS['dbs_to_test'] === false) { + if (UserPrivileges::$databasesToTest === false) { return $this->getDataFromShowDatabases($pos, $searchClause); } @@ -402,7 +403,7 @@ class Node return (int) $dbi->fetchValue($query); } - if ($GLOBALS['dbs_to_test'] === false) { + if (UserPrivileges::$databasesToTest === false) { $query = 'SHOW DATABASES '; $query .= $this->getWhereClause('Database', $searchClause); @@ -432,7 +433,7 @@ class Node return (int) $dbi->fetchValue($query); } - if ($GLOBALS['dbs_to_test'] !== false) { + if (UserPrivileges::$databasesToTest !== false) { $prefixMap = []; foreach ($this->getDatabasesToSearch($searchClause) as $db) { $query = 'SHOW DATABASES LIKE ' . $dbi->quoteString($db); @@ -507,8 +508,8 @@ class Node $databases = ['%' . DatabaseInterface::getInstance()->escapeMysqlWildcards($searchClause) . '%']; } elseif (! empty($config->selectedServer['only_db'])) { $databases = $config->selectedServer['only_db']; - } elseif (! empty($GLOBALS['dbs_to_test'])) { - $databases = $GLOBALS['dbs_to_test']; + } elseif (UserPrivileges::$databasesToTest !== false && UserPrivileges::$databasesToTest !== []) { + $databases = UserPrivileges::$databasesToTest; } sort($databases); diff --git a/src/Navigation/Nodes/NodeDatabaseContainer.php b/src/Navigation/Nodes/NodeDatabaseContainer.php index c2b60f267d..a6ede1b484 100644 --- a/src/Navigation/Nodes/NodeDatabaseContainer.php +++ b/src/Navigation/Nodes/NodeDatabaseContainer.php @@ -11,6 +11,7 @@ use PhpMyAdmin\CheckUserPrivileges; use PhpMyAdmin\Config; use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\Navigation\NodeType; +use PhpMyAdmin\UserPrivileges; use function _pgettext; @@ -31,7 +32,7 @@ class NodeDatabaseContainer extends Node parent::__construct($name, NodeType::Container); - if (! $GLOBALS['is_create_db_priv'] || Config::getInstance()->settings['ShowCreateDb'] === false) { + if (! UserPrivileges::$isCreateDatabase || Config::getInstance()->settings['ShowCreateDb'] === false) { return; } diff --git a/src/Normalization.php b/src/Normalization.php index 80e62494ef..8f70083092 100644 --- a/src/Normalization.php +++ b/src/Normalization.php @@ -175,7 +175,7 @@ class Normalization 'max_rows' => intval($config->settings['MaxRows']), 'char_editing' => $config->settings['CharEditing'], 'attribute_types' => $this->dbi->types->getAttributes(), - 'privs_available' => $GLOBALS['col_priv'] && $GLOBALS['is_reload_priv'], + 'privs_available' => UserPrivileges::$column && UserPrivileges::$isReload, 'max_length' => $this->dbi->getVersion() >= 50503 ? 1024 : 255, 'charsets' => $charsetsList, ]); diff --git a/src/Operations.php b/src/Operations.php index 4d62100c4c..1d641651a0 100644 --- a/src/Operations.php +++ b/src/Operations.php @@ -292,9 +292,9 @@ class Operations public function adjustPrivilegesMoveDb(string $oldDb, DatabaseName $newDatabaseName): void { if ( - ! $GLOBALS['db_priv'] || ! $GLOBALS['table_priv'] - || ! $GLOBALS['col_priv'] || ! $GLOBALS['proc_priv'] - || ! $GLOBALS['is_reload_priv'] + ! UserPrivileges::$database || ! UserPrivileges::$table + || ! UserPrivileges::$column || ! UserPrivileges::$routines + || ! UserPrivileges::$isReload ) { return; } @@ -335,9 +335,9 @@ class Operations public function adjustPrivilegesCopyDb(string $oldDb, DatabaseName $newDatabaseName): void { if ( - ! $GLOBALS['db_priv'] || ! $GLOBALS['table_priv'] - || ! $GLOBALS['col_priv'] || ! $GLOBALS['proc_priv'] - || ! $GLOBALS['is_reload_priv'] + ! UserPrivileges::$database || ! UserPrivileges::$table + || ! UserPrivileges::$column || ! UserPrivileges::$routines + || ! UserPrivileges::$isReload ) { return; } @@ -741,7 +741,7 @@ class Operations string $newDb, string $newTable, ): void { - if (! $GLOBALS['table_priv'] || ! $GLOBALS['col_priv'] || ! $GLOBALS['is_reload_priv']) { + if (! UserPrivileges::$table || ! UserPrivileges::$column || ! UserPrivileges::$isReload) { return; } @@ -777,7 +777,7 @@ class Operations */ public function adjustPrivilegesCopyTable(string $oldDb, string $oldTable, string $newDb, string $newTable): void { - if (! $GLOBALS['table_priv'] || ! $GLOBALS['col_priv'] || ! $GLOBALS['is_reload_priv']) { + if (! UserPrivileges::$table || ! UserPrivileges::$column || ! UserPrivileges::$isReload) { return; } diff --git a/src/Server/Privileges.php b/src/Server/Privileges.php index 05bfc17d17..293397d2d8 100644 --- a/src/Server/Privileges.php +++ b/src/Server/Privileges.php @@ -25,6 +25,7 @@ use PhpMyAdmin\Query\Compatibility; use PhpMyAdmin\ResponseRenderer; use PhpMyAdmin\Template; use PhpMyAdmin\Url; +use PhpMyAdmin\UserPrivileges; use PhpMyAdmin\Util; use function __; @@ -2608,7 +2609,7 @@ class Privileges $response = ResponseRenderer::getInstance(); if (! $response->isAjax() || ! empty($_REQUEST['ajax_page_request'])) { - if ($GLOBALS['is_reload_priv']) { + if (UserPrivileges::$isReload) { $flushnote = new Message( __( 'Note: phpMyAdmin gets the users’ privileges directly ' diff --git a/src/Table/ColumnsDefinition.php b/src/Table/ColumnsDefinition.php index 10dcbda48c..915d074299 100644 --- a/src/Table/ColumnsDefinition.php +++ b/src/Table/ColumnsDefinition.php @@ -14,6 +14,7 @@ use PhpMyAdmin\Partitioning\TablePartitionDefinition; use PhpMyAdmin\Query\Compatibility; use PhpMyAdmin\StorageEngine; use PhpMyAdmin\Transformations; +use PhpMyAdmin\UserPrivileges; use PhpMyAdmin\Util; use function array_keys; @@ -69,8 +70,6 @@ final class ColumnsDefinition array|null $selected = null, array|null $fieldsMeta = null, ): array { - $GLOBALS['col_priv'] ??= null; - $GLOBALS['is_reload_priv'] ??= null; $GLOBALS['mime_map'] ??= null; $regenerate = false; @@ -357,7 +356,7 @@ final class ColumnsDefinition 'max_rows' => intval($config->settings['MaxRows']), 'char_editing' => $config->settings['CharEditing'] ?? null, 'attribute_types' => $this->dbi->types->getAttributes(), - 'privs_available' => ($GLOBALS['col_priv'] ?? false) && ($GLOBALS['is_reload_priv'] ?? false), + 'privs_available' => UserPrivileges::$column && UserPrivileges::$isReload, 'max_length' => $this->dbi->getVersion() >= 50503 ? 1024 : 255, 'have_partitioning' => Partition::havePartitioning(), 'disable_is' => $config->selectedServer['DisableIS'], diff --git a/src/UserPrivileges.php b/src/UserPrivileges.php new file mode 100644 index 0000000000..d845a5307e --- /dev/null +++ b/src/UserPrivileges.php @@ -0,0 +1,18 @@ +setGlobalConfig(); Cache::purge(); diff --git a/tests/classes/CheckUserPrivilegesTest.php b/tests/classes/CheckUserPrivilegesTest.php index 44d5e46d31..46d3544d3c 100644 --- a/tests/classes/CheckUserPrivilegesTest.php +++ b/tests/classes/CheckUserPrivilegesTest.php @@ -8,6 +8,7 @@ use PhpMyAdmin\CheckUserPrivileges; use PhpMyAdmin\Config; use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\ShowGrants; +use PhpMyAdmin\UserPrivileges; use PHPUnit\Framework\Attributes\CoversClass; #[CoversClass(CheckUserPrivileges::class)] @@ -24,11 +25,11 @@ class CheckUserPrivilegesTest extends AbstractTestCase DatabaseInterface::$instance = $this->createDatabaseInterface(); Config::getInstance()->selectedServer['DisableIS'] = false; - $GLOBALS['col_priv'] = false; - $GLOBALS['db_priv'] = false; - $GLOBALS['proc_priv'] = false; - $GLOBALS['table_priv'] = false; - $GLOBALS['is_reload_priv'] = false; + UserPrivileges::$column = false; + UserPrivileges::$database = false; + UserPrivileges::$routines = false; + UserPrivileges::$table = false; + UserPrivileges::$isReload = false; $this->checkUserPrivileges = new CheckUserPrivileges(DatabaseInterface::getInstance()); } @@ -44,13 +45,10 @@ class CheckUserPrivilegesTest extends AbstractTestCase // call the to-be-tested function $this->checkUserPrivileges->checkRequiredPrivilegesForAdjust($showGrants); - $this->assertTrue($GLOBALS['col_priv']); - - $this->assertTrue($GLOBALS['db_priv']); - - $this->assertTrue($GLOBALS['proc_priv']); - - $this->assertTrue($GLOBALS['table_priv']); + $this->assertTrue(UserPrivileges::$column); + $this->assertTrue(UserPrivileges::$database); + $this->assertTrue(UserPrivileges::$routines); + $this->assertTrue(UserPrivileges::$table); // re-initialise the privileges $this->setUp(); @@ -61,13 +59,10 @@ class CheckUserPrivilegesTest extends AbstractTestCase // call the to-be-tested function $this->checkUserPrivileges->checkRequiredPrivilegesForAdjust($showGrants); - $this->assertTrue($GLOBALS['col_priv']); - - $this->assertTrue($GLOBALS['db_priv']); - - $this->assertTrue($GLOBALS['proc_priv']); - - $this->assertTrue($GLOBALS['table_priv']); + $this->assertTrue(UserPrivileges::$column); + $this->assertTrue(UserPrivileges::$database); + $this->assertTrue(UserPrivileges::$routines); + $this->assertTrue(UserPrivileges::$table); // re-initialise the privileges $this->setUp(); @@ -78,13 +73,10 @@ class CheckUserPrivilegesTest extends AbstractTestCase // call the to-be-tested function $this->checkUserPrivileges->checkRequiredPrivilegesForAdjust($showGrants); - $this->assertTrue($GLOBALS['col_priv']); - - $this->assertTrue($GLOBALS['db_priv']); - - $this->assertTrue($GLOBALS['proc_priv']); - - $this->assertTrue($GLOBALS['table_priv']); + $this->assertTrue(UserPrivileges::$column); + $this->assertTrue(UserPrivileges::$database); + $this->assertTrue(UserPrivileges::$routines); + $this->assertTrue(UserPrivileges::$table); // re-initialise the privileges $this->setUp(); @@ -95,12 +87,9 @@ class CheckUserPrivilegesTest extends AbstractTestCase // call the to-be-tested function $this->checkUserPrivileges->checkRequiredPrivilegesForAdjust($showGrants); - $this->assertFalse($GLOBALS['col_priv']); - - $this->assertTrue($GLOBALS['db_priv']); - - $this->assertFalse($GLOBALS['proc_priv']); - - $this->assertFalse($GLOBALS['table_priv']); + $this->assertFalse(UserPrivileges::$column); + $this->assertTrue(UserPrivileges::$database); + $this->assertFalse(UserPrivileges::$routines); + $this->assertFalse(UserPrivileges::$table); } } diff --git a/tests/classes/Controllers/Normalization/AddNewPrimaryControllerTest.php b/tests/classes/Controllers/Normalization/AddNewPrimaryControllerTest.php index d2f5657dce..3e19fcfe99 100644 --- a/tests/classes/Controllers/Normalization/AddNewPrimaryControllerTest.php +++ b/tests/classes/Controllers/Normalization/AddNewPrimaryControllerTest.php @@ -15,6 +15,7 @@ use PhpMyAdmin\Template; use PhpMyAdmin\Tests\AbstractTestCase; use PhpMyAdmin\Tests\Stubs\ResponseRenderer; use PhpMyAdmin\Transformations; +use PhpMyAdmin\UserPrivileges; use PHPUnit\Framework\Attributes\CoversClass; #[CoversClass(AddNewPrimaryController::class)] @@ -23,7 +24,7 @@ class AddNewPrimaryControllerTest extends AbstractTestCase public function testDefault(): void { Config::getInstance()->selectedServer['DisableIS'] = false; - $GLOBALS['col_priv'] = false; + UserPrivileges::$column = false; Current::$database = 'test_db'; Current::$table = 'test_table'; diff --git a/tests/classes/Controllers/Normalization/CreateNewColumnControllerTest.php b/tests/classes/Controllers/Normalization/CreateNewColumnControllerTest.php index fd30a7710c..993d1d1a67 100644 --- a/tests/classes/Controllers/Normalization/CreateNewColumnControllerTest.php +++ b/tests/classes/Controllers/Normalization/CreateNewColumnControllerTest.php @@ -15,6 +15,7 @@ use PhpMyAdmin\Template; use PhpMyAdmin\Tests\AbstractTestCase; use PhpMyAdmin\Tests\Stubs\ResponseRenderer; use PhpMyAdmin\Transformations; +use PhpMyAdmin\UserPrivileges; use PHPUnit\Framework\Attributes\CoversClass; #[CoversClass(CreateNewColumnController::class)] @@ -23,7 +24,7 @@ class CreateNewColumnControllerTest extends AbstractTestCase public function testDefault(): void { Config::getInstance()->selectedServer['DisableIS'] = false; - $GLOBALS['col_priv'] = false; + UserPrivileges::$column = false; Current::$database = 'test_db'; Current::$table = 'test_table'; diff --git a/tests/classes/Controllers/Server/DatabasesControllerTest.php b/tests/classes/Controllers/Server/DatabasesControllerTest.php index 026c97b230..31177f9f2d 100644 --- a/tests/classes/Controllers/Server/DatabasesControllerTest.php +++ b/tests/classes/Controllers/Server/DatabasesControllerTest.php @@ -13,6 +13,7 @@ use PhpMyAdmin\Template; use PhpMyAdmin\Tests\AbstractTestCase; use PhpMyAdmin\Tests\Stubs\DbiDummy; use PhpMyAdmin\Tests\Stubs\ResponseRenderer; +use PhpMyAdmin\UserPrivileges; use PHPUnit\Framework\Attributes\CoversClass; use function __; @@ -80,7 +81,7 @@ class DatabasesControllerTest extends AbstractTestCase $controller = new DatabasesController($response, $template, DatabaseInterface::getInstance()); $config->settings['ShowCreateDb'] = true; - $GLOBALS['is_create_db_priv'] = true; + UserPrivileges::$isCreateDatabase = true; $_REQUEST['statistics'] = '1'; $_REQUEST['sort_by'] = 'SCHEMA_TABLES'; $_REQUEST['sort_order'] = 'desc'; diff --git a/tests/classes/Database/RoutinesTest.php b/tests/classes/Database/RoutinesTest.php index 79a18cd9a3..c5eff3e2e1 100644 --- a/tests/classes/Database/RoutinesTest.php +++ b/tests/classes/Database/RoutinesTest.php @@ -11,6 +11,7 @@ use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\Dbal\Connection; use PhpMyAdmin\Tests\AbstractTestCase; use PhpMyAdmin\Types; +use PhpMyAdmin\UserPrivileges; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; @@ -36,8 +37,8 @@ class RoutinesTest extends AbstractTestCase $config->settings['ActionLinksMode'] = 'icons'; Current::$database = 'db'; Current::$table = 'table'; - $GLOBALS['proc_priv'] = false; - $GLOBALS['is_reload_priv'] = false; + UserPrivileges::$routines = false; + UserPrivileges::$isReload = false; $GLOBALS['errors'] = []; $this->routines = new Routines(DatabaseInterface::getInstance()); diff --git a/tests/classes/Navigation/Nodes/NodeTest.php b/tests/classes/Navigation/Nodes/NodeTest.php index fc2ece32ca..f4eabb8214 100644 --- a/tests/classes/Navigation/Nodes/NodeTest.php +++ b/tests/classes/Navigation/Nodes/NodeTest.php @@ -11,6 +11,7 @@ use PhpMyAdmin\Navigation\Nodes\Node; use PhpMyAdmin\Navigation\NodeType; use PhpMyAdmin\Tests\AbstractTestCase; use PhpMyAdmin\Tests\Stubs\DummyResult; +use PhpMyAdmin\UserPrivileges; use PHPUnit\Framework\Attributes\CoversClass; use ReflectionMethod; @@ -398,7 +399,7 @@ final class NodeTest extends AbstractTestCase { $config = Config::getInstance(); $config->selectedServer['DisableIS'] = true; - $GLOBALS['dbs_to_test'] = false; + UserPrivileges::$databasesToTest = false; $config->settings['NavigationTreeEnableGrouping'] = true; $config->settings['FirstLevelNavigationItems'] = 10; $config->settings['NavigationTreeDbSeparator'] = '_'; @@ -491,7 +492,7 @@ final class NodeTest extends AbstractTestCase { $config = Config::getInstance(); $config->selectedServer['DisableIS'] = true; - $GLOBALS['dbs_to_test'] = false; + UserPrivileges::$databasesToTest = false; $config->settings['NavigationTreeEnableGrouping'] = true; $node = new Node('node'); diff --git a/tests/classes/NormalizationTest.php b/tests/classes/NormalizationTest.php index 88e6f96082..7fd47f70a8 100644 --- a/tests/classes/NormalizationTest.php +++ b/tests/classes/NormalizationTest.php @@ -17,6 +17,7 @@ use PhpMyAdmin\Tests\Stubs\DbiDummy; use PhpMyAdmin\Tests\Stubs\DummyResult; use PhpMyAdmin\Transformations; use PhpMyAdmin\Types; +use PhpMyAdmin\UserPrivileges; use PHPUnit\Framework\Attributes\CoversClass; use stdClass; @@ -124,7 +125,7 @@ class NormalizationTest extends AbstractTestCase $config = Config::getInstance(); $config->settings['BrowseMIME'] = true; $config->settings['MaxRows'] = 25; - $GLOBALS['col_priv'] = false; + UserPrivileges::$column = false; $config->selectedServer['DisableIS'] = false; DatabaseInterface::$instance = $this->dbi; $db = 'testdb'; diff --git a/tests/classes/Server/PrivilegesTest.php b/tests/classes/Server/PrivilegesTest.php index 4e35e337e2..22411c066e 100644 --- a/tests/classes/Server/PrivilegesTest.php +++ b/tests/classes/Server/PrivilegesTest.php @@ -21,6 +21,7 @@ use PhpMyAdmin\Tests\AbstractTestCase; use PhpMyAdmin\Tests\Stubs\DbiDummy; use PhpMyAdmin\Tests\Stubs\DummyResult; use PhpMyAdmin\Url; +use PhpMyAdmin\UserPrivileges; use PhpMyAdmin\Util; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; @@ -1464,7 +1465,7 @@ class PrivilegesTest extends AbstractTestCase { Config::getInstance()->selectedServer['DisableIS'] = false; $GLOBALS['lang'] = 'en'; - $GLOBALS['is_reload_priv'] = true; + UserPrivileges::$isReload = true; $dummyDbi = $this->createDbiDummy(); // phpcs:disable Generic.Files.LineLength.TooLong diff --git a/tests/classes/Table/ColumnsDefinitionTest.php b/tests/classes/Table/ColumnsDefinitionTest.php index 450ecf989b..c36f08c817 100644 --- a/tests/classes/Table/ColumnsDefinitionTest.php +++ b/tests/classes/Table/ColumnsDefinitionTest.php @@ -12,6 +12,7 @@ use PhpMyAdmin\Table\ColumnsDefinition; use PhpMyAdmin\Tests\AbstractTestCase; use PhpMyAdmin\Tests\FieldHelper; use PhpMyAdmin\Transformations; +use PhpMyAdmin\UserPrivileges; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; @@ -76,8 +77,8 @@ SQL; Current::$database = 'sakila'; Current::$table = 'actor'; - $GLOBALS['col_priv'] = true; - $GLOBALS['is_reload_priv'] = true; + UserPrivileges::$column = true; + UserPrivileges::$isReload = true; $GLOBALS['mime_map'] = null; $actual = $columnsDefinition->displayForm('/table/structure/save', 1, ['actor_id'], [$columnMeta]);