Skip to content

Commit 3794031

Browse files
authored
Ref #17769 - Replace superglobals with ServerRequest in Import & Export Controllers (#17949)
* Replace superglobals with ServerRequest in Import & Export Controllers Signed-off-by: Evgeny Skorlov <eugene@skorlov.name> * small fixes Signed-off-by: Evgeny Skorlov <eugene@skorlov.name> * fixes Signed-off-by: Evgeny Skorlov <eugene@skorlov.name> Signed-off-by: Evgeny Skorlov <eugene@skorlov.name>
1 parent a84cae6 commit 3794031

5 files changed

Lines changed: 62 additions & 53 deletions

File tree

libraries/classes/Controllers/Export/TablesController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct(ResponseRenderer $response, Template $template, Expo
2525

2626
public function __invoke(ServerRequest $request): void
2727
{
28-
if (empty($_POST['selected_tbl'])) {
28+
if (! $request->hasBodyParam('selected_tbl')) {
2929
$this->response->setRequestStatus(false);
3030
$this->response->addJSON('message', __('No table selected.'));
3131

libraries/classes/Controllers/Import/ImportController.php

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,16 @@ public function __invoke(ServerRequest $request): void
109109
$GLOBALS['reload'] = $GLOBALS['reload'] ?? null;
110110
$GLOBALS['charset_connection'] = $GLOBALS['charset_connection'] ?? null;
111111

112-
$GLOBALS['charset_of_file'] = $_POST['charset_of_file'] ?? null;
113-
$GLOBALS['format'] = $_POST['format'] ?? '';
114-
$GLOBALS['import_type'] = $_POST['import_type'] ?? null;
115-
$GLOBALS['is_js_confirmed'] = $_POST['is_js_confirmed'] ?? null;
116-
$GLOBALS['MAX_FILE_SIZE'] = $_POST['MAX_FILE_SIZE'] ?? null;
117-
$GLOBALS['message_to_show'] = $_POST['message_to_show'] ?? null;
118-
$GLOBALS['noplugin'] = $_POST['noplugin'] ?? null;
119-
$GLOBALS['skip_queries'] = $_POST['skip_queries'] ?? null;
120-
$GLOBALS['local_import_file'] = $_POST['local_import_file'] ?? null;
121-
$GLOBALS['show_as_php'] = $_POST['show_as_php'] ?? null;
112+
$GLOBALS['charset_of_file'] = $request->getParsedBodyParam('charset_of_file');
113+
$GLOBALS['format'] = $request->getParsedBodyParam('format', '');
114+
$GLOBALS['import_type'] = $request->getParsedBodyParam('import_type');
115+
$GLOBALS['is_js_confirmed'] = $request->getParsedBodyParam('is_js_confirmed');
116+
$GLOBALS['MAX_FILE_SIZE'] = $request->getParsedBodyParam('MAX_FILE_SIZE');
117+
$GLOBALS['message_to_show'] = $request->getParsedBodyParam('message_to_show');
118+
$GLOBALS['noplugin'] = $request->getParsedBodyParam('noplugin');
119+
$GLOBALS['skip_queries'] = $request->getParsedBodyParam('skip_queries');
120+
$GLOBALS['local_import_file'] = $request->getParsedBodyParam('local_import_file');
121+
$GLOBALS['show_as_php'] = $request->getParsedBodyParam('show_as_php');
122122

123123
// reset import messages for ajax request
124124
$_SESSION['Import_message']['message'] = null;
@@ -138,9 +138,9 @@ public function __invoke(ServerRequest $request): void
138138
// (eg. non import, but query box/window run)
139139
if (! empty($GLOBALS['sql_query'])) {
140140
// apply values for parameters
141-
if (! empty($_POST['parameterized']) && ! empty($_POST['parameters']) && is_array($_POST['parameters'])) {
142-
/** @var array<string, string> $parameters */
143-
$parameters = $_POST['parameters'];
141+
/** @var array<string, string> $parameters */
142+
$parameters = $request->getParsedBodyParam('parameters');
143+
if ($request->hasBodyParam('parameterized') && is_array($parameters)) {
144144
foreach ($parameters as $parameter => $replacementValue) {
145145
if (! is_numeric($replacementValue)) {
146146
$replacementValue = $this->dbi->quoteString($replacementValue);
@@ -169,7 +169,7 @@ public function __invoke(ServerRequest $request): void
169169
$_SESSION['sql_from_query_box'] = true;
170170

171171
// If there is a request to ROLLBACK when finished.
172-
if (isset($_POST['rollback_query'])) {
172+
if ($request->hasBodyParam('rollback_query')) {
173173
$this->import->handleRollbackRequest($GLOBALS['import_text']);
174174
}
175175

@@ -204,15 +204,17 @@ public function __invoke(ServerRequest $request): void
204204
$GLOBALS['import_type'] = 'queryfile';
205205
$GLOBALS['format'] = 'sql';
206206
unset($GLOBALS['sql_file']);
207-
} elseif (! empty($_POST['id_bookmark'])) {
207+
} elseif ($request->hasBodyParam('id_bookmark')) {
208208
// run bookmark
209209
$GLOBALS['import_type'] = 'query';
210210
$GLOBALS['format'] = 'sql';
211211
}
212212

213213
// If we didn't get any parameters, either user called this directly, or
214214
// upload limit has been reached, let's assume the second possibility.
215-
if ($_POST == [] && $_GET == []) {
215+
$getParams = $request->getQueryParams();
216+
$postParams = $request->getParsedBody();
217+
if ($postParams === [] && $getParams === []) {
216218
$GLOBALS['message'] = Message::error(
217219
__(
218220
'You probably tried to upload a file that is too large. Please refer ' .
@@ -233,8 +235,9 @@ public function __invoke(ServerRequest $request): void
233235
}
234236

235237
// Add console message id to response output
236-
if (isset($_POST['console_message_id'])) {
237-
$this->response->addJSON('console_message_id', $_POST['console_message_id']);
238+
$console_message_id = $request->getParsedBodyParam('console_message_id');
239+
if ($console_message_id !== null) {
240+
$this->response->addJSON('console_message_id', $console_message_id);
238241
}
239242

240243
/**
@@ -301,7 +304,7 @@ public function __invoke(ServerRequest $request): void
301304
}
302305

303306
$GLOBALS['timestamp'] = time();
304-
if (isset($_POST['allow_interrupt'])) {
307+
if ($request->hasBodyParam('allow_interrupt')) {
305308
$GLOBALS['maximum_time'] = ini_get('max_execution_time');
306309
} else {
307310
$GLOBALS['maximum_time'] = 0;
@@ -326,24 +329,26 @@ public function __invoke(ServerRequest $request): void
326329
$GLOBALS['result'] = false;
327330

328331
// Bookmark Support: get a query back from bookmark if required
329-
if (! empty($_POST['id_bookmark'])) {
330-
$id_bookmark = (int) $_POST['id_bookmark'];
331-
switch ($_POST['action_bookmark']) {
332+
$id_bookmark = (int) $request->getParsedBodyParam('id_bookmark');
333+
$action_bookmark = (int) $request->getParsedBodyParam('action_bookmark');
334+
if ($id_bookmark !== 0) {
335+
switch ($action_bookmark) {
332336
case 0: // bookmarked query that have to be run
333337
$bookmark = Bookmark::get(
334338
$this->dbi,
335339
$GLOBALS['cfg']['Server']['user'],
336340
DatabaseName::fromValue($GLOBALS['db']),
337341
$id_bookmark,
338342
'id',
339-
isset($_POST['action_bookmark_all'])
343+
$request->hasBodyParam('action_bookmark_all')
340344
);
341345
if (! $bookmark instanceof Bookmark) {
342346
break;
343347
}
344348

345-
if (! empty($_POST['bookmark_variable'])) {
346-
$GLOBALS['import_text'] = $bookmark->applyVariables($_POST['bookmark_variable']);
349+
$bookmark_variables = $request->getParsedBodyParam('bookmark_variable');
350+
if (is_array($bookmark_variables)) {
351+
$GLOBALS['import_text'] = $bookmark->applyVariables($bookmark_variables);
347352
} else {
348353
$GLOBALS['import_text'] = $bookmark->getQuery();
349354
}
@@ -377,7 +382,7 @@ public function __invoke(ServerRequest $request): void
377382
$this->response->setRequestStatus($GLOBALS['message']->isSuccess());
378383
$this->response->addJSON('message', $GLOBALS['message']);
379384
$this->response->addJSON('sql_query', $GLOBALS['import_text']);
380-
$this->response->addJSON('action_bookmark', $_POST['action_bookmark']);
385+
$this->response->addJSON('action_bookmark', $action_bookmark);
381386

382387
return;
383388
} else {
@@ -403,7 +408,7 @@ public function __invoke(ServerRequest $request): void
403408
);
404409
$this->response->setRequestStatus($GLOBALS['message']->isSuccess());
405410
$this->response->addJSON('message', $GLOBALS['message']);
406-
$this->response->addJSON('action_bookmark', $_POST['action_bookmark']);
411+
$this->response->addJSON('action_bookmark', $action_bookmark);
407412
$this->response->addJSON('id_bookmark', $id_bookmark);
408413

409414
return;
@@ -550,8 +555,8 @@ public function __invoke(ServerRequest $request): void
550555
}
551556

552557
// Something to skip? (because timeout has passed)
553-
if (! $GLOBALS['error'] && isset($_POST['skip'])) {
554-
$original_skip = $skip = intval($_POST['skip']);
558+
if (! $GLOBALS['error'] && $request->hasBodyParam('skip')) {
559+
$original_skip = $skip = intval($request->getParsedBodyParam('skip'));
555560
while ($skip > 0 && ! $GLOBALS['finished']) {
556561
$this->import->getNextChunk(
557562
$importHandle ?? null,
@@ -611,11 +616,11 @@ public function __invoke(ServerRequest $request): void
611616
}
612617

613618
// Show correct message
614-
if (! empty($id_bookmark) && $_POST['action_bookmark'] == 2) {
619+
if ($id_bookmark !== 0 && $action_bookmark === 2) {
615620
$GLOBALS['message'] = Message::success(__('The bookmark has been deleted.'));
616621
$GLOBALS['display_query'] = $GLOBALS['import_text'];
617622
$GLOBALS['error'] = false; // unset error marker, it was used just to skip processing
618-
} elseif (! empty($id_bookmark) && $_POST['action_bookmark'] == 1) {
623+
} elseif ($id_bookmark !== 0 && $action_bookmark === 1) {
619624
$GLOBALS['message'] = Message::notice(__('Showing bookmark'));
620625
} elseif ($GLOBALS['finished'] && ! $GLOBALS['error']) {
621626
// Do not display the query with message, we do it separately
@@ -773,16 +778,16 @@ public function __invoke(ServerRequest $request): void
773778
// sql_query_for_bookmark is not included in Sql::executeQueryAndGetQueryResponse
774779
// since only one bookmark has to be added for all the queries submitted through
775780
// the SQL tab
776-
if (! empty($_POST['bkm_label']) && ! empty($GLOBALS['import_text'])) {
781+
if (! empty($request->getParsedBodyParam('bkm_label')) && ! empty($GLOBALS['import_text'])) {
777782
$relation = new Relation($this->dbi);
778783

779784
$this->sql->storeTheQueryAsBookmark(
780785
$relation->getRelationParameters()->bookmarkFeature,
781786
$GLOBALS['db'],
782787
$GLOBALS['cfg']['Server']['user'],
783-
$_POST['sql_query'],
784-
$_POST['bkm_label'],
785-
isset($_POST['bkm_replace'])
788+
$request->getParsedBodyParam('sql_query'),
789+
$request->getParsedBodyParam('bkm_label'),
790+
$request->hasBodyParam('bkm_replace')
786791
);
787792
}
788793

@@ -794,16 +799,16 @@ public function __invoke(ServerRequest $request): void
794799

795800
if ($GLOBALS['result']) {
796801
// Save a Bookmark with more than one queries (if Bookmark label given).
797-
if (! empty($_POST['bkm_label']) && ! empty($GLOBALS['import_text'])) {
802+
if (! empty($request->getParsedBodyParam('bkm_label')) && ! empty($GLOBALS['import_text'])) {
798803
$relation = new Relation($this->dbi);
799804

800805
$this->sql->storeTheQueryAsBookmark(
801806
$relation->getRelationParameters()->bookmarkFeature,
802807
$GLOBALS['db'],
803808
$GLOBALS['cfg']['Server']['user'],
804-
$_POST['sql_query'],
805-
$_POST['bkm_label'],
806-
isset($_POST['bkm_replace'])
809+
$request->getParsedBodyParam('sql_query'),
810+
$request->getParsedBodyParam('bkm_label'),
811+
$request->hasBodyParam('bkm_replace')
807812
);
808813
}
809814

@@ -823,7 +828,7 @@ public function __invoke(ServerRequest $request): void
823828
}
824829

825830
// If there is request for ROLLBACK in the end.
826-
if (! isset($_POST['rollback_query'])) {
831+
if (! $request->hasBodyParam('rollback_query')) {
827832
return;
828833
}
829834

libraries/classes/Controllers/Import/SimulateDmlController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __invoke(ServerRequest $request): void
3838
$error = '';
3939
$errorMsg = __('Only single-table UPDATE and DELETE queries can be simulated.');
4040
/** @var string $sqlDelimiter */
41-
$sqlDelimiter = $_POST['sql_delimiter'];
41+
$sqlDelimiter = $request->getParsedBodyParam('sql_delimiter', '');
4242
$sqlData = [];
4343
/** @var string[] $queries */
4444
$queries = explode($sqlDelimiter, $GLOBALS['sql_query']);

libraries/classes/Controllers/Import/StatusController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __invoke(ServerRequest $request): void
4646
] = Ajax::uploadProgressSetup();
4747

4848
// $_GET["message"] is used for asking for an import message
49-
if (isset($_GET['message']) && $_GET['message']) {
49+
if ($request->hasQueryParam('message')) {
5050
// AJAX requests can't be cached!
5151
foreach (Core::getNoCacheHeaders() as $name => $value) {
5252
header(sprintf('%s: %s', $name, $value));
@@ -84,7 +84,7 @@ public function __invoke(ServerRequest $request): void
8484
]);
8585
}
8686
} else {
87-
Ajax::status($_GET['id']);
87+
Ajax::status($request->getQueryParam('id'));
8888
}
8989
}
9090
}

test/classes/Controllers/Import/ImportControllerTest.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,20 @@ public function testIndexParametrized(): void
4343
parent::loadResponseIntoContainerBuilder();
4444

4545
// Some params where not added as they where not required for this test
46-
$_POST['db'] = 'pma_test';
47-
$_POST['table'] = 'table1';
48-
$GLOBALS['db'] = $_POST['db'];
49-
$GLOBALS['table'] = $_POST['table'];
50-
$_POST['parameterized'] = 'on';
51-
$_POST['parameters'] = [':nomEta' => 'Saint-Louis - Châteaulin', ':1' => '4'];
52-
$_POST['sql_query'] = 'SELECT A.*' . "\n"
46+
$GLOBALS['db'] = 'pma_test';
47+
$GLOBALS['table'] = 'table1';
48+
$GLOBALS['sql_query'] = 'SELECT A.*' . "\n"
5349
. 'FROM table1 A' . "\n"
5450
. 'WHERE A.nomEtablissement = :nomEta AND foo = :1 AND `:a` IS NULL';
55-
$GLOBALS['sql_query'] = $_POST['sql_query'];
51+
52+
$request = $this->createStub(ServerRequest::class);
53+
$request->method('getParsedBodyParam')->willReturnMap([
54+
['db', null, $GLOBALS['db']],
55+
['table', null, $GLOBALS['table']],
56+
['parameterized', null, 'on'],
57+
['parameters', null, [':nomEta' => 'Saint-Louis - Châteaulin', ':1' => '4']],
58+
['sql_query', null, $GLOBALS['sql_query']],
59+
]);
5660

5761
$this->dummyDbi->addResult(
5862
'SELECT A.* FROM table1 A WHERE A.nomEtablissement = \'Saint-Louis - Châteaulin\''
@@ -74,7 +78,7 @@ public function testIndexParametrized(): void
7478
$importController = $GLOBALS['containerBuilder']->get(ImportController::class);
7579
$this->dummyDbi->addSelectDb('pma_test');
7680
$this->dummyDbi->addSelectDb('pma_test');
77-
$importController($this->createStub(ServerRequest::class));
81+
$importController($request);
7882
$this->dummyDbi->assertAllSelectsConsumed();
7983
$this->assertResponseWasSuccessfull();
8084

0 commit comments

Comments
 (0)