Add <div> to allow selecting whole SQL by tripple click (patch #1611591).
[phpmyadmin/crack.git] / db_operations.php
blobadd3e6e282fdf76f832331cddd8974c618fd1b8b
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * handles miscellaneous db operations:
7 * - move/rename
8 * - copy
9 * - changing collation
10 * - changing comment
11 * - adding tables
12 * - viewing PDF schemas
15 /**
16 * requirements
18 require_once './libraries/common.lib.php';
19 require_once './libraries/Table.class.php';
20 require_once './libraries/mysql_charsets.lib.php';
22 /**
23 * Rename/move or copy database
25 if (isset($db) &&
26 ((isset($db_rename) && $db_rename == 'true') ||
27 (isset($db_copy) && $db_copy == 'true'))) {
29 if (isset($db_rename) && $db_rename == 'true') {
30 $move = true;
31 } else {
32 $move = false;
35 if (!isset($newname) || !strlen($newname)) {
36 $message = $strDatabaseEmpty;
37 } else {
38 $sql_query = ''; // in case target db exists
39 if ($move ||
40 (isset($create_database_before_copying) && $create_database_before_copying)) {
41 $local_query = 'CREATE DATABASE ' . PMA_backquote($newname);
42 if (isset($db_collation)) {
43 $local_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation);
45 $local_query .= ';';
46 $sql_query = $local_query;
47 PMA_DBI_query($local_query);
48 // rebuild the database list because PMA_Table::moveCopy
49 // checks in this list if the target db exists
50 $GLOBALS['PMA_List_Database']->build();
53 if (isset($GLOBALS['add_constraints'])) {
54 $GLOBALS['sql_constraints_query_full_db'] = '';
57 $tables_full = PMA_DBI_get_tables_full($db);
58 $views = array();
59 foreach ($tables_full as $table => $tmp) {
60 // to be able to rename a db containing views, we
61 // first collect in $views all the views we find and we
62 // will handle them after the tables
63 /**
64 * @todo support a view of a view
66 if (PMA_Table::isView($db, $table)) {
67 $views[] = $table;
68 continue;
71 $back = $sql_query;
72 $sql_query = '';
74 // value of $what for this table only
75 $this_what = $what;
77 if (!isset($tables_full[$table]['Engine'])) {
78 $tables_full[$table]['Engine'] = $tables_full[$table]['Type'];
80 // do not copy the data from a Merge table
81 // note: on the calling FORM, 'data' means 'structure and data'
82 if ($tables_full[$table]['Engine'] == 'MRG_MyISAM') {
83 if ($this_what == 'data') {
84 $this_what = 'structure';
86 if ($this_what == 'dataonly') {
87 $this_what = 'nocopy';
91 if ($this_what != 'nocopy') {
92 PMA_Table::moveCopy($db, $table, $newname, $table,
93 isset($this_what) ? $this_what : 'data', $move, 'db_copy');
94 if (isset($GLOBALS['add_constraints'])) {
95 $GLOBALS['sql_constraints_query_full_db'] .= $GLOBALS['sql_constraints_query'];
96 unset($GLOBALS['sql_constraints_query']);
100 $sql_query = $back . $sql_query;
101 } // end (foreach)
102 unset($table);
104 // handle the views
105 foreach ($views as $view) {
106 PMA_Table::moveCopy($db, $view, $newname, $view,
107 'structure', $move, 'db_copy');
109 unset($view, $views);
111 // now that all tables exist, create all the accumulated constraints
112 if (isset($GLOBALS['add_constraints'])) {
114 * @todo this works with mysqli but not with mysql, because
115 * mysql extension does not accept more than one statement; maybe
116 * interface with the sql import plugin that handles statement delimiter
118 PMA_DBI_query($GLOBALS['sql_constraints_query_full_db']);
120 // and prepare to display them
121 $GLOBALS['sql_query'] .= "\n" . $GLOBALS['sql_constraints_query_full_db'];
122 unset($GLOBALS['sql_constraints_query_full_db']);
125 // Duplicate the bookmarks for this db (done once for each db)
126 if ($db != $newname) {
127 $get_fields = array('user', 'label', 'query');
128 $where_fields = array('dbase' => $db);
129 $new_fields = array('dbase' => $newname);
130 PMA_Table::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
131 $where_fields, $new_fields);
134 if ($move) {
135 // cleanup pmadb stuff for this db
136 require_once './libraries/relation_cleanup.lib.php';
137 PMA_relationsCleanupDatabase($db);
139 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
140 $sql_query .= "\n" . $local_query;
141 PMA_DBI_query($local_query);
142 $message = sprintf($strRenameDatabaseOK, htmlspecialchars($db),
143 htmlspecialchars($newname));
144 } else {
145 $message = sprintf($strCopyDatabaseOK, htmlspecialchars($db),
146 htmlspecialchars($newname));
148 $reload = true;
150 /* Change database to be used */
151 if ($move) {
152 $db = $newname;
153 } else {
154 if (isset($switch_to_new) && $switch_to_new == 'true') {
155 PMA_setCookie('pma_switch_to_new', 'true');
156 $db = $newname;
157 } else {
158 PMA_setCookie('pma_switch_to_new', '');
164 * Settings for relations stuff
167 require_once './libraries/relation.lib.php';
168 $cfgRelation = PMA_getRelationsParam();
171 * Check if comments were updated
172 * (must be done before displaying the menu tabs)
174 if ($cfgRelation['commwork'] && isset($db_comment) && $db_comment == 'true') {
175 PMA_SetComment($db, '', '(db_comment)', $comment);
179 * Prepares the tables list if the user where not redirected to this script
180 * because there is no table in the database ($is_info is true)
182 if (empty($is_info)) {
183 require './libraries/db_common.inc.php';
184 $url_query .= '&amp;goto=db_operations.php';
186 // Gets the database structure
187 $sub_part = '_structure';
188 require './libraries/db_info.inc.php';
189 echo "\n";
192 if (PMA_MYSQL_INT_VERSION >= 40101) {
193 $db_collation = PMA_getDbCollation($db);
195 if (PMA_MYSQL_INT_VERSION < 50002
196 || (PMA_MYSQL_INT_VERSION >= 50002 && $db != 'information_schema')) {
197 $is_information_schema = false;
198 } else {
199 $is_information_schema = true;
202 if (!$is_information_schema) {
204 require './libraries/display_create_table.lib.php';
206 if ($cfgRelation['commwork']) {
208 * database comment
211 <form method="post" action="db_operations.php">
212 <?php echo PMA_generate_common_hidden_inputs($db); ?>
213 <input type="hidden" name="db_comment" value="true" />
214 <fieldset>
215 <legend>
216 <?php
217 if ($cfg['PropertiesIconic']) {
218 echo '<img class="icon" src="' . $pmaThemeImage . 'b_comment.png"'
219 .' alt="" border="0" width="16" height="16" hspace="2" align="middle" />';
221 echo $strDBComment;
222 $comment = PMA_getComments($db);
224 </legend>
225 <input type="text" name="comment" class="textfield" size="30"
226 value="<?php
227 echo (isset($comment) && is_array($comment)
228 ? htmlspecialchars(implode(' ', $comment))
229 : ''); ?>" />
230 <input type="submit" value="<?php echo $strGo; ?>" />
231 </fieldset>
232 </form>
233 <?php
236 * rename database
239 <form method="post" action="db_operations.php"
240 onsubmit="return emptyFormElements(this, 'newname')">
241 <input type="hidden" name="what" value="data" />
242 <input type="hidden" name="db_rename" value="true" />
243 <?php echo PMA_generate_common_hidden_inputs($db); ?>
244 <fieldset>
245 <legend>
246 <?php
247 if ($cfg['PropertiesIconic']) {
248 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
249 .' alt="" width="16" height="16" />';
251 echo $strDBRename . ':';
253 </legend>
254 <input type="text" name="newname" size="30" class="textfield" value="" />
255 <input type="submit" value="<?php echo $strGo; ?>" />
256 </fieldset>
257 </form>
259 <?php
261 * Copy database
264 <form method="post" action="db_operations.php"
265 onsubmit="return emptyFormElements(this, 'newname')">
266 <?php
267 if (isset($db_collation)) {
268 echo '<input type="hidden" name="db_collation" value="' . $db_collation
269 .'" />' . "\n";
271 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
272 echo PMA_generate_common_hidden_inputs($db);
274 <fieldset>
275 <legend>
276 <?php
277 if ($cfg['PropertiesIconic']) {
278 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
279 .' alt="" width="16" height="16" />';
281 echo $strDBCopy . ':';
282 if (PMA_MYSQL_INT_VERSION >= 50000) {
283 $drop_clause = 'DROP TABLE / DROP VIEW';
284 } else {
285 $drop_clause = 'DROP TABLE';
288 </legend>
289 <input type="text" name="newname" size="30" class="textfield" value="" /><br />
290 <input type="radio" name="what" value="structure"
291 id="radio_copy_structure" style="vertical-align: middle" />
292 <label for="radio_copy_structure"><?php echo $strStrucOnly; ?></label><br />
293 <input type="radio" name="what" value="data" id="radio_copy_data"
294 checked="checked" style="vertical-align: middle" />
295 <label for="radio_copy_data"><?php echo $strStrucData; ?></label><br />
296 <input type="radio" name="what" value="dataonly"
297 id="radio_copy_dataonly" style="vertical-align: middle" />
298 <label for="radio_copy_dataonly"><?php echo $strDataOnly; ?></label><br />
300 <input type="checkbox" name="create_database_before_copying" value="1"
301 id="checkbox_create_database_before_copying"
302 style="vertical-align: middle" checked="checked" />
303 <label for="checkbox_create_database_before_copying">
304 <?php echo $strCreateDatabaseBeforeCopying; ?></label><br />
305 <input type="checkbox" name="drop_if_exists" value="true"
306 id="checkbox_drop" style="vertical-align: middle" />
307 <label for="checkbox_drop"><?php echo sprintf($strAddClause, $drop_clause); ?></label><br />
308 <input type="checkbox" name="sql_auto_increment" value="1"
309 id="checkbox_auto_increment" style="vertical-align: middle" />
310 <label for="checkbox_auto_increment">
311 <?php echo $strAddAutoIncrement; ?></label><br />
312 <input type="checkbox" name="add_constraints" value="1"
313 id="checkbox_constraints" style="vertical-align: middle" />
314 <label for="checkbox_constraints">
315 <?php echo $strAddConstraints; ?></label><br />
316 <?php
317 unset($drop_clause);
319 if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
320 && $_COOKIE['pma_switch_to_new'] == 'true') {
321 $pma_switch_to_new = 'true';
324 <input type="checkbox" name="switch_to_new" value="true"
325 id="checkbox_switch"
326 <?php echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ? ' checked="checked"' : ''); ?>
327 style="vertical-align: middle" />
328 <label for="checkbox_switch"><?php echo $strSwitchToDatabase; ?></label>
329 </fieldset>
330 <fieldset class="tblFooters">
331 <input type="submit" name="submit_copy" value="<?php echo $strGo; ?>" />
332 </fieldset>
333 </form>
335 <?php
337 * Change database charset
339 if (PMA_MYSQL_INT_VERSION >= 40101) {
340 // MySQL supports setting default charsets / collations for databases since
341 // version 4.1.1.
342 echo '<form method="post" action="./db_operations.php">' . "\n"
343 . PMA_generate_common_hidden_inputs($db, isset($table) ? $table : '')
344 . '<fieldset>' . "\n"
345 . ' <legend>';
346 if ($cfg['PropertiesIconic']) {
347 echo '<img class="icon" src="' . $pmaThemeImage . 's_asci.png"'
348 .' alt="" width="16" height="16" />';
350 echo ' <label for="select_db_collation">' . $strCollation . ':</label>' . "\n"
351 . ' </legend>' . "\n"
352 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION,
353 'db_collation', 'select_db_collation', $db_collation, false, 3)
354 . ' <input type="submit" name="submitcollation"'
355 . ' value="' . $strGo . '" style="vertical-align: middle" />' . "\n"
356 . '</fieldset>' . "\n"
357 . '</form>' . "\n";
360 if ($num_tables > 0
361 && !$cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
362 /* Show error if user has configured something, notice elsewhere */
363 if (!empty($cfg['Servers'][$server]['pmadb'])) {
364 echo '<div class="error"><h1>' . $strError . '</h1>';
365 } else {
366 echo '<div class="notice">';
368 printf($strRelationNotWorking, '<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">', '</a>');
369 echo '</div>';
370 } // end if
371 } // end if (!$is_information_schema)
374 // not sure about leaving the PDF dialog for information_schema
375 if ($num_tables > 0 && isset($table)) {
376 $takeaway = $url_query . '&amp;table=' . urlencode($table);
377 } else {
378 $takeaway = '';
381 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?>
382 <!-- Work on PDF Pages -->
384 <?php
385 // We only show this if we find something in the new pdf_pages table
387 $test_query = '
388 SELECT *
389 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
390 WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
391 $test_rs = PMA_query_as_cu($test_query, null, PMA_DBI_QUERY_STORE);
393 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) { ?>
394 <!-- PDF schema -->
395 <form method="post" action="pdf_schema.php">
396 <fieldset>
397 <legend>
398 <?php
399 echo PMA_generate_common_hidden_inputs($db);
400 if ($cfg['PropertiesIconic']) {
401 echo '<img class="icon" src="' . $pmaThemeImage . 'b_view.png"'
402 .' alt="" width="16" height="16" />';
404 echo $strDisplayPDF;
406 </legend>
407 <label for="pdf_page_number_opt"><?php echo $strPageNumber; ?></label>
408 <select name="pdf_page_number" id="pdf_page_number_opt">
409 <?php
410 while ($pages = @PMA_DBI_fetch_assoc($test_rs)) {
411 echo ' <option value="' . $pages['page_nr'] . '">'
412 . $pages['page_nr'] . ': ' . $pages['page_descr'] . '</option>' . "\n";
413 } // end while
414 PMA_DBI_free_result($test_rs);
415 unset($test_rs);
417 </select><br />
419 <input type="checkbox" name="show_grid" id="show_grid_opt" />
420 <label for="show_grid_opt"><?php echo $strShowGrid; ?></label><br />
421 <input type="checkbox" name="show_color" id="show_color_opt"
422 checked="checked" />
423 <label for="show_color_opt"><?php echo $strShowColor; ?></label><br />
424 <input type="checkbox" name="show_table_dimension" id="show_table_dim_opt" />
425 <label for="show_table_dim_opt"><?php echo $strShowTableDimension; ?>
426 </label><br />
427 <input type="checkbox" name="all_tab_same_wide" id="all_tab_same_wide" />
428 <label for="all_tab_same_wide"><?php echo $strAllTableSameWidth; ?>
429 </label><br />
430 <input type="checkbox" name="with_doc" id="with_doc" checked="checked" />
431 <label for="with_doc"><?php echo $strDataDict; ?></label><br />
433 <label for="orientation_opt"><?php echo $strShowDatadictAs; ?></label>
434 <select name="orientation" id="orientation_opt">
435 <option value="L"><?php echo $strLandscape;?></option>
436 <option value="P"><?php echo $strPortrait;?></option>
437 </select><br />
439 <label for="paper_opt"><?php echo $strPaperSize; ?></label>
440 <select name="paper" id="paper_opt">
441 <?php
442 foreach ($cfg['PDFPageSizes'] AS $key => $val) {
443 echo '<option value="' . $val . '"';
444 if ($val == $cfg['PDFDefaultPageSize']) {
445 echo ' selected="selected"';
447 echo ' >' . $val . '</option>' . "\n";
450 </select>
451 </fieldset>
452 <fieldset class="tblFooters">
453 <input type="submit" value="<?php echo $strGo; ?>" />
454 </fieldset>
455 </form>
456 <?php
457 } // end if
459 <ul>
460 <li>
461 <?php
462 echo '<a href="pdf_pages.php?' . $takeaway . '">';
463 if ($cfg['PropertiesIconic']) {
464 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
465 .' alt="" width="16" height="16" />';
467 echo $strEditPDFPages . '</a>';
469 </li>
470 </ul>
471 <?php
472 } // end if
475 * Displays the footer
477 require_once './libraries/footer.inc.php';