Skip to content

Commit c51b5fa

Browse files
committed
Use DatabaseName for the newname param in OperationsController
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 344981d commit c51b5fa

File tree

2 files changed

+41
-39
lines changed

2 files changed

+41
-39
lines changed

libraries/classes/Controllers/Database/OperationsController.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use PhpMyAdmin\ConfigStorage\RelationCleanup;
1111
use PhpMyAdmin\Controllers\AbstractController;
1212
use PhpMyAdmin\DatabaseInterface;
13+
use PhpMyAdmin\Dbal\DatabaseName;
14+
use PhpMyAdmin\Dbal\InvalidDatabaseName;
1315
use PhpMyAdmin\Html\Generator;
1416
use PhpMyAdmin\Http\ServerRequest;
1517
use PhpMyAdmin\Message;
@@ -23,7 +25,6 @@
2325

2426
use function __;
2527
use function count;
26-
use function is_string;
2728
use function mb_strtolower;
2829
use function strlen;
2930

@@ -104,18 +105,18 @@ public function __invoke(ServerRequest $request): void
104105
$GLOBALS['move'] = false;
105106
}
106107

107-
/** @var mixed|null $newDatabaseName */
108-
$newDatabaseName = $request->getParsedBodyParam('newname');
109-
if (! is_string($newDatabaseName) || $newDatabaseName === '') {
110-
$GLOBALS['message'] = Message::error(__('The database name is empty!'));
111-
$newDatabaseName = null;
112-
} else {
113-
// lower_case_table_names=1 `DB` becomes `db`
108+
try {
109+
$newDatabaseName = DatabaseName::fromValue($request->getParsedBodyParam('newname'));
114110
if ($this->dbi->getLowerCaseNames() === '1') {
115-
$newDatabaseName = mb_strtolower($newDatabaseName);
111+
$newDatabaseName = DatabaseName::fromValue(mb_strtolower($newDatabaseName->getName()));
116112
}
113+
} catch (InvalidDatabaseName $exception) {
114+
$newDatabaseName = null;
115+
$GLOBALS['message'] = Message::error($exception->getMessage());
116+
}
117117

118-
if ($newDatabaseName === $_REQUEST['db']) {
118+
if ($newDatabaseName !== null) {
119+
if ($newDatabaseName->getName() === $_REQUEST['db']) {
119120
$GLOBALS['message'] = Message::error(
120121
__('Cannot copy database to the same name. Change the name and try again.')
121122
);
@@ -215,7 +216,7 @@ public function __invoke(ServerRequest $request): void
215216
__('Database %1$s has been renamed to %2$s.')
216217
);
217218
$GLOBALS['message']->addParam($GLOBALS['db']);
218-
$GLOBALS['message']->addParam($newDatabaseName);
219+
$GLOBALS['message']->addParam($newDatabaseName->getName());
219220
} elseif (! $_error) {
220221
if (isset($_POST['adjust_privileges']) && ! empty($_POST['adjust_privileges'])) {
221222
$this->operations->adjustPrivilegesCopyDb($GLOBALS['db'], $newDatabaseName);
@@ -225,7 +226,7 @@ public function __invoke(ServerRequest $request): void
225226
__('Database %1$s has been copied to %2$s.')
226227
);
227228
$GLOBALS['message']->addParam($GLOBALS['db']);
228-
$GLOBALS['message']->addParam($newDatabaseName);
229+
$GLOBALS['message']->addParam($newDatabaseName->getName());
229230
} else {
230231
$GLOBALS['message'] = Message::error();
231232
}
@@ -234,11 +235,11 @@ public function __invoke(ServerRequest $request): void
234235

235236
/* Change database to be used */
236237
if (! $_error && $GLOBALS['move']) {
237-
$GLOBALS['db'] = $newDatabaseName;
238+
$GLOBALS['db'] = $newDatabaseName->getName();
238239
} elseif (! $_error) {
239240
if (isset($_POST['switch_to_new']) && $_POST['switch_to_new'] === 'true') {
240241
$_SESSION['pma_switch_to_new'] = true;
241-
$GLOBALS['db'] = $newDatabaseName;
242+
$GLOBALS['db'] = $newDatabaseName->getName();
242243
} else {
243244
$_SESSION['pma_switch_to_new'] = false;
244245
}
@@ -253,7 +254,7 @@ public function __invoke(ServerRequest $request): void
253254
if ($this->response->isAjax()) {
254255
$this->response->setRequestStatus($GLOBALS['message']->isSuccess());
255256
$this->response->addJSON('message', $GLOBALS['message']);
256-
$this->response->addJSON('newname', $newDatabaseName);
257+
$this->response->addJSON('newname', $newDatabaseName !== null ? $newDatabaseName->getName() : '');
257258
$this->response->addJSON(
258259
'sql_query',
259260
Generator::getMessage('', $GLOBALS['sql_query'])

libraries/classes/Operations.php

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpMyAdmin\Database\Events;
99
use PhpMyAdmin\Database\Routines;
1010
use PhpMyAdmin\Database\Triggers;
11+
use PhpMyAdmin\Dbal\DatabaseName;
1112
use PhpMyAdmin\Engines\Innodb;
1213
use PhpMyAdmin\Partitioning\Partition;
1314
use PhpMyAdmin\Plugins\Export\ExportSql;
@@ -55,7 +56,7 @@ public function __construct(DatabaseInterface $dbi, Relation $relation)
5556
*
5657
* @param string $db database name
5758
*/
58-
public function runProcedureAndFunctionDefinitions($db, string $newDatabaseName): void
59+
public function runProcedureAndFunctionDefinitions($db, DatabaseName $newDatabaseName): void
5960
{
6061
$procedure_names = Routines::getProcedureNames($this->dbi, $db);
6162
if ($procedure_names) {
@@ -95,7 +96,7 @@ public function runProcedureAndFunctionDefinitions($db, string $newDatabaseName)
9596
/**
9697
* Create database before copy
9798
*/
98-
public function createDbBeforeCopy(string $newDatabaseName): void
99+
public function createDbBeforeCopy(DatabaseName $newDatabaseName): void
99100
{
100101
$local_query = 'CREATE DATABASE IF NOT EXISTS '
101102
. Util::backquote($newDatabaseName);
@@ -137,7 +138,7 @@ public function getViewsAndCreateSqlViewStandIn(
137138
array $tables_full,
138139
$export_sql_plugin,
139140
$db,
140-
string $newDatabaseName
141+
DatabaseName $newDatabaseName
141142
) {
142143
$views = [];
143144
foreach (array_keys($tables_full) as $each_table) {
@@ -178,7 +179,7 @@ public function getViewsAndCreateSqlViewStandIn(
178179
*
179180
* @return array SQL queries for the constraints
180181
*/
181-
public function copyTables(array $tables_full, $move, $db, string $newDatabaseName)
182+
public function copyTables(array $tables_full, $move, $db, DatabaseName $newDatabaseName)
182183
{
183184
$sqlContraints = [];
184185
foreach (array_keys($tables_full) as $each_table) {
@@ -215,7 +216,7 @@ public function copyTables(array $tables_full, $move, $db, string $newDatabaseNa
215216
! Table::moveCopy(
216217
$db,
217218
$each_table,
218-
$newDatabaseName,
219+
$newDatabaseName->getName(),
219220
$each_table,
220221
($this_what ?? 'data'),
221222
$move,
@@ -257,7 +258,7 @@ public function copyTables(array $tables_full, $move, $db, string $newDatabaseNa
257258
*
258259
* @param string $db database name
259260
*/
260-
public function runEventDefinitionsForDb($db, string $newDatabaseName): void
261+
public function runEventDefinitionsForDb($db, DatabaseName $newDatabaseName): void
261262
{
262263
$event_names = $this->dbi->fetchResult(
263264
'SELECT EVENT_NAME FROM information_schema.EVENTS WHERE EVENT_SCHEMA= \''
@@ -284,14 +285,14 @@ public function runEventDefinitionsForDb($db, string $newDatabaseName): void
284285
* @param bool $move whether database name is empty or not
285286
* @param string $db database name
286287
*/
287-
public function handleTheViews(array $views, $move, $db, string $newDatabaseName): void
288+
public function handleTheViews(array $views, $move, $db, DatabaseName $newDatabaseName): void
288289
{
289290
// Add DROP IF EXIST to CREATE VIEW query, to remove stand-in VIEW that was created earlier.
290291
foreach ($views as $view) {
291292
$copying_succeeded = Table::moveCopy(
292293
$db,
293294
$view,
294-
$newDatabaseName,
295+
$newDatabaseName->getName(),
295296
$view,
296297
'structure',
297298
$move,
@@ -310,7 +311,7 @@ public function handleTheViews(array $views, $move, $db, string $newDatabaseName
310311
*
311312
* @param string $oldDb Database name before renaming
312313
*/
313-
public function adjustPrivilegesMoveDb($oldDb, string $newDatabaseName): void
314+
public function adjustPrivilegesMoveDb($oldDb, DatabaseName $newDatabaseName): void
314315
{
315316
if (
316317
! $GLOBALS['db_priv'] || ! $GLOBALS['table_priv']
@@ -321,30 +322,30 @@ public function adjustPrivilegesMoveDb($oldDb, string $newDatabaseName): void
321322
}
322323

323324
$this->dbi->selectDb('mysql');
324-
$newDatabaseName = str_replace('_', '\_', $newDatabaseName);
325+
$newName = str_replace('_', '\_', $newDatabaseName->getName());
325326
$oldDb = str_replace('_', '\_', $oldDb);
326327

327328
// For Db specific privileges
328329
$query_db_specific = 'UPDATE ' . Util::backquote('db')
329-
. 'SET Db = \'' . $this->dbi->escapeString($newDatabaseName)
330+
. 'SET Db = \'' . $this->dbi->escapeString($newName)
330331
. '\' where Db = \'' . $this->dbi->escapeString($oldDb) . '\';';
331332
$this->dbi->query($query_db_specific);
332333

333334
// For table specific privileges
334335
$query_table_specific = 'UPDATE ' . Util::backquote('tables_priv')
335-
. 'SET Db = \'' . $this->dbi->escapeString($newDatabaseName)
336+
. 'SET Db = \'' . $this->dbi->escapeString($newName)
336337
. '\' where Db = \'' . $this->dbi->escapeString($oldDb) . '\';';
337338
$this->dbi->query($query_table_specific);
338339

339340
// For column specific privileges
340341
$query_col_specific = 'UPDATE ' . Util::backquote('columns_priv')
341-
. 'SET Db = \'' . $this->dbi->escapeString($newDatabaseName)
342+
. 'SET Db = \'' . $this->dbi->escapeString($newName)
342343
. '\' where Db = \'' . $this->dbi->escapeString($oldDb) . '\';';
343344
$this->dbi->query($query_col_specific);
344345

345346
// For procedures specific privileges
346347
$query_proc_specific = 'UPDATE ' . Util::backquote('procs_priv')
347-
. 'SET Db = \'' . $this->dbi->escapeString($newDatabaseName)
348+
. 'SET Db = \'' . $this->dbi->escapeString($newName)
348349
. '\' where Db = \'' . $this->dbi->escapeString($oldDb) . '\';';
349350
$this->dbi->query($query_proc_specific);
350351

@@ -358,7 +359,7 @@ public function adjustPrivilegesMoveDb($oldDb, string $newDatabaseName): void
358359
*
359360
* @param string $oldDb Database name before copying
360361
*/
361-
public function adjustPrivilegesCopyDb($oldDb, string $newDatabaseName): void
362+
public function adjustPrivilegesCopyDb($oldDb, DatabaseName $newDatabaseName): void
362363
{
363364
if (
364365
! $GLOBALS['db_priv'] || ! $GLOBALS['table_priv']
@@ -369,7 +370,7 @@ public function adjustPrivilegesCopyDb($oldDb, string $newDatabaseName): void
369370
}
370371

371372
$this->dbi->selectDb('mysql');
372-
$newDatabaseName = str_replace('_', '\_', $newDatabaseName);
373+
$newName = str_replace('_', '\_', $newDatabaseName->getName());
373374
$oldDb = str_replace('_', '\_', $oldDb);
374375

375376
$query_db_specific_old = 'SELECT * FROM '
@@ -380,7 +381,7 @@ public function adjustPrivilegesCopyDb($oldDb, string $newDatabaseName): void
380381

381382
foreach ($old_privs_db as $old_priv) {
382383
$newDb_db_privs_query = 'INSERT INTO ' . Util::backquote('db')
383-
. ' VALUES("' . $old_priv[0] . '", "' . $newDatabaseName . '"';
384+
. ' VALUES("' . $old_priv[0] . '", "' . $newName . '"';
384385
$privCount = count($old_priv);
385386
for ($i = 2; $i < $privCount; $i++) {
386387
$newDb_db_privs_query .= ', "' . $old_priv[$i] . '"';
@@ -401,7 +402,7 @@ public function adjustPrivilegesCopyDb($oldDb, string $newDatabaseName): void
401402
foreach ($old_privs_table as $old_priv) {
402403
$newDb_table_privs_query = 'INSERT INTO ' . Util::backquote(
403404
'tables_priv'
404-
) . ' VALUES("' . $old_priv[0] . '", "' . $newDatabaseName . '", "'
405+
) . ' VALUES("' . $old_priv[0] . '", "' . $newName . '", "'
405406
. $old_priv[2] . '", "' . $old_priv[3] . '", "' . $old_priv[4]
406407
. '", "' . $old_priv[5] . '", "' . $old_priv[6] . '", "'
407408
. $old_priv[7] . '");';
@@ -419,7 +420,7 @@ public function adjustPrivilegesCopyDb($oldDb, string $newDatabaseName): void
419420
foreach ($old_privs_col as $old_priv) {
420421
$newDb_col_privs_query = 'INSERT INTO ' . Util::backquote(
421422
'columns_priv'
422-
) . ' VALUES("' . $old_priv[0] . '", "' . $newDatabaseName . '", "'
423+
) . ' VALUES("' . $old_priv[0] . '", "' . $newName . '", "'
423424
. $old_priv[2] . '", "' . $old_priv[3] . '", "' . $old_priv[4]
424425
. '", "' . $old_priv[5] . '", "' . $old_priv[6] . '");';
425426

@@ -436,7 +437,7 @@ public function adjustPrivilegesCopyDb($oldDb, string $newDatabaseName): void
436437
foreach ($old_privs_proc as $old_priv) {
437438
$newDb_proc_privs_query = 'INSERT INTO ' . Util::backquote(
438439
'procs_priv'
439-
) . ' VALUES("' . $old_priv[0] . '", "' . $newDatabaseName . '", "'
440+
) . ' VALUES("' . $old_priv[0] . '", "' . $newName . '", "'
440441
. $old_priv[2] . '", "' . $old_priv[3] . '", "' . $old_priv[4]
441442
. '", "' . $old_priv[5] . '", "' . $old_priv[6] . '", "'
442443
. $old_priv[7] . '");';
@@ -454,7 +455,7 @@ public function adjustPrivilegesCopyDb($oldDb, string $newDatabaseName): void
454455
*
455456
* @param array $sqlConstratints array of sql constraints for the database
456457
*/
457-
public function createAllAccumulatedConstraints(array $sqlConstratints, string $newDatabaseName): void
458+
public function createAllAccumulatedConstraints(array $sqlConstratints, DatabaseName $newDatabaseName): void
458459
{
459460
$this->dbi->selectDb($newDatabaseName);
460461
foreach ($sqlConstratints as $one_query) {
@@ -470,9 +471,9 @@ public function createAllAccumulatedConstraints(array $sqlConstratints, string $
470471
* @param bool $_error whether table rename/copy or not
471472
* @param string $db database name
472473
*/
473-
public function duplicateBookmarks($_error, $db, string $newDatabaseName): void
474+
public function duplicateBookmarks($_error, $db, DatabaseName $newDatabaseName): void
474475
{
475-
if ($_error || $db === $newDatabaseName) {
476+
if ($_error || $db === $newDatabaseName->getName()) {
476477
return;
477478
}
478479

@@ -482,7 +483,7 @@ public function duplicateBookmarks($_error, $db, string $newDatabaseName): void
482483
'query',
483484
];
484485
$where_fields = ['dbase' => $db];
485-
$new_fields = ['dbase' => $newDatabaseName];
486+
$new_fields = ['dbase' => $newDatabaseName->getName()];
486487
Table::duplicateInfo('bookmarkwork', 'bookmark', $get_fields, $where_fields, $new_fields);
487488
}
488489

0 commit comments

Comments
 (0)