Skip to content

Commit 55a6f09

Browse files
Server privileges controller globals removal (#17986)
* $GLOBALS['url_dbname'] * $GLOBALS['grants'], $GLOBALS['one_grant'] * $GLOBALS['export'] * $GLOBALS['title'] * $GLOBALS['itemType'] * $GLOBALS['_add_user_error'] * $GLOBALS['queries_for_display'] * $GLOBALS['ret_queries'] * $GLOBALS['ret_message'] * $GLOBALS['queries'] * Remove redundant code * $GLOBALS['password'] * $GLOBALS['dbname_is_wildcard'] * $GLOBALS['db_and_table'] * $GLOBALS['routinename'] * $GLOBALS['tablename'] * $GLOBALS['post_patterns'] * Update psalm-baseline.xml Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
1 parent a4a2915 commit 55a6f09

6 files changed

Lines changed: 75 additions & 188 deletions

File tree

libraries/classes/Controllers/Server/PrivilegesController.php

Lines changed: 45 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -54,32 +54,15 @@ public function __invoke(ServerRequest $request): void
5454
$GLOBALS['errorUrl'] = $GLOBALS['errorUrl'] ?? null;
5555
$GLOBALS['message'] = $GLOBALS['message'] ?? null;
5656
$GLOBALS['text_dir'] = $GLOBALS['text_dir'] ?? null;
57-
$GLOBALS['post_patterns'] = $GLOBALS['post_patterns'] ?? null;
5857
$GLOBALS['username'] = $GLOBALS['username'] ?? null;
5958
$GLOBALS['hostname'] = $GLOBALS['hostname'] ?? null;
6059
$GLOBALS['dbname'] = $GLOBALS['dbname'] ?? null;
61-
$GLOBALS['tablename'] = $GLOBALS['tablename'] ?? null;
62-
$GLOBALS['routinename'] = $GLOBALS['routinename'] ?? null;
63-
$GLOBALS['db_and_table'] = $GLOBALS['db_and_table'] ?? null;
64-
$GLOBALS['dbname_is_wildcard'] = $GLOBALS['dbname_is_wildcard'] ?? null;
65-
$GLOBALS['queries'] = $GLOBALS['queries'] ?? null;
66-
$GLOBALS['password'] = $GLOBALS['password'] ?? null;
67-
$GLOBALS['ret_message'] = $GLOBALS['ret_message'] ?? null;
68-
$GLOBALS['ret_queries'] = $GLOBALS['ret_queries'] ?? null;
69-
$GLOBALS['queries_for_display'] = $GLOBALS['queries_for_display'] ?? null;
70-
$GLOBALS['_add_user_error'] = $GLOBALS['_add_user_error'] ?? null;
71-
$GLOBALS['itemType'] = $GLOBALS['itemType'] ?? null;
7260
$GLOBALS['tables'] = $GLOBALS['tables'] ?? null;
7361
$GLOBALS['num_tables'] = $GLOBALS['num_tables'] ?? null;
7462
$GLOBALS['total_num_tables'] = $GLOBALS['total_num_tables'] ?? null;
7563
$GLOBALS['tooltip_truename'] = $GLOBALS['tooltip_truename'] ?? null;
7664
$GLOBALS['tooltip_aliasname'] = $GLOBALS['tooltip_aliasname'] ?? null;
7765
$GLOBALS['pos'] = $GLOBALS['pos'] ?? null;
78-
$GLOBALS['title'] = $GLOBALS['title'] ?? null;
79-
$GLOBALS['export'] = $GLOBALS['export'] ?? null;
80-
$GLOBALS['grants'] = $GLOBALS['grants'] ?? null;
81-
$GLOBALS['one_grant'] = $GLOBALS['one_grant'] ?? null;
82-
$GLOBALS['url_dbname'] = $GLOBALS['url_dbname'] ?? null;
8366

8467
$checkUserPrivileges = new CheckUserPrivileges($this->dbi);
8568
$checkUserPrivileges->getPrivileges();
@@ -108,20 +91,17 @@ public function __invoke(ServerRequest $request): void
10891
/**
10992
* Sets globals from $_POST patterns, for privileges and max_* vars
11093
*/
111-
$GLOBALS['post_patterns'] = [
94+
Core::setPostAsGlobal([
11295
'/_priv$/i',
11396
'/^max_/i',
114-
];
115-
116-
Core::setPostAsGlobal($GLOBALS['post_patterns']);
97+
]);
11798

11899
$GLOBALS['errorUrl'] = Url::getFromRoute('/');
119100

120101
if ($this->dbi->isSuperUser()) {
121102
$this->dbi->selectDb('mysql');
122103
}
123104

124-
$GLOBALS['_add_user_error'] = false;
125105
/**
126106
* Get DB information: username, hostname, dbname,
127107
* tablename, db_and_table, dbname_is_wildcard
@@ -130,10 +110,9 @@ public function __invoke(ServerRequest $request): void
130110
$GLOBALS['username'],
131111
$GLOBALS['hostname'],
132112
$GLOBALS['dbname'],
133-
$GLOBALS['tablename'],
134-
$GLOBALS['routinename'],
135-
$GLOBALS['db_and_table'],
136-
$GLOBALS['dbname_is_wildcard'],
113+
$tablename,
114+
$routinename,
115+
$dbnameIsWildcard,
137116
] = $serverPrivileges->getDataForDBInfo();
138117

139118
/**
@@ -187,50 +166,45 @@ public function __invoke(ServerRequest $request): void
187166
/**
188167
* Changes / copies a user, part I
189168
*/
190-
[$GLOBALS['queries'], $GLOBALS['password']] = $serverPrivileges->getDataForChangeOrCopyUser();
169+
$password = $serverPrivileges->getDataForChangeOrCopyUser();
191170

192171
/**
193172
* Adds a user
194173
* (Changes / copies a user, part II)
195174
*/
196175
[
197-
$GLOBALS['ret_message'],
198-
$GLOBALS['ret_queries'],
199-
$GLOBALS['queries_for_display'],
176+
$retMessage,
177+
$queries,
178+
$queriesForDisplay,
200179
$GLOBALS['sql_query'],
201-
$GLOBALS['_add_user_error'],
180+
$addUserError,
202181
] = $serverPrivileges->addUser(
203182
$GLOBALS['dbname'] ?? null,
204183
$GLOBALS['username'] ?? '',
205184
$GLOBALS['hostname'] ?? '',
206-
$GLOBALS['password'] ?? null,
185+
$password,
207186
$relationParameters->configurableMenusFeature !== null
208187
);
209188
//update the old variables
210-
if (isset($GLOBALS['ret_queries'])) {
211-
$GLOBALS['queries'] = $GLOBALS['ret_queries'];
212-
unset($GLOBALS['ret_queries']);
213-
}
214-
215-
if (isset($GLOBALS['ret_message'])) {
216-
$GLOBALS['message'] = $GLOBALS['ret_message'];
217-
unset($GLOBALS['ret_message']);
189+
if (isset($retMessage)) {
190+
$GLOBALS['message'] = $retMessage;
191+
unset($retMessage);
218192
}
219193

220194
/**
221195
* Changes / copies a user, part III
222196
*/
223197
if ($request->hasBodyParam('change_copy') && $GLOBALS['username'] !== null && $GLOBALS['hostname'] !== null) {
224-
$GLOBALS['queries'] = $serverPrivileges->getDbSpecificPrivsQueriesForChangeOrCopyUser(
225-
$GLOBALS['queries'],
198+
$queries = $serverPrivileges->getDbSpecificPrivsQueriesForChangeOrCopyUser(
199+
$queries,
226200
$GLOBALS['username'],
227201
$GLOBALS['hostname']
228202
);
229203
}
230204

231-
$GLOBALS['itemType'] = '';
232-
if (! empty($GLOBALS['routinename']) && is_string($GLOBALS['dbname'])) {
233-
$GLOBALS['itemType'] = $serverPrivileges->getRoutineType($GLOBALS['dbname'], $GLOBALS['routinename']);
205+
$itemType = '';
206+
if (! empty($routinename) && is_string($GLOBALS['dbname'])) {
207+
$itemType = $serverPrivileges->getRoutineType($GLOBALS['dbname'], $routinename);
234208
}
235209

236210
/**
@@ -242,9 +216,9 @@ public function __invoke(ServerRequest $request): void
242216
[$GLOBALS['sql_query'][$key], $GLOBALS['message']] = $serverPrivileges->updatePrivileges(
243217
($GLOBALS['username'] ?? ''),
244218
($GLOBALS['hostname'] ?? ''),
245-
($GLOBALS['tablename'] ?? ($GLOBALS['routinename'] ?? '')),
219+
($tablename ?? ($routinename ?? '')),
246220
($db_name ?? ''),
247-
$GLOBALS['itemType']
221+
$itemType
248222
);
249223
}
250224

@@ -253,9 +227,9 @@ public function __invoke(ServerRequest $request): void
253227
[$GLOBALS['sql_query'], $GLOBALS['message']] = $serverPrivileges->updatePrivileges(
254228
($GLOBALS['username'] ?? ''),
255229
($GLOBALS['hostname'] ?? ''),
256-
($GLOBALS['tablename'] ?? ($GLOBALS['routinename'] ?? '')),
230+
($tablename ?? ($routinename ?? '')),
257231
($GLOBALS['dbname'] ?? ''),
258-
$GLOBALS['itemType']
232+
$itemType
259233
);
260234
}
261235
}
@@ -277,10 +251,10 @@ public function __invoke(ServerRequest $request): void
277251
if ($request->hasBodyParam('revokeall')) {
278252
[$GLOBALS['message'], $GLOBALS['sql_query']] = $serverPrivileges->getMessageAndSqlQueryForPrivilegesRevoke(
279253
(is_string($GLOBALS['dbname']) ? $GLOBALS['dbname'] : ''),
280-
($GLOBALS['tablename'] ?? ($GLOBALS['routinename'] ?? '')),
254+
($tablename ?? ($routinename ?? '')),
281255
$GLOBALS['username'] ?? '',
282256
$GLOBALS['hostname'] ?? '',
283-
$GLOBALS['itemType']
257+
$itemType
284258
);
285259
}
286260

@@ -303,22 +277,22 @@ public function __invoke(ServerRequest $request): void
303277
$request->hasBodyParam('delete')
304278
|| ($request->hasBodyParam('change_copy') && $request->getParsedBodyParam('mode') < 4)
305279
) {
306-
$GLOBALS['queries'] = $serverPrivileges->getDataForDeleteUsers($GLOBALS['queries']);
280+
$queries = $serverPrivileges->getDataForDeleteUsers($queries);
307281
if (! $request->hasBodyParam('change_copy')) {
308-
[$GLOBALS['sql_query'], $GLOBALS['message']] = $serverPrivileges->deleteUser($GLOBALS['queries']);
282+
[$GLOBALS['sql_query'], $GLOBALS['message']] = $serverPrivileges->deleteUser($queries);
309283
}
310284
}
311285

312286
/**
313287
* Changes / copies a user, part V
314288
*/
315289
if ($request->hasBodyParam('change_copy')) {
316-
$GLOBALS['queries'] = $serverPrivileges->getDataForQueries(
317-
$GLOBALS['queries'],
318-
$GLOBALS['queries_for_display']
290+
$queries = $serverPrivileges->getDataForQueries(
291+
$queries,
292+
$queriesForDisplay
319293
);
320294
$GLOBALS['message'] = Message::success();
321-
$GLOBALS['sql_query'] = implode("\n", $GLOBALS['queries']);
295+
$GLOBALS['sql_query'] = implode("\n", $queries);
322296
}
323297

324298
/**
@@ -344,7 +318,7 @@ public function __invoke(ServerRequest $request): void
344318
&& ! $request->hasQueryParam('showall')
345319
) {
346320
$extra_data = $serverPrivileges->getExtraDataForAjaxBehavior(
347-
($GLOBALS['password'] ?? ''),
321+
($password ?? ''),
348322
($GLOBALS['sql_query'] ?? ''),
349323
($GLOBALS['hostname'] ?? ''),
350324
($GLOBALS['username'] ?? '')
@@ -369,32 +343,32 @@ public function __invoke(ServerRequest $request): void
369343

370344
// export user definition
371345
if ($request->hasQueryParam('export') || $request->getParsedBodyParam('submit_mult') === 'export') {
372-
[$GLOBALS['title'], $GLOBALS['export']] = $serverPrivileges->getListForExportUserDefinition(
346+
[$title, $export] = $serverPrivileges->getListForExportUserDefinition(
373347
$GLOBALS['username'] ?? '',
374348
$GLOBALS['hostname'] ?? ''
375349
);
376350

377-
unset($GLOBALS['username'], $GLOBALS['hostname'], $GLOBALS['grants'], $GLOBALS['one_grant']);
351+
unset($GLOBALS['username'], $GLOBALS['hostname']);
378352

379353
if ($this->response->isAjax()) {
380-
$this->response->addJSON('message', $GLOBALS['export']);
381-
$this->response->addJSON('title', $GLOBALS['title']);
354+
$this->response->addJSON('message', $export);
355+
$this->response->addJSON('title', $title);
382356

383357
return;
384358
}
385359

386-
$this->response->addHTML('<h2>' . $GLOBALS['title'] . '</h2>' . $GLOBALS['export']);
360+
$this->response->addHTML('<h2>' . $title . '</h2>' . $export);
387361
}
388362

389363
// Show back the form if an error occurred
390-
if ($request->hasQueryParam('adduser') || $GLOBALS['_add_user_error'] === true) {
364+
if ($request->hasQueryParam('adduser') || $addUserError === true) {
391365
// Add user
392366
$this->response->addHTML($serverPrivileges->getHtmlForAddUser(
393367
$serverPrivileges->escapeGrantWildcards(is_string($GLOBALS['dbname']) ? $GLOBALS['dbname'] : '')
394368
));
395369
} else {
396370
if (isset($GLOBALS['dbname']) && ! is_array($GLOBALS['dbname'])) {
397-
$GLOBALS['url_dbname'] = urlencode(
371+
$urlDbname = urlencode(
398372
str_replace(
399373
[
400374
'\_',
@@ -414,14 +388,14 @@ public function __invoke(ServerRequest $request): void
414388
$this->response->addHTML(
415389
$serverPrivileges->getHtmlForUserOverview($GLOBALS['text_dir'])
416390
);
417-
} elseif (! empty($GLOBALS['routinename'])) {
391+
} elseif (! empty($routinename)) {
418392
$this->response->addHTML(
419393
$serverPrivileges->getHtmlForRoutineSpecificPrivileges(
420394
$GLOBALS['username'],
421395
$GLOBALS['hostname'] ?? '',
422396
is_string($GLOBALS['dbname']) ? $GLOBALS['dbname'] : '',
423-
$GLOBALS['routinename'],
424-
$serverPrivileges->escapeGrantWildcards($GLOBALS['url_dbname'] ?? '')
397+
$routinename,
398+
$serverPrivileges->escapeGrantWildcards($urlDbname ?? '')
425399
)
426400
);
427401
} else {
@@ -433,12 +407,12 @@ public function __invoke(ServerRequest $request): void
433407

434408
$this->response->addHTML(
435409
$serverPrivileges->getHtmlForUserProperties(
436-
$GLOBALS['dbname_is_wildcard'],
437-
$serverPrivileges->escapeGrantWildcards($GLOBALS['url_dbname'] ?? ''),
410+
$dbnameIsWildcard,
411+
$serverPrivileges->escapeGrantWildcards($urlDbname ?? ''),
438412
$GLOBALS['username'],
439413
$GLOBALS['hostname'] ?? '',
440414
$GLOBALS['dbname'] ?? '',
441-
$GLOBALS['tablename'] ?? '',
415+
$tablename ?? '',
442416
$request->getRoute()
443417
)
444418
);

libraries/classes/Controllers/UserPasswordController.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public function __invoke(ServerRequest $request): void
4040
{
4141
$GLOBALS['hostname'] = $GLOBALS['hostname'] ?? null;
4242
$GLOBALS['username'] = $GLOBALS['username'] ?? null;
43-
$GLOBALS['password'] = $GLOBALS['password'] ?? null;
4443
$GLOBALS['change_password_message'] = $GLOBALS['change_password_message'] ?? null;
4544
$GLOBALS['msg'] = $GLOBALS['msg'] ?? null;
4645

@@ -72,9 +71,9 @@ public function __invoke(ServerRequest $request): void
7271
*/
7372
if ($noPass !== null) {
7473
if ($noPass == '1') {
75-
$GLOBALS['password'] = '';
74+
$password = '';
7675
} else {
77-
$GLOBALS['password'] = $pmaPw;
76+
$password = $pmaPw;
7877
}
7978

8079
$GLOBALS['change_password_message'] = $this->userPassword->setChangePasswordMsg(
@@ -86,7 +85,7 @@ public function __invoke(ServerRequest $request): void
8685

8786
if (! $GLOBALS['change_password_message']['error']) {
8887
$sql_query = $this->userPassword->changePassword(
89-
$GLOBALS['password'],
88+
$password,
9089
$request->getParsedBodyParam('authentication_plugin')
9190
);
9291

0 commit comments

Comments
 (0)