patch #2750541 [parser] Incorrect parsing of constraints in ALTER TABLE
[phpmyadmin/crack.git] / db_operations.php
blob5edeaa92547355e424ff96c247573224c7ca4f91
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$
13 * @package phpMyAdmin
16 /**
17 * requirements
19 require_once './libraries/common.inc.php';
20 require_once './libraries/Table.class.php';
21 require_once './libraries/mysql_charsets.lib.php';
23 // add blobstreaming library functions
24 require_once "./libraries/blobstreaming.lib.php";
26 /**
27 * Rename/move or copy database
29 if (strlen($db) && (! empty($db_rename) || ! empty($db_copy))) {
31 if (! empty($db_rename)) {
32 $move = true;
33 } else {
34 $move = false;
37 if (!isset($newname) || !strlen($newname)) {
38 $message = PMA_Message::error('strDatabaseEmpty');
39 } else {
40 $sql_query = ''; // in case target db exists
41 $_error = false;
42 if ($move ||
43 (isset($create_database_before_copying) && $create_database_before_copying)) {
44 // lower_case_table_names=1 `DB` becomes `db`
45 $lower_case_table_names = PMA_DBI_fetch_value('SHOW VARIABLES LIKE "lower_case_table_names"', 0, 1);
46 if ($lower_case_table_names === '1') {
47 $newname = strtolower($newname);
50 $local_query = 'CREATE DATABASE ' . PMA_backquote($newname);
51 if (isset($db_collation)) {
52 $local_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation);
54 $local_query .= ';';
55 $sql_query = $local_query;
56 PMA_DBI_query($local_query);
58 // rebuild the database list because PMA_Table::moveCopy
59 // checks in this list if the target db exists
60 $GLOBALS['pma']->databases->build();
63 if (isset($GLOBALS['add_constraints'])) {
64 $GLOBALS['sql_constraints_query_full_db'] = '';
67 $tables_full = PMA_DBI_get_tables_full($db);
68 $views = array();
69 foreach ($tables_full as $each_table => $tmp) {
70 // to be able to rename a db containing views, we
71 // first collect in $views all the views we find and we
72 // will handle them after the tables
73 /**
74 * @todo support a view of a view
75 * @todo support triggers
77 if (PMA_Table::isView($db, $each_table)) {
78 $views[] = $each_table;
79 continue;
82 $back = $sql_query;
83 $sql_query = '';
85 // value of $what for this table only
86 $this_what = $what;
88 // do not copy the data from a Merge table
89 // note: on the calling FORM, 'data' means 'structure and data'
90 if ($tables_full[$each_table]['Engine'] == 'MRG_MyISAM') {
91 if ($this_what == 'data') {
92 $this_what = 'structure';
94 if ($this_what == 'dataonly') {
95 $this_what = 'nocopy';
99 if ($this_what != 'nocopy') {
100 if (! PMA_Table::moveCopy($db, $each_table, $newname, $each_table,
101 isset($this_what) ? $this_what : 'data', $move, 'db_copy'))
103 $_error = true;
104 // $sql_query is filled by PMA_Table::moveCopy()
105 $sql_query = $back . $sql_query;
106 break;
108 if (isset($GLOBALS['add_constraints'])) {
109 $GLOBALS['sql_constraints_query_full_db'] .= $GLOBALS['sql_constraints_query'];
110 unset($GLOBALS['sql_constraints_query']);
113 // $sql_query is filled by PMA_Table::moveCopy()
114 $sql_query = $back . $sql_query;
115 } // end (foreach)
116 unset($each_table);
118 // handle the views
119 if (! $_error) {
120 foreach ($views as $view) {
121 if (! PMA_Table::moveCopy($db, $view, $newname, $view,
122 'structure', $move, 'db_copy')) {
123 $_error = true;
124 break;
128 unset($view, $views);
130 // now that all tables exist, create all the accumulated constraints
131 if (! $_error && isset($GLOBALS['add_constraints'])) {
133 * @todo this works with mysqli but not with mysql, because
134 * mysql extension does not accept more than one statement; maybe
135 * interface with the sql import plugin that handles statement delimiter
137 PMA_DBI_query($GLOBALS['sql_constraints_query_full_db']);
139 // and prepare to display them
140 $GLOBALS['sql_query'] .= "\n" . $GLOBALS['sql_constraints_query_full_db'];
141 unset($GLOBALS['sql_constraints_query_full_db']);
144 if (PMA_MYSQL_INT_VERSION >= 50000) {
145 // here I don't use DELIMITER because it's not part of the
146 // language; I have to send each statement one by one
148 // to avoid selecting alternatively the current and new db
149 // we would need to modify the CREATE definitions to qualify
150 // the db name
151 $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
152 if ($procedure_names) {
153 foreach($procedure_names as $procedure_name) {
154 PMA_DBI_select_db($db);
155 $tmp_query = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure_name);
156 // collect for later display
157 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
158 PMA_DBI_select_db($newname);
159 PMA_DBI_query($tmp_query);
163 $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
164 if ($function_names) {
165 foreach($function_names as $function_name) {
166 PMA_DBI_select_db($db);
167 $tmp_query = PMA_DBI_get_definition($db, 'FUNCTION', $function_name);
168 // collect for later display
169 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
170 PMA_DBI_select_db($newname);
171 PMA_DBI_query($tmp_query);
175 // go back to current db, just in case
176 PMA_DBI_select_db($db);
178 // Duplicate the bookmarks for this db (done once for each db)
179 if (! $_error && $db != $newname) {
180 $get_fields = array('user', 'label', 'query');
181 $where_fields = array('dbase' => $db);
182 $new_fields = array('dbase' => $newname);
183 PMA_Table::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
184 $where_fields, $new_fields);
187 if (! $_error && $move) {
189 * cleanup pmadb stuff for this db
191 require_once './libraries/relation_cleanup.lib.php';
192 PMA_relationsCleanupDatabase($db);
194 // if someday the RENAME DATABASE reappears, do not DROP
195 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
196 $sql_query .= "\n" . $local_query;
197 PMA_DBI_query($local_query);
199 $message = PMA_Message::success('strRenameDatabaseOK');
200 $message->addParam($db);
201 $message->addParam($newname);
202 } elseif (! $_error) {
203 $message = PMA_Message::success('strCopyDatabaseOK');
204 $message->addParam($db);
205 $message->addParam($newname);
207 $reload = true;
209 /* Change database to be used */
210 if (! $_error && $move) {
211 $db = $newname;
212 } elseif (! $_error) {
213 if (isset($switch_to_new) && $switch_to_new == 'true') {
214 PMA_setCookie('pma_switch_to_new', 'true');
215 $db = $newname;
216 } else {
217 PMA_setCookie('pma_switch_to_new', '');
221 if ($_error && ! isset($message)) {
222 $message = PMA_Message::error();
228 * Enable/Disable/Repair BLOB Repository Monitoring for current database
230 if (strlen($db) > 0 && !empty($db_blob_streaming_op))
232 // load PMA_Config
233 $PMA_Config = $_SESSION['PMA_Config'];
235 if (!empty($PMA_Config))
237 if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
239 // if Blobstreaming plugins exist, begin checking for Blobstreaming tables
240 if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
242 $bs_tables = $PMA_Config->get('BLOBSTREAMABLE_DATABASES');
243 $bs_tables = $bs_tables[$db];
245 $oneBSTableExists = FALSE;
247 // check if at least one blobstreaming table exists
248 foreach ($bs_tables as $table_key=>$tbl)
249 if ($bs_tables[$table_key]['Exists'])
251 $oneBSTableExists = TRUE;
252 break;
255 switch ($db_blob_streaming_op)
257 // enable BLOB repository monitoring
258 case "enable":
259 // if blobstreaming tables do not exist, create them
260 if (!$oneBSTableExists)
261 PMA_BS_CreateTables($db);
262 break;
263 // disable BLOB repository monitoring
264 case "disable":
265 // if at least one blobstreaming table exists, execute drop
266 if ($oneBSTableExists)
267 PMA_BS_DropTables($db);
268 break;
269 // repair BLOB repository
270 case "repair":
271 // check if a blobstreaming table is missing
272 foreach ($bs_tables as $table_key=>$tbl)
273 if (!$bs_tables[$table_key]['Exists'])
275 PMA_DBI_select_db($db);
276 PMA_DBI_query(PMA_BS_GetTableStruct($table_key));
280 // refresh side menu
281 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');
282 } // end if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
283 } // end if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
288 * Settings for relations stuff
291 require_once './libraries/relation.lib.php';
292 $cfgRelation = PMA_getRelationsParam();
295 * Check if comments were updated
296 * (must be done before displaying the menu tabs)
298 if (isset($_REQUEST['comment'])) {
299 PMA_setDbComment($db, $comment);
303 * Prepares the tables list if the user where not redirected to this script
304 * because there is no table in the database ($is_info is true)
306 if (empty($is_info)) {
307 require './libraries/db_common.inc.php';
308 $url_query .= '&amp;goto=db_operations.php';
310 // Gets the database structure
311 $sub_part = '_structure';
312 require './libraries/db_info.inc.php';
313 echo "\n";
315 if (isset($message)) {
316 PMA_showMessage($message, $sql_query);
317 unset($message);
321 $db_collation = PMA_getDbCollation($db);
322 if ($db == 'information_schema') {
323 $is_information_schema = true;
324 } else {
325 $is_information_schema = false;
328 if (!$is_information_schema) {
330 require './libraries/display_create_table.lib.php';
332 if ($cfgRelation['commwork']) {
334 * database comment
337 <form method="post" action="db_operations.php">
338 <?php echo PMA_generate_common_hidden_inputs($db); ?>
339 <fieldset>
340 <legend>
341 <?php echo PMA_getIcon('b_comment.png', $strDBComment, false, true); ?>
342 </legend>
343 <input type="text" name="comment" class="textfield" size="30"
344 value="<?php
345 echo htmlspecialchars(PMA_getDBComment($db)); ?>" />
346 <input type="submit" value="<?php echo $strGo; ?>" />
347 </fieldset>
348 </form>
349 <?php
352 * rename database
355 <form method="post" action="db_operations.php"
356 onsubmit="return emptyFormElements(this, 'newname')">
357 <?php
358 if (isset($db_collation)) {
359 echo '<input type="hidden" name="db_collation" value="' . $db_collation
360 .'" />' . "\n";
363 <input type="hidden" name="what" value="data" />
364 <input type="hidden" name="db_rename" value="true" />
365 <?php echo PMA_generate_common_hidden_inputs($db); ?>
366 <fieldset>
367 <legend>
368 <?php
369 if ($cfg['PropertiesIconic']) {
370 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
371 .' alt="" width="16" height="16" />';
373 echo $strDBRename . ':';
375 </legend>
376 <input type="text" name="newname" size="30" class="textfield" value="" />
377 <?php
378 echo '(' . $strCommand . ': ';
380 * @todo (see explanations above in a previous todo)
382 //if (PMA_MYSQL_INT_VERSION >= XYYZZ) {
383 // echo 'RENAME DATABASE';
384 //} else {
385 echo 'INSERT INTO ... SELECT';
387 echo ')'; ?>
388 <input type="submit" value="<?php echo $strGo; ?>" onclick="return confirmLink(this, 'CREATE DATABASE ... <?php echo $strAndThen; ?> DROP DATABASE <?php echo PMA_jsFormat($db); ?>')" />
389 </fieldset>
390 </form>
392 <?php
394 * Copy database
397 <form method="post" action="db_operations.php"
398 onsubmit="return emptyFormElements(this, 'newname')">
399 <?php
400 if (isset($db_collation)) {
401 echo '<input type="hidden" name="db_collation" value="' . $db_collation
402 .'" />' . "\n";
404 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
405 echo PMA_generate_common_hidden_inputs($db);
407 <fieldset>
408 <legend>
409 <?php
410 if ($cfg['PropertiesIconic']) {
411 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
412 .' alt="" width="16" height="16" />';
414 echo $strDBCopy . ':';
415 $drop_clause = 'DROP TABLE / DROP VIEW';
417 </legend>
418 <input type="text" name="newname" size="30" class="textfield" value="" /><br />
419 <?php
420 $choices = array(
421 'structure' => $strStrucOnly,
422 'data' => $strStrucData,
423 'dataonly' => $strDataOnly);
424 PMA_generate_html_radio('what', $choices, 'data', true);
425 unset($choices);
427 <input type="checkbox" name="create_database_before_copying" value="1"
428 id="checkbox_create_database_before_copying"
429 style="vertical-align: middle" checked="checked" />
430 <label for="checkbox_create_database_before_copying">
431 <?php echo $strCreateDatabaseBeforeCopying; ?></label><br />
432 <input type="checkbox" name="drop_if_exists" value="true"
433 id="checkbox_drop" style="vertical-align: middle" />
434 <label for="checkbox_drop"><?php echo sprintf($strAddClause, $drop_clause); ?></label><br />
435 <input type="checkbox" name="sql_auto_increment" value="1" checked="checked"
436 id="checkbox_auto_increment" style="vertical-align: middle" />
437 <label for="checkbox_auto_increment">
438 <?php echo $strAddAutoIncrement; ?></label><br />
439 <input type="checkbox" name="add_constraints" value="1"
440 id="checkbox_constraints" style="vertical-align: middle" />
441 <label for="checkbox_constraints">
442 <?php echo $strAddConstraints; ?></label><br />
443 <?php
444 unset($drop_clause);
446 if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
447 && $_COOKIE['pma_switch_to_new'] == 'true') {
448 $pma_switch_to_new = 'true';
451 <input type="checkbox" name="switch_to_new" value="true"
452 id="checkbox_switch"
453 <?php echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ? ' checked="checked"' : ''); ?>
454 style="vertical-align: middle" />
455 <label for="checkbox_switch"><?php echo $strSwitchToDatabase; ?></label>
456 </fieldset>
457 <fieldset class="tblFooters">
458 <input type="submit" name="submit_copy" value="<?php echo $strGo; ?>" />
459 </fieldset>
460 </form>
462 <?php
464 * BLOB streaming support
467 // load PMA_Config
468 $PMA_Config = $_SESSION['PMA_Config'];
470 // if all blobstreaming plugins exist, begin checking for blobstreaming tables
471 if (!empty($PMA_Config))
473 if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
475 if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
477 $bs_tables = $PMA_Config->get('BLOBSTREAMABLE_DATABASES');
478 $bs_tables = $bs_tables[$db];
480 $oneBSTableExists = FALSE;
481 $allBSTablesExist = TRUE;
483 // first check that all blobstreaming tables do not exist
484 foreach ($bs_tables as $table_key=>$tbl)
485 if ($bs_tables[$table_key]['Exists'])
486 $oneBSTableExists = TRUE;
487 else
488 $allBSTablesExist = FALSE;
492 <form method="post" action="./db_operations.php">
493 <?php echo PMA_generate_common_hidden_inputs($db); ?>
494 <fieldset>
495 <legend>
496 <?php echo PMA_getIcon('b_edit.png', $strBLOBRepository, false, true); ?>
497 </legend>
499 <?php echo $strBLOBRepositoryStatus; ?>:
501 <?php
503 // if the blobstreaming tables exist, provide option to disable the BLOB repository
504 if ($allBSTablesExist)
507 <?php echo $strBLOBRepositoryEnabled; ?>
508 </fieldset>
509 <fieldset class="tblFooters">
510 <input type="hidden" name="db_blob_streaming_op" value="disable" />
511 <input type="submit" onclick="return confirmDisableRepository('<?php echo $db; ?>');" value="<?php echo $strBLOBRepositoryDisable; ?>" />
512 </fieldset>
513 <?php
515 else
517 // if any of the blobstreaming tables are missing, provide option to repair the BLOB repository
518 if ($oneBSTableExists && !$allBSTablesExist)
521 <?php echo $strBLOBRepositoryDamaged; ?>
522 </fieldset>
523 <fieldset class="tblFooters">
524 <input type="hidden" name="db_blob_streaming_op" value="repair" />
525 <input type="submit" value="<?php echo $strBLOBRepositoryRepair; ?>" />
526 </fieldset>
527 <?php
529 // if none of the blobstreaming tables exist, provide option to enable BLOB repository
530 else
533 <?php echo $strBLOBRepositoryDisabled; ?>
534 </fieldset>
535 <fieldset class="tblFooters">
536 <input type="hidden" name="db_blob_streaming_op" value="enable" />
537 <input type="submit" value="<?php echo $strBLOBRepositoryEnable; ?>" />
538 </fieldset>
539 <?php
541 } // end if ($allBSTablesExist)
544 </form>
545 <?php
546 } // end if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
547 } // end if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
551 * Change database charset
553 echo '<form method="post" action="./db_operations.php">' . "\n"
554 . PMA_generate_common_hidden_inputs($db, $table)
555 . '<fieldset>' . "\n"
556 . ' <legend>';
557 if ($cfg['PropertiesIconic']) {
558 echo '<img class="icon" src="' . $pmaThemeImage . 's_asci.png"'
559 .' alt="" width="16" height="16" />';
561 echo ' <label for="select_db_collation">' . $strCollation . ':</label>' . "\n"
562 . ' </legend>' . "\n"
563 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION,
564 'db_collation', 'select_db_collation', $db_collation, false, 3)
565 . ' <input type="submit" name="submitcollation"'
566 . ' value="' . $strGo . '" style="vertical-align: middle" />' . "\n"
567 . '</fieldset>' . "\n"
568 . '</form>' . "\n";
570 if ($num_tables > 0
571 && !$cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
572 $message = PMA_Message::notice('strRelationNotWorking');
573 $message->addParam('<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">', false);
574 $message->addParam('</a>', false);
575 /* Show error if user has configured something, notice elsewhere */
576 if (!empty($cfg['Servers'][$server]['pmadb'])) {
577 $message->isError(true);
579 $message->display();
580 } // end if
581 } // end if (!$is_information_schema)
584 // not sure about displaying the PDF dialog in case db is information_schema
585 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?>
586 <!-- Work on PDF Pages -->
588 <?php
589 // We only show this if we find something in the new pdf_pages table
591 $test_query = '
592 SELECT *
593 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
594 WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
595 $test_rs = PMA_query_as_cu($test_query, null, PMA_DBI_QUERY_STORE);
597 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) { ?>
598 <!-- PDF schema -->
599 <form method="post" action="pdf_schema.php">
600 <fieldset>
601 <legend>
602 <?php
603 echo PMA_generate_common_hidden_inputs($db);
604 if ($cfg['PropertiesIconic']) {
605 echo '<img class="icon" src="' . $pmaThemeImage . 'b_view.png"'
606 .' alt="" width="16" height="16" />';
608 echo $strDisplayPDF;
610 </legend>
611 <label for="pdf_page_number_opt"><?php echo $strPageNumber; ?></label>
612 <select name="pdf_page_number" id="pdf_page_number_opt">
613 <?php
614 while ($pages = @PMA_DBI_fetch_assoc($test_rs)) {
615 echo ' <option value="' . $pages['page_nr'] . '">'
616 . $pages['page_nr'] . ': ' . $pages['page_descr'] . '</option>' . "\n";
617 } // end while
618 PMA_DBI_free_result($test_rs);
619 unset($test_rs);
621 </select><br />
623 <input type="checkbox" name="show_grid" id="show_grid_opt" />
624 <label for="show_grid_opt"><?php echo $strShowGrid; ?></label><br />
625 <input type="checkbox" name="show_color" id="show_color_opt"
626 checked="checked" />
627 <label for="show_color_opt"><?php echo $strShowColor; ?></label><br />
628 <input type="checkbox" name="show_table_dimension" id="show_table_dim_opt" />
629 <label for="show_table_dim_opt"><?php echo $strShowTableDimension; ?>
630 </label><br />
631 <input type="checkbox" name="all_tab_same_wide" id="all_tab_same_wide" />
632 <label for="all_tab_same_wide"><?php echo $strAllTableSameWidth; ?>
633 </label><br />
634 <input type="checkbox" name="with_doc" id="with_doc" checked="checked" />
635 <label for="with_doc"><?php echo $strDataDict; ?></label><br />
636 <input type="checkbox" name="show_keys" id="show_keys" />
637 <label for="show_keys"><?php echo $strShowKeys; ?></label><br />
639 <label for="orientation_opt"><?php echo $strShowDatadictAs; ?></label>
640 <select name="orientation" id="orientation_opt">
641 <option value="L"><?php echo $strLandscape;?></option>
642 <option value="P"><?php echo $strPortrait;?></option>
643 </select><br />
645 <label for="paper_opt"><?php echo $strPaperSize; ?></label>
646 <select name="paper" id="paper_opt">
647 <?php
648 foreach ($cfg['PDFPageSizes'] AS $key => $val) {
649 echo '<option value="' . $val . '"';
650 if ($val == $cfg['PDFDefaultPageSize']) {
651 echo ' selected="selected"';
653 echo ' >' . $val . '</option>' . "\n";
656 </select>
657 </fieldset>
658 <fieldset class="tblFooters">
659 <input type="submit" value="<?php echo $strGo; ?>" />
660 </fieldset>
661 </form>
662 <?php
663 } // end if
664 echo '<br /><a href="pdf_pages.php?' . $url_query . '">';
665 if ($cfg['PropertiesIconic']) {
666 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
667 .' alt="" width="16" height="16" />';
669 echo $strEditPDFPages . '</a>';
670 } // end if
673 * Displays the footer
675 require_once './libraries/footer.inc.php';