3.1.3.1
[phpmyadmin/crack.git] / db_operations.php
blob61b4dc8c7464725435b2d4714f40e6f1d64ea258
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * handles miscellaneous db operations:
5 * - move/rename
6 * - copy
7 * - changing collation
8 * - changing comment
9 * - adding tables
10 * - viewing PDF schemas
12 * @version $Id$
15 /**
16 * requirements
18 require_once './libraries/common.inc.php';
19 require_once './libraries/Table.class.php';
20 require_once './libraries/mysql_charsets.lib.php';
22 // add blobstreaming library functions
23 require_once "./libraries/blobstreaming.lib.php";
25 /**
26 * Rename/move or copy database
28 if (strlen($db) && (! empty($db_rename) || ! empty($db_copy))) {
30 if (! empty($db_rename)) {
31 $move = true;
32 } else {
33 $move = false;
36 if (!isset($newname) || !strlen($newname)) {
37 $message = PMA_Message::error('strDatabaseEmpty');
38 } else {
39 $sql_query = ''; // in case target db exists
40 $_error = false;
41 if ($move ||
42 (isset($create_database_before_copying) && $create_database_before_copying)) {
43 // lower_case_table_names=1 `DB` becomes `db`
44 $lower_case_table_names = PMA_DBI_fetch_value('SHOW VARIABLES LIKE "lower_case_table_names"', 0, 1);
45 if ($lower_case_table_names === '1') {
46 $newname = strtolower($newname);
49 $local_query = 'CREATE DATABASE ' . PMA_backquote($newname);
50 if (isset($db_collation)) {
51 $local_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation);
53 $local_query .= ';';
54 $sql_query = $local_query;
55 PMA_DBI_query($local_query);
57 // rebuild the database list because PMA_Table::moveCopy
58 // checks in this list if the target db exists
59 $GLOBALS['pma']->databases->build();
62 if (isset($GLOBALS['add_constraints'])) {
63 $GLOBALS['sql_constraints_query_full_db'] = '';
66 $tables_full = PMA_DBI_get_tables_full($db);
67 $views = array();
68 foreach ($tables_full as $each_table => $tmp) {
69 // to be able to rename a db containing views, we
70 // first collect in $views all the views we find and we
71 // will handle them after the tables
72 /**
73 * @todo support a view of a view
74 * @todo support triggers
76 if (PMA_Table::isView($db, $each_table)) {
77 $views[] = $each_table;
78 continue;
81 $back = $sql_query;
82 $sql_query = '';
84 // value of $what for this table only
85 $this_what = $what;
87 // do not copy the data from a Merge table
88 // note: on the calling FORM, 'data' means 'structure and data'
89 if ($tables_full[$each_table]['Engine'] == 'MRG_MyISAM') {
90 if ($this_what == 'data') {
91 $this_what = 'structure';
93 if ($this_what == 'dataonly') {
94 $this_what = 'nocopy';
98 if ($this_what != 'nocopy') {
99 if (! PMA_Table::moveCopy($db, $each_table, $newname, $each_table,
100 isset($this_what) ? $this_what : 'data', $move, 'db_copy'))
102 $_error = true;
103 // $sql_query is filled by PMA_Table::moveCopy()
104 $sql_query = $back . $sql_query;
105 break;
107 if (isset($GLOBALS['add_constraints'])) {
108 $GLOBALS['sql_constraints_query_full_db'] .= $GLOBALS['sql_constraints_query'];
109 unset($GLOBALS['sql_constraints_query']);
112 // $sql_query is filled by PMA_Table::moveCopy()
113 $sql_query = $back . $sql_query;
114 } // end (foreach)
115 unset($each_table);
117 // handle the views
118 if (! $_error) {
119 foreach ($views as $view) {
120 if (! PMA_Table::moveCopy($db, $view, $newname, $view,
121 'structure', $move, 'db_copy')) {
122 $_error = true;
123 break;
127 unset($view, $views);
129 // now that all tables exist, create all the accumulated constraints
130 if (! $_error && isset($GLOBALS['add_constraints'])) {
132 * @todo this works with mysqli but not with mysql, because
133 * mysql extension does not accept more than one statement; maybe
134 * interface with the sql import plugin that handles statement delimiter
136 PMA_DBI_query($GLOBALS['sql_constraints_query_full_db']);
138 // and prepare to display them
139 $GLOBALS['sql_query'] .= "\n" . $GLOBALS['sql_constraints_query_full_db'];
140 unset($GLOBALS['sql_constraints_query_full_db']);
143 if (PMA_MYSQL_INT_VERSION >= 50000) {
144 // here I don't use DELIMITER because it's not part of the
145 // language; I have to send each statement one by one
147 // to avoid selecting alternatively the current and new db
148 // we would need to modify the CREATE definitions to qualify
149 // the db name
150 $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
151 if ($procedure_names) {
152 foreach($procedure_names as $procedure_name) {
153 PMA_DBI_select_db($db);
154 $tmp_query = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure_name);
155 // collect for later display
156 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
157 PMA_DBI_select_db($newname);
158 PMA_DBI_query($tmp_query);
162 $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
163 if ($function_names) {
164 foreach($function_names as $function_name) {
165 PMA_DBI_select_db($db);
166 $tmp_query = PMA_DBI_get_definition($db, 'FUNCTION', $function_name);
167 // collect for later display
168 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
169 PMA_DBI_select_db($newname);
170 PMA_DBI_query($tmp_query);
174 // go back to current db, just in case
175 PMA_DBI_select_db($db);
177 // Duplicate the bookmarks for this db (done once for each db)
178 if (! $_error && $db != $newname) {
179 $get_fields = array('user', 'label', 'query');
180 $where_fields = array('dbase' => $db);
181 $new_fields = array('dbase' => $newname);
182 PMA_Table::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
183 $where_fields, $new_fields);
186 if (! $_error && $move) {
187 // cleanup pmadb stuff for this db
188 require_once './libraries/relation_cleanup.lib.php';
189 PMA_relationsCleanupDatabase($db);
191 // if someday the RENAME DATABASE reappears, do not DROP
192 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
193 $sql_query .= "\n" . $local_query;
194 PMA_DBI_query($local_query);
196 $message = PMA_Message::success('strRenameDatabaseOK');
197 $message->addParam($db);
198 $message->addParam($newname);
199 } elseif (! $_error) {
200 $message = PMA_Message::success('strCopyDatabaseOK');
201 $message->addParam($db);
202 $message->addParam($newname);
204 $reload = true;
206 /* Change database to be used */
207 if (! $_error && $move) {
208 $db = $newname;
209 } elseif (! $_error) {
210 if (isset($switch_to_new) && $switch_to_new == 'true') {
211 PMA_setCookie('pma_switch_to_new', 'true');
212 $db = $newname;
213 } else {
214 PMA_setCookie('pma_switch_to_new', '');
218 if ($_error && ! isset($message)) {
219 $message = PMA_Message::error();
225 * Enable/Disable/Repair BLOB Repository Monitoring for current database
227 if (strlen($db) > 0 && !empty($db_blob_streaming_op))
229 // load PMA_Config
230 $PMA_Config = $_SESSION['PMA_Config'];
232 if (!empty($PMA_Config))
234 if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
236 // if Blobstreaming plugins exist, begin checking for Blobstreaming tables
237 if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
239 $bs_tables = $PMA_Config->get('BLOBSTREAMABLE_DATABASES');
240 $bs_tables = $bs_tables[$db];
242 $oneBSTableExists = FALSE;
244 // check if at least one blobstreaming table exists
245 foreach ($bs_tables as $table_key=>$tbl)
246 if ($bs_tables[$table_key]['Exists'])
248 $oneBSTableExists = TRUE;
249 break;
252 switch ($db_blob_streaming_op)
254 // enable BLOB repository monitoring
255 case "enable":
256 // if blobstreaming tables do not exist, create them
257 if (!$oneBSTableExists)
258 PMA_BS_CreateTables($db);
259 break;
260 // disable BLOB repository monitoring
261 case "disable":
262 // if at least one blobstreaming table exists, execute drop
263 if ($oneBSTableExists)
264 PMA_BS_DropTables($db);
265 break;
266 // repair BLOB repository
267 case "repair":
268 // check if a blobstreaming table is missing
269 foreach ($bs_tables as $table_key=>$tbl)
270 if (!$bs_tables[$table_key]['Exists'])
272 PMA_DBI_select_db($db);
273 PMA_DBI_query(PMA_BS_GetTableStruct($table_key));
277 // refresh side menu
278 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . 'db_operations.php?' . PMA_generate_common_url ('','', '&') . (isset($db) ? '&db=' . urlencode($db) : '') . (isset($token) ? '&token=' . urlencode($token) : '') . (isset($goto) ? '&goto=' . urlencode($goto) : '') . 'reload=1&purge=1');
279 } // end if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
280 } // end if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
285 * Settings for relations stuff
288 require_once './libraries/relation.lib.php';
289 $cfgRelation = PMA_getRelationsParam();
292 * Check if comments were updated
293 * (must be done before displaying the menu tabs)
295 if (isset($_REQUEST['comment'])) {
296 PMA_setDbComment($db, $comment);
300 * Prepares the tables list if the user where not redirected to this script
301 * because there is no table in the database ($is_info is true)
303 if (empty($is_info)) {
304 require './libraries/db_common.inc.php';
305 $url_query .= '&amp;goto=db_operations.php';
307 // Gets the database structure
308 $sub_part = '_structure';
309 require './libraries/db_info.inc.php';
310 echo "\n";
312 if (isset($message)) {
313 PMA_showMessage($message, $sql_query);
314 unset($message);
318 $db_collation = PMA_getDbCollation($db);
319 if ($db == 'information_schema') {
320 $is_information_schema = true;
321 } else {
322 $is_information_schema = false;
325 if (!$is_information_schema) {
327 require './libraries/display_create_table.lib.php';
329 if ($cfgRelation['commwork']) {
331 * database comment
334 <form method="post" action="db_operations.php">
335 <?php echo PMA_generate_common_hidden_inputs($db); ?>
336 <fieldset>
337 <legend>
338 <?php echo PMA_getIcon('b_comment.png', $strDBComment, false, true); ?>
339 </legend>
340 <input type="text" name="comment" class="textfield" size="30"
341 value="<?php
342 echo htmlspecialchars(PMA_getDBComment($db)); ?>" />
343 <input type="submit" value="<?php echo $strGo; ?>" />
344 </fieldset>
345 </form>
346 <?php
349 * rename database
352 <form method="post" action="db_operations.php"
353 onsubmit="return emptyFormElements(this, 'newname')">
354 <?php
355 if (isset($db_collation)) {
356 echo '<input type="hidden" name="db_collation" value="' . $db_collation
357 .'" />' . "\n";
360 <input type="hidden" name="what" value="data" />
361 <input type="hidden" name="db_rename" value="true" />
362 <?php echo PMA_generate_common_hidden_inputs($db); ?>
363 <fieldset>
364 <legend>
365 <?php
366 if ($cfg['PropertiesIconic']) {
367 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
368 .' alt="" width="16" height="16" />';
370 echo $strDBRename . ':';
372 </legend>
373 <input type="text" name="newname" size="30" class="textfield" value="" />
374 <?php
375 echo '(' . $strCommand . ': ';
377 * @todo (see explanations above in a previous todo)
379 //if (PMA_MYSQL_INT_VERSION >= XYYZZ) {
380 // echo 'RENAME DATABASE';
381 //} else {
382 echo 'INSERT INTO ... SELECT';
384 echo ')'; ?>
385 <input type="submit" value="<?php echo $strGo; ?>" onclick="return confirmLink(this, 'CREATE DATABASE ... <?php echo $strAndThen; ?> DROP DATABASE <?php echo PMA_jsFormat($db); ?>')" />
386 </fieldset>
387 </form>
389 <?php
391 * Copy database
394 <form method="post" action="db_operations.php"
395 onsubmit="return emptyFormElements(this, 'newname')">
396 <?php
397 if (isset($db_collation)) {
398 echo '<input type="hidden" name="db_collation" value="' . $db_collation
399 .'" />' . "\n";
401 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
402 echo PMA_generate_common_hidden_inputs($db);
404 <fieldset>
405 <legend>
406 <?php
407 if ($cfg['PropertiesIconic']) {
408 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
409 .' alt="" width="16" height="16" />';
411 echo $strDBCopy . ':';
412 $drop_clause = 'DROP TABLE / DROP VIEW';
414 </legend>
415 <input type="text" name="newname" size="30" class="textfield" value="" /><br />
416 <?php
417 $choices = array(
418 'structure' => $strStrucOnly,
419 'data' => $strStrucData,
420 'dataonly' => $strDataOnly);
421 PMA_generate_html_radio('what', $choices, 'data', true);
422 unset($choices);
424 <input type="checkbox" name="create_database_before_copying" value="1"
425 id="checkbox_create_database_before_copying"
426 style="vertical-align: middle" checked="checked" />
427 <label for="checkbox_create_database_before_copying">
428 <?php echo $strCreateDatabaseBeforeCopying; ?></label><br />
429 <input type="checkbox" name="drop_if_exists" value="true"
430 id="checkbox_drop" style="vertical-align: middle" />
431 <label for="checkbox_drop"><?php echo sprintf($strAddClause, $drop_clause); ?></label><br />
432 <input type="checkbox" name="sql_auto_increment" value="1" checked="checked"
433 id="checkbox_auto_increment" style="vertical-align: middle" />
434 <label for="checkbox_auto_increment">
435 <?php echo $strAddAutoIncrement; ?></label><br />
436 <input type="checkbox" name="add_constraints" value="1"
437 id="checkbox_constraints" style="vertical-align: middle" />
438 <label for="checkbox_constraints">
439 <?php echo $strAddConstraints; ?></label><br />
440 <?php
441 unset($drop_clause);
443 if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
444 && $_COOKIE['pma_switch_to_new'] == 'true') {
445 $pma_switch_to_new = 'true';
448 <input type="checkbox" name="switch_to_new" value="true"
449 id="checkbox_switch"
450 <?php echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ? ' checked="checked"' : ''); ?>
451 style="vertical-align: middle" />
452 <label for="checkbox_switch"><?php echo $strSwitchToDatabase; ?></label>
453 </fieldset>
454 <fieldset class="tblFooters">
455 <input type="submit" name="submit_copy" value="<?php echo $strGo; ?>" />
456 </fieldset>
457 </form>
459 <?php
461 * BLOB streaming support
464 // load PMA_Config
465 $PMA_Config = $_SESSION['PMA_Config'];
467 // if all blobstreaming plugins exist, begin checking for blobstreaming tables
468 if (!empty($PMA_Config))
470 if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
472 if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
474 $bs_tables = $PMA_Config->get('BLOBSTREAMABLE_DATABASES');
475 $bs_tables = $bs_tables[$db];
477 $oneBSTableExists = FALSE;
478 $allBSTablesExist = TRUE;
480 // first check that all blobstreaming tables do not exist
481 foreach ($bs_tables as $table_key=>$tbl)
482 if ($bs_tables[$table_key]['Exists'])
483 $oneBSTableExists = TRUE;
484 else
485 $allBSTablesExist = FALSE;
489 <form method="post" action="./db_operations.php">
490 <?php echo PMA_generate_common_hidden_inputs($db); ?>
491 <fieldset>
492 <legend>
493 <?php echo PMA_getIcon('b_edit.png', $strBLOBRepository, false, true); ?>
494 </legend>
496 <?php echo $strBLOBRepositoryStatus; ?>:
498 <?php
500 // if the blobstreaming tables exist, provide option to disable the BLOB repository
501 if ($allBSTablesExist)
504 <?php echo $strBLOBRepositoryEnabled; ?>
505 </fieldset>
506 <fieldset class="tblFooters">
507 <input type="hidden" name="db_blob_streaming_op" value="disable" />
508 <input type="submit" onclick="return confirmDisableRepository('<?php echo $db; ?>');" value="<?php echo $strBLOBRepositoryDisable; ?>" />
509 </fieldset>
510 <?php
512 else
514 // if any of the blobstreaming tables are missing, provide option to repair the BLOB repository
515 if ($oneBSTableExists && !$allBSTablesExist)
518 <?php echo $strBLOBRepositoryDamaged; ?>
519 </fieldset>
520 <fieldset class="tblFooters">
521 <input type="hidden" name="db_blob_streaming_op" value="repair" />
522 <input type="submit" value="<?php echo $strBLOBRepositoryRepair; ?>" />
523 </fieldset>
524 <?php
526 // if none of the blobstreaming tables exist, provide option to enable BLOB repository
527 else
530 <?php echo $strBLOBRepositoryDisabled; ?>
531 </fieldset>
532 <fieldset class="tblFooters">
533 <input type="hidden" name="db_blob_streaming_op" value="enable" />
534 <input type="submit" value="<?php echo $strBLOBRepositoryEnable; ?>" />
535 </fieldset>
536 <?php
538 } // end if ($allBSTablesExist)
541 </form>
542 <?php
543 } // end if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
544 } // end if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
548 * Change database charset
550 echo '<form method="post" action="./db_operations.php">' . "\n"
551 . PMA_generate_common_hidden_inputs($db, $table)
552 . '<fieldset>' . "\n"
553 . ' <legend>';
554 if ($cfg['PropertiesIconic']) {
555 echo '<img class="icon" src="' . $pmaThemeImage . 's_asci.png"'
556 .' alt="" width="16" height="16" />';
558 echo ' <label for="select_db_collation">' . $strCollation . ':</label>' . "\n"
559 . ' </legend>' . "\n"
560 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION,
561 'db_collation', 'select_db_collation', $db_collation, false, 3)
562 . ' <input type="submit" name="submitcollation"'
563 . ' value="' . $strGo . '" style="vertical-align: middle" />' . "\n"
564 . '</fieldset>' . "\n"
565 . '</form>' . "\n";
567 if ($num_tables > 0
568 && !$cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
569 $message = PMA_Message::notice('strRelationNotWorking');
570 $message->addParam('<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">', false);
571 $message->addParam('</a>', false);
572 /* Show error if user has configured something, notice elsewhere */
573 if (!empty($cfg['Servers'][$server]['pmadb'])) {
574 $message->isError(true);
576 $message->display();
577 } // end if
578 } // end if (!$is_information_schema)
581 // not sure about displaying the PDF dialog in case db is information_schema
582 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?>
583 <!-- Work on PDF Pages -->
585 <?php
586 // We only show this if we find something in the new pdf_pages table
588 $test_query = '
589 SELECT *
590 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
591 WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
592 $test_rs = PMA_query_as_cu($test_query, null, PMA_DBI_QUERY_STORE);
594 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) { ?>
595 <!-- PDF schema -->
596 <form method="post" action="pdf_schema.php">
597 <fieldset>
598 <legend>
599 <?php
600 echo PMA_generate_common_hidden_inputs($db);
601 if ($cfg['PropertiesIconic']) {
602 echo '<img class="icon" src="' . $pmaThemeImage . 'b_view.png"'
603 .' alt="" width="16" height="16" />';
605 echo $strDisplayPDF;
607 </legend>
608 <label for="pdf_page_number_opt"><?php echo $strPageNumber; ?></label>
609 <select name="pdf_page_number" id="pdf_page_number_opt">
610 <?php
611 while ($pages = @PMA_DBI_fetch_assoc($test_rs)) {
612 echo ' <option value="' . $pages['page_nr'] . '">'
613 . $pages['page_nr'] . ': ' . $pages['page_descr'] . '</option>' . "\n";
614 } // end while
615 PMA_DBI_free_result($test_rs);
616 unset($test_rs);
618 </select><br />
620 <input type="checkbox" name="show_grid" id="show_grid_opt" />
621 <label for="show_grid_opt"><?php echo $strShowGrid; ?></label><br />
622 <input type="checkbox" name="show_color" id="show_color_opt"
623 checked="checked" />
624 <label for="show_color_opt"><?php echo $strShowColor; ?></label><br />
625 <input type="checkbox" name="show_table_dimension" id="show_table_dim_opt" />
626 <label for="show_table_dim_opt"><?php echo $strShowTableDimension; ?>
627 </label><br />
628 <input type="checkbox" name="all_tab_same_wide" id="all_tab_same_wide" />
629 <label for="all_tab_same_wide"><?php echo $strAllTableSameWidth; ?>
630 </label><br />
631 <input type="checkbox" name="with_doc" id="with_doc" checked="checked" />
632 <label for="with_doc"><?php echo $strDataDict; ?></label><br />
633 <input type="checkbox" name="show_keys" id="show_keys" />
634 <label for="show_keys"><?php echo $strShowKeys; ?></label><br />
636 <label for="orientation_opt"><?php echo $strShowDatadictAs; ?></label>
637 <select name="orientation" id="orientation_opt">
638 <option value="L"><?php echo $strLandscape;?></option>
639 <option value="P"><?php echo $strPortrait;?></option>
640 </select><br />
642 <label for="paper_opt"><?php echo $strPaperSize; ?></label>
643 <select name="paper" id="paper_opt">
644 <?php
645 foreach ($cfg['PDFPageSizes'] AS $key => $val) {
646 echo '<option value="' . $val . '"';
647 if ($val == $cfg['PDFDefaultPageSize']) {
648 echo ' selected="selected"';
650 echo ' >' . $val . '</option>' . "\n";
653 </select>
654 </fieldset>
655 <fieldset class="tblFooters">
656 <input type="submit" value="<?php echo $strGo; ?>" />
657 </fieldset>
658 </form>
659 <?php
660 } // end if
661 echo '<br /><a href="pdf_pages.php?' . $url_query . '">';
662 if ($cfg['PropertiesIconic']) {
663 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
664 .' alt="" width="16" height="16" />';
666 echo $strEditPDFPages . '</a>';
667 } // end if
670 * Displays the footer
672 require_once './libraries/footer.inc.php';