patch #1198492, current version check
[phpmyadmin/crack.git] / tbl_indexes.php
blob7ef684ad5ab5c767e684c27602cc3b93df7aa910
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Gets some core libraries
7 */
8 require_once('./libraries/grab_globals.lib.php');
9 require_once('./libraries/common.lib.php');
10 require_once('./libraries/tbl_indexes.lib.php');
12 /**
13 * Defines the index types ("FULLTEXT" is available since MySQL 3.23.23)
15 $index_types = PMA_get_indextypes();
16 $index_types_cnt = count($index_types);
18 /**
19 * Ensures the db & table are valid, then loads headers and gets indexes
20 * informations.
21 * Skipped if this script is called by "tbl_properties.php"
23 if (!defined('PMA_IDX_INCLUDED')) {
24 // Not a valid db name -> back to the welcome page
25 if (!empty($db)) {
26 $is_db = PMA_DBI_select_db($db);
28 if (empty($db) || !$is_db) {
29 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . 'main.php?' . PMA_generate_common_url('', '', '&') . (isset($message) ? '&message=' . urlencode($message) : '') . '&reload=1');
30 exit;
32 // Not a valid table name -> back to the default db_details sub-page
33 if (!empty($table)) {
34 $is_table = PMA_DBI_query('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'', NULL, PMA_DBI_QUERY_STORE);
36 if (empty($table)
37 || !($is_table && PMA_DBI_num_rows($is_table))) {
38 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . $cfg['DefaultTabDatabase'] . '?' . PMA_generate_common_url($db, '', '&') . (isset($message) ? '&message=' . urlencode($message) : '') . '&reload=1');
39 exit;
40 } else if (isset($is_table)) {
41 PMA_DBI_free_result($is_table);
44 // Displays headers (if needed)
45 $js_to_run = ((isset($index) && isset($do_save_data)) ? 'functions.js' : 'indexes.js');
46 require_once('./header.inc.php');
47 } // end if
50 /**
51 * Gets fields and indexes informations
53 if (!defined('PMA_IDX_INCLUDED')) {
54 $err_url_0 = 'db_details.php?' . PMA_generate_common_url($db);
57 // Gets table keys and store them in arrays
58 $indexes = array();
59 $indexes_info = array();
60 $indexes_data = array();
61 // keys had already been grabbed in "tbl_properties.php"
62 if (!defined('PMA_IDX_INCLUDED')) {
63 $ret_keys = PMA_get_indexes($table, $err_url_0);
66 PMA_extract_indexes($ret_keys, $indexes, $indexes_info, $indexes_data);
68 // Get fields and stores their name/type
69 // fields had already been grabbed in "tbl_properties.php"
70 if (!defined('PMA_IDX_INCLUDED')) {
71 $fields_rs = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';');
72 $save_row = array();
73 while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
74 $save_row[] = $row;
78 $fields_names = array();
79 $fields_types = array();
80 foreach ($save_row AS $saved_row_key => $row) {
81 $fields_names[] = $row['Field'];
82 // loic1: set or enum types: slashes single quotes inside options
83 if (preg_match('@^(set|enum)\((.+)\)$@i', $row['Type'], $tmp)) {
84 $tmp[2] = substr(preg_replace('@([^,])\'\'@', '\\1\\\'', ',' . $tmp[2]), 1);
85 $fields_types[] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
86 } else {
87 $fields_types[] = $row['Type'];
89 } // end while
91 if ($fields_rs) {
92 PMA_DBI_free_result($fields_rs);
96 /**
97 * Do run the query to build the new index and moves back to
98 * "tbl_properties.php"
100 if (!defined('PMA_IDX_INCLUDED')
101 && (isset($index) && isset($do_save_data))) {
103 $err_url = 'tbl_indexes.php?' . PMA_generate_common_url($db, $table);
104 if (empty($old_index)) {
105 $err_url .= '&amp;create_index=1&amp;idx_num_fields=' . $idx_num_fields;
106 } else {
107 $err_url .= '&amp;index=' . urlencode($old_index);
110 if ($index_type == 'PRIMARY') {
111 if ($index == '') {
112 $index = 'PRIMARY';
113 } else if ($index != 'PRIMARY') {
114 PMA_mysqlDie($strPrimaryKeyName, '', FALSE, $err_url);
116 } else if ($index == 'PRIMARY') {
117 PMA_mysqlDie($strCantRenameIdxToPrimary, '', FALSE, $err_url);
121 // $sql_query is the one displayed in the query box
122 $sql_query = 'ALTER TABLE ' . PMA_backquote($table);
124 // Drops the old index
125 if (!empty($old_index)) {
126 if ($old_index == 'PRIMARY') {
127 $sql_query .= ' DROP PRIMARY KEY,';
128 } else {
129 $sql_query .= ' DROP INDEX ' . PMA_backquote($old_index) .',';
131 } // end if
133 // Builds the new one
134 switch ($index_type) {
135 case 'PRIMARY':
136 $sql_query .= ' ADD PRIMARY KEY (';
137 break;
138 case 'FULLTEXT':
139 $sql_query .= ' ADD FULLTEXT ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
140 break;
141 case 'UNIQUE':
142 $sql_query .= ' ADD UNIQUE ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
143 break;
144 case 'INDEX':
145 $sql_query .= ' ADD INDEX ' . (empty($index) ? '' : PMA_backquote($index)) . ' (';
146 break;
147 } // end switch
148 $index_fields = '';
149 foreach ($column AS $i => $name) {
150 if ($name != '--ignore--') {
151 $index_fields .= (empty($index_fields) ? '' : ',')
152 . PMA_backquote($name)
153 . (empty($sub_part[$i]) ? '' : '(' . $sub_part[$i] . ')');
155 } // end while
156 if (empty($index_fields)){
157 PMA_mysqlDie($strNoIndexPartsDefined, '', FALSE, $err_url);
158 } else {
159 $sql_query .= $index_fields . ')';
162 $result = PMA_DBI_query($sql_query);
163 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
165 $active_page = 'tbl_properties_structure.php';
166 require('./tbl_properties_structure.php');
167 } // end builds the new index
171 * Edits an index or defines a new one
173 else if (!defined('PMA_IDX_INCLUDED')
174 && (isset($index) || isset($create_index))) {
176 // Prepares the form values
177 if (!isset($index)) {
178 $index = '';
180 if (!isset($old_index)){
181 $old_index = $index;
183 if (!isset($index_type)) {
184 $index_type = '';
186 if ($old_index == '' || !isset($indexes_info[$old_index])) {
187 $edited_index_info['Sequences'] = array();
188 $edited_index_data = array();
189 for ($i = 1; $i <= $idx_num_fields; $i++) {
190 $edited_index_info['Sequences'][] = $i;
191 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => '');
192 } // end for
193 if ($old_index == ''
194 && !isset($indexes_info['PRIMARY'])
195 && ($index_type == '' || $index_type == 'PRIMARY')) {
196 $old_index = 'PRIMARY';
198 } else {
199 $edited_index_info = $indexes_info[$old_index];
200 $edited_index_data = $indexes_data[$old_index];
203 if ((PMA_MYSQL_INT_VERSION < 40002 && $edited_index_info['Comment'] == 'FULLTEXT')
204 || (PMA_MYSQL_INT_VERSION >= 40002 && $edited_index_info['Index_type'] == 'FULLTEXT')) {
205 $index_type = 'FULLTEXT';
206 } else if ($index == 'PRIMARY') {
207 $index_type = 'PRIMARY';
208 } else if ($edited_index_info['Non_unique'] == '0') {
209 $index_type = 'UNIQUE';
210 } else {
211 $index_type = 'INDEX';
213 } // end if... else...
215 if (isset($add_fields)) {
216 if (isset($prev_add_fields)) {
217 $added_fields += $prev_add_fields;
219 $field_cnt = count($edited_index_info['Sequences']) + 1;
220 for ($i = $field_cnt; $i < ($added_fields + $field_cnt); $i++) {
221 $edited_index_info['Sequences'][] = $i;
222 $edited_index_data[$i] = array('Column_name' => '', 'Sub_part' => '');
223 } // end for
225 // Restore entered values
226 foreach ($column AS $i => $name) {
227 if ($name != '--ignore--'){
228 $edited_index_data[$i+1]['Column_name'] = $name;
229 $edited_index_data[$i+1]['Sub_part'] = $sub_part[$i];
231 } // end while
232 } // end if
233 // end preparing form values
236 <!-- Build index form -->
237 <form action="./tbl_indexes.php" method="post" name="index_frm"
238 onsubmit="if (typeof(this.elements['index'].disabled) != 'undefined') {this.elements['index'].disabled = false}">
239 <table border="0" cellpadding="2" cellspacing="1">
240 <tr><td class="tblHeaders" colspan="2">
241 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
242 <?php
243 if (isset($create_index)) {
244 echo '<input type="hidden" name="create_index" value="1" />';
246 echo "\n";
248 <input type="hidden" name="old_index" value="<?php echo (isset($create_index) ? '' : htmlspecialchars($old_index)); ?>" />
249 <?php echo ' ' . (isset($create_index) ? $strCreateIndexTopic : $strModifyIndexTopic) . ' '; ?>
250 </th></tr>
253 <tr>
254 <td align="right"><b><?php echo $strIndexName; ?></b>&nbsp;</th>
255 <td>
256 <input type="text" name="index" value="<?php echo htmlspecialchars($index); ?>" size="25" onfocus="this.select()" />
257 </td>
258 </tr>
259 <tr><td align="right"><?php
260 if ($cfg['ErrorIconic']) {
261 echo '<img src="' . $pmaThemeImage . 's_warn.png" width="16" height="16" border="0" alt="Attention" />';
263 ?></td><td><?php echo $strPrimaryKeyWarning . "\n"; ?></td></tr>
264 <tr>
265 <td align="right"><b><?php echo $strIndexType; ?></b>&nbsp;</td>
266 <td>
267 <select name="index_type" onchange="return checkIndexName()">
268 <?php
269 echo "\n";
270 for ($i = 0; $i < $index_types_cnt; $i++) {
271 if ($index_types[$i] == 'PRIMARY') {
272 if ($index == 'PRIMARY' || !isset($indexes_info['PRIMARY'])) {
273 echo ' '
274 . '<option value="PRIMARY"' . (($index_type == 'PRIMARY') ? ' selected="selected"' : '') . '>PRIMARY</option>'
275 . "\n";
277 } else {
278 echo ' '
279 . '<option value="' . $index_types[$i] . '"' . (($index_type == $index_types[$i]) ? ' selected="selected"' : '') . '>'. $index_types[$i] . '</option>'
280 . "\n";
282 } // end if... else...
283 } // end for
285 </select>
286 <?php echo PMA_showMySQLDocu('Reference', 'ALTER_TABLE') . "\n"; ?>
287 </td>
288 </tr>
290 <tr><td valign="top" align="right"><b><?php echo $strFields; ?> :</b>&nbsp;</td><td><table border="<?php echo $cfg['Border']; ?>" cellpadding="2" cellspacing="1">
291 <tr>
292 <th><?php echo $strField; ?></th>
293 <th><?php echo $strSize; ?></th>
294 </tr>
295 <?php
296 foreach ($edited_index_info['Sequences'] AS $row_no => $seq_index) {
297 $add_type = (is_array($fields_types) && count($fields_types) == count($fields_names));
298 $selected = $edited_index_data[$seq_index]['Column_name'];
299 if (!empty($edited_index_data[$seq_index]['Sub_part'])) {
300 $sub_part = ' value="' . $edited_index_data[$seq_index]['Sub_part'] . '"';
301 } else {
302 $sub_part = '';
304 $bgcolor = (($row_no % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo']);
305 echo "\n";
307 <tr>
308 <td bgcolor="<?php echo $bgcolor; ?>">
309 <select name="column[]">
310 <option value="--ignore--"<?php if ('--ignore--' == $selected) echo ' selected="selected"'; ?>>
311 -- <?php echo $strIgnore; ?> --</option>
312 <?php
313 foreach ($fields_names AS $key => $val) {
314 if ($index_type != 'FULLTEXT'
315 || preg_match('@^(varchar|text|tinytext|mediumtext|longtext)@i', $fields_types[$key])) {
316 echo "\n" . ' '
317 . '<option value="' . htmlspecialchars($val) . '"' . (($val == $selected) ? ' selected="selected"' : '') . '>'
318 . htmlspecialchars($val) . (($add_type) ? ' [' . $fields_types[$key] . ']' : '' ) . '</option>' . "\n";
320 } // end while
321 echo "\n";
323 </select>
324 </td>
325 <td bgcolor="<?php echo $bgcolor; ?>">
326 <input type="text" size="5" name="sub_part[]"<?php echo $sub_part; ?> onfocus="this.select()" />
327 </td>
328 </tr>
329 <?php
330 } // end while
332 echo "\n";
334 <tr><td colspan="2"><?php
335 echo "\n";
336 if (isset($added_fields)) {
337 echo ' <input type="hidden" name="prev_add_fields" value="' . $added_fields . '" />';
339 if (isset($idx_num_fields)) {
340 echo ' <input type="hidden" name="idx_num_fields" value="' . $idx_num_fields . '" />' . "\n";
342 echo ' ' . "\n";
343 echo ' ' . sprintf($strAddToIndex, '<input type="text" name="added_fields" size="2" value="1" onfocus="this.select()" style="vertical-align: middle;" />') . "\n";
344 echo ' &nbsp;<input type="submit" name="add_fields" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'added_fields\', \'' . str_replace('\'', '\\\'', $GLOBALS['strInvalidColumnCount']) . '\', 1)" style="vertical-align: middle;" />' . "\n";
345 ?></td>
346 </tr>
347 </table></td></tr>
348 <tr><td colspan="2" class="tblFooters" align="center">
349 <input type="submit" name="do_save_data" value="<?php echo $strSave; ?>" /></td></tr>
350 </table>
351 </form>
352 <?php
353 } else {
355 * Display indexes
358 <!-- Indexes form -->
359 <form action="./tbl_indexes.php" method="post" onsubmit="return checkFormElementInRange(this, 'idx_num_fields', '<?php echo str_replace('\'', '\\\'', $GLOBALS['strInvalidColumnCount']); ?>', 1)">
360 <table border="0" cellpadding="2" cellspacing="1">
361 <tr><td class="tblHeaders" colspan="7">
362 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
363 <?php
364 echo "\n";
365 echo ' ' . $strIndexes . ':' . "\n";
366 echo ' ' . PMA_showMySQLDocu('MySQL_Optimisation', 'Optimising_Database_Structure') . "\n";
367 ?></td></tr><?php
368 $edit_link_text = '';
369 $drop_link_text = '';
371 // We need to copy the value or else the == 'both' check will always return true
372 $propicon = (string)$cfg['PropertiesIconic'];
374 if ($cfg['PropertiesIconic'] === true || $propicon == 'both') {
375 $edit_link_text = '<img src="' . $pmaThemeImage . 'b_edit.png" width="16" height="16" hspace="2" border="0" title="' . $strEdit . '" alt="' . $strEdit . '" />';
376 $drop_link_text = '<img src="' . $pmaThemeImage . 'b_drop.png" width="16" height="16" hspace="2" border="0" title="' . $strDrop . '" alt="' . $strDrop . '" />';
378 if ($cfg['PropertiesIconic'] === false || $propicon == 'both') {
379 $edit_link_text .= $strEdit;
380 $drop_link_text .= $strDrop;
382 if ($propicon == 'both') {
383 $edit_link_text = '<nobr>' . $edit_link_text . '</nobr>';
384 $drop_link_text = '<nobr>' . $drop_link_text . '</nobr>';
387 if (count($ret_keys) > 0) {
389 <!--table border="<?php echo $cfg['Border']; ?>" cellpadding="2" cellspacing="1"-->
390 <tr>
391 <th><?php echo $strKeyname; ?></th>
392 <th><?php echo $strType; ?></th>
393 <th><?php echo $strCardinality; ?></th>
394 <th colspan="2"><?php echo $strAction; ?></th>
395 <th colspan="2"><?php echo $strField; ?></th>
396 </tr>
397 <?php
398 $idx_collection = PMA_show_indexes($table, $indexes, $indexes_info, $indexes_data, true);
399 echo PMA_check_indexes($idx_collection);
400 } // end display indexes
401 else {
402 // none indexes
403 echo "\n" . ' <tr><td colspan=7" align="center">' . "\n";
404 if ($cfg['ErrorIconic']) {
405 echo '<img src="' . $pmaThemeImage . 's_warn.png" width="16" height="16" border="0" alt="Warning" hspace="2" align="middle" />';
407 echo ' <b>' . $strNoIndex . '</b></td></tr>' . "\n\n";
410 echo '<tr><td colspan="7" class="tblFooters" nowrap="nowrap" align="center"> '
411 . sprintf($strCreateIndex, '<input type="text" size="2" name="idx_num_fields" value="1" style="vertical-align: middle;" />') . "\n";
412 echo ' &nbsp;<input type="submit" name="create_index" value="' . $strGo . '" onclick="return checkFormElementInRange(this.form, \'idx_num_fields\', \'' . str_replace('\'', '\\\'', $GLOBALS['strInvalidColumnCount']) . '\', 1)" style="vertical-align: middle;" />' . "\n";
413 echo '</td></tr> ';
415 </table></form>
416 <?php
417 } // end display indexes
421 * Displays the footer
423 echo "\n";
425 if (!defined('PMA_IDX_INCLUDED')){
426 require_once('./footer.inc.php');