Welcome to mirror list, hosted at ThFree Co, Russian Federation.

OperationsController.php « Database « Controllers « classes « libraries - github.com/phpmyadmin/phpmyadmin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 031a8096f42b63b9b138e8f962c41b9e6ce1ef35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Controllers\Database;

use PhpMyAdmin\Charsets;
use PhpMyAdmin\CheckUserPrivileges;
use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\ConfigStorage\RelationCleanup;
use PhpMyAdmin\Controllers\AbstractController;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Dbal\DatabaseName;
use PhpMyAdmin\Dbal\InvalidDatabaseName;
use PhpMyAdmin\Html\Generator;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\Message;
use PhpMyAdmin\Operations;
use PhpMyAdmin\Plugins;
use PhpMyAdmin\Query\Utilities;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Template;
use PhpMyAdmin\Url;
use PhpMyAdmin\Util;

use function __;
use function count;
use function mb_strtolower;
use function strlen;

/**
 * Handles miscellaneous database operations.
 */
class OperationsController extends AbstractController
{
    /** @var Operations */
    private $operations;

    /** @var CheckUserPrivileges */
    private $checkUserPrivileges;

    /** @var Relation */
    private $relation;

    /** @var RelationCleanup */
    private $relationCleanup;

    /** @var DatabaseInterface */
    private $dbi;

    public function __construct(
        ResponseRenderer $response,
        Template $template,
        Operations $operations,
        CheckUserPrivileges $checkUserPrivileges,
        Relation $relation,
        RelationCleanup $relationCleanup,
        DatabaseInterface $dbi
    ) {
        parent::__construct($response, $template);
        $this->operations = $operations;
        $this->checkUserPrivileges = $checkUserPrivileges;
        $this->relation = $relation;
        $this->relationCleanup = $relationCleanup;
        $this->dbi = $dbi;
    }

