update
[phpmyadmin/crack.git] / tbl_indexes.php3
blob13be7810a49f7e6c8e2534e05e137f48bd43e0df
1 <?php
2 /* $Id$ */
5 /**
6 * Gets some core libraries
7 */
8 require('./libraries/grab_globals.lib.php3');
9 require('./libraries/common.lib.php3');
12 /**
13 * Defines the index types ("FULLTEXT" is available since MySQL 3.23.23)
15 $index_types_cnt = 3;
16 $index_types = array(
17 'PRIMARY',
18 'INDEX',
19 'UNIQUE'
21 if (PMA_MYSQL_INT_VERSION >= 32323) {
22 $index_types[] = 'FULLTEXT';
23 $index_types_cnt++;
27 /**
28 * Ensures the db & table are valid, then loads headers and gets indexes
29 * informations.
30 * Skipped if this script is called by "tbl_properties.php3"
32 if (!defined('PMA_IDX_INCLUDED')) {
33 // Not a valid db name -> back to the welcome page
34 if (!empty($db)) {
35 $is_db = @PMA_mysql_select_db($db);
37 if (empty($db) || !$is_db) {
38 header('Location: ' . $cfg['PmaAbsoluteUri'] . 'main.php3?lang=' . $lang . '&convcharset=' . $convcharset . '&server=' . $server . (isset($message) ? '&message=' . urlencode($message) : '') . '&reload=1');
39 exit();
41 // Not a valid table name -> back to the db_details.php3
42 if (!empty($table)) {
43 $is_table = @PMA_mysql_query('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'');
45 if (empty($table)
46 || !($is_table && @mysql_numrows($is_table))) {
47 header('Location: ' . $cfg['PmaAbsoluteUri'] . 'db_details.php3?lang=' . $lang . '&convcharset=' . $convcharset . '&server=' . $server .'&db=' . urlencode($db) . (isset($message) ? '&message=' . urlencode($message) : '') . '&reload=1');
48 exit();
49 } else if (isset($is_table)) {
50 mysql_free_result($is_table);
53 // Displays headers (if needed)
54 $js_to_run = ((isset($index) && isset($do_save_data)) ? 'functions.js' : 'indexes.js');
55 include('./header.inc.php3');
56 } // end if
59 /**
60 * Gets fields and indexes informations
62 if (defined('PMA_IDX_INCLUDED')) {
63 $err_url_0 = 'db_details.php3'
64 . '?lang=' . $lang
65 . '&amp;convcharset=' . $convcharset
66 . '&amp;server=' . $server
67 . '&amp;db=' . urlencode($db);
70 // Gets table keys and store them in arrays
71 $indexes = array();
72 $prev_index = '';
73 $indexes_info = array();
74 $indexes_data = array();
75 // keys had already been grabbed in "tbl_properties.php3"
76 if (defined('PMA_IDX_INCLUDED')) {
77 $idx_cnt = count($ret_keys);
78 } else {
79 $local_query = 'SHOW KEYS FROM ' . PMA_backquote($table);
80 $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url_0);
81 $idx_cnt = mysql_num_rows($result);
84 for ($i = 0; $i < $idx_cnt; $i++) {
85 $row = (defined('PMA_IDX_INCLUDED') ? $ret_keys[$i] : PMA_mysql_fetch_array($result));
87 if ($row['Key_name'] != $prev_index ){
88 $indexes[] = $row['Key_name'];
89 $prev_index = $row['Key_name'];
91 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
92 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
93 if (isset($row['Cardinality'])) {
94 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
96 // I don't know what does following column mean....
97 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
98 $indexes_info[$row['Key_name']]['Comment'] = (isset($row['Comment']))
99 ? $row['Comment']
100 : '';
102 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
103 if (isset($row['Sub_part'])) {
104 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
106 } // end while
108 if (defined('PMA_IDX_INCLUDED')) {
109 unset($ret_keys);
110 } else if ($result) {
111 mysql_free_result($result);
114 // Get fields and stores their name/type
115 // fields had already been grabbed in "tbl_properties.php3"
116 if (defined('PMA_IDX_INCLUDED')) {
117 mysql_data_seek($fields_rs, 0);
118 } else {
119 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($table);
120 $fields_rs = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url_0);
121 $fields_cnt = mysql_num_rows($fields_rs);
124 $fields_names = array();
125 $fields_types = array();
126 while ($row = PMA_mysql_fetch_array($fields_rs)) {
127 $fields_names[] = $row['Field'];
128 // loic1: set or enum types: slashes single quotes inside options
129 if (eregi('^(set|enum)\((.+)\)$', $row['Type'], $tmp)) {
130 $tmp[2] = substr(ereg_replace('([^,])\'\'', '\\1\\\'', ',' . $tmp[2]), 1);
131 $fields_types[] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
132 } else {
133 $fields_types[] = $row['Type'];
135 } // end while
137 if ($fields_rs) {
138 mysql_free_result($fields_rs);
143 * Stipslashes some variables if required
145 if (get_magic_quotes_gpc()) {
146 if (isset($index)) {
147 $index = stripslashes($index);
149 if (isset($old_index)) {
150 $old_index = stripslashes($old_index);
152 } // end if
156 * Do run the query to build the new index and moves back to
157 * "tbl_properties.php3"
159 if (!defined('PMA_IDX_INCLUDED')
160 && (isset($index) && isset($do_save_data))) {
162 $err_url = 'tbl_indexes.php3'
163 . '?lang=' . $lang
164 . '&amp;convcharset=' . $convcharset
165 . '&amp;server=' . $server
166 . '&amp;db=' . urlencode($db)
167 . '&amp;table=' . urlencode($table);
168 if (empty($old_index)) {
169 $err_url .= '&amp;create_index=1&amp;idx_num_fields=' . $idx_num_fields;
170 } else {
171 $err_url .= '&amp;index=' . urlencode($old_index);
174 if ($index_type == 'PRIMARY') {
175 if ($index == '') {
176 $index = 'PRIMARY';
177 } else if ($index != 'PRIMARY') {
178 PMA_mysqlDie($strPrimaryKeyName, '', FALSE, $err_url);
180 } else if ($index == 'PRIMARY') {
181 PMA_mysqlDie($strCantRenameIdxToPrimary, '', FALSE, $err_url);
185 // $sql_query is the one displayed in the query box, don't use it when you
186 // need to generate a query in this script
187 $local_query = 'ALTER TABLE ' . PMA_backquote($table);
189 // Drops the old index
190 if (!empty($old_index)) {
191 if ($old_index == 'PRIMARY') {
192 $local_query .= ' DROP PRIMARY KEY,';
193 } else {
194 $local_query .= ' DROP INDEX ' . PMA_backquote($old_index) .',';
196 } // end if
198 // Builds the new one
199 switch ($index_type) {
200 case 'PRIMARY':
201 $local_query .= ' ADD PRIMARY KEY (';
202 break;
203 case 'FULLTEXT':
204 $local_query .= ' ADD FULLTEXT ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
205 break;
206 case 'UNIQUE':
207 $local_query .= ' ADD UNIQUE ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
208 break;
209 case 'INDEX':
210 $local_query .= ' ADD INDEX ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
211 break;
212 } // end switch
213 $index_fields = '';
214 while (list($i, $name) = each($column)) {
215 if ($name != '--ignore--') {
216 $index_fields .= (empty($index_fields) ? '' : ',')
217 . PMA_backquote(get_magic_quotes_gpc() ? stripslashes($name) : $name)
218 . (empty($sub_part[$i]) ? '' : '(' . $sub_part[$i] . ')');
220 } // end while
221 if (empty($index_fields)){
222 PMA_mysqlDie($strNoIndexPartsDefined, '', FALSE, $err_url);
223 } else {
224 $local_query .= $index_fields . ')';
227 $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, FALSE, $err_url);
228 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
230 include('./tbl_properties.php3');
231 exit();
232 } // end builds the new index
236 * Edits an index or defines a new one
238 else if (!defined('PMA_IDX_INCLUDED')
239 && (isset($index) || isset($create_index))) {
241 // Prepares the form values
242 if (!isset($index)) {
243 $index = '';
245 if (!isset($old_index)){
246 $old_index = $index;
248 if (!isset($index_type)) {
249 $index_type = '';
251 if ($old_index == '' || !isset($indexes_info[$old_index])) {
252 $edited_index_info['Sequences'] = array();
253 $edited_index_data = array();
254 for ($i = 1; $i <= $idx_num_fields; $i++) {
255 $edited_index_info['Sequences'][] = $i;
256 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => '');
257 } // end for
258 if ($old_index == ''
259 && !isset($indexes_info['PRIMARY'])
260 && ($index_type == '' || $index_type == 'PRIMARY')) {
261 $old_index = 'PRIMARY';
263 } else {
264 $edited_index_info = $indexes_info[$old_index];
265 $edited_index_data = $indexes_data[$old_index];
266 if ($edited_index_info['Comment'] == 'FULLTEXT') {
267 $index_type = 'FULLTEXT';
268 } else if ($index == 'PRIMARY') {
269 $index_type = 'PRIMARY';
270 } else if ($edited_index_info['Non_unique'] == '0') {
271 $index_type = 'UNIQUE';
272 } else {
273 $index_type = 'INDEX';
275 } // end if... else...
277 if (isset($add_fields)) {
278 if (isset($prev_add_fields)) {
279 $added_fields += $prev_add_fields;
281 $field_cnt = count($edited_index_info['Sequences']) + 1;
282 for ($i = $field_cnt; $i < ($added_fields + $field_cnt); $i++) {
283 $edited_index_info['Sequences'][] = $i;
284 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => '');
285 } // end for
287 // Restore entered values
288 while (list($i, $name) = each($column)) {
289 if ($name != '--ignore--'){
290 $edited_index_data[$i+1]['Column_name'] = $name;
291 $edited_index_data[$i+1]['Sub_part'] = $sub_part[$i];
293 } // end while
294 } // end if
295 // end preparing form values
298 <!-- Build index form -->
299 <form action="tbl_indexes.php3" method="post" name="index_frm"
300 onsubmit="if (typeof(this.elements['index'].disabled) != 'undefined') {this.elements['index'].disabled = false}">
301 <input type="hidden" name="lang" value="<?php echo $lang; ?>" />
302 <input type="hidden" name="convcharset" value="<?php echo $convcharset; ?>" />
303 <input type="hidden" name="server" value="<?php echo $server; ?>" />
304 <input type="hidden" name="db" value="<?php echo $db; ?>" />
305 <input type="hidden" name="table" value="<?php echo $table; ?>" />
306 <?php
307 if (isset($create_index)) {
308 echo '<input type="hidden" name="create_index" value="1" />';
310 echo "\n";
312 <input type="hidden" name="old_index" value="<?php echo (isset($create_index) ? '' : $old_index); ?>" />
313 <b><?php echo '------ ' . (isset($create_index) ? $strCreateIndexTopic : $strModifyIndexTopic) . ' ------'; ?></b>
314 <br /><br />
316 <table border="0">
317 <tr>
318 <td><?php echo $strIndexName; ?>&nbsp;</td>
319 <td>
320 <input type="text" name="index" value="<?php echo htmlspecialchars($index); ?>" class="textfield" onfocus="this.select()" />
321 &nbsp;<?php echo $strPrimaryKeyWarning . "\n"; ?>
322 </td>
323 </tr>
324 <tr>
325 <td><?php echo $strIndexType; ?>&nbsp;</td>
326 <td>
327 <select name="index_type" onchange="return checkIndexName()">
328 <?php
329 echo "\n";
330 for ($i = 0; $i < $index_types_cnt; $i++) {
331 if ($index_types[$i] == 'PRIMARY') {
332 if ($index == 'PRIMARY' || !isset($indexes_info['PRIMARY'])) {
333 echo ' '
334 . '<option value="PRIMARY"' . (($index_type == 'PRIMARY') ? ' selected="selected"' : '') . '>PRIMARY</option>'
335 . "\n";
337 } else {
338 echo ' '
339 . '<option value="' . $index_types[$i] . '"' . (($index_type == $index_types[$i]) ? ' selected="selected"' : '') . '>'. $index_types[$i] . '</option>'
340 . "\n";
342 } // end if... else...
343 } // end for
345 </select>&nbsp;
346 <?php echo PMA_showDocuShort('A/L/ALTER_TABLE.html') . "\n"; ?>
347 </td>
348 </tr>
349 </table><br />
351 <table border="<?php echo $cfg['Border']; ?>" cellpadding="5">
352 <tr>
353 <th><?php echo $strField; ?></th>
354 <th><?php echo $strSize; ?></th>
355 </tr>
356 <?php
357 while (list($row_no, $seq_index) = each($edited_index_info['Sequences'])) {
358 $add_type = (is_array($fields_types) && count($fields_types) == count($fields_names));
359 $selected = $edited_index_data[$seq_index]['Column_name'];
360 if (!empty($edited_index_data[$seq_index]['Sub_part'])) {
361 $sub_part = ' value="' . $edited_index_data[$seq_index]['Sub_part'] . '"';
362 } else {
363 $sub_part = '';
365 $bgcolor = (($row_no % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo']);
366 echo "\n";
368 <tr>
369 <td bgcolor="<?php echo $bgcolor; ?>">
370 <select name="column[]">
371 <option value="--ignore--"<?php if ('--ignore--' == $selected) echo ' selected="selected"'; ?>>
372 -- <?php echo $strIgnore; ?> --</option>
373 <?php
374 reset($fields_names);
375 while (list($key, $val) = each($fields_names)) {
376 if ($index_type != 'FULLTEXT'
377 || eregi('^(varchar|text|tinytext|mediumtext|longtext)', $fields_types[$key])) {
378 echo "\n" . ' '
379 . '<option value="' . htmlspecialchars($val) . '"' . (($val == $selected) ? ' selected="selected"' : '') . '>'
380 . htmlspecialchars($val) . (($add_type) ? ' [' . $fields_types[$key] . ']' : '' ) . '</option>' . "\n";
382 } // end while
383 echo "\n";
385 </select>
386 </td>
387 <td bgcolor="<?php echo $bgcolor; ?>">
388 <input type="text" size="5" name="sub_part[]"<?php echo $sub_part; ?> onfocus="this.select()" />
389 </td>
390 </tr>
391 <?php
392 } // end while
394 echo "\n";
396 </table><br />
398 <input type="submit" name="do_save_data" value="<?php echo $strSave; ?>" /><br />
400 <?php
401 echo "\n";
402 if (isset($added_fields)) {
403 echo ' <input type="hidden" name="prev_add_fields" value="' . $added_fields . '" />';
405 if (isset($idx_num_fields)) {
406 echo ' <input type="hidden" name="idx_num_fields" value="' . $idx_num_fields . '" />' . "\n";
408 echo ' <hr /><br />' . "\n";
409 echo ' ' . sprintf($strAddToIndex, '<input type="text" name="added_fields" size="4" value="1" class="textfield" onfocus="this.select()" />') . "\n";
410 echo ' &nbsp;<input type="submit" name="add_fields" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'added_fields\', 1)" />' . "\n";
412 } else {
414 * Display indexes
417 <!-- Indexes form -->
418 <form action="tbl_indexes.php3" method="post">
419 <input type="hidden" name="lang" value="<?php echo $lang; ?>" />
420 <input type="hidden" name="convcharset" value="<?php echo $convcharset; ?>" />
421 <input type="hidden" name="server" value="<?php echo $server; ?>" />
422 <input type="hidden" name="db" value="<?php echo $db; ?>" />
423 <input type="hidden" name="table" value="<?php echo $table; ?>" />
424 <?php
425 echo "\n";
426 echo ' ' . $strIndexes . '&nbsp;:' . "\n";
427 echo ' ' . PMA_showDocuShort('O/p/Optimising_Database_Structure.html') . '<br />' ."\n";
429 if ($idx_cnt > 0) {
431 <table border="<?php echo $cfg['Border']; ?>">
432 <tr>
433 <th><?php echo $strKeyname; ?></th>
434 <th><?php echo $strType; ?></th>
435 <th><?php echo $strCardinality; ?></th>
436 <th colspan="2"><?php echo $strAction; ?></th>
437 <th colspan="2"><?php echo $strField; ?></th>
438 </tr>
439 <?php
440 echo "\n";
441 while (list($index_no, $index_name) = each($indexes)) {
442 $cell_bgd = (($index_no % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo']);
443 $index_td = ' <td bgcolor="' . $cell_bgd . '" rowspan="' . count($indexes_info[$index_name]['Sequences']) . '">' . "\n";
444 echo ' <tr>' . "\n";
445 echo $index_td
446 . ' ' . htmlspecialchars($index_name) . "\n"
447 . ' </td>' . "\n";
449 if ($indexes_info[$index_name]['Comment'] == 'FULLTEXT') {
450 $index_type = 'FULLTEXT';
451 } else if ($index_name == 'PRIMARY') {
452 $index_type = 'PRIMARY';
453 } else if ($indexes_info[$index_name]['Non_unique'] == '0') {
454 $index_type = 'UNIQUE';
455 } else {
456 $index_type = 'INDEX';
458 echo $index_td
459 . ' ' . $index_type . "\n"
460 . ' </td>' . "\n";
462 echo str_replace('">' . "\n", '" align="right">' . "\n", $index_td)
463 . ' ' . (isset($indexes_info[$index_name]['Cardinality']) ? $indexes_info[$index_name]['Cardinality'] : $strNone) . '&nbsp;' . "\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.php3?' . $url_query . '&amp;sql_query=' . $local_query . '&amp;zero_rows=' . $zero_rows . '" onclick="return confirmLink(this, \'' . $js_msg . '\')">' . $strDrop . '</a>' . "\n"
477 . ' </td>' . "\n";
479 echo $index_td
480 . ' <a href="tbl_indexes.php3?' . $url_query . '&amp;index=' . urlencode($index_name) . '">' . $strEdit . '</a>' . "\n"
481 . ' </td>' . "\n";
483 while (list($row_no, $seq_index) = each($indexes_info[$index_name]['Sequences'])) {
484 if ($row_no > 0) {
485 echo ' <tr>' . "\n";
487 if (!empty($indexes_data[$index_name][$seq_index]['Sub_part'])) {
488 echo ' <td bgcolor="' . $cell_bgd . '">' . "\n"
489 . ' ' . $indexes_data[$index_name][$seq_index]['Column_name'] . "\n"
490 . ' </td>' . "\n";
491 echo ' <td align="right" bgcolor="' . $cell_bgd . '">' . "\n"
492 . ' ' . $indexes_data[$index_name][$seq_index]['Sub_part'] . "\n"
493 . ' </td>' . "\n";
494 echo ' </tr>' . "\n";
495 } else {
496 echo ' <td bgcolor="' . $cell_bgd . '" colspan="2">' . "\n"
497 . ' ' . $indexes_data[$index_name][$seq_index]['Column_name'] . "\n"
498 . ' </td>' . "\n";
499 echo ' </tr>' . "\n";
501 } // end while
502 } // end while
504 </table><br />
505 <?php
506 echo "\n\n";
507 } // end display indexes
508 else {
509 // none indexes
510 echo "\n" . ' <br />' . "\n";
511 echo ' <i>' . $strNoIndex . '</i><br /><br />' . "\n\n";
514 echo ' ' . sprintf($strCreateIndex, '<input type="text" size="4" name="idx_num_fields" value="1" class="textfield" />') . "\n";
515 echo ' &nbsp;<input type="submit" name="create_index" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'idx_num_fields\', 1)" />' . "\n";
516 echo ' ';
517 } // end display indexes
520 </form>
523 <?php
525 * Displays the footer
527 echo "\n";
529 if (!defined('PMA_IDX_INCLUDED')){
530 include('./footer.inc.php3');