1 | <?php |
||||
2 | /* vim: set expandtab sw=4 ts=4 sts=4: */ |
||||
3 | |||||
4 | /** |
||||
5 | * Holds the PhpMyAdmin\Controllers\Server\ServerDatabasesController |
||||
6 | * |
||||
7 | * @package PhpMyAdmin\Controllers |
||||
8 | */ |
||||
9 | declare(strict_types=1); |
||||
10 | |||||
11 | namespace PhpMyAdmin\Controllers\Server; |
||||
12 | |||||
13 | use PhpMyAdmin\Charsets; |
||||
14 | use PhpMyAdmin\CheckUserPrivileges; |
||||
15 | use PhpMyAdmin\Controllers\Controller; |
||||
16 | use PhpMyAdmin\DatabaseInterface; |
||||
17 | use PhpMyAdmin\Message; |
||||
18 | use PhpMyAdmin\Response; |
||||
19 | use PhpMyAdmin\Url; |
||||
20 | use PhpMyAdmin\Util; |
||||
21 | |||||
22 | /** |
||||
23 | * Handles viewing and creating and deleting databases |
||||
24 | * |
||||
25 | * @package PhpMyAdmin\Controllers |
||||
26 | */ |
||||
27 | class ServerDatabasesController extends Controller |
||||
28 | { |
||||
29 | /** |
||||
30 | * @var array array of database details |
||||
31 | */ |
||||
32 | private $_databases; |
||||
33 | /** |
||||
34 | * @var int number of databases |
||||
35 | */ |
||||
36 | private $_database_count; |
||||
37 | /** |
||||
38 | * @var string sort by column |
||||
39 | */ |
||||
40 | private $_sort_by; |
||||
41 | /** |
||||
42 | * @var string sort order of databases |
||||
43 | */ |
||||
44 | private $_sort_order; |
||||
45 | /** |
||||
46 | * @var boolean whether to show database statistics |
||||
47 | */ |
||||
48 | private $_dbstats; |
||||
49 | /** |
||||
50 | * @var int position in list navigation |
||||
51 | */ |
||||
52 | private $_pos; |
||||
53 | |||||
54 | /** |
||||
55 | * Index action |
||||
56 | * |
||||
57 | * @return void |
||||
58 | */ |
||||
59 | public function indexAction() |
||||
60 | { |
||||
61 | $checkUserPrivileges = new CheckUserPrivileges($this->dbi); |
||||
62 | $checkUserPrivileges->getPrivileges(); |
||||
63 | |||||
64 | $response = Response::getInstance(); |
||||
65 | |||||
66 | if (isset($_POST['drop_selected_dbs']) |
||||
67 | && $response->isAjax() |
||||
68 | && ($this->dbi->isSuperuser() || $GLOBALS['cfg']['AllowUserDropDatabase']) |
||||
69 | ) { |
||||
70 | $this->dropDatabasesAction(); |
||||
71 | return; |
||||
72 | } |
||||
73 | |||||
74 | include_once ROOT_PATH . 'libraries/replication.inc.php'; |
||||
75 | |||||
76 | if (isset($_POST['new_db']) |
||||
77 | && $response->isAjax() |
||||
78 | ) { |
||||
79 | $this->createDatabaseAction(); |
||||
80 | return; |
||||
81 | } |
||||
82 | |||||
83 | include_once ROOT_PATH . 'libraries/server_common.inc.php'; |
||||
84 | |||||
85 | $header = $this->response->getHeader(); |
||||
86 | $scripts = $header->getScripts(); |
||||
87 | $scripts->addFile('server_databases.js'); |
||||
88 | |||||
89 | $this->_setSortDetails(); |
||||
90 | $this->_dbstats = empty($_REQUEST['dbstats']) ? false : true; |
||||
91 | $this->_pos = empty($_REQUEST['pos']) ? 0 : (int) $_REQUEST['pos']; |
||||
92 | |||||
93 | /** |
||||
94 | * Gets the databases list |
||||
95 | */ |
||||
96 | if ($GLOBALS['server'] > 0) { |
||||
97 | $this->_databases = $this->dbi->getDatabasesFull( |
||||
98 | null, |
||||
99 | $this->_dbstats, |
||||
100 | DatabaseInterface::CONNECT_USER, |
||||
101 | $this->_sort_by, |
||||
102 | $this->_sort_order, |
||||
103 | $this->_pos, |
||||
104 | true |
||||
105 | ); |
||||
106 | $this->_database_count = count($GLOBALS['dblist']->databases); |
||||
107 | } else { |
||||
108 | $this->_database_count = 0; |
||||
109 | } |
||||
110 | |||||
111 | $_url_params = [ |
||||
112 | 'pos' => $this->_pos, |
||||
113 | 'dbstats' => $this->_dbstats, |
||||
114 | 'sort_by' => $this->_sort_by, |
||||
115 | 'sort_order' => $this->_sort_order, |
||||
116 | ]; |
||||
117 | |||||
118 | $column_order = null; |
||||
119 | $first_database = null; |
||||
120 | |||||
121 | if ($this->_database_count > 0 && ! empty($this->_databases)) { |
||||
122 | $first_database = reset($this->_databases); |
||||
123 | // table col order |
||||
124 | $column_order = $this->_getColumnOrder(); |
||||
125 | $databases = $this->_getHtmlForDatabases($replication_types); |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||||
126 | } |
||||
127 | |||||
128 | $this->response->addHTML($this->template->render('server/databases/index', [ |
||||
129 | 'show_create_db' => $GLOBALS['cfg']['ShowCreateDb'], |
||||
130 | 'is_create_db_priv' => $GLOBALS['is_create_db_priv'], |
||||
131 | 'dbstats' => $this->_dbstats, |
||||
132 | 'db_to_create' => $GLOBALS['db_to_create'], |
||||
133 | 'databases' => isset($databases) ? $databases : null, |
||||
134 | 'dbi' => $this->dbi, |
||||
135 | 'disable_is' => $GLOBALS['cfg']['Server']['DisableIS'], |
||||
136 | 'database_count' => $this->_database_count, |
||||
137 | 'pos' => $this->_pos, |
||||
138 | 'url_params' => $_url_params, |
||||
139 | 'max_db_list' => $GLOBALS['cfg']['MaxDbList'], |
||||
140 | 'sort_by' => $this->_sort_by, |
||||
141 | 'sort_order' => $this->_sort_order, |
||||
142 | 'column_order' => $column_order, |
||||
143 | 'first_database' => $first_database, |
||||
144 | 'master_replication' => $GLOBALS['replication_info']['master']['status'], |
||||
145 | 'slave_replication' => $GLOBALS['replication_info']['slave']['status'], |
||||
146 | 'is_superuser' => $this->dbi->isSuperuser(), |
||||
147 | 'allow_user_drop_database' => $GLOBALS['cfg']['AllowUserDropDatabase'], |
||||
148 | ])); |
||||
149 | } |
||||
150 | |||||
151 | /** |
||||
152 | * Handles creating a new database |
||||
153 | * |
||||
154 | * @return void |
||||
155 | */ |
||||
156 | public function createDatabaseAction() |
||||
157 | { |
||||
158 | /** |
||||
159 | * Builds and executes the db creation sql query |
||||
160 | */ |
||||
161 | $sql_query = 'CREATE DATABASE ' . Util::backquote($_POST['new_db']); |
||||
0 ignored issues
–
show
Are you sure
PhpMyAdmin\Util::backquote($_POST['new_db']) of type array|mixed|string can be used in concatenation ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
162 | if (! empty($_POST['db_collation'])) { |
||||
163 | list($db_charset) = explode('_', $_POST['db_collation']); |
||||
164 | $charsets = Charsets::getMySQLCharsets( |
||||
165 | $this->dbi, |
||||
166 | $GLOBALS['cfg']['Server']['DisableIS'] |
||||
167 | ); |
||||
168 | $collations = Charsets::getMySQLCollations( |
||||
169 | $this->dbi, |
||||
170 | $GLOBALS['cfg']['Server']['DisableIS'] |
||||
171 | ); |
||||
172 | if (in_array($db_charset, $charsets) |
||||
173 | && in_array($_POST['db_collation'], $collations[$db_charset]) |
||||
174 | ) { |
||||
175 | $sql_query .= ' DEFAULT' |
||||
176 | . Util::getCharsetQueryPart($_POST['db_collation']); |
||||
177 | } |
||||
178 | } |
||||
179 | $sql_query .= ';'; |
||||
180 | |||||
181 | $result = $this->dbi->tryQuery($sql_query); |
||||
182 | |||||
183 | if (! $result) { |
||||
184 | // avoid displaying the not-created db name in header or navi panel |
||||
185 | $GLOBALS['db'] = ''; |
||||
186 | |||||
187 | $message = Message::rawError($this->dbi->getError()); |
||||
188 | $this->response->setRequestStatus(false); |
||||
189 | $this->response->addJSON('message', $message); |
||||
190 | } else { |
||||
191 | $GLOBALS['db'] = $_POST['new_db']; |
||||
192 | |||||
193 | $message = Message::success(__('Database %1$s has been created.')); |
||||
194 | $message->addParam($_POST['new_db']); |
||||
195 | $this->response->addJSON('message', $message); |
||||
196 | $this->response->addJSON( |
||||
197 | 'sql_query', |
||||
198 | Util::getMessage(null, $sql_query, 'success') |
||||
199 | ); |
||||
200 | |||||
201 | $this->response->addJSON( |
||||
202 | 'url_query', |
||||
203 | Util::getScriptNameForOption( |
||||
204 | $GLOBALS['cfg']['DefaultTabDatabase'], |
||||
205 | 'database' |
||||
206 | ) |
||||
207 | . Url::getCommon(['db' => $_POST['new_db']]) |
||||
208 | ); |
||||
209 | } |
||||
210 | } |
||||
211 | |||||
212 | /** |
||||
213 | * Handles dropping multiple databases |
||||
214 | * |
||||
215 | * @return void |
||||
216 | */ |
||||
217 | public function dropDatabasesAction() |
||||
218 | { |
||||
219 | if (! isset($_POST['selected_dbs'])) { |
||||
220 | $message = Message::error(__('No databases selected.')); |
||||
221 | } else { |
||||
222 | $action = 'server_databases.php'; |
||||
223 | $err_url = $action . Url::getCommon(); |
||||
224 | |||||
225 | $GLOBALS['submit_mult'] = 'drop_db'; |
||||
226 | $GLOBALS['mult_btn'] = __('Yes'); |
||||
227 | |||||
228 | include ROOT_PATH . 'libraries/mult_submits.inc.php'; |
||||
229 | |||||
230 | if (empty($message)) { // no error message |
||||
231 | $number_of_databases = count($selected); |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
232 | $message = Message::success( |
||||
233 | _ngettext( |
||||
234 | '%1$d database has been dropped successfully.', |
||||
235 | '%1$d databases have been dropped successfully.', |
||||
236 | $number_of_databases |
||||
237 | ) |
||||
238 | ); |
||||
239 | $message->addParam($number_of_databases); |
||||
240 | } |
||||
241 | } |
||||
242 | |||||
243 | if ($message instanceof Message) { |
||||
244 | $this->response->setRequestStatus($message->isSuccess()); |
||||
245 | $this->response->addJSON('message', $message); |
||||
246 | } |
||||
247 | } |
||||
248 | |||||
249 | /** |
||||
250 | * Extracts parameters $sort_order and $sort_by |
||||
251 | * |
||||
252 | * @return void |
||||
253 | */ |
||||
254 | private function _setSortDetails() |
||||
255 | { |
||||
256 | if (empty($_REQUEST['sort_by'])) { |
||||
257 | $this->_sort_by = 'SCHEMA_NAME'; |
||||
258 | } else { |
||||
259 | $sort_by_whitelist = [ |
||||
260 | 'SCHEMA_NAME', |
||||
261 | 'DEFAULT_COLLATION_NAME', |
||||
262 | 'SCHEMA_TABLES', |
||||
263 | 'SCHEMA_TABLE_ROWS', |
||||
264 | 'SCHEMA_DATA_LENGTH', |
||||
265 | 'SCHEMA_INDEX_LENGTH', |
||||
266 | 'SCHEMA_LENGTH', |
||||
267 | 'SCHEMA_DATA_FREE', |
||||
268 | ]; |
||||
269 | if (in_array($_REQUEST['sort_by'], $sort_by_whitelist)) { |
||||
270 | $this->_sort_by = $_REQUEST['sort_by']; |
||||
271 | } else { |
||||
272 | $this->_sort_by = 'SCHEMA_NAME'; |
||||
273 | } |
||||
274 | } |
||||
275 | |||||
276 | if (isset($_REQUEST['sort_order']) |
||||
277 | && mb_strtolower($_REQUEST['sort_order']) == 'desc' |
||||
278 | ) { |
||||
279 | $this->_sort_order = 'desc'; |
||||
280 | } else { |
||||
281 | $this->_sort_order = 'asc'; |
||||
282 | } |
||||
283 | } |
||||
284 | |||||
285 | /** |
||||
286 | * Returns the html for Database List |
||||
287 | * |
||||
288 | * @param array $replication_types replication types |
||||
289 | * |
||||
290 | * @return string |
||||
291 | */ |
||||
292 | private function _getHtmlForDatabases(array $replication_types) |
||||
293 | { |
||||
294 | $first_database = reset($this->_databases); |
||||
295 | // table col order |
||||
296 | $column_order = $this->_getColumnOrder(); |
||||
297 | $dbColumnOrders = []; |
||||
298 | |||||
299 | // calculate aggregate stats to display in footer |
||||
300 | foreach ($this->_databases as $current) { |
||||
301 | $dbColumnOrders[$current['SCHEMA_NAME']] = $this->_getColumnOrder(); |
||||
302 | foreach ($column_order as $stat_name => $stat) { |
||||
303 | if (array_key_exists($stat_name, $current) |
||||
304 | && is_numeric($stat['footer']) |
||||
305 | ) { |
||||
306 | $column_order[$stat_name]['footer'] += $current[$stat_name]; |
||||
307 | $dbColumnOrders[$current['SCHEMA_NAME']][$stat_name]['footer'] = $current[$stat_name]; |
||||
308 | } |
||||
309 | } |
||||
310 | } |
||||
311 | |||||
312 | $values = []; |
||||
313 | $units = []; |
||||
314 | foreach ($column_order as $stat_name => $stat) { |
||||
315 | if (array_key_exists($stat_name, $first_database)) { |
||||
316 | if ($stat['format'] == 'byte') { |
||||
317 | $byte_format = Util::formatByteDown($stat['footer'], 3, 1); |
||||
318 | $values[$stat_name] = $byte_format[0]; |
||||
319 | $units[$stat_name] = $byte_format[1]; |
||||
320 | } elseif ($stat['format'] == 'number') { |
||||
321 | $values[$stat_name] = Util::formatNumber($stat['footer'], 0); |
||||
322 | } else { |
||||
323 | $values[$stat_name] = htmlentities($stat['footer'], 0); |
||||
324 | } |
||||
325 | } |
||||
326 | } |
||||
327 | |||||
328 | $_url_params = [ |
||||
0 ignored issues
–
show
|
|||||
329 | 'pos' => $this->_pos, |
||||
330 | 'dbstats' => $this->_dbstats, |
||||
331 | 'sort_by' => $this->_sort_by, |
||||
332 | 'sort_order' => $this->_sort_order, |
||||
333 | ]; |
||||
334 | |||||
335 | $html = $this->_getHtmlForTableBody($dbColumnOrders, $replication_types); |
||||
336 | |||||
337 | $html .= $this->template->render('server/databases/databases_footer', [ |
||||
338 | 'column_order' => $column_order, |
||||
339 | 'first_database' => $first_database, |
||||
340 | 'master_replication' => $GLOBALS['replication_info']['master']['status'], |
||||
341 | 'slave_replication' => $GLOBALS['replication_info']['slave']['status'], |
||||
342 | 'database_count' => $this->_database_count, |
||||
343 | 'is_superuser' => $this->dbi->isSuperuser(), |
||||
344 | 'allow_user_drop_database' => $GLOBALS['cfg']['AllowUserDropDatabase'], |
||||
345 | 'pma_theme_image' => $GLOBALS['pmaThemeImage'], |
||||
346 | 'text_dir' => $GLOBALS['text_dir'], |
||||
347 | 'dbstats' => $this->_dbstats, |
||||
348 | 'values' => $values, |
||||
349 | 'units' => $units, |
||||
350 | ]); |
||||
351 | |||||
352 | return $html; |
||||
353 | } |
||||
354 | |||||
355 | /** |
||||
356 | * Prepares the $column_order array |
||||
357 | * |
||||
358 | * @return array |
||||
359 | */ |
||||
360 | private function _getColumnOrder() |
||||
361 | { |
||||
362 | $column_order = []; |
||||
363 | $column_order['DEFAULT_COLLATION_NAME'] = [ |
||||
364 | 'disp_name' => __('Collation'), |
||||
365 | 'description_function' => [ |
||||
366 | Charsets::class, |
||||
367 | 'getCollationDescr', |
||||
368 | ], |
||||
369 | 'format' => 'string', |
||||
370 | 'footer' => '', |
||||
371 | ]; |
||||
372 | $column_order['SCHEMA_TABLES'] = [ |
||||
373 | 'disp_name' => __('Tables'), |
||||
374 | 'format' => 'number', |
||||
375 | 'footer' => 0, |
||||
376 | ]; |
||||
377 | $column_order['SCHEMA_TABLE_ROWS'] = [ |
||||
378 | 'disp_name' => __('Rows'), |
||||
379 | 'format' => 'number', |
||||
380 | 'footer' => 0, |
||||
381 | ]; |
||||
382 | $column_order['SCHEMA_DATA_LENGTH'] = [ |
||||
383 | 'disp_name' => __('Data'), |
||||
384 | 'format' => 'byte', |
||||
385 | 'footer' => 0, |
||||
386 | ]; |
||||
387 | $column_order['SCHEMA_INDEX_LENGTH'] = [ |
||||
388 | 'disp_name' => __('Indexes'), |
||||
389 | 'format' => 'byte', |
||||
390 | 'footer' => 0, |
||||
391 | ]; |
||||
392 | $column_order['SCHEMA_LENGTH'] = [ |
||||
393 | 'disp_name' => __('Total'), |
||||
394 | 'format' => 'byte', |
||||
395 | 'footer' => 0, |
||||
396 | ]; |
||||
397 | $column_order['SCHEMA_DATA_FREE'] = [ |
||||
398 | 'disp_name' => __('Overhead'), |
||||
399 | 'format' => 'byte', |
||||
400 | 'footer' => 0, |
||||
401 | ]; |
||||
402 | |||||
403 | return $column_order; |
||||
404 | } |
||||
405 | |||||
406 | /** |
||||
407 | * Returns the html for Database List |
||||
408 | * |
||||
409 | * @param array $dbColumnOrders databases column order |
||||
410 | * @param array $replicationTypes replication types |
||||
411 | * |
||||
412 | * @return string |
||||
413 | */ |
||||
414 | private function _getHtmlForTableBody(array $dbColumnOrders, array $replicationTypes) |
||||
415 | { |
||||
416 | $html = '<tbody>' . "\n"; |
||||
417 | |||||
418 | foreach ($this->_databases as $current) { |
||||
419 | $tr_class = ' db-row'; |
||||
420 | if ($this->dbi->isSystemSchema($current['SCHEMA_NAME'], true)) { |
||||
421 | $tr_class .= ' noclick'; |
||||
422 | } |
||||
423 | |||||
424 | $generated_html = $this->_buildHtmlForDb( |
||||
425 | $current, |
||||
426 | $dbColumnOrders[$current['SCHEMA_NAME']], |
||||
427 | $replicationTypes, |
||||
428 | $GLOBALS['replication_info'], |
||||
429 | $tr_class |
||||
430 | ); |
||||
431 | $html .= $generated_html; |
||||
432 | } // end foreach ($this->_databases as $key => $current) |
||||
433 | $html .= '</tbody>'; |
||||
434 | |||||
435 | return $html; |
||||
436 | } |
||||
437 | |||||
438 | /** |
||||
439 | * Builds the HTML for one database to display in the list |
||||
440 | * of databases from server_databases.php |
||||
441 | * |
||||
442 | * @param array $current current database |
||||
443 | * @param array $column_order column order |
||||
444 | * @param array $replication_types replication types |
||||
445 | * @param array $replication_info replication info |
||||
446 | * @param string $tr_class HTMl class for the row |
||||
447 | * |
||||
448 | * @return string |
||||
449 | */ |
||||
450 | public function _buildHtmlForDb( |
||||
451 | array $current, |
||||
452 | array $column_order, |
||||
453 | array $replication_types, |
||||
454 | array $replication_info, |
||||
455 | $tr_class = '' |
||||
456 | ) { |
||||
457 | $master_replication = $slave_replication = ''; |
||||
458 | foreach ($replication_types as $type) { |
||||
459 | if ($replication_info[$type]['status']) { |
||||
460 | $out = ''; |
||||
461 | $key = array_search( |
||||
462 | $current["SCHEMA_NAME"], |
||||
463 | $replication_info[$type]['Ignore_DB'] |
||||
464 | ); |
||||
465 | if (strlen((string) $key) > 0) { |
||||
466 | $out = Util::getIcon( |
||||
467 | 's_cancel', |
||||
468 | __('Not replicated') |
||||
469 | ); |
||||
470 | } else { |
||||
471 | $key = array_search( |
||||
472 | $current["SCHEMA_NAME"], |
||||
473 | $replication_info[$type]['Do_DB'] |
||||
474 | ); |
||||
475 | |||||
476 | if (strlen((string) $key) > 0 |
||||
477 | || count($replication_info[$type]['Do_DB']) == 0 |
||||
478 | ) { |
||||
479 | // if ($key != null) did not work for index "0" |
||||
480 | $out = Util::getIcon( |
||||
481 | 's_success', |
||||
482 | __('Replicated') |
||||
483 | ); |
||||
484 | } |
||||
485 | } |
||||
486 | |||||
487 | if ($type == 'master') { |
||||
488 | $master_replication = $out; |
||||
489 | } elseif ($type == 'slave') { |
||||
490 | $slave_replication = $out; |
||||
491 | } |
||||
492 | } |
||||
493 | } |
||||
494 | |||||
495 | $values = []; |
||||
496 | $units = []; |
||||
497 | foreach ($column_order as $stat_name => $stat) { |
||||
498 | if (array_key_exists($stat_name, $current)) { |
||||
499 | if ($stat['format'] == 'byte') { |
||||
500 | $byte_format = Util::formatByteDown($stat['footer'], 3, 1); |
||||
501 | $values[$stat_name] = $byte_format[0]; |
||||
502 | $units[$stat_name] = $byte_format[1]; |
||||
503 | } elseif ($stat['format'] == 'number') { |
||||
504 | $values[$stat_name] = Util::formatNumber($stat['footer'], 0); |
||||
505 | } else { |
||||
506 | $values[$stat_name] = htmlentities($stat['footer'], 0); |
||||
507 | } |
||||
508 | } |
||||
509 | } |
||||
510 | |||||
511 | return $this->template->render('server/databases/table_row', [ |
||||
512 | 'current' => $current, |
||||
513 | 'tr_class' => $tr_class, |
||||
514 | 'column_order' => $column_order, |
||||
515 | 'master_replication_status' => $GLOBALS['replication_info']['master']['status'], |
||||
516 | 'master_replication' => $master_replication, |
||||
517 | 'slave_replication_status' => $GLOBALS['replication_info']['slave']['status'], |
||||
518 | 'slave_replication' => $slave_replication, |
||||
519 | 'is_superuser' => $this->dbi->isSuperuser(), |
||||
520 | 'allow_user_drop_database' => $GLOBALS['cfg']['AllowUserDropDatabase'], |
||||
521 | 'is_system_schema' => $this->dbi->isSystemSchema($current['SCHEMA_NAME'], true), |
||||
522 | 'default_tab_database' => $GLOBALS['cfg']['DefaultTabDatabase'], |
||||
523 | 'values' => $values, |
||||
524 | 'units' => $units, |
||||
525 | ]); |
||||
526 | } |
||||
527 | } |
||||
528 |