link releases to tags in CVS
[phpmyadmin/crack.git] / db_operations.php
blob30168deec3d0c59c8b72a39a8f149a5a7f9fd92d
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 if ($move ||
39 (isset($create_database_before_copying) && $create_database_before_copying)) {
40 $local_query = 'CREATE DATABASE ' . PMA_backquote($newname);
41 if (isset($db_collation)) {
42 $local_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation);
44 $local_query .= ';';
45 $sql_query = $local_query;
46 PMA_DBI_query($local_query);
49 $tables_full = PMA_DBI_get_tables_full($db);
50 foreach ($tables_full as $table => $tmp) {
51 $back = $sql_query;
52 $sql_query = '';
54 // value of $what for this table only
55 $this_what = $what;
57 if (!isset($tables_full[$table]['Engine'])) {
58 $tables_full[$table]['Engine'] = $tables_full[$table]['Type'];
60 // do not copy the data from a Merge table
61 // note: on the calling FORM, 'data' means 'structure and data'
62 if ($tables_full[$table]['Engine'] == 'MRG_MyISAM') {
63 if ($this_what == 'data') {
64 $this_what = 'structure';
66 if ($this_what == 'dataonly') {
67 $this_what = 'nocopy';
71 if ($this_what != 'nocopy') {
72 PMA_Table::moveCopy($db, $table, $newname, $table,
73 isset($this_what) ? $this_what : 'data', $move);
76 $sql_query = $back . $sql_query;
78 unset($table);
80 // Duplicate the bookmarks for this db (done once for each db)
81 if ($db != $newname) {
82 $get_fields = array('user', 'label', 'query');
83 $where_fields = array('dbase' => $db);
84 $new_fields = array('dbase' => $newname);
85 PMA_Table::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
86 $where_fields, $new_fields);
89 if ($move) {
90 // cleanup pmadb stuff for this db
91 require_once './libraries/relation_cleanup.lib.php';
92 PMA_relationsCleanupDatabase($db);
94 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
95 $sql_query .= "\n" . $local_query;
96 PMA_DBI_query($local_query);
97 $message = sprintf($strRenameDatabaseOK, htmlspecialchars($db),
98 htmlspecialchars($newname));
99 } else {
100 $message = sprintf($strCopyDatabaseOK, htmlspecialchars($db),
101 htmlspecialchars($newname));
103 $reload = true;
105 /* Change database to be used */
106 if ($move) {
107 $db = $newname;
108 } else {
109 if (isset($switch_to_new) && $switch_to_new == 'true') {
110 PMA_setCookie('pma_switch_to_new', 'true');
111 $db = $newname;
112 } else {
113 PMA_setCookie('pma_switch_to_new', '');
119 * Settings for relations stuff
122 require_once './libraries/relation.lib.php';
123 $cfgRelation = PMA_getRelationsParam();
126 * Check if comments were updated
127 * (must be done before displaying the menu tabs)
129 if ($cfgRelation['commwork'] && isset($db_comment) && $db_comment == 'true') {
130 PMA_SetComment($db, '', '(db_comment)', $comment);
134 * Prepares the tables list if the user where not redirected to this script
135 * because there is no table in the database ($is_info is true)
137 if (empty($is_info)) {
138 require './libraries/db_details_common.inc.php';
139 $url_query .= '&amp;goto=db_operations.php';
141 // Gets the database structure
142 $sub_part = '_structure';
143 require './libraries/db_details_db_info.inc.php';
144 echo "\n";
147 if (PMA_MYSQL_INT_VERSION >= 40101) {
148 $db_collation = PMA_getDbCollation($db);
150 if (PMA_MYSQL_INT_VERSION < 50002
151 || (PMA_MYSQL_INT_VERSION >= 50002 && $db != 'information_schema')) {
152 $is_information_schema = false;
153 } else {
154 $is_information_schema = true;
157 if (!$is_information_schema) {
159 require './libraries/display_create_table.lib.php';
161 if ($cfgRelation['commwork']) {
163 * database comment
166 <form method="post" action="db_operations.php">
167 <?php echo PMA_generate_common_hidden_inputs($db); ?>
168 <input type="hidden" name="db_comment" value="true" />
169 <fieldset>
170 <legend>
171 <?php
172 if ($cfg['PropertiesIconic']) {
173 echo '<img class="icon" src="' . $pmaThemeImage . 'b_comment.png"'
174 .' alt="" border="0" width="16" height="16" hspace="2" align="middle" />';
176 echo $strDBComment;
177 $comment = PMA_getComments($db);
179 </legend>
180 <input type="text" name="comment" class="textfield" size="30"
181 value="<?php
182 echo (isset($comment) && is_array($comment)
183 ? htmlspecialchars(implode(' ', $comment))
184 : ''); ?>" />
185 <input type="submit" value="<?php echo $strGo; ?>" />
186 </fieldset>
187 </form>
188 <?php
191 * rename database
194 <form method="post" action="db_operations.php"
195 onsubmit="return emptyFormElements(this, 'newname')">
196 <input type="hidden" name="what" value="data" />
197 <input type="hidden" name="db_rename" value="true" />
198 <?php echo PMA_generate_common_hidden_inputs($db); ?>
199 <fieldset>
200 <legend>
201 <?php
202 if ($cfg['PropertiesIconic']) {
203 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
204 .' alt="" width="16" height="16" />';
206 echo $strDBRename . ':';
208 </legend>
209 <input type="text" name="newname" size="30" class="textfield" value="" />
210 <input type="submit" value="<?php echo $strGo; ?>" />
211 </fieldset>
212 </form>
214 <?php
216 * Copy database
219 <form method="post" action="db_operations.php"
220 onsubmit="return emptyFormElements(this, 'newname')">
221 <?php
222 if (isset($db_collation)) {
223 echo '<input type="hidden" name="db_collation" value="' . $db_collation
224 .'" />' . "\n";
226 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
227 echo PMA_generate_common_hidden_inputs($db);
229 <fieldset>
230 <legend>
231 <?php
232 if ($cfg['PropertiesIconic']) {
233 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
234 .' alt="" width="16" height="16" />';
236 echo $strDBCopy . ':';
237 if (PMA_MYSQL_INT_VERSION >= 50000) {
238 $drop_clause = 'DROP TABLE / DROP VIEW';
239 } else {
240 $drop_clause = 'DROP TABLE';
243 </legend>
244 <input type="text" name="newname" size="30" class="textfield" value="" /><br />
245 <input type="radio" name="what" value="structure"
246 id="radio_copy_structure" style="vertical-align: middle" />
247 <label for="radio_copy_structure"><?php echo $strStrucOnly; ?></label><br />
248 <input type="radio" name="what" value="data" id="radio_copy_data"
249 checked="checked" style="vertical-align: middle" />
250 <label for="radio_copy_data"><?php echo $strStrucData; ?></label><br />
251 <input type="radio" name="what" value="dataonly"
252 id="radio_copy_dataonly" style="vertical-align: middle" />
253 <label for="radio_copy_dataonly"><?php echo $strDataOnly; ?></label><br />
255 <input type="checkbox" name="create_database_before_copying" value="1"
256 id="checkbox_create_database_before_copying"
257 style="vertical-align: middle" checked="checked" />
258 <label for="checkbox_create_database_before_copying">
259 <?php echo $strCreateDatabaseBeforeCopying; ?></label><br />
260 <input type="checkbox" name="drop_if_exists" value="true"
261 id="checkbox_drop" style="vertical-align: middle" />
262 <label for="checkbox_drop"><?php echo sprintf($strAddClause, $drop_clause); ?></label><br />
263 <input type="checkbox" name="sql_auto_increment" value="1"
264 id="checkbox_auto_increment" style="vertical-align: middle" />
265 <label for="checkbox_auto_increment">
266 <?php echo $strAddAutoIncrement; ?></label><br />
267 <input type="checkbox" name="constraints" value="1"
268 id="checkbox_constraints" style="vertical-align: middle" />
269 <label for="checkbox_constraints">
270 <?php echo $strAddConstraints; ?></label><br />
271 <?php
272 unset($drop_clause);
274 if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
275 && $_COOKIE['pma_switch_to_new'] == 'true') {
276 $pma_switch_to_new = 'true';
279 <input type="checkbox" name="switch_to_new" value="true"
280 id="checkbox_switch"
281 <?php echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ? ' checked="checked"' : ''); ?>
282 style="vertical-align: middle" />
283 <label for="checkbox_switch"><?php echo $strSwitchToDatabase; ?></label>
284 </fieldset>
285 <fieldset class="tblFooters">
286 <input type="submit" name="submit_copy" value="<?php echo $strGo; ?>" />
287 </fieldset>
288 </form>
290 <?php
292 * Change database charset
294 if (PMA_MYSQL_INT_VERSION >= 40101) {
295 // MySQL supports setting default charsets / collations for databases since
296 // version 4.1.1.
297 echo '<form method="post" action="./db_operations.php">' . "\n"
298 . PMA_generate_common_hidden_inputs($db, $table)
299 . '<fieldset>' . "\n"
300 . ' <legend>';
301 if ($cfg['PropertiesIconic']) {
302 echo '<img class="icon" src="' . $pmaThemeImage . 's_asci.png"'
303 .' alt="" width="16" height="16" />';
305 echo ' <label for="select_db_collation">' . $strCollation . ':</label>' . "\n"
306 . ' </legend>' . "\n"
307 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION,
308 'db_collation', 'select_db_collation', $db_collation, false, 3)
309 . ' <input type="submit" name="submitcollation"'
310 . ' value="' . $strGo . '" style="vertical-align: middle" />' . "\n"
311 . '</fieldset>' . "\n"
312 . '</form>' . "\n";
315 if ($num_tables > 0
316 && !$cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
317 echo '<div class="error"><h1>' . $strError . '</h1>'
318 . sprintf($strRelationNotWorking,
319 '<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">',
320 '</a>')
321 . '</div>';
322 } // end if
323 } // end if (!$is_information_schema)
326 // not sure about leaving the PDF dialog for information_schema
327 if ($num_tables > 0) {
328 $takeaway = $url_query . '&amp;table=' . urlencode($table);
331 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?>
332 <!-- Work on PDF Pages -->
334 <?php
335 // We only show this if we find something in the new pdf_pages table
337 $test_query = '
338 SELECT *
339 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
340 WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
341 $test_rs = PMA_query_as_cu($test_query, null, PMA_DBI_QUERY_STORE);
343 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) { ?>
344 <!-- PDF schema -->
345 <form method="post" action="pdf_schema.php">
346 <fieldset>
347 <legend>
348 <?php
349 echo PMA_generate_common_hidden_inputs($db);
350 if ($cfg['PropertiesIconic']) {
351 echo '<img class="icon" src="' . $pmaThemeImage . 'b_view.png"'
352 .' alt="" width="16" height="16" />';
354 echo $strDisplayPDF;
356 </legend>
357 <label for="pdf_page_number_opt"><?php echo $strPageNumber; ?></label>
358 <select name="pdf_page_number" id="pdf_page_number_opt">
359 <?php
360 while ($pages = @PMA_DBI_fetch_assoc($test_rs)) {
361 echo ' <option value="' . $pages['page_nr'] . '">'
362 . $pages['page_nr'] . ': ' . $pages['page_descr'] . '</option>' . "\n";
363 } // end while
364 PMA_DBI_free_result($test_rs);
365 unset($test_rs);
367 </select><br />
369 <input type="checkbox" name="show_grid" id="show_grid_opt" />
370 <label for="show_grid_opt"><?php echo $strShowGrid; ?></label><br />
371 <input type="checkbox" name="show_color" id="show_color_opt"
372 checked="checked" />
373 <label for="show_color_opt"><?php echo $strShowColor; ?></label><br />
374 <input type="checkbox" name="show_table_dimension" id="show_table_dim_opt" />
375 <label for="show_table_dim_opt"><?php echo $strShowTableDimension; ?>
376 </label><br />
377 <input type="checkbox" name="all_tab_same_wide" id="all_tab_same_wide" />
378 <label for="all_tab_same_wide"><?php echo $strAllTableSameWidth; ?>
379 </label><br />
380 <input type="checkbox" name="with_doc" id="with_doc" checked="checked" />
381 <label for="with_doc"><?php echo $strDataDict; ?></label><br />
383 <label for="orientation_opt"><?php echo $strShowDatadictAs; ?></label>
384 <select name="orientation" id="orientation_opt">
385 <option value="L"><?php echo $strLandscape;?></option>
386 <option value="P"><?php echo $strPortrait;?></option>
387 </select><br />
389 <label for="paper_opt"><?php echo $strPaperSize; ?></label>
390 <select name="paper" id="paper_opt">
391 <?php
392 foreach ($cfg['PDFPageSizes'] AS $key => $val) {
393 echo '<option value="' . $val . '"';
394 if ($val == $cfg['PDFDefaultPageSize']) {
395 echo ' selected="selected"';
397 echo ' >' . $val . '</option>' . "\n";
400 </select>
401 </fieldset>
402 <fieldset class="tblFooters">
403 <input type="submit" value="<?php echo $strGo; ?>" />
404 </fieldset>
405 </form>
406 <?php
407 } // end if
409 <ul>
410 <li>
411 <?php
412 echo '<a href="pdf_pages.php?' . $takeaway . '">';
413 if ($cfg['PropertiesIconic']) {
414 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
415 .' alt="" width="16" height="16" />';
417 echo $strEditPDFPages . '</a>';
419 </li>
420 </ul>
421 <?php
422 } // end if
424 if ($num_tables > 0
425 && $cfgRelation['relwork'] && $cfgRelation['commwork']
426 && isset($cfg['docSQLDir']) && !empty($cfg['docSQLDir'])) {
428 * import docSQL files
430 echo '<ul>' . "\n"
431 .'<li><a href="db_details_importdocsql.php?' . $takeaway . '">' . "\n";
432 if ($cfg['PropertiesIconic']) {
433 echo '<img class="icon" src="' . $pmaThemeImage . 'b_docsql.png"'
434 .' alt="" width="16" height="16" />';
436 echo $strImportDocSQL . '</a></li>' . "\n"
437 .'</ul>';
441 * Displays the footer
443 require_once './libraries/footer.inc.php';