update
[phpmyadmin/crack.git] / tbl_indexes.php3
blobce948ca23b63295d91eb3cc80f59ec5310e98fd7
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
6 /**
7 * Gets some core libraries
8 */
9 if (!defined('PMA_GRAB_GLOBALS_INCLUDED')) {
10 include('./libraries/grab_globals.lib.php3');
12 if (!defined('PMA_COMMON_LIB_INCLUDED')) {
13 include('./libraries/common.lib.php3');
17 /**
18 * Defines the index types ("FULLTEXT" is available since MySQL 3.23.23)
20 $index_types_cnt = 3;
21 $index_types = array(
22 'PRIMARY',
23 'INDEX',
24 'UNIQUE'
26 if (PMA_MYSQL_INT_VERSION >= 32323) {
27 $index_types[] = 'FULLTEXT';
28 $index_types_cnt++;
32 /**
33 * Ensures the db & table are valid, then loads headers and gets indexes
34 * informations.
35 * Skipped if this script is called by "tbl_properties.php3"
37 if (!defined('PMA_IDX_INCLUDED')) {
38 // Not a valid db name -> back to the welcome page
39 if (!empty($db)) {
40 $is_db = @PMA_mysql_select_db($db);
42 if (empty($db) || !$is_db) {
43 header('Location: ' . $cfg['PmaAbsoluteUri'] . 'main.php3?lang=' . $lang . '&convcharset=' . $convcharset . '&server=' . $server . (isset($message) ? '&message=' . urlencode($message) : '') . '&reload=1');
44 exit();
46 // Not a valid table name -> back to the default db_details sub-page
47 if (!empty($table)) {
48 $is_table = @PMA_mysql_query('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'');
50 if (empty($table)
51 || !($is_table && @mysql_numrows($is_table))) {
52 header('Location: ' . $cfg['PmaAbsoluteUri'] . $cfg['DefaultTabDatabase'] . '?lang=' . $lang . '&convcharset=' . $convcharset . '&server=' . $server .'&db=' . urlencode($db) . (isset($message) ? '&message=' . urlencode($message) : '') . '&reload=1');
53 exit();
54 } else if (isset($is_table)) {
55 mysql_free_result($is_table);
58 // Displays headers (if needed)
59 $js_to_run = ((isset($index) && isset($do_save_data)) ? 'functions.js' : 'indexes.js');
60 include('./header.inc.php3');
61 } // end if
64 /**
65 * Gets fields and indexes informations
67 if (defined('PMA_IDX_INCLUDED')) {
68 $err_url_0 = 'db_details.php3'
69 . '?lang=' . $lang
70 . '&amp;convcharset=' . $convcharset
71 . '&amp;server=' . $server
72 . '&amp;db=' . urlencode($db);
75 // Gets table keys and store them in arrays
76 $indexes = array();
77 $prev_index = '';
78 $indexes_info = array();
79 $indexes_data = array();
80 // keys had already been grabbed in "tbl_properties.php3"
81 if (defined('PMA_IDX_INCLUDED')) {
82 $idx_cnt = count($ret_keys);
83 } else {
84 $local_query = 'SHOW KEYS FROM ' . PMA_backquote($table);
85 $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url_0);
86 $idx_cnt = mysql_num_rows($result);
89 for ($i = 0; $i < $idx_cnt; $i++) {
90 $row = (defined('PMA_IDX_INCLUDED') ? $ret_keys[$i] : PMA_mysql_fetch_array($result));
92 if ($row['Key_name'] != $prev_index ){
93 $indexes[] = $row['Key_name'];
94 $prev_index = $row['Key_name'];
96 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
97 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
98 if (isset($row['Cardinality'])) {
99 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
101 // I don't know what does following column mean....
102 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
103 $indexes_info[$row['Key_name']]['Comment'] = (isset($row['Comment']))
104 ? $row['Comment']
105 : '';
106 $indexes_info[$row['Key_name']]['Index_type'] = (isset($row['Index_type']))
107 ? $row['Index_type']
108 : '';
110 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
111 if (isset($row['Sub_part'])) {
112 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
114 } // end while
116 if (defined('PMA_IDX_INCLUDED')) {
117 unset($ret_keys);
118 } else if ($result) {
119 mysql_free_result($result);
122 // Get fields and stores their name/type
123 // fields had already been grabbed in "tbl_properties.php3"
124 if (defined('PMA_IDX_INCLUDED')) {
125 mysql_data_seek($fields_rs, 0);
126 } else {
127 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($table);
128 $fields_rs = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url_0);
129 $fields_cnt = mysql_num_rows($fields_rs);
132 $fields_names = array();
133 $fields_types = array();
134 while ($row = PMA_mysql_fetch_array($fields_rs)) {
135 $fields_names[] = $row['Field'];
136 // loic1: set or enum types: slashes single quotes inside options
137 if (eregi('^(set|enum)\((.+)\)$', $row['Type'], $tmp)) {
138 $tmp[2] = substr(ereg_replace('([^,])\'\'', '\\1\\\'', ',' . $tmp[2]), 1);
139 $fields_types[] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
140 } else {
141 $fields_types[] = $row['Type'];
143 } // end while
145 if ($fields_rs) {
146 mysql_free_result($fields_rs);
151 * Stipslashes some variables if required
153 if (get_magic_quotes_gpc()) {
154 if (isset($index)) {
155 $index = stripslashes($index);
157 if (isset($old_index)) {
158 $old_index = stripslashes($old_index);
160 } // end if
164 * Do run the query to build the new index and moves back to
165 * "tbl_properties.php3"
167 if (!defined('PMA_IDX_INCLUDED')
168 && (isset($index) && isset($do_save_data))) {
170 $err_url = 'tbl_indexes.php3'
171 . '?lang=' . $lang
172 . '&amp;convcharset=' . $convcharset
173 . '&amp;server=' . $server
174 . '&amp;db=' . urlencode($db)
175 . '&amp;table=' . urlencode($table);
176 if (empty($old_index)) {
177 $err_url .= '&amp;create_index=1&amp;idx_num_fields=' . $idx_num_fields;
178 } else {
179 $err_url .= '&amp;index=' . urlencode($old_index);
182 if ($index_type == 'PRIMARY') {
183 if ($index == '') {
184 $index = 'PRIMARY';
185 } else if ($index != 'PRIMARY') {
186 PMA_mysqlDie($strPrimaryKeyName, '', FALSE, $err_url);
188 } else if ($index == 'PRIMARY') {
189 PMA_mysqlDie($strCantRenameIdxToPrimary, '', FALSE, $err_url);
193 // $sql_query is the one displayed in the query box, don't use it when you
194 // need to generate a query in this script
195 $local_query = 'ALTER TABLE ' . PMA_backquote($table);
197 // Drops the old index
198 if (!empty($old_index)) {
199 if ($old_index == 'PRIMARY') {
200 $local_query .= ' DROP PRIMARY KEY,';
201 } else {
202 $local_query .= ' DROP INDEX ' . PMA_backquote($old_index) .',';
204 } // end if
206 // Builds the new one
207 switch ($index_type) {
208 case 'PRIMARY':
209 $local_query .= ' ADD PRIMARY KEY (';
210 break;
211 case 'FULLTEXT':
212 $local_query .= ' ADD FULLTEXT ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
213 break;
214 case 'UNIQUE':
215 $local_query .= ' ADD UNIQUE ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
216 break;
217 case 'INDEX':
218 $local_query .= ' ADD INDEX ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
219 break;
220 } // end switch
221 $index_fields = '';
222 while (list($i, $name) = each($column)) {
223 if ($name != '--ignore--') {
224 $index_fields .= (empty($index_fields) ? '' : ',')
225 . PMA_backquote(get_magic_quotes_gpc() ? stripslashes($name) : $name)
226 . (empty($sub_part[$i]) ? '' : '(' . $sub_part[$i] . ')');
228 } // end while
229 if (empty($index_fields)){
230 PMA_mysqlDie($strNoIndexPartsDefined, '', FALSE, $err_url);
231 } else {
232 $local_query .= $index_fields . ')';
235 $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, FALSE, $err_url);
236 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
238 include('./tbl_properties.php3');
239 exit();
240 } // end builds the new index
244 * Edits an index or defines a new one
246 else if (!defined('PMA_IDX_INCLUDED')
247 && (isset($index) || isset($create_index))) {
249 // Prepares the form values
250 if (!isset($index)) {
251 $index = '';
253 if (!isset($old_index)){
254 $old_index = $index;
256 if (!isset($index_type)) {
257 $index_type = '';
259 if ($old_index == '' || !isset($indexes_info[$old_index])) {
260 $edited_index_info['Sequences'] = array();
261 $edited_index_data = array();
262 for ($i = 1; $i <= $idx_num_fields; $i++) {
263 $edited_index_info['Sequences'][] = $i;
264 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => '');
265 } // end for
266 if ($old_index == ''
267 && !isset($indexes_info['PRIMARY'])
268 && ($index_type == '' || $index_type == 'PRIMARY')) {
269 $old_index = 'PRIMARY';
271 } else {
272 $edited_index_info = $indexes_info[$old_index];
273 $edited_index_data = $indexes_data[$old_index];
274 if ($edited_index_info['Comment'] == 'FULLTEXT') {
275 $index_type = 'FULLTEXT';
276 } else if ($index == 'PRIMARY') {
277 $index_type = 'PRIMARY';
278 } else if ($edited_index_info['Non_unique'] == '0') {
279 $index_type = 'UNIQUE';
280 } else {
281 $index_type = 'INDEX';
283 } // end if... else...
285 if (isset($add_fields)) {
286 if (isset($prev_add_fields)) {
287 $added_fields += $prev_add_fields;
289 $field_cnt = count($edited_index_info['Sequences']) + 1;
290 for ($i = $field_cnt; $i < ($added_fields + $field_cnt); $i++) {
291 $edited_index_info['Sequences'][] = $i;
292 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => '');
293 } // end for
295 // Restore entered values
296 while (list($i, $name) = each($column)) {
297 if ($name != '--ignore--'){
298 $edited_index_data[$i+1]['Column_name'] = $name;
299 $edited_index_data[$i+1]['Sub_part'] = $sub_part[$i];
301 } // end while
302 } // end if
303 // end preparing form values
306 <!-- Build index form -->
307 <form action="tbl_indexes.php3" method="post" name="index_frm"
308 onsubmit="if (typeof(this.elements['index'].disabled) != 'undefined') {this.elements['index'].disabled = false}">
309 <input type="hidden" name="lang" value="<?php echo $lang; ?>" />
310 <input type="hidden" name="convcharset" value="<?php echo $convcharset; ?>" />
311 <input type="hidden" name="server" value="<?php echo $server; ?>" />
312 <input type="hidden" name="db" value="<?php echo htmlspecialchars($db); ?>" />
313 <input type="hidden" name="table" value="<?php echo htmlspecialchars($table); ?>" />
314 <?php
315 if (isset($create_index)) {
316 echo '<input type="hidden" name="create_index" value="1" />';
318 echo "\n";
320 <input type="hidden" name="old_index" value="<?php echo (isset($create_index) ? '' : $old_index); ?>" />
321 <b><?php echo '------ ' . (isset($create_index) ? $strCreateIndexTopic : $strModifyIndexTopic) . ' ------'; ?></b>
322 <br /><br />
324 <table border="0">
325 <tr>
326 <td><?php echo $strIndexName; ?>&nbsp;</td>
327 <td>
328 <input type="text" name="index" value="<?php echo htmlspecialchars($index); ?>" class="textfield" onfocus="this.select()" />
329 &nbsp;<?php echo $strPrimaryKeyWarning . "\n"; ?>
330 </td>
331 </tr>
332 <tr>
333 <td><?php echo $strIndexType; ?>&nbsp;</td>
334 <td>
335 <select name="index_type" onchange="return checkIndexName()">
336 <?php
337 echo "\n";
338 for ($i = 0; $i < $index_types_cnt; $i++) {
339 if ($index_types[$i] == 'PRIMARY') {
340 if ($index == 'PRIMARY' || !isset($indexes_info['PRIMARY'])) {
341 echo ' '
342 . '<option value="PRIMARY"' . (($index_type == 'PRIMARY') ? ' selected="selected"' : '') . '>PRIMARY</option>'
343 . "\n";
345 } else {
346 echo ' '
347 . '<option value="' . $index_types[$i] . '"' . (($index_type == $index_types[$i]) ? ' selected="selected"' : '') . '>'. $index_types[$i] . '</option>'
348 . "\n";
350 } // end if... else...
351 } // end for
353 </select>&nbsp;
354 <?php echo PMA_showMySQLDocu('Reference', 'ALTER_TABLE') . "\n"; ?>
355 </td>
356 </tr>
357 </table><br />
359 <table border="<?php echo $cfg['Border']; ?>" cellpadding="5">
360 <tr>
361 <th><?php echo $strField; ?></th>
362 <th><?php echo $strSize; ?></th>
363 </tr>
364 <?php
365 while (list($row_no, $seq_index) = each($edited_index_info['Sequences'])) {
366 $add_type = (is_array($fields_types) && count($fields_types) == count($fields_names));
367 $selected = $edited_index_data[$seq_index]['Column_name'];
368 if (!empty($edited_index_data[$seq_index]['Sub_part'])) {
369 $sub_part = ' value="' . $edited_index_data[$seq_index]['Sub_part'] . '"';
370 } else {
371 $sub_part = '';
373 $bgcolor = (($row_no % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo']);
374 echo "\n";
376 <tr>
377 <td bgcolor="<?php echo $bgcolor; ?>">
378 <select name="column[]">
379 <option value="--ignore--"<?php if ('--ignore--' == $selected) echo ' selected="selected"'; ?>>
380 -- <?php echo $strIgnore; ?> --</option>
381 <?php
382 reset($fields_names);
383 while (list($key, $val) = each($fields_names)) {
384 if ($index_type != 'FULLTEXT'
385 || eregi('^(varchar|text|tinytext|mediumtext|longtext)', $fields_types[$key])) {
386 echo "\n" . ' '
387 . '<option value="' . htmlspecialchars($val) . '"' . (($val == $selected) ? ' selected="selected"' : '') . '>'
388 . htmlspecialchars($val) . (($add_type) ? ' [' . $fields_types[$key] . ']' : '' ) . '</option>' . "\n";
390 } // end while
391 echo "\n";
393 </select>
394 </td>
395 <td bgcolor="<?php echo $bgcolor; ?>">
396 <input type="text" size="5" name="sub_part[]"<?php echo $sub_part; ?> onfocus="this.select()" />
397 </td>
398 </tr>
399 <?php
400 } // end while
402 echo "\n";
404 </table><br />
406 <input type="submit" name="do_save_data" value="<?php echo $strSave; ?>" /><br />
408 <?php
409 echo "\n";
410 if (isset($added_fields)) {
411 echo ' <input type="hidden" name="prev_add_fields" value="' . $added_fields . '" />';
413 if (isset($idx_num_fields)) {
414 echo ' <input type="hidden" name="idx_num_fields" value="' . $idx_num_fields . '" />' . "\n";
416 echo ' <hr /><br />' . "\n";
417 echo ' ' . sprintf($strAddToIndex, '<input type="text" name="added_fields" size="4" value="1" class="textfield" onfocus="this.select()" />') . "\n";
418 echo ' &nbsp;<input type="submit" name="add_fields" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'added_fields\', 1)" />' . "\n";
420 } else {
422 * Display indexes
425 <!-- Indexes form -->
426 <form action="tbl_indexes.php3" method="post">
427 <input type="hidden" name="lang" value="<?php echo $lang; ?>" />
428 <input type="hidden" name="convcharset" value="<?php echo $convcharset; ?>" />
429 <input type="hidden" name="server" value="<?php echo $server; ?>" />
430 <input type="hidden" name="db" value="<?php echo htmlspecialchars($db); ?>" />
431 <input type="hidden" name="table" value="<?php echo htmlspecialchars($table); ?>" />
432 <?php
433 echo "\n";
434 echo ' ' . $strIndexes . '&nbsp;:' . "\n";
435 echo ' ' . PMA_showMySQLDocu('MySQL_Optimisation', 'Optimising_Database_Structure') . '<br />' ."\n";
437 if ($idx_cnt > 0) {
439 <table border="<?php echo $cfg['Border']; ?>">
440 <tr>
441 <th><?php echo $strKeyname; ?></th>
442 <th><?php echo $strType; ?></th>
443 <th><?php echo $strCardinality; ?></th>
444 <th colspan="2"><?php echo $strAction; ?></th>
445 <th colspan="2"><?php echo $strField; ?></th>
446 </tr>
447 <?php
448 echo "\n";
449 while (list($index_no, $index_name) = each($indexes)) {
450 $cell_bgd = (($index_no % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo']);
451 $index_td = ' <td bgcolor="' . $cell_bgd . '" rowspan="' . count($indexes_info[$index_name]['Sequences']) . '">' . "\n";
452 echo ' <tr>' . "\n";
453 echo $index_td
454 . ' ' . htmlspecialchars($index_name) . "\n"
455 . ' </td>' . "\n";
457 if ((PMA_MYSQL_INT_VERSION >= 32323 && PMA_MYSQL_INT_VERSION < 40002 && $indexes_info[$index_name]['Comment'] == 'FULLTEXT')
458 || (PMA_MYSQL_INT_VERSION >= 40002 && $indexes_info[$index_name]['Index_type'] == 'FULLTEXT')) {
459 $index_type = 'FULLTEXT';
460 } else if ($index_name == 'PRIMARY') {
461 $index_type = 'PRIMARY';
462 } else if ($indexes_info[$index_name]['Non_unique'] == '0') {
463 $index_type = 'UNIQUE';
464 } else {
465 $index_type = 'INDEX';
467 echo $index_td
468 . ' ' . $index_type . "\n"
469 . ' </td>' . "\n";
471 echo str_replace('">' . "\n", '" align="right">' . "\n", $index_td)
472 . ' ' . (isset($indexes_info[$index_name]['Cardinality']) ? $indexes_info[$index_name]['Cardinality'] : $strNone) . '&nbsp;' . "\n"
473 . ' </td>' . "\n";
475 if ($index_name == 'PRIMARY') {
476 $local_query = urlencode('ALTER TABLE ' . PMA_backquote($table) . ' DROP PRIMARY KEY');
477 $js_msg = 'ALTER TABLE ' . PMA_jsFormat($table) . ' DROP PRIMARY KEY';
478 $zero_rows = urlencode($strPrimaryKeyHasBeenDropped);
479 } else {
480 $local_query = urlencode('ALTER TABLE ' . PMA_backquote($table) . ' DROP INDEX ' . PMA_backquote($index_name));
481 $js_msg = 'ALTER TABLE ' . PMA_jsFormat($table) . ' DROP INDEX ' . PMA_jsFormat($index_name);
482 $zero_rows = urlencode(sprintf($strIndexHasBeenDropped, htmlspecialchars($index_name)));
484 echo $index_td
485 . ' <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"
486 . ' </td>' . "\n";
488 echo $index_td
489 . ' <a href="tbl_indexes.php3?' . $url_query . '&amp;index=' . urlencode($index_name) . '">' . $strEdit . '</a>' . "\n"
490 . ' </td>' . "\n";
492 while (list($row_no, $seq_index) = each($indexes_info[$index_name]['Sequences'])) {
493 if ($row_no > 0) {
494 echo ' <tr>' . "\n";
496 if (!empty($indexes_data[$index_name][$seq_index]['Sub_part'])) {
497 echo ' <td bgcolor="' . $cell_bgd . '">' . "\n"
498 . ' ' . $indexes_data[$index_name][$seq_index]['Column_name'] . "\n"
499 . ' </td>' . "\n";
500 echo ' <td align="right" bgcolor="' . $cell_bgd . '">' . "\n"
501 . ' ' . $indexes_data[$index_name][$seq_index]['Sub_part'] . "\n"
502 . ' </td>' . "\n";
503 echo ' </tr>' . "\n";
504 } else {
505 echo ' <td bgcolor="' . $cell_bgd . '" colspan="2">' . "\n"
506 . ' ' . $indexes_data[$index_name][$seq_index]['Column_name'] . "\n"
507 . ' </td>' . "\n";
508 echo ' </tr>' . "\n";
510 } // end while
511 } // end while
513 </table><br />
514 <?php
515 echo "\n\n";
516 } // end display indexes
517 else {
518 // none indexes
519 echo "\n" . ' <br />' . "\n";
520 echo ' <i>' . $strNoIndex . '</i><br /><br />' . "\n\n";
523 echo ' ' . sprintf($strCreateIndex, '<input type="text" size="4" name="idx_num_fields" value="1" class="textfield" />') . "\n";
524 echo ' &nbsp;<input type="submit" name="create_index" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'idx_num_fields\', 1)" />' . "\n";
525 echo ' ';
526 } // end display indexes
529 </form>
532 <?php
534 * Displays the footer
536 echo "\n";
538 if (!defined('PMA_IDX_INCLUDED')){
539 include('./footer.inc.php3');