    public function __invoke(ServerRequest $request): void
    {
        $GLOBALS['server'] = $GLOBALS['server'] ?? null;
        $GLOBALS['move'] = $GLOBALS['move'] ?? null;
        $GLOBALS['message'] = $GLOBALS['message'] ?? null;
        $GLOBALS['tables_full'] = $GLOBALS['tables_full'] ?? null;
        $GLOBALS['errorUrl'] = $GLOBALS['errorUrl'] ?? null;
        $GLOBALS['export_sql_plugin'] = $GLOBALS['export_sql_plugin'] ?? null;
        $GLOBALS['views'] = $GLOBALS['views'] ?? null;
        $GLOBALS['sqlConstratints'] = $GLOBALS['sqlConstratints'] ?? null;
        $GLOBALS['local_query'] = $GLOBALS['local_query'] ?? null;
        $GLOBALS['reload'] = $GLOBALS['reload'] ?? null;
        $GLOBALS['urlParams'] = $GLOBALS['urlParams'] ?? null;
        $GLOBALS['tables'] = $GLOBALS['tables'] ?? null;
        $GLOBALS['total_num_tables'] = $GLOBALS['total_num_tables'] ?? null;
        $GLOBALS['sub_part'] = $GLOBALS['sub_part'] ?? null;
        $GLOBALS['tooltip_truename'] = $GLOBALS['tooltip_truename'] ?? null;
        $GLOBALS['db_collation'] = $GLOBALS['db_collation'] ?? null;
        $GLOBALS['tooltip_aliasname'] = $GLOBALS['tooltip_aliasname'] ?? null;
        $GLOBALS['pos'] = $GLOBALS['pos'] ?? null;
        $GLOBALS['is_information_schema'] = $GLOBALS['is_information_schema'] ?? null;
        $GLOBALS['single_table'] = $GLOBALS['single_table'] ?? null;
        $GLOBALS['num_tables'] = $GLOBALS['num_tables'] ?? null;

        $this->checkUserPrivileges->getPrivileges();

        $this->addScriptFiles(['database/operations.js']);

        $GLOBALS['sql_query'] = '';

        /**
         * Rename/move or copy database
         */
        if (strlen($GLOBALS['db']) > 0 && (! empty($_POST['db_rename']) || ! empty($_POST['db_copy']))) {
            if (! empty($_POST['db_rename'])) {
                $GLOBALS['move'] = true;
            } else {
                $GLOBALS['move'] = false;
            }

            try {
                $newDatabaseName = DatabaseName::fromValue($request->getParsedBodyParam('newname'));
                if ($this->dbi->getLowerCaseNames() === '1') {
                    $newDatabaseName = DatabaseName::fromValue(mb_strtolower($newDatabaseName->getName()));
                }
            } catch (InvalidDatabaseName $exception) {
                $newDatabaseName = null;
                $GLOBALS['message'] = Message::error($exception->getMessage());
            }

            if ($newDatabaseName !== null) {
                if ($newDatabaseName->getName() === $_REQUEST['db']) {
                    $GLOBALS['message'] = Message::error(
                        __('Cannot copy database to the same name. Change the name and try again.')
                    );
                } else {
                    $_error = false;
                    if ($GLOBALS['move'] || ! empty($_POST['create_database_before_copying'])) {
                        $this->operations->createDbBeforeCopy($newDatabaseName);
                    }

                    // here I don't use DELIMITER because it's not part of the
                    // language; I have to send each statement one by one

                    // to avoid selecting alternatively the current and new db
                    // we would need to modify the CREATE definitions to qualify
                    // the db name
                    $this->operations->runProcedureAndFunctionDefinitions($GLOBALS['db'], $newDatabaseName);

                    // go back to current db, just in case
                    $this->dbi->selectDb($GLOBALS['db']);

                    $GLOBALS['tables_full'] = $this->dbi->getTablesFull($GLOBALS['db']);

                    // remove all foreign key constraints, otherwise we can get errors
                    $GLOBALS['export_sql_plugin'] = Plugins::getPlugin('export', 'sql', [
                        'export_type' => 'database',
                        'single_table' => isset($GLOBALS['single_table']),
                    ]);

                    // create stand-in tables for views
                    $GLOBALS['views'] = $this->operations->getViewsAndCreateSqlViewStandIn(
                        $GLOBALS['tables_full'],
                        $GLOBALS['export_sql_plugin'],
                        $GLOBALS['db'],
                        $newDatabaseName
                    );

                    // copy tables
                    $GLOBALS['sqlConstratints'] = $this->operations->copyTables(
                        $GLOBALS['tables_full'],
                        $GLOBALS['move'],
                        $GLOBALS['db'],
                        $newDatabaseName
                    );

                    // handle the views
                    if (! $_error) {
                        $this->operations->handleTheViews(
                            $GLOBALS['views'],
                            $GLOBALS['move'],
                            $GLOBALS['db'],
                            $newDatabaseName
                        );
                    }

                    unset($GLOBALS['views']);

                    // now that all tables exist, create all the accumulated constraints
                    if (! $_error && count($GLOBALS['sqlConstratints']) > 0) {
                        $this->operations->createAllAccumulatedConstraints(
                            $GLOBALS['sqlConstratints'],
                            $newDatabaseName
                        );
                    }

                    unset($GLOBALS['sqlConstratints']);

                    if ($this->dbi->getVersion() >= 50100) {
                        // here DELIMITER is not used because it's not part of the
                        // language; each statement is sent one by one

                        $this->operations->runEventDefinitionsForDb($GLOBALS['db'], $newDatabaseName);
                    }

                    // go back to current db, just in case
                    $this->dbi->selectDb($GLOBALS['db']);

                    // Duplicate the bookmarks for this db (done once for each db)
                    $this->operations->duplicateBookmarks($_error, $GLOBALS['db'], $newDatabaseName);

                    if (! $_error && $GLOBALS['move']) {
                        if (isset($_POST['adjust_privileges']) && ! empty($_POST['adjust_privileges'])) {
                            $this->operations->adjustPrivilegesMoveDb($GLOBALS['db'], $newDatabaseName);
                        }

                        /**
                         * cleanup pmadb stuff for this db
                         */
                        $this->relationCleanup->database($GLOBALS['db']);

                        // if someday the RENAME DATABASE reappears, do not DROP
                        $GLOBALS['local_query'] = 'DROP DATABASE '
                            . Util::backquote($GLOBALS['db']) . ';';
                        $GLOBALS['sql_query'] .= "\n" . $GLOBALS['local_query'];
                        $this->dbi->query($GLOBALS['local_query']);

                        $GLOBALS['message'] = Message::success(
                            __('Database %1$s has been renamed to %2$s.')
                        );
                        $GLOBALS['message']->addParam($GLOBALS['db']);
                        $GLOBALS['message']->addParam($newDatabaseName->getName());
                    } elseif (! $_error) {
                        if (isset($_POST['adjust_privileges']) && ! empty($_POST['adjust_privileges'])) {
                            $this->operations->adjustPrivilegesCopyDb($GLOBALS['db'], $newDatabaseName);
                        }

                        $GLOBALS['message'] = Message::success(
                            __('Database %1$s has been copied to %2$s.')
                        );
                        $GLOBALS['message']->addParam($GLOBALS['db']);
                        $GLOBALS['message']->addParam($newDatabaseName->getName());
                    } else {
                        $GLOBALS['message'] = Message::error();
                    }

                    $GLOBALS['reload'] = true;

                    /* Change database to be used */
                    if (! $_error && $GLOBALS['move']) {
                        $GLOBALS['db'] = $newDatabaseName->getName();
                    } elseif (! $_error) {
                        if (isset($_POST['switch_to_new']) && $_POST['switch_to_new'] === 'true') {
                            $_SESSION['pma_switch_to_new'] = true;
                            $GLOBALS['db'] = $newDatabaseName->getName();
                        } else {
                            $_SESSION['pma_switch_to_new'] = false;
                        }
                    }
                }
            }

            /**
             * Database has been successfully renamed/moved.  If in an Ajax request,
             * generate the output with {@link ResponseRenderer} and exit
             */
            if ($this->response->isAjax()) {
                $this->response->setRequestStatus($GLOBALS['message']->isSuccess());
                $this->response->addJSON('message', $GLOBALS['message']);
                $this->response->addJSON('newname', $newDatabaseName !== null ? $newDatabaseName->getName() : '');
                $this->response->addJSON(
                    'sql_query',
                    Generator::getMessage('', $GLOBALS['sql_query'])
                );
                $this->response->addJSON('db', $GLOBALS['db']);

                return;
            }
        }

        $relationParameters = $this->relation->getRelationParameters();

        /**
         * Check if comments were updated
         * (must be done before displaying the menu tabs)
         */
        if (isset($_POST['comment'])) {
            $this->relation->setDbComment($GLOBALS['db'], $_POST['comment']);
        }

        $this->checkParameters(['db']);

        $GLOBALS['errorUrl'] = Util::getScriptNameForOption($GLOBALS['cfg']['DefaultTabDatabase'], 'database');
        $GLOBALS['errorUrl'] .= Url::getCommon(['db' => $GLOBALS['db']], '&');

        if (! $this->hasDatabase()) {
            return;
        }

        $GLOBALS['urlParams']['goto'] = Url::getFromRoute('/database/operations');

        // Gets the database structure
        $GLOBALS['sub_part'] = '_structure';

        [
            $GLOBALS['tables'],
            $GLOBALS['num_tables'],
            $GLOBALS['total_num_tables'],
            $GLOBALS['sub_part'],,
            $isSystemSchema,
            $GLOBALS['tooltip_truename'],
            $GLOBALS['tooltip_aliasname'],
            $GLOBALS['pos'],
        ] = Util::getDbInfo($GLOBALS['db'], $GLOBALS['sub_part']);

        $oldMessage = '';
        if (isset($GLOBALS['message'])) {
            $oldMessage = Generator::getMessage($GLOBALS['message'], $GLOBALS['sql_query']);
            unset($GLOBALS['message']);
        }

        $GLOBALS['db_collation'] = $this->dbi->getDbCollation($GLOBALS['db']);
        $GLOBALS['is_information_schema'] = Utilities::isSystemSchema($GLOBALS['db']);

        if ($GLOBALS['is_information_schema']) {
            return;
        }

        $databaseComment = '';
        if ($relationParameters->columnCommentsFeature !== null) {
            $databaseComment = $this->relation->getDbComment($GLOBALS['db']);
        }

        $hasAdjustPrivileges = $GLOBALS['db_priv'] && $GLOBALS['table_priv']
            && $GLOBALS['col_priv'] && $GLOBALS['proc_priv'] && $GLOBALS['is_reload_priv'];

        $isDropDatabaseAllowed = ($this->dbi->isSuperUser() || $GLOBALS['cfg']['AllowUserDropDatabase'])
            && ! $isSystemSchema && $GLOBALS['db'] !== 'mysql';

        $switchToNew = isset($_SESSION['pma_switch_to_new']) && $_SESSION['pma_switch_to_new'];

        $charsets = Charsets::getCharsets($this->dbi, $GLOBALS['cfg']['Server']['DisableIS']);
        $collations = Charsets::getCollations($this->dbi, $GLOBALS['cfg']['Server']['DisableIS']);

        if (! $relationParameters->hasAllFeatures() && $GLOBALS['cfg']['PmaNoRelation_DisableWarning'] == false) {
            $GLOBALS['message'] = Message::notice(
                __(
                    'The phpMyAdmin configuration storage has been deactivated. %sFind out why%s.'
                )
            );
            $GLOBALS['message']->addParamHtml(
                '<a href="' . Url::getFromRoute('/check-relations')
                . '" data-post="' . Url::getCommon(['db' => $GLOBALS['db']]) . '">'
            );
            $GLOBALS['message']->addParamHtml('</a>');
            /* Show error if user has configured something, notice elsewhere */
            if (! empty($GLOBALS['cfg']['Servers'][$GLOBALS['server']]['pmadb'])) {
                $GLOBALS['message']->isError(true);
            }
        }

        $this->render('database/operations/index', [
            'message' => $oldMessage,
            'db' => $GLOBALS['db'],
            'has_comment' => $relationParameters->columnCommentsFeature !== null,
            'db_comment' => $databaseComment,
            'db_collation' => $GLOBALS['db_collation'],
            'has_adjust_privileges' => $hasAdjustPrivileges,
            'is_drop_database_allowed' => $isDropDatabaseAllowed,
            'switch_to_new' => $switchToNew,
            'charsets' => $charsets,
            'collations' => $collations,
        ]);
    }
}