Fix left frame reloading after dropping table (bug #1034531).
[phpmyadmin/crack.git] / tbl_indexes.php
blobab6521d328d21085763934d83dab8c0b67c3fac9
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
6 /**
7 * Gets some core libraries
8 */
9 require_once('./libraries/grab_globals.lib.php');
10 require_once('./libraries/common.lib.php');
13 /**
14 * Defines the index types ("FULLTEXT" is available since MySQL 3.23.23)
16 $index_types_cnt = 4;
17 $index_types = array(
18 'PRIMARY',
19 'INDEX',
20 'UNIQUE',
21 'FULLTEXT'
24 /**
25 * Ensures the db & table are valid, then loads headers and gets indexes
26 * informations.
27 * Skipped if this script is called by "tbl_properties.php"
29 if (!defined('PMA_IDX_INCLUDED')) {
30 // Not a valid db name -> back to the welcome page
31 if (!empty($db)) {
32 $is_db = PMA_DBI_select_db($db);
34 if (empty($db) || !$is_db) {
35 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . 'main.php?' . PMA_generate_common_url('', '', '&') . (isset($message) ? '&message=' . urlencode($message) : '') . '&reload=1');
36 exit;
38 // Not a valid table name -> back to the default db_details sub-page
39 if (!empty($table)) {
40 $is_table = PMA_DBI_query('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'', NULL, PMA_DBI_QUERY_STORE);
42 if (empty($table)
43 || !($is_table && PMA_DBI_num_rows($is_table))) {
44 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . $cfg['DefaultTabDatabase'] . '?' . PMA_generate_common_url($db, '', '&') . (isset($message) ? '&message=' . urlencode($message) : '') . '&reload=1');
45 exit;
46 } else if (isset($is_table)) {
47 PMA_DBI_free_result($is_table);
50 // Displays headers (if needed)
51 $js_to_run = ((isset($index) && isset($do_save_data)) ? 'functions.js' : 'indexes.js');
52 require_once('./header.inc.php');
53 } // end if
56 /**
57 * Gets fields and indexes informations
59 if (defined('PMA_IDX_INCLUDED')) {
60 $err_url_0 = 'db_details.php?' . PMA_generate_common_url($db);
63 // Gets table keys and store them in arrays
64 $indexes = array();
65 $prev_index = '';
66 $indexes_info = array();
67 $indexes_data = array();
68 // keys had already been grabbed in "tbl_properties.php"
69 if (!defined('PMA_IDX_INCLUDED')) {
70 $local_query = 'SHOW KEYS FROM ' . PMA_backquote($table);
71 $result = PMA_DBI_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url_0);
72 $ret_keys = array();
73 while ($row = PMA_DBI_fetch_assoc($result)) {
74 $ret_keys[] = $row;
76 PMA_DBI_free_result($result);
79 foreach ($ret_keys as $row) {
80 if ($row['Key_name'] != $prev_index ){
81 $indexes[] = $row['Key_name'];
82 $prev_index = $row['Key_name'];
84 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
85 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
86 if (isset($row['Cardinality'])) {
87 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
89 // I don't know what does following column mean....
90 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
91 $indexes_info[$row['Key_name']]['Comment'] = (isset($row['Comment']))
92 ? $row['Comment']
93 : '';
94 $indexes_info[$row['Key_name']]['Index_type'] = (isset($row['Index_type']))
95 ? $row['Index_type']
96 : '';
98 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
99 if (isset($row['Sub_part'])) {
100 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
102 } // end while
104 // Get fields and stores their name/type
105 // fields had already been grabbed in "tbl_properties.php"
106 if (!defined('PMA_IDX_INCLUDED')) {
107 $fields_rs = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';');
108 $save_row = array();
109 while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
110 $save_row[] = $row;
114 $fields_names = array();
115 $fields_types = array();
116 foreach ($save_row AS $saved_row_key => $row) {
117 $fields_names[] = $row['Field'];
118 // loic1: set or enum types: slashes single quotes inside options
119 if (preg_match('@^(set|enum)\((.+)\)$@i', $row['Type'], $tmp)) {
120 $tmp[2] = substr(preg_replace('@([^,])\'\'@', '\\1\\\'', ',' . $tmp[2]), 1);
121 $fields_types[] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
122 } else {
123 $fields_types[] = $row['Type'];
125 } // end while
127 if ($fields_rs) {
128 PMA_DBI_free_result($fields_rs);
133 * Do run the query to build the new index and moves back to
134 * "tbl_properties.php"
136 if (!defined('PMA_IDX_INCLUDED')
137 && (isset($index) && isset($do_save_data))) {
139 $err_url = 'tbl_indexes.php?' . PMA_generate_common_url($db, $table);
140 if (empty($old_index)) {
141 $err_url .= '&amp;create_index=1&amp;idx_num_fields=' . $idx_num_fields;
142 } else {
143 $err_url .= '&amp;index=' . urlencode($old_index);
146 if ($index_type == 'PRIMARY') {
147 if ($index == '') {
148 $index = 'PRIMARY';
149 } else if ($index != 'PRIMARY') {
150 PMA_mysqlDie($strPrimaryKeyName, '', FALSE, $err_url);
152 } else if ($index == 'PRIMARY') {
153 PMA_mysqlDie($strCantRenameIdxToPrimary, '', FALSE, $err_url);
157 // $sql_query is the one displayed in the query box
158 $sql_query = 'ALTER TABLE ' . PMA_backquote($table);
160 // Drops the old index
161 if (!empty($old_index)) {
162 if ($old_index == 'PRIMARY') {
163 $sql_query .= ' DROP PRIMARY KEY,';
164 } else {
165 $sql_query .= ' DROP INDEX ' . PMA_backquote($old_index) .',';
167 } // end if
169 // Builds the new one
170 switch ($index_type) {
171 case 'PRIMARY':
172 $sql_query .= ' ADD PRIMARY KEY (';
173 break;
174 case 'FULLTEXT':
175 $sql_query .= ' ADD FULLTEXT ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
176 break;
177 case 'UNIQUE':
178 $sql_query .= ' ADD UNIQUE ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
179 break;
180 case 'INDEX':
181 $sql_query .= ' ADD INDEX ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
182 break;
183 } // end switch
184 $index_fields = '';
185 foreach ($column AS $i => $name) {
186 if ($name != '--ignore--') {
187 $index_fields .= (empty($index_fields) ? '' : ',')
188 . PMA_backquote($name)
189 . (empty($sub_part[$i]) ? '' : '(' . $sub_part[$i] . ')');
191 } // end while
192 if (empty($index_fields)){
193 PMA_mysqlDie($strNoIndexPartsDefined, '', FALSE, $err_url);
194 } else {
195 $sql_query .= $index_fields . ')';
198 $result = PMA_DBI_query($sql_query);
199 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
201 $active_page = 'tbl_properties_structure.php';
202 require('./tbl_properties_structure.php');
203 } // end builds the new index
207 * Edits an index or defines a new one
209 else if (!defined('PMA_IDX_INCLUDED')
210 && (isset($index) || isset($create_index))) {
212 // Prepares the form values
213 if (!isset($index)) {
214 $index = '';
216 if (!isset($old_index)){
217 $old_index = $index;
219 if (!isset($index_type)) {
220 $index_type = '';
222 if ($old_index == '' || !isset($indexes_info[$old_index])) {
223 $edited_index_info['Sequences'] = array();
224 $edited_index_data = array();
225 for ($i = 1; $i <= $idx_num_fields; $i++) {
226 $edited_index_info['Sequences'][] = $i;
227 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => '');
228 } // end for
229 if ($old_index == ''
230 && !isset($indexes_info['PRIMARY'])
231 && ($index_type == '' || $index_type == 'PRIMARY')) {
232 $old_index = 'PRIMARY';
234 } else {
235 $edited_index_info = $indexes_info[$old_index];
236 $edited_index_data = $indexes_data[$old_index];
239 if ((PMA_MYSQL_INT_VERSION < 40002 && $edited_index_info['Comment'] == 'FULLTEXT')
240 || (PMA_MYSQL_INT_VERSION >= 40002 && $edited_index_info['Index_type'] == 'FULLTEXT')) {
241 $index_type = 'FULLTEXT';
242 } else if ($index == 'PRIMARY') {
243 $index_type = 'PRIMARY';
244 } else if ($edited_index_info['Non_unique'] == '0') {
245 $index_type = 'UNIQUE';
246 } else {
247 $index_type = 'INDEX';
249 } // end if... else...
251 if (isset($add_fields)) {
252 if (isset($prev_add_fields)) {
253 $added_fields += $prev_add_fields;
255 $field_cnt = count($edited_index_info['Sequences']) + 1;
256 for ($i = $field_cnt; $i < ($added_fields + $field_cnt); $i++) {
257 $edited_index_info['Sequences'][] = $i;
258 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => '');
259 } // end for
261 // Restore entered values
262 foreach ($column AS $i => $name) {
263 if ($name != '--ignore--'){
264 $edited_index_data[$i+1]['Column_name'] = $name;
265 $edited_index_data[$i+1]['Sub_part'] = $sub_part[$i];
267 } // end while
268 } // end if
269 // end preparing form values
272 <!-- Build index form -->
273 <form action="./tbl_indexes.php" method="post" name="index_frm"
274 onsubmit="if (typeof(this.elements['index'].disabled) != 'undefined') {this.elements['index'].disabled = false}">
275 <table border="0" cellpadding="2" cellspacing="1">
276 <tr><td class="tblHeaders" colspan="2">
277 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
278 <?php
279 if (isset($create_index)) {
280 echo '<input type="hidden" name="create_index" value="1" />';
282 echo "\n";
284 <input type="hidden" name="old_index" value="<?php echo (isset($create_index) ? '' : htmlspecialchars($old_index)); ?>" />
285 <?php echo ' ' . (isset($create_index) ? $strCreateIndexTopic : $strModifyIndexTopic) . ' '; ?>
286 </th></tr>
289 <tr>
290 <td align="right"><b><?php echo $strIndexName; ?></b>&nbsp;</th>
291 <td>
292 <input type="text" name="index" value="<?php echo htmlspecialchars($index); ?>" size="25" onfocus="this.select()" />
293 </td>
294 </tr>
295 <tr><td align="right"><?php
296 if ($cfg['ErrorIconic']) {
297 echo '<img src="' . $pmaThemeImage . 's_warn.png" width="16" height="16" border="0" alt="Attention" />';
299 ?></td><td><?php echo $strPrimaryKeyWarning . "\n"; ?></td></tr>
300 <tr>
301 <td align="right"><b><?php echo $strIndexType; ?></b>&nbsp;</td>
302 <td>
303 <select name="index_type" onchange="return checkIndexName()">
304 <?php
305 echo "\n";
306 for ($i = 0; $i < $index_types_cnt; $i++) {
307 if ($index_types[$i] == 'PRIMARY') {
308 if ($index == 'PRIMARY' || !isset($indexes_info['PRIMARY'])) {
309 echo ' '
310 . '<option value="PRIMARY"' . (($index_type == 'PRIMARY') ? ' selected="selected"' : '') . '>PRIMARY</option>'
311 . "\n";
313 } else {
314 echo ' '
315 . '<option value="' . $index_types[$i] . '"' . (($index_type == $index_types[$i]) ? ' selected="selected"' : '') . '>'. $index_types[$i] . '</option>'
316 . "\n";
318 } // end if... else...
319 } // end for
321 </select>
322 <?php echo PMA_showMySQLDocu('Reference', 'ALTER_TABLE') . "\n"; ?>
323 </td>
324 </tr>
326 <tr><td valign="top" align="right"><b><?php echo $strFields; ?> :</b>&nbsp;</td><td><table border="<?php echo $cfg['Border']; ?>" cellpadding="2" cellspacing="1">
327 <tr>
328 <th><?php echo $strField; ?></th>
329 <th><?php echo $strSize; ?></th>
330 </tr>
331 <?php
332 foreach ($edited_index_info['Sequences'] AS $row_no => $seq_index) {
333 $add_type = (is_array($fields_types) && count($fields_types) == count($fields_names));
334 $selected = $edited_index_data[$seq_index]['Column_name'];
335 if (!empty($edited_index_data[$seq_index]['Sub_part'])) {
336 $sub_part = ' value="' . $edited_index_data[$seq_index]['Sub_part'] . '"';
337 } else {
338 $sub_part = '';
340 $bgcolor = (($row_no % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo']);
341 echo "\n";
343 <tr>
344 <td bgcolor="<?php echo $bgcolor; ?>">
345 <select name="column[]">
346 <option value="--ignore--"<?php if ('--ignore--' == $selected) echo ' selected="selected"'; ?>>
347 -- <?php echo $strIgnore; ?> --</option>
348 <?php
349 foreach ($fields_names AS $key => $val) {
350 if ($index_type != 'FULLTEXT'
351 || preg_match('@^(varchar|text|tinytext|mediumtext|longtext)@i', $fields_types[$key])) {
352 echo "\n" . ' '
353 . '<option value="' . htmlspecialchars($val) . '"' . (($val == $selected) ? ' selected="selected"' : '') . '>'
354 . htmlspecialchars($val) . (($add_type) ? ' [' . $fields_types[$key] . ']' : '' ) . '</option>' . "\n";
356 } // end while
357 echo "\n";
359 </select>
360 </td>
361 <td bgcolor="<?php echo $bgcolor; ?>">
362 <input type="text" size="5" name="sub_part[]"<?php echo $sub_part; ?> onfocus="this.select()" />
363 </td>
364 </tr>
365 <?php
366 } // end while
368 echo "\n";
370 <tr><td colspan="2"><?php
371 echo "\n";
372 if (isset($added_fields)) {
373 echo ' <input type="hidden" name="prev_add_fields" value="' . $added_fields . '" />';
375 if (isset($idx_num_fields)) {
376 echo ' <input type="hidden" name="idx_num_fields" value="' . $idx_num_fields . '" />' . "\n";
378 echo ' ' . "\n";
379 echo ' ' . sprintf($strAddToIndex, '<input type="text" name="added_fields" size="2" value="1" onfocus="this.select()" style="vertical-align: middle;" />') . "\n";
380 echo ' &nbsp;<input type="submit" name="add_fields" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'added_fields\', 1)" style="vertical-align: middle;" />' . "\n";
381 ?></td>
382 </tr>
383 </table></td></tr>
384 <tr><td colspan="2" class="tblFooters" align="center">
385 <input type="submit" name="do_save_data" value="<?php echo $strSave; ?>" /></td></tr>
386 </table>
387 </form>
388 <?php
389 } else {
391 * Display indexes
394 <!-- Indexes form -->
395 <form action="./tbl_indexes.php" method="post">
396 <table border="0" cellpadding="2" cellspacing="1">
397 <tr><td class="tblHeaders" colspan="7">
398 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
399 <?php
400 echo "\n";
401 echo ' ' . $strIndexes . ':' . "\n";
402 echo ' ' . PMA_showMySQLDocu('MySQL_Optimisation', 'Optimising_Database_Structure') . "\n";
403 ?></td></tr><?php
404 $edit_link_text = '';
405 $drop_link_text = '';
407 // We need to copy the value or else the == 'both' check will always return true
408 $propicon = (string)$cfg['PropertiesIconic'];
410 if ($cfg['PropertiesIconic'] === true || $propicon == 'both') {
411 $edit_link_text = '<img src="' . $pmaThemeImage . 'b_edit.png" width="16" height="16" hspace="2" border="0" title="' . $strEdit . '" alt="' . $strEdit . '" />';
412 $drop_link_text = '<img src="' . $pmaThemeImage . 'b_drop.png" width="16" height="16" hspace="2" border="0" title="' . $strDrop . '" alt="' . $strDrop . '" />';
414 //print_r($cfg['PropertiesIconic']);
415 if ($cfg['PropertiesIconic'] === false || $propicon == 'both') {
416 $edit_link_text .= $strEdit;
417 $drop_link_text .= $strDrop;
419 if ($propicon == 'both') {
420 $edit_link_text = '<nobr>' . $edit_link_text . '</nobr>';
421 $drop_link_text = '<nobr>' . $drop_link_text . '</nobr>';
424 if (count($ret_keys) > 0) {
426 <!--table border="<?php echo $cfg['Border']; ?>" cellpadding="2" cellspacing="1"-->
427 <tr>
428 <th><?php echo $strKeyname; ?></th>
429 <th><?php echo $strType; ?></th>
430 <th><?php echo $strCardinality; ?></th>
431 <th colspan="2"><?php echo $strAction; ?></th>
432 <th colspan="2"><?php echo $strField; ?></th>
433 </tr>
434 <?php
435 echo "\n";
436 foreach ($indexes AS $index_no => $index_name) {
437 $cell_bgd = (($index_no % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo']);
438 $index_td = ' <td bgcolor="' . $cell_bgd . '" rowspan="' . count($indexes_info[$index_name]['Sequences']) . '">' . "\n";
439 echo ' <tr>' . "\n";
440 echo $index_td
441 . ' ' . htmlspecialchars($index_name) . "\n"
442 . ' </td>' . "\n";
444 if ((PMA_MYSQL_INT_VERSION < 40002 && $indexes_info[$index_name]['Comment'] == 'FULLTEXT')
445 || (PMA_MYSQL_INT_VERSION >= 40002 && $indexes_info[$index_name]['Index_type'] == 'FULLTEXT')) {
446 $index_type = 'FULLTEXT';
447 } else if ($index_name == 'PRIMARY') {
448 $index_type = 'PRIMARY';
449 } else if ($indexes_info[$index_name]['Non_unique'] == '0') {
450 $index_type = 'UNIQUE';
451 } else {
452 $index_type = 'INDEX';
454 echo $index_td
455 . ' ' . $index_type . "\n"
456 . ' </td>' . "\n";
458 echo str_replace('">' . "\n", '" align="right">' . "\n", $index_td)
459 . ' ' . (isset($indexes_info[$index_name]['Cardinality']) ? $indexes_info[$index_name]['Cardinality'] : $strNone) . '&nbsp;' . "\n"
460 . ' </td>' . "\n";
462 echo $index_td
463 . ' <a href="tbl_indexes.php?' . $url_query . '&amp;index=' . urlencode($index_name) . '">' . $edit_link_text . '</a>' . "\n"
464 . ' </td>' . "\n";
466 if ($index_name == 'PRIMARY') {
467 $local_query = urlencode('ALTER TABLE ' . PMA_backquote($table) . ' DROP PRIMARY KEY');
468 $js_msg = 'ALTER TABLE ' . PMA_jsFormat($table) . ' DROP PRIMARY KEY';
469 $zero_rows = urlencode($strPrimaryKeyHasBeenDropped);
470 } else {
471 $local_query = urlencode('ALTER TABLE ' . PMA_backquote($table) . ' DROP INDEX ' . PMA_backquote($index_name));
472 $js_msg = 'ALTER TABLE ' . PMA_jsFormat($table) . ' DROP INDEX ' . PMA_jsFormat($index_name);
473 $zero_rows = urlencode(sprintf($strIndexHasBeenDropped, htmlspecialchars($index_name)));
475 echo $index_td
476 . ' <a href="sql.php?' . $url_query . '&amp;sql_query=' . $local_query . '&amp;zero_rows=' . $zero_rows . '" onclick="return confirmLink(this, \'' . $js_msg . '\')">' . $drop_link_text . '</a>' . "\n"
477 . ' </td>' . "\n";
479 foreach ($indexes_info[$index_name]['Sequences'] AS $row_no => $seq_index) {
480 if ($row_no > 0) {
481 echo ' <tr>' . "\n";
483 if (!empty($indexes_data[$index_name][$seq_index]['Sub_part'])) {
484 echo ' <td bgcolor="' . $cell_bgd . '">' . "\n"
485 . ' ' . $indexes_data[$index_name][$seq_index]['Column_name'] . "\n"
486 . ' </td>' . "\n";
487 echo ' <td align="right" bgcolor="' . $cell_bgd . '">' . "\n"
488 . ' ' . $indexes_data[$index_name][$seq_index]['Sub_part'] . "\n"
489 . ' </td>' . "\n";
490 echo ' </tr>' . "\n";
491 } else {
492 echo ' <td bgcolor="' . $cell_bgd . '" colspan="2">' . "\n"
493 . ' ' . htmlspecialchars($indexes_data[$index_name][$seq_index]['Column_name']) . "\n"
494 . ' </td>' . "\n";
495 echo ' </tr>' . "\n";
497 } // end while
498 } // end while
500 <!--/table><br /-->
501 <?php
502 echo "\n\n";
503 } // end display indexes
504 else {
505 // none indexes
506 echo "\n" . ' <tr><td colspan=7" align="center">' . "\n";
507 if ($cfg['ErrorIconic']) {
508 echo '<img src="' . $pmaThemeImage . 's_warn.png" width="16" height="16" border="0" alt="Warning" hspace="2" align="middle" />';
510 echo ' <b>' . $strNoIndex . '</b></td></tr>' . "\n\n";
513 echo '<tr><td colspan="7" class="tblFooters" nowrap="nowrap" align="center"> '
514 . sprintf($strCreateIndex, '<input type="text" size="2" name="idx_num_fields" value="1" style="vertical-align: middle;" />') . "\n";
515 echo ' &nbsp;<input type="submit" name="create_index" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'idx_num_fields\', 1)" style="vertical-align: middle;" />' . "\n";
516 echo '</td></tr> ';
518 </table></form>
519 <?php
520 } // end display indexes
524 * Displays the footer
526 echo "\n";
528 if (!defined('PMA_IDX_INCLUDED')){
529 require_once('./footer.inc.php');