2.3.3
[phpmyadmin/crack.git] / tbl_indexes.php3
blob845126e86e48641f4a1d635ace37130384949a95
1 <?php
2 /* $Id$ */
5 /**
6 * Gets some core libraries
7 */
8 if (!defined('PMA_GRAB_GLOBALS_INCLUDED')) {
9 include('./libraries/grab_globals.lib.php3');
11 if (!defined('PMA_COMMON_LIB_INCLUDED')) {
12 include('./libraries/common.lib.php3');
16 /**
17 * Defines the index types ("FULLTEXT" is available since MySQL 3.23.23)
19 $index_types_cnt = 3;
20 $index_types = array(
21 'PRIMARY',
22 'INDEX',
23 'UNIQUE'
25 if (PMA_MYSQL_INT_VERSION >= 32323) {
26 $index_types[] = 'FULLTEXT';
27 $index_types_cnt++;
31 /**
32 * Ensures the db & table are valid, then loads headers and gets indexes
33 * informations.
34 * Skipped if this script is called by "tbl_properties.php3"
36 if (!defined('PMA_IDX_INCLUDED')) {
37 // Not a valid db name -> back to the welcome page
38 if (!empty($db)) {
39 $is_db = @PMA_mysql_select_db($db);
41 if (empty($db) || !$is_db) {
42 header('Location: ' . $cfg['PmaAbsoluteUri'] . 'main.php3?lang=' . $lang . '&convcharset=' . $convcharset . '&server=' . $server . (isset($message) ? '&message=' . urlencode($message) : '') . '&reload=1');
43 exit();
45 // Not a valid table name -> back to the default db_details sub-page
46 if (!empty($table)) {
47 $is_table = @PMA_mysql_query('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'');
49 if (empty($table)
50 || !($is_table && @mysql_numrows($is_table))) {
51 header('Location: ' . $cfg['PmaAbsoluteUri'] . $cfg['DefaultTabDatabase'] . '?lang=' . $lang . '&convcharset=' . $convcharset . '&server=' . $server .'&db=' . urlencode($db) . (isset($message) ? '&message=' . urlencode($message) : '') . '&reload=1');
52 exit();
53 } else if (isset($is_table)) {
54 mysql_free_result($is_table);
57 // Displays headers (if needed)
58 $js_to_run = ((isset($index) && isset($do_save_data)) ? 'functions.js' : 'indexes.js');
59 include('./header.inc.php3');
60 } // end if
63 /**
64 * Gets fields and indexes informations
66 if (defined('PMA_IDX_INCLUDED')) {
67 $err_url_0 = 'db_details.php3'
68 . '?lang=' . $lang
69 . '&amp;convcharset=' . $convcharset
70 . '&amp;server=' . $server
71 . '&amp;db=' . urlencode($db);
74 // Gets table keys and store them in arrays
75 $indexes = array();
76 $prev_index = '';
77 $indexes_info = array();
78 $indexes_data = array();
79 // keys had already been grabbed in "tbl_properties.php3"
80 if (defined('PMA_IDX_INCLUDED')) {
81 $idx_cnt = count($ret_keys);
82 } else {
83 $local_query = 'SHOW KEYS FROM ' . PMA_backquote($table);
84 $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url_0);
85 $idx_cnt = mysql_num_rows($result);
88 for ($i = 0; $i < $idx_cnt; $i++) {
89 $row = (defined('PMA_IDX_INCLUDED') ? $ret_keys[$i] : PMA_mysql_fetch_array($result));
91 if ($row['Key_name'] != $prev_index ){
92 $indexes[] = $row['Key_name'];
93 $prev_index = $row['Key_name'];
95 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
96 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
97 if (isset($row['Cardinality'])) {
98 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
100 // I don't know what does following column mean....
101 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
102 $indexes_info[$row['Key_name']]['Comment'] = (isset($row['Comment']))
103 ? $row['Comment']
104 : '';
106 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
107 if (isset($row['Sub_part'])) {
108 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
110 } // end while
112 if (defined('PMA_IDX_INCLUDED')) {
113 unset($ret_keys);
114 } else if ($result) {
115 mysql_free_result($result);
118 // Get fields and stores their name/type
119 // fields had already been grabbed in "tbl_properties.php3"
120 if (defined('PMA_IDX_INCLUDED')) {
121 mysql_data_seek($fields_rs, 0);
122 } else {
123 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($table);
124 $fields_rs = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url_0);
125 $fields_cnt = mysql_num_rows($fields_rs);
128 $fields_names = array();
129 $fields_types = array();
130 while ($row = PMA_mysql_fetch_array($fields_rs)) {
131 $fields_names[] = $row['Field'];
132 // loic1: set or enum types: slashes single quotes inside options
133 if (eregi('^(set|enum)\((.+)\)$', $row['Type'], $tmp)) {
134 $tmp[2] = substr(ereg_replace('([^,])\'\'', '\\1\\\'', ',' . $tmp[2]), 1);
135 $fields_types[] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
136 } else {
137 $fields_types[] = $row['Type'];
139 } // end while
141 if ($fields_rs) {
142 mysql_free_result($fields_rs);
147 * Stipslashes some variables if required
149 if (get_magic_quotes_gpc()) {
150 if (isset($index)) {
151 $index = stripslashes($index);
153 if (isset($old_index)) {
154 $old_index = stripslashes($old_index);
156 } // end if
160 * Do run the query to build the new index and moves back to
161 * "tbl_properties.php3"
163 if (!defined('PMA_IDX_INCLUDED')
164 && (isset($index) && isset($do_save_data))) {
166 $err_url = 'tbl_indexes.php3'
167 . '?lang=' . $lang
168 . '&amp;convcharset=' . $convcharset
169 . '&amp;server=' . $server
170 . '&amp;db=' . urlencode($db)
171 . '&amp;table=' . urlencode($table);
172 if (empty($old_index)) {
173 $err_url .= '&amp;create_index=1&amp;idx_num_fields=' . $idx_num_fields;
174 } else {
175 $err_url .= '&amp;index=' . urlencode($old_index);
178 if ($index_type == 'PRIMARY') {
179 if ($index == '') {
180 $index = 'PRIMARY';
181 } else if ($index != 'PRIMARY') {
182 PMA_mysqlDie($strPrimaryKeyName, '', FALSE, $err_url);
184 } else if ($index == 'PRIMARY') {
185 PMA_mysqlDie($strCantRenameIdxToPrimary, '', FALSE, $err_url);
189 // $sql_query is the one displayed in the query box, don't use it when you
190 // need to generate a query in this script
191 $local_query = 'ALTER TABLE ' . PMA_backquote($table);
193 // Drops the old index
194 if (!empty($old_index)) {
195 if ($old_index == 'PRIMARY') {
196 $local_query .= ' DROP PRIMARY KEY,';
197 } else {
198 $local_query .= ' DROP INDEX ' . PMA_backquote($old_index) .',';
200 } // end if
202 // Builds the new one
203 switch ($index_type) {
204 case 'PRIMARY':
205 $local_query .= ' ADD PRIMARY KEY (';
206 break;
207 case 'FULLTEXT':
208 $local_query .= ' ADD FULLTEXT ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
209 break;
210 case 'UNIQUE':
211 $local_query .= ' ADD UNIQUE ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
212 break;
213 case 'INDEX':
214 $local_query .= ' ADD INDEX ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
215 break;
216 } // end switch
217 $index_fields = '';
218 while (list($i, $name) = each($column)) {
219 if ($name != '--ignore--') {
220 $index_fields .= (empty($index_fields) ? '' : ',')
221 . PMA_backquote(get_magic_quotes_gpc() ? stripslashes($name) : $name)
222 . (empty($sub_part[$i]) ? '' : '(' . $sub_part[$i] . ')');
224 } // end while
225 if (empty($index_fields)){
226 PMA_mysqlDie($strNoIndexPartsDefined, '', FALSE, $err_url);
227 } else {
228 $local_query .= $index_fields . ')';
231 $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, FALSE, $err_url);
232 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
234 include('./tbl_properties.php3');
235 exit();
236 } // end builds the new index
240 * Edits an index or defines a new one
242 else if (!defined('PMA_IDX_INCLUDED')
243 && (isset($index) || isset($create_index))) {
245 // Prepares the form values
246 if (!isset($index)) {
247 $index = '';
249 if (!isset($old_index)){
250 $old_index = $index;
252 if (!isset($index_type)) {
253 $index_type = '';
255 if ($old_index == '' || !isset($indexes_info[$old_index])) {
256 $edited_index_info['Sequences'] = array();
257 $edited_index_data = array();
258 for ($i = 1; $i <= $idx_num_fields; $i++) {
259 $edited_index_info['Sequences'][] = $i;
260 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => '');
261 } // end for
262 if ($old_index == ''
263 && !isset($indexes_info['PRIMARY'])
264 && ($index_type == '' || $index_type == 'PRIMARY')) {
265 $old_index = 'PRIMARY';
267 } else {
268 $edited_index_info = $indexes_info[$old_index];
269 $edited_index_data = $indexes_data[$old_index];
270 if ($edited_index_info['Comment'] == 'FULLTEXT') {
271 $index_type = 'FULLTEXT';
272 } else if ($index == 'PRIMARY') {
273 $index_type = 'PRIMARY';
274 } else if ($edited_index_info['Non_unique'] == '0') {
275 $index_type = 'UNIQUE';
276 } else {
277 $index_type = 'INDEX';
279 } // end if... else...
281 if (isset($add_fields)) {
282 if (isset($prev_add_fields)) {
283 $added_fields += $prev_add_fields;
285 $field_cnt = count($edited_index_info['Sequences']) + 1;
286 for ($i = $field_cnt; $i < ($added_fields + $field_cnt); $i++) {
287 $edited_index_info['Sequences'][] = $i;
288 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => '');
289 } // end for
291 // Restore entered values
292 while (list($i, $name) = each($column)) {
293 if ($name != '--ignore--'){
294 $edited_index_data[$i+1]['Column_name'] = $name;
295 $edited_index_data[$i+1]['Sub_part'] = $sub_part[$i];
297 } // end while
298 } // end if
299 // end preparing form values
302 <!-- Build index form -->
303 <form action="tbl_indexes.php3" method="post" name="index_frm"
304 onsubmit="if (typeof(this.elements['index'].disabled) != 'undefined') {this.elements['index'].disabled = false}">
305 <input type="hidden" name="lang" value="<?php echo $lang; ?>" />
306 <input type="hidden" name="convcharset" value="<?php echo $convcharset; ?>" />
307 <input type="hidden" name="server" value="<?php echo $server; ?>" />
308 <input type="hidden" name="db" value="<?php echo $db; ?>" />
309 <input type="hidden" name="table" value="<?php echo $table; ?>" />
310 <?php
311 if (isset($create_index)) {
312 echo '<input type="hidden" name="create_index" value="1" />';
314 echo "\n";
316 <input type="hidden" name="old_index" value="<?php echo (isset($create_index) ? '' : $old_index); ?>" />
317 <b><?php echo '------ ' . (isset($create_index) ? $strCreateIndexTopic : $strModifyIndexTopic) . ' ------'; ?></b>
318 <br /><br />
320 <table border="0">
321 <tr>
322 <td><?php echo $strIndexName; ?>&nbsp;</td>
323 <td>
324 <input type="text" name="index" value="<?php echo htmlspecialchars($index); ?>" class="textfield" onfocus="this.select()" />
325 &nbsp;<?php echo $strPrimaryKeyWarning . "\n"; ?>
326 </td>
327 </tr>
328 <tr>
329 <td><?php echo $strIndexType; ?>&nbsp;</td>
330 <td>
331 <select name="index_type" onchange="return checkIndexName()">
332 <?php
333 echo "\n";
334 for ($i = 0; $i < $index_types_cnt; $i++) {
335 if ($index_types[$i] == 'PRIMARY') {
336 if ($index == 'PRIMARY' || !isset($indexes_info['PRIMARY'])) {
337 echo ' '
338 . '<option value="PRIMARY"' . (($index_type == 'PRIMARY') ? ' selected="selected"' : '') . '>PRIMARY</option>'
339 . "\n";
341 } else {
342 echo ' '
343 . '<option value="' . $index_types[$i] . '"' . (($index_type == $index_types[$i]) ? ' selected="selected"' : '') . '>'. $index_types[$i] . '</option>'
344 . "\n";
346 } // end if... else...
347 } // end for
349 </select>&nbsp;
350 <?php echo PMA_showMySQLDocu('Reference', 'ALTER_TABLE.html') . "\n"; ?>
351 </td>
352 </tr>
353 </table><br />
355 <table border="<?php echo $cfg['Border']; ?>" cellpadding="5">
356 <tr>
357 <th><?php echo $strField; ?></th>
358 <th><?php echo $strSize; ?></th>
359 </tr>
360 <?php
361 while (list($row_no, $seq_index) = each($edited_index_info['Sequences'])) {
362 $add_type = (is_array($fields_types) && count($fields_types) == count($fields_names));
363 $selected = $edited_index_data[$seq_index]['Column_name'];
364 if (!empty($edited_index_data[$seq_index]['Sub_part'])) {
365 $sub_part = ' value="' . $edited_index_data[$seq_index]['Sub_part'] . '"';
366 } else {
367 $sub_part = '';
369 $bgcolor = (($row_no % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo']);
370 echo "\n";
372 <tr>
373 <td bgcolor="<?php echo $bgcolor; ?>">
374 <select name="column[]">
375 <option value="--ignore--"<?php if ('--ignore--' == $selected) echo ' selected="selected"'; ?>>
376 -- <?php echo $strIgnore; ?> --</option>
377 <?php
378 reset($fields_names);
379 while (list($key, $val) = each($fields_names)) {
380 if ($index_type != 'FULLTEXT'
381 || eregi('^(varchar|text|tinytext|mediumtext|longtext)', $fields_types[$key])) {
382 echo "\n" . ' '
383 . '<option value="' . htmlspecialchars($val) . '"' . (($val == $selected) ? ' selected="selected"' : '') . '>'
384 . htmlspecialchars($val) . (($add_type) ? ' [' . $fields_types[$key] . ']' : '' ) . '</option>' . "\n";
386 } // end while
387 echo "\n";
389 </select>
390 </td>
391 <td bgcolor="<?php echo $bgcolor; ?>">
392 <input type="text" size="5" name="sub_part[]"<?php echo $sub_part; ?> onfocus="this.select()" />
393 </td>
394 </tr>
395 <?php
396 } // end while
398 echo "\n";
400 </table><br />
402 <input type="submit" name="do_save_data" value="<?php echo $strSave; ?>" /><br />
404 <?php
405 echo "\n";
406 if (isset($added_fields)) {
407 echo ' <input type="hidden" name="prev_add_fields" value="' . $added_fields . '" />';
409 if (isset($idx_num_fields)) {
410 echo ' <input type="hidden" name="idx_num_fields" value="' . $idx_num_fields . '" />' . "\n";
412 echo ' <hr /><br />' . "\n";
413 echo ' ' . sprintf($strAddToIndex, '<input type="text" name="added_fields" size="4" value="1" class="textfield" onfocus="this.select()" />') . "\n";
414 echo ' &nbsp;<input type="submit" name="add_fields" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'added_fields\', 1)" />' . "\n";
416 } else {
418 * Display indexes
421 <!-- Indexes form -->
422 <form action="tbl_indexes.php3" method="post">
423 <input type="hidden" name="lang" value="<?php echo $lang; ?>" />
424 <input type="hidden" name="convcharset" value="<?php echo $convcharset; ?>" />
425 <input type="hidden" name="server" value="<?php echo $server; ?>" />
426 <input type="hidden" name="db" value="<?php echo $db; ?>" />
427 <input type="hidden" name="table" value="<?php echo $table; ?>" />
428 <?php
429 echo "\n";
430 echo ' ' . $strIndexes . '&nbsp;:' . "\n";
431 echo ' ' . PMA_showMySQLDocu('MySQL_Optimisation', 'Optimising_Database_Structure') . '<br />' ."\n";
433 if ($idx_cnt > 0) {
435 <table border="<?php echo $cfg['Border']; ?>">
436 <tr>
437 <th><?php echo $strKeyname; ?></th>
438 <th><?php echo $strType; ?></th>
439 <th><?php echo $strCardinality; ?></th>
440 <th colspan="2"><?php echo $strAction; ?></th>
441 <th colspan="2"><?php echo $strField; ?></th>
442 </tr>
443 <?php
444 echo "\n";
445 while (list($index_no, $index_name) = each($indexes)) {
446 $cell_bgd = (($index_no % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo']);
447 $index_td = ' <td bgcolor="' . $cell_bgd . '" rowspan="' . count($indexes_info[$index_name]['Sequences']) . '">' . "\n";
448 echo ' <tr>' . "\n";
449 echo $index_td
450 . ' ' . htmlspecialchars($index_name) . "\n"
451 . ' </td>' . "\n";
453 if ($indexes_info[$index_name]['Comment'] == 'FULLTEXT') {
454 $index_type = 'FULLTEXT';
455 } else if ($index_name == 'PRIMARY') {
456 $index_type = 'PRIMARY';
457 } else if ($indexes_info[$index_name]['Non_unique'] == '0') {
458 $index_type = 'UNIQUE';
459 } else {
460 $index_type = 'INDEX';
462 echo $index_td
463 . ' ' . $index_type . "\n"
464 . ' </td>' . "\n";
466 echo str_replace('">' . "\n", '" align="right">' . "\n", $index_td)
467 . ' ' . (isset($indexes_info[$index_name]['Cardinality']) ? $indexes_info[$index_name]['Cardinality'] : $strNone) . '&nbsp;' . "\n"
468 . ' </td>' . "\n";
470 if ($index_name == 'PRIMARY') {
471 $local_query = urlencode('ALTER TABLE ' . PMA_backquote($table) . ' DROP PRIMARY KEY');
472 $js_msg = 'ALTER TABLE ' . PMA_jsFormat($table) . ' DROP PRIMARY KEY';
473 $zero_rows = urlencode($strPrimaryKeyHasBeenDropped);
474 } else {
475 $local_query = urlencode('ALTER TABLE ' . PMA_backquote($table) . ' DROP INDEX ' . PMA_backquote($index_name));
476 $js_msg = 'ALTER TABLE ' . PMA_jsFormat($table) . ' DROP INDEX ' . PMA_jsFormat($index_name);
477 $zero_rows = urlencode(sprintf($strIndexHasBeenDropped, htmlspecialchars($index_name)));
479 echo $index_td
480 . ' <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"
481 . ' </td>' . "\n";
483 echo $index_td
484 . ' <a href="tbl_indexes.php3?' . $url_query . '&amp;index=' . urlencode($index_name) . '">' . $strEdit . '</a>' . "\n"
485 . ' </td>' . "\n";
487 while (list($row_no, $seq_index) = each($indexes_info[$index_name]['Sequences'])) {
488 if ($row_no > 0) {
489 echo ' <tr>' . "\n";
491 if (!empty($indexes_data[$index_name][$seq_index]['Sub_part'])) {
492 echo ' <td bgcolor="' . $cell_bgd . '">' . "\n"
493 . ' ' . $indexes_data[$index_name][$seq_index]['Column_name'] . "\n"
494 . ' </td>' . "\n";
495 echo ' <td align="right" bgcolor="' . $cell_bgd . '">' . "\n"
496 . ' ' . $indexes_data[$index_name][$seq_index]['Sub_part'] . "\n"
497 . ' </td>' . "\n";
498 echo ' </tr>' . "\n";
499 } else {
500 echo ' <td bgcolor="' . $cell_bgd . '" colspan="2">' . "\n"
501 . ' ' . $indexes_data[$index_name][$seq_index]['Column_name'] . "\n"
502 . ' </td>' . "\n";
503 echo ' </tr>' . "\n";
505 } // end while
506 } // end while
508 </table><br />
509 <?php
510 echo "\n\n";
511 } // end display indexes
512 else {
513 // none indexes
514 echo "\n" . ' <br />' . "\n";
515 echo ' <i>' . $strNoIndex . '</i><br /><br />' . "\n\n";
518 echo ' ' . sprintf($strCreateIndex, '<input type="text" size="4" name="idx_num_fields" value="1" class="textfield" />') . "\n";
519 echo ' &nbsp;<input type="submit" name="create_index" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'idx_num_fields\', 1)" />' . "\n";
520 echo ' ';
521 } // end display indexes
524 </form>
527 <?php
529 * Displays the footer
531 echo "\n";
533 if (!defined('PMA_IDX_INCLUDED')){
534 include('./footer.inc.php3');