Merge branch 'master' of ssh://phpmyadmin.git.sourceforge.net/gitroot/phpmyadmin...
[phpmyadmin/crack.git] / db_operations.php
blob288de7bfe2e4bbcf20414e75ec726d4d1d56abad
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 = 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 if (PMA_MYSQL_INT_VERSION >= 50000) {
73 // here I don't use DELIMITER because it's not part of the
74 // language; I have to send each statement one by one
76 // to avoid selecting alternatively the current and new db
77 // we would need to modify the CREATE definitions to qualify
78 // the db name
79 $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
80 if ($procedure_names) {
81 foreach($procedure_names as $procedure_name) {
82 PMA_DBI_select_db($db);
83 $tmp_query = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure_name);
84 // collect for later display
85 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
86 PMA_DBI_select_db($newname);
87 PMA_DBI_query($tmp_query);
91 $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
92 if ($function_names) {
93 foreach($function_names as $function_name) {
94 PMA_DBI_select_db($db);
95 $tmp_query = PMA_DBI_get_definition($db, 'FUNCTION', $function_name);
96 // collect for later display
97 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
98 PMA_DBI_select_db($newname);
99 PMA_DBI_query($tmp_query);
103 // go back to current db, just in case
104 PMA_DBI_select_db($db);
106 if (isset($GLOBALS['add_constraints']) || $move) {
107 $GLOBALS['sql_constraints_query_full_db'] = array();
110 $tables_full = PMA_DBI_get_tables_full($db);
111 $views = array();
113 // remove all foreign key constraints, otherwise we can get errors
114 require_once './libraries/export/sql.php';
115 foreach ($tables_full as $each_table => $tmp) {
116 $sql_constraints = '';
117 $sql_drop_foreign_keys = '';
118 $sql_structure = PMA_getTableDef($db, $each_table, "\n", '', false, false);
119 if ($move && ! empty($sql_drop_foreign_keys)) {
120 PMA_DBI_query($sql_drop_foreign_keys);
122 // keep the constraint we just dropped
123 if (! empty($sql_constraints)) {
124 $GLOBALS['sql_constraints_query_full_db'][] = $sql_constraints;
127 unset($sql_constraints, $sql_drop_foreign_keys, $sql_structure);
130 foreach ($tables_full as $each_table => $tmp) {
131 // to be able to rename a db containing views, we
132 // first collect in $views all the views we find and we
133 // will handle them after the tables
135 * @todo support a view of a view
137 if (PMA_Table::isView($db, $each_table)) {
138 $views[] = $each_table;
139 continue;
142 $back = $sql_query;
143 $sql_query = '';
145 // value of $what for this table only
146 $this_what = $what;
148 // do not copy the data from a Merge table
149 // note: on the calling FORM, 'data' means 'structure and data'
150 if (PMA_Table::isMerge($db, $each_table)) {
151 if ($this_what == 'data') {
152 $this_what = 'structure';
154 if ($this_what == 'dataonly') {
155 $this_what = 'nocopy';
159 if ($this_what != 'nocopy') {
160 // keep the triggers from the original db+table
161 // (third param is empty because delimiters are only intended
162 // for importing via the mysql client or our Import feature)
163 $triggers = PMA_DBI_get_triggers($db, $each_table, '');
165 if (! PMA_Table::moveCopy($db, $each_table, $newname, $each_table,
166 isset($this_what) ? $this_what : 'data', $move, 'db_copy'))
168 $_error = true;
169 // $sql_query is filled by PMA_Table::moveCopy()
170 $sql_query = $back . $sql_query;
171 break;
173 // apply the triggers to the destination db+table
174 if ($triggers) {
175 PMA_DBI_select_db($newname);
176 foreach ($triggers as $trigger) {
177 PMA_DBI_query($trigger['create']);
179 unset($trigger);
181 unset($triggers);
183 // this does not apply to a rename operation
184 if (isset($GLOBALS['add_constraints']) && !empty($GLOBALS['sql_constraints_query'])) {
185 $GLOBALS['sql_constraints_query_full_db'][] = $GLOBALS['sql_constraints_query'];
186 unset($GLOBALS['sql_constraints_query']);
189 // $sql_query is filled by PMA_Table::moveCopy()
190 $sql_query = $back . $sql_query;
191 } // end (foreach)
192 unset($each_table);
194 // handle the views
195 if (! $_error) {
196 foreach ($views as $view) {
197 if (! PMA_Table::moveCopy($db, $view, $newname, $view,
198 'structure', $move, 'db_copy')) {
199 $_error = true;
200 break;
204 unset($view, $views);
206 // now that all tables exist, create all the accumulated constraints
207 if (! $_error && count($GLOBALS['sql_constraints_query_full_db']) > 0) {
208 PMA_DBI_select_db($newname);
209 foreach ($GLOBALS['sql_constraints_query_full_db'] as $one_query) {
210 PMA_DBI_query($one_query);
211 // and prepare to display them
212 $GLOBALS['sql_query'] .= "\n" . $one_query;
215 unset($GLOBALS['sql_constraints_query_full_db'], $one_query);
218 // Duplicate the bookmarks for this db (done once for each db)
219 if (! $_error && $db != $newname) {
220 $get_fields = array('user', 'label', 'query');
221 $where_fields = array('dbase' => $db);
222 $new_fields = array('dbase' => $newname);
223 PMA_Table::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
224 $where_fields, $new_fields);
227 if (! $_error && $move) {
229 * cleanup pmadb stuff for this db
231 require_once './libraries/relation_cleanup.lib.php';
232 PMA_relationsCleanupDatabase($db);
234 // if someday the RENAME DATABASE reappears, do not DROP
235 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
236 $sql_query .= "\n" . $local_query;
237 PMA_DBI_query($local_query);
239 $message = PMA_Message::success(__('Database %s has been renamed to %s'));
240 $message->addParam($db);
241 $message->addParam($newname);
242 } elseif (! $_error) {
243 $message = PMA_Message::success(__('Database %s has been copied to %s'));
244 $message->addParam($db);
245 $message->addParam($newname);
247 $reload = true;
249 /* Change database to be used */
250 if (! $_error && $move) {
251 $db = $newname;
252 } elseif (! $_error) {
253 if (isset($switch_to_new) && $switch_to_new == 'true') {
254 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
255 $db = $newname;
256 } else {
257 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
261 if ($_error && ! isset($message)) {
262 $message = PMA_Message::error();
267 * Database has been successfully renamed/moved. If in an Ajax request,
268 * generate the output with {@link PMA_ajaxResponse} and exit
270 if( $GLOBALS['is_ajax_request'] == true) {
271 $extra_data['newname'] = $newname;
272 $extra_data['sql_query'] = PMA_showMessage(NULL, $sql_query);
273 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
279 * Settings for relations stuff
282 $cfgRelation = PMA_getRelationsParam();
285 * Check if comments were updated
286 * (must be done before displaying the menu tabs)
288 if (isset($_REQUEST['comment'])) {
289 PMA_setDbComment($db, $comment);
293 * Prepares the tables list if the user where not redirected to this script
294 * because there is no table in the database ($is_info is true)
296 if (empty($is_info)) {
297 require './libraries/db_common.inc.php';
298 $url_query .= '&amp;goto=db_operations.php';
300 // Gets the database structure
301 $sub_part = '_structure';
302 require './libraries/db_info.inc.php';
303 echo "\n";
305 if (isset($message)) {
306 PMA_showMessage($message, $sql_query);
307 unset($message);
311 $db_collation = PMA_getDbCollation($db);
312 if ($db == 'information_schema') {
313 $is_information_schema = true;
314 } else {
315 $is_information_schema = false;
318 if (!$is_information_schema) {
320 require './libraries/display_create_table.lib.php';
322 if ($cfgRelation['commwork']) {
324 * database comment
327 <form method="post" action="db_operations.php">
328 <?php echo PMA_generate_common_hidden_inputs($db); ?>
329 <fieldset>
330 <legend>
331 <?php echo PMA_getIcon('b_comment.png', __('Database comment: '), false, true); ?>
332 </legend>
333 <input type="text" name="comment" class="textfield" size="30"
334 value="<?php
335 echo htmlspecialchars(PMA_getDBComment($db)); ?>" />
336 </fieldset>
337 <fieldset class="tblFooters">
338 <input type="submit" value="<?php echo __('Go'); ?>" />
339 </fieldset>
340 </form>
341 <?php
344 * rename database
346 if ($db != 'mysql') {
348 <form id="rename_db_form" method="post" action="db_operations.php"
349 onsubmit="return emptyFormElements(this, 'newname')">
350 <?php
351 if (isset($db_collation)) {
352 echo '<input type="hidden" name="db_collation" value="' . $db_collation
353 .'" />' . "\n";
356 <input type="hidden" name="what" value="data" />
357 <input type="hidden" name="db_rename" value="true" />
358 <?php echo PMA_generate_common_hidden_inputs($db); ?>
359 <fieldset>
360 <legend>
361 <?php
362 if ($cfg['PropertiesIconic']) {
363 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
364 .' alt="" width="16" height="16" />';
366 echo __('Rename database to') . ':';
368 </legend>
369 <input type="text" name="newname" size="30" class="textfield" value="" />
370 <?php
371 echo '(' . __('Command') . ': ';
373 * @todo (see explanations above in a previous todo)
375 //if (PMA_MYSQL_INT_VERSION >= XYYZZ) {
376 // echo 'RENAME DATABASE';
377 //} else {
378 echo 'INSERT INTO ... SELECT';
380 echo ')'; ?>
381 </fieldset>
382 <fieldset class="tblFooters">
383 <input id="rename_db_input" type="submit" value="<?php echo __('Go'); ?>" />
384 </fieldset>
385 </form>
386 <?php
387 } // end if
389 // Drop link if allowed
390 // Don't even try to drop information_schema. You won't be able to. Believe me. You won't.
391 // Don't allow to easily drop mysql database, RFE #1327514.
392 if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase']) && ! $db_is_information_schema && ($db != 'mysql')) {
394 <fieldset class="caution">
395 <legend><?php
396 if ($cfg['PropertiesIconic']) {
397 echo '<img class="icon" src="' . $pmaThemeImage . 'b_deltbl.png"'
398 .' alt="" width="16" height="16" />';
400 echo __('Remove database');
401 ?></legend>
403 <ul>
404 <?php
405 $this_sql_query = 'DROP DATABASE ' . PMA_backquote($GLOBALS['db']);
406 $this_url_params = array(
407 'sql_query' => $this_sql_query,
408 'back' => 'db_operations.php',
409 'goto' => 'main.php',
410 'reload' => '1',
411 'purge' => '1',
412 'message_to_show' => sprintf(__('Database %s has been dropped.'), htmlspecialchars(PMA_backquote($db))),
413 'db' => NULL,
416 <li><a href="sql.php<?php echo PMA_generate_common_url($this_url_params); ?>" onclick="return confirmLinkDropDB(this, '<?php echo PMA_jsFormat($this_sql_query); ?>')">
417 <?php echo __('Drop the database (DROP)'); ?></a>
418 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'DROP_DATABASE'); ?>
419 </li>
420 </ul>
421 </fieldset>
422 <?php } ?>
423 <?php
425 * Copy database
428 <form id="copy_db_form" method="post" action="db_operations.php"
429 onsubmit="return emptyFormElements(this, 'newname')">
430 <?php
431 if (isset($db_collation)) {
432 echo '<input type="hidden" name="db_collation" value="' . $db_collation
433 .'" />' . "\n";
435 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
436 echo PMA_generate_common_hidden_inputs($db);
438 <fieldset>
439 <legend>
440 <?php
441 if ($cfg['PropertiesIconic']) {
442 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
443 .' alt="" width="16" height="16" />';
445 echo __('Copy database to') . ':';
446 $drop_clause = 'DROP TABLE / DROP VIEW';
448 </legend>
449 <input type="text" name="newname" size="30" class="textfield" value="" /><br />
450 <?php
451 $choices = array(
452 'structure' => __('Structure only'),
453 'data' => __('Structure and data'),
454 'dataonly' => __('Data only'));
455 PMA_display_html_radio('what', $choices, 'data', true);
456 unset($choices);
458 <input type="checkbox" name="create_database_before_copying" value="1"
459 id="checkbox_create_database_before_copying"
460 checked="checked" />
461 <label for="checkbox_create_database_before_copying">
462 <?php echo __('CREATE DATABASE before copying'); ?></label><br />
463 <input type="checkbox" name="drop_if_exists" value="true"
464 id="checkbox_drop" />
465 <label for="checkbox_drop"><?php echo sprintf(__('Add %s'), $drop_clause); ?></label><br />
466 <input type="checkbox" name="sql_auto_increment" value="1" checked="checked"
467 id="checkbox_auto_increment" />
468 <label for="checkbox_auto_increment">
469 <?php echo __('Add AUTO_INCREMENT value'); ?></label><br />
470 <input type="checkbox" name="add_constraints" value="1"
471 id="checkbox_constraints" />
472 <label for="checkbox_constraints">
473 <?php echo __('Add constraints'); ?></label><br />
474 <?php
475 unset($drop_clause);
477 if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
478 && $_COOKIE['pma_switch_to_new'] == 'true') {
479 $pma_switch_to_new = 'true';
482 <input type="checkbox" name="switch_to_new" value="true"
483 id="checkbox_switch"
484 <?php echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ? ' checked="checked"' : ''); ?>
486 <label for="checkbox_switch"><?php echo __('Switch to copied database'); ?></label>
487 </fieldset>
488 <fieldset class="tblFooters">
489 <input type="submit" name="submit_copy" value="<?php echo __('Go'); ?>" />
490 </fieldset>
491 </form>
493 <?php
496 * Change database charset
498 echo '<form id="change_db_charset_form" method="post" action="./db_operations.php">' . "\n"
499 . PMA_generate_common_hidden_inputs($db, $table)
500 . '<fieldset>' . "\n"
501 . ' <legend>';
502 if ($cfg['PropertiesIconic']) {
503 echo '<img class="icon" src="' . $pmaThemeImage . 's_asci.png"'
504 .' alt="" width="16" height="16" />';
506 echo ' <label for="select_db_collation">' . __('Collation') . ':</label>' . "\n"
507 . ' </legend>' . "\n"
508 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION,
509 'db_collation', 'select_db_collation', $db_collation, false, 3)
510 . '</fieldset>'
511 . '<fieldset class="tblFooters">'
512 . ' <input type="submit" name="submitcollation"'
513 . ' value="' . __('Go') . '" />' . "\n"
514 . '</fieldset>' . "\n"
515 . '</form>' . "\n";
517 if ($num_tables > 0
518 && !$cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
519 $message = PMA_Message::notice(__('The phpMyAdmin configuration storage has been deactivated. To find out why click %shere%s.'));
520 $message->addParam('<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">', false);
521 $message->addParam('</a>', false);
522 /* Show error if user has configured something, notice elsewhere */
523 if (!empty($cfg['Servers'][$server]['pmadb'])) {
524 $message->isError(true);
526 $message->display();
527 } // end if
528 } // end if (!$is_information_schema)
531 // not sure about displaying the PDF dialog in case db is information_schema
532 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?>
533 <!-- Work on PDF Pages -->
535 <?php
536 // We only show this if we find something in the new pdf_pages table
538 $test_query = '
539 SELECT *
540 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
541 WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
542 $test_rs = PMA_query_as_controluser($test_query, null, PMA_DBI_QUERY_STORE);
545 * Export Relational Schema View
547 echo '<fieldset><a href="schema_edit.php?' . $url_query . '">';
548 if ($cfg['PropertiesIconic']) {
549 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
550 .' alt="" width="16" height="16" />';
552 echo __('Edit or export relational schema') . '</a></fieldset>';
553 } // end if
556 * Displays the footer
558 require './libraries/footer.inc.php';