Merge branch 'master' of ssh://phpmyadmin.git.sourceforge.net/gitroot/phpmyadmin...
[phpmyadmin/crack.git] / db_operations.php
blob16630e3217138f132ab19b2a903661d2e20623ee
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 * @package phpMyAdmin
15 /**
16 * requirements
18 require_once './libraries/common.inc.php';
19 require_once './libraries/mysql_charsets.lib.php';
21 // add blobstreaming library functions
22 require_once "./libraries/blobstreaming.lib.php";
24 // add a javascript file for jQuery functions to handle Ajax actions
25 // also add jQueryUI
26 $GLOBALS['js_include'][] = 'jquery/jquery-ui-1.8.custom.js';
27 $GLOBALS['js_include'][] = 'db_operations.js';
29 /**
30 * Rename/move or copy database
32 if (strlen($db) && (! empty($db_rename) || ! empty($db_copy))) {
34 if (! empty($db_rename)) {
35 $move = true;
36 } else {
37 $move = false;
40 if (! isset($newname) || ! strlen($newname)) {
41 $message = PMA_Message::error(__('The database name is empty!'));
42 } else {
43 $sql_query = ''; // in case target db exists
44 $_error = false;
45 if ($move ||
46 (isset($create_database_before_copying) && $create_database_before_copying)) {
47 // lower_case_table_names=1 `DB` becomes `db`
48 $lower_case_table_names = PMA_DBI_fetch_value('SHOW VARIABLES LIKE "lower_case_table_names"', 0, 1);
49 if ($lower_case_table_names === '1') {
50 $newname = PMA_strtolower($newname);
53 $local_query = 'CREATE DATABASE ' . PMA_backquote($newname);
54 if (isset($db_collation)) {
55 $local_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation);
57 $local_query .= ';';
58 $sql_query = $local_query;
59 // save the original db name because Tracker.class.php which
60 // may be called under PMA_DBI_query() changes $GLOBALS['db']
61 // for some statements, one of which being CREATE DATABASE
62 $original_db = $db;
63 PMA_DBI_query($local_query);
64 $db = $original_db;
65 unset($original_db);
67 // rebuild the database list because PMA_Table::moveCopy
68 // checks in this list if the target db exists
69 $GLOBALS['pma']->databases->build();
72 // here I don't use DELIMITER because it's not part of the
73 // language; I have to send each statement one by one
75 // to avoid selecting alternatively the current and new db
76 // we would need to modify the CREATE definitions to qualify
77 // the db name
78 $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
79 if ($procedure_names) {
80 foreach ($procedure_names as $procedure_name) {
81 PMA_DBI_select_db($db);
82 $tmp_query = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure_name);
83 // collect for later display
84 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
85 PMA_DBI_select_db($newname);
86 PMA_DBI_query($tmp_query);
90 $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
91 if ($function_names) {
92 foreach ($function_names as $function_name) {
93 PMA_DBI_select_db($db);
94 $tmp_query = PMA_DBI_get_definition($db, 'FUNCTION', $function_name);
95 // collect for later display
96 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
97 PMA_DBI_select_db($newname);
98 PMA_DBI_query($tmp_query);
102 // go back to current db, just in case
103 PMA_DBI_select_db($db);
105 $GLOBALS['sql_constraints_query_full_db'] = array();
107 $tables_full = PMA_DBI_get_tables_full($db);
108 $views = array();
110 // remove all foreign key constraints, otherwise we can get errors
111 require_once './libraries/export/sql.php';
112 foreach ($tables_full as $each_table => $tmp) {
113 $sql_constraints = '';
114 $sql_drop_foreign_keys = '';
115 $sql_structure = PMA_getTableDef($db, $each_table, "\n", '', false, false);
116 if ($move && ! empty($sql_drop_foreign_keys)) {
117 PMA_DBI_query($sql_drop_foreign_keys);
119 // keep the constraint we just dropped
120 if (! empty($sql_constraints)) {
121 $GLOBALS['sql_constraints_query_full_db'][] = $sql_constraints;
124 unset($sql_constraints, $sql_drop_foreign_keys, $sql_structure);
127 foreach ($tables_full as $each_table => $tmp) {
128 // to be able to rename a db containing views,
129 // first all the views are collected and a stand-in is created
130 // the real views are created after the tables
131 if (PMA_Table::isView($db, $each_table)) {
132 $views[] = $each_table;
133 // Create stand-in definition to resolve view dependencies
134 $sql_view_standin = PMA_getTableDefStandIn($db, $each_table, "\n");
135 PMA_DBI_query($sql_view_standin);
136 $GLOBALS['sql_query'] .= "\n" . $sql_view_standin . ';';
137 continue;
140 $back = $sql_query;
141 $sql_query = '';
143 // value of $what for this table only
144 $this_what = $what;
146 // do not copy the data from a Merge table
147 // note: on the calling FORM, 'data' means 'structure and data'
148 if (PMA_Table::isMerge($db, $each_table)) {
149 if ($this_what == 'data') {
150 $this_what = 'structure';
152 if ($this_what == 'dataonly') {
153 $this_what = 'nocopy';
157 if ($this_what != 'nocopy') {
158 // keep the triggers from the original db+table
159 // (third param is empty because delimiters are only intended
160 // for importing via the mysql client or our Import feature)
161 $triggers = PMA_DBI_get_triggers($db, $each_table, '');
163 if (! PMA_Table::moveCopy($db, $each_table, $newname, $each_table,
164 isset($this_what) ? $this_what : 'data', $move, 'db_copy'))
166 $_error = true;
167 // $sql_query is filled by PMA_Table::moveCopy()
168 $sql_query = $back . $sql_query;
169 break;
171 // apply the triggers to the destination db+table
172 if ($triggers) {
173 PMA_DBI_select_db($newname);
174 foreach ($triggers as $trigger) {
175 PMA_DBI_query($trigger['create']);
177 unset($trigger);
179 unset($triggers);
181 // this does not apply to a rename operation
182 if (isset($GLOBALS['add_constraints']) && !empty($GLOBALS['sql_constraints_query'])) {
183 $GLOBALS['sql_constraints_query_full_db'][] = $GLOBALS['sql_constraints_query'];
184 unset($GLOBALS['sql_constraints_query']);
187 // $sql_query is filled by PMA_Table::moveCopy()
188 $sql_query = $back . $sql_query;
189 } // end (foreach)
190 unset($each_table);
192 // handle the views
193 if (! $_error) {
194 // temporarily force to add DROP IF EXIST to CREATE VIEW query,
195 // to remove stand-in VIEW that was created earlier
196 if (isset($GLOBALS['drop_if_exists'])) {
197 $temp_drop_if_exists = $GLOBALS['drop_if_exists'];
199 $GLOBALS['drop_if_exists'] = 'true';
201 foreach ($views as $view) {
202 if (! PMA_Table::moveCopy($db, $view, $newname, $view, 'structure', $move, 'db_copy')) {
203 $_error = true;
204 break;
207 unset($GLOBALS['drop_if_exists']);
208 if (isset($temp_drop_if_exists)) {
209 // restore previous value
210 $GLOBALS['drop_if_exists'] = $temp_drop_if_exists;
211 unset($temp_drop_if_exists);
214 unset($view, $views);
216 // now that all tables exist, create all the accumulated constraints
217 if (! $_error && count($GLOBALS['sql_constraints_query_full_db']) > 0) {
218 PMA_DBI_select_db($newname);
219 foreach ($GLOBALS['sql_constraints_query_full_db'] as $one_query) {
220 PMA_DBI_query($one_query);
221 // and prepare to display them
222 $GLOBALS['sql_query'] .= "\n" . $one_query;
225 unset($GLOBALS['sql_constraints_query_full_db'], $one_query);
228 if (PMA_MYSQL_INT_VERSION >= 50100) {
229 // here DELIMITER is not used because it's not part of the
230 // language; each statement is sent one by one
232 // to avoid selecting alternatively the current and new db
233 // we would need to modify the CREATE definitions to qualify
234 // the db name
235 $event_names = PMA_DBI_fetch_result('SELECT EVENT_NAME FROM information_schema.EVENTS WHERE EVENT_SCHEMA= \'' . PMA_sqlAddSlashes($db,true) . '\';');
236 if ($event_names) {
237 foreach ($event_names as $event_name) {
238 PMA_DBI_select_db($db);
239 $tmp_query = PMA_DBI_get_definition($db, 'EVENT', $event_name);
240 // collect for later display
241 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
242 PMA_DBI_select_db($newname);
243 PMA_DBI_query($tmp_query);
248 // go back to current db, just in case
249 PMA_DBI_select_db($db);
251 // Duplicate the bookmarks for this db (done once for each db)
252 if (! $_error && $db != $newname) {
253 $get_fields = array('user', 'label', 'query');
254 $where_fields = array('dbase' => $db);
255 $new_fields = array('dbase' => $newname);
256 PMA_Table::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
257 $where_fields, $new_fields);
260 if (! $_error && $move) {
262 * cleanup pmadb stuff for this db
264 require_once './libraries/relation_cleanup.lib.php';
265 PMA_relationsCleanupDatabase($db);
267 // if someday the RENAME DATABASE reappears, do not DROP
268 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
269 $sql_query .= "\n" . $local_query;
270 PMA_DBI_query($local_query);
272 $message = PMA_Message::success(__('Database %s has been renamed to %s'));
273 $message->addParam($db);
274 $message->addParam($newname);
275 } elseif (! $_error) {
276 $message = PMA_Message::success(__('Database %s has been copied to %s'));
277 $message->addParam($db);
278 $message->addParam($newname);
280 $reload = true;
282 /* Change database to be used */
283 if (! $_error && $move) {
284 $db = $newname;
285 } elseif (! $_error) {
286 if (isset($switch_to_new) && $switch_to_new == 'true') {
287 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
288 $db = $newname;
289 } else {
290 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
294 if ($_error && ! isset($message)) {
295 $message = PMA_Message::error();
300 * Database has been successfully renamed/moved. If in an Ajax request,
301 * generate the output with {@link PMA_ajaxResponse} and exit
303 if( $GLOBALS['is_ajax_request'] == true) {
304 $extra_data['newname'] = $newname;
305 $extra_data['sql_query'] = PMA_showMessage(NULL, $sql_query);
306 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
312 * Settings for relations stuff
315 $cfgRelation = PMA_getRelationsParam();
318 * Check if comments were updated
319 * (must be done before displaying the menu tabs)
321 if (isset($_REQUEST['comment'])) {
322 PMA_setDbComment($db, $comment);
326 * Prepares the tables list if the user where not redirected to this script
327 * because there is no table in the database ($is_info is true)
329 if (empty($is_info)) {
330 require './libraries/db_common.inc.php';
331 $url_query .= '&amp;goto=db_operations.php';
333 // Gets the database structure
334 $sub_part = '_structure';
335 require './libraries/db_info.inc.php';
336 echo "\n";
338 if (isset($message)) {
339 PMA_showMessage($message, $sql_query);
340 unset($message);
344 $db_collation = PMA_getDbCollation($db);
345 if ($db == 'information_schema') {
346 $is_information_schema = true;
347 } else {
348 $is_information_schema = false;
351 if (!$is_information_schema) {
352 if ($cfgRelation['commwork']) {
354 * database comment
357 <div class="operations_half_width">
358 <form method="post" action="db_operations.php">
359 <?php echo PMA_generate_common_hidden_inputs($db); ?>
360 <fieldset>
361 <legend>
362 <?php echo PMA_getIcon('b_comment.png', __('Database comment: '), false, true); ?>
363 </legend>
364 <input type="text" name="comment" class="textfield" size="30"
365 value="<?php
366 echo htmlspecialchars(PMA_getDBComment($db)); ?>" />
367 </fieldset>
368 <fieldset class="tblFooters">
369 <input type="submit" value="<?php echo __('Go'); ?>" />
370 </fieldset>
371 </form>
372 </div>
373 <?php
376 <div class="operations_half_width">
377 <?php require './libraries/display_create_table.lib.php'; ?>
378 </div>
379 <?php
381 * rename database
383 if ($db != 'mysql') {
385 <div class="operations_half_width">
386 <form id="rename_db_form" <?php echo ($GLOBALS['cfg']['AjaxEnable'] ? ' class="ajax" ' : ''); ?>method="post" action="db_operations.php"
387 onsubmit="return emptyFormElements(this, 'newname')">
388 <?php
389 if (isset($db_collation)) {
390 echo '<input type="hidden" name="db_collation" value="' . $db_collation
391 .'" />' . "\n";
394 <input type="hidden" name="what" value="data" />
395 <input type="hidden" name="db_rename" value="true" />
396 <?php echo PMA_generate_common_hidden_inputs($db); ?>
397 <fieldset>
398 <legend>
399 <?php
400 if ($cfg['PropertiesIconic']) {
401 echo '<img class="icon ic_b_edit" src="themes/dot.gif" alt="" />';
403 echo __('Rename database to') . ':';
405 </legend>
406 <input id="new_db_name" type="text" name="newname" size="30" class="textfield" value="" />
407 <?php
408 echo '(' . __('Command') . ': ';
410 * @todo (see explanations above in a previous todo)
412 //if (PMA_MYSQL_INT_VERSION >= XYYZZ) {
413 // echo 'RENAME DATABASE';
414 //} else {
415 echo 'INSERT INTO ... SELECT';
417 echo ')'; ?>
418 </fieldset>
419 <fieldset class="tblFooters">
420 <input id="rename_db_input" type="submit" value="<?php echo __('Go'); ?>" />
421 </fieldset>
422 </form>
423 </div>
424 <?php
425 } // end if
427 // Drop link if allowed
428 // Don't even try to drop information_schema. You won't be able to. Believe me. You won't.
429 // Don't allow to easily drop mysql database, RFE #1327514.
430 if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase']) && ! $db_is_information_schema && ($db != 'mysql')) {
432 <div class="operations_half_width">
433 <fieldset class="caution">
434 <legend><?php
435 if ($cfg['PropertiesIconic']) {
436 echo '<img class="icon ic_b_deltbl" src="themes/dot.gif" alt="" />';
438 echo __('Remove database');
439 ?></legend>
441 <ul>
442 <?php
443 $this_sql_query = 'DROP DATABASE ' . PMA_backquote($GLOBALS['db']);
444 $this_url_params = array(
445 'sql_query' => $this_sql_query,
446 'back' => 'db_operations.php',
447 'goto' => 'main.php',
448 'reload' => '1',
449 'purge' => '1',
450 'message_to_show' => sprintf(__('Database %s has been dropped.'), htmlspecialchars(PMA_backquote($db))),
451 'db' => NULL,
454 <li><a href="sql.php<?php echo PMA_generate_common_url($this_url_params); ?>" <?php echo ($GLOBALS['cfg']['AjaxEnable'] ? 'id="drop_db_anchor"' : ''); ?>>
455 <?php echo __('Drop the database (DROP)'); ?></a>
456 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'DROP_DATABASE'); ?>
457 </li>
458 </ul>
459 </fieldset>
460 </div>
461 <?php } ?>
462 <?php
464 * Copy database
467 <div class="operations_half_width clearfloat">
468 <form id="copy_db_form" <?php echo ($GLOBALS['cfg']['AjaxEnable'] ? ' class="ajax" ' : ''); ?>method="post" action="db_operations.php"
469 onsubmit="return emptyFormElements(this, 'newname')">
470 <?php
471 if (isset($db_collation)) {
472 echo '<input type="hidden" name="db_collation" value="' . $db_collation
473 .'" />' . "\n";
475 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
476 echo PMA_generate_common_hidden_inputs($db);
478 <fieldset>
479 <legend>
480 <?php
481 if ($cfg['PropertiesIconic']) {
482 echo '<img class="icon ic_b_edit" src="themes/dot.gif" alt="" />';
484 echo __('Copy database to') . ':';
485 $drop_clause = 'DROP TABLE / DROP VIEW';
487 </legend>
488 <input type="text" name="newname" size="30" class="textfield" value="" /><br />
489 <?php
490 $choices = array(
491 'structure' => __('Structure only'),
492 'data' => __('Structure and data'),
493 'dataonly' => __('Data only'));
494 PMA_display_html_radio('what', $choices, 'data', true);
495 unset($choices);
497 <input type="checkbox" name="create_database_before_copying" value="1"
498 id="checkbox_create_database_before_copying"
499 checked="checked" />
500 <label for="checkbox_create_database_before_copying">
501 <?php echo __('CREATE DATABASE before copying'); ?></label><br />
502 <input type="checkbox" name="drop_if_exists" value="true"
503 id="checkbox_drop" />
504 <label for="checkbox_drop"><?php echo sprintf(__('Add %s'), $drop_clause); ?></label><br />
505 <input type="checkbox" name="sql_auto_increment" value="1" checked="checked"
506 id="checkbox_auto_increment" />
507 <label for="checkbox_auto_increment">
508 <?php echo __('Add AUTO_INCREMENT value'); ?></label><br />
509 <input type="checkbox" name="add_constraints" value="1"
510 id="checkbox_constraints" />
511 <label for="checkbox_constraints">
512 <?php echo __('Add constraints'); ?></label><br />
513 <?php
514 unset($drop_clause);
516 if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
517 && $_COOKIE['pma_switch_to_new'] == 'true') {
518 $pma_switch_to_new = 'true';
521 <input type="checkbox" name="switch_to_new" value="true"
522 id="checkbox_switch"
523 <?php echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ? ' checked="checked"' : ''); ?>
525 <label for="checkbox_switch"><?php echo __('Switch to copied database'); ?></label>
526 </fieldset>
527 <fieldset class="tblFooters">
528 <input type="submit" name="submit_copy" value="<?php echo __('Go'); ?>" />
529 </fieldset>
530 </form>
531 </div>
532 <?php
535 * Change database charset
537 echo '<div class="operations_half_width"><form id="change_db_charset_form" ';
538 if ($GLOBALS['cfg']['AjaxEnable']) {
539 echo ' class="ajax" ';
541 echo 'method="post" action="./db_operations.php">'
542 . PMA_generate_common_hidden_inputs($db, $table)
543 . '<fieldset>' . "\n"
544 . ' <legend>';
545 if ($cfg['PropertiesIconic']) {
546 echo '<img class="icon ic_s_asci" src="themes/dot.gif" alt="" />';
548 echo ' <label for="select_db_collation">' . __('Collation') . ':</label>' . "\n"
549 . ' </legend>' . "\n"
550 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION,
551 'db_collation', 'select_db_collation', $db_collation, false, 3)
552 . '</fieldset>'
553 . '<fieldset class="tblFooters">'
554 . ' <input type="submit" name="submitcollation"'
555 . ' value="' . __('Go') . '" />' . "\n"
556 . '</fieldset>' . "\n"
557 . '</form></div>' . "\n";
559 if ($num_tables > 0
560 && ! $cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
561 $message = PMA_Message::notice(__('The phpMyAdmin configuration storage has been deactivated. To find out why click %shere%s.'));
562 $message->addParam('<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">', false);
563 $message->addParam('</a>', false);
564 /* Show error if user has configured something, notice elsewhere */
565 if (!empty($cfg['Servers'][$server]['pmadb'])) {
566 $message->isError(true);
568 echo '<div class="operations_full_width">';
569 $message->display();
570 echo '</div>';
571 } // end if
572 } // end if (!$is_information_schema)
575 // not sure about displaying the PDF dialog in case db is information_schema
576 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?>
577 <!-- Work on PDF Pages -->
579 <?php
580 // We only show this if we find something in the new pdf_pages table
582 $test_query = '
583 SELECT *
584 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
585 WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\'';
586 $test_rs = PMA_query_as_controluser($test_query, null, PMA_DBI_QUERY_STORE);
589 * Export Relational Schema View
591 echo '<div class="operations_full_width"><fieldset><a href="schema_edit.php?' . $url_query . '">';
592 if ($cfg['PropertiesIconic']) {
593 echo '<img class="icon ic_b_edit" src="themes/dot.gif" alt="" />';
595 echo __('Edit or export relational schema') . '</a></fieldset></div>';
596 } // end if
599 * Displays the footer
601 require './libraries/footer.inc.php';