ChangeLog and 3.4.4 XSS fix
[phpmyadmin/crack.git] / tbl_indexes.php
blob54923a0f854bd6c1da7b96a8d81a616a69be9e5a
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Displays index edit/creation form and handles it
6 * @package phpMyAdmin
7 */
9 /**
10 * Gets some core libraries
12 require_once './libraries/common.inc.php';
13 require_once './libraries/Index.class.php';
14 require_once './libraries/tbl_common.php';
16 // Get fields and stores their name/type
17 $fields = array();
18 foreach (PMA_DBI_get_fields($db, $table) as $row) {
19 if (preg_match('@^(set|enum)\((.+)\)$@i', $row['Type'], $tmp)) {
20 $tmp[2] = substr(preg_replace('@([^,])\'\'@', '\\1\\\'',
21 ',' . $tmp[2]), 1);
22 $fields[$row['Field']] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
23 } else {
24 $fields[$row['Field']] = $row['Type'];
26 } // end while
28 // Prepares the form values
29 if (isset($_REQUEST['index'])) {
30 if (is_array($_REQUEST['index'])) {
31 // coming already from form
32 $index = new PMA_Index($_REQUEST['index']);
33 } else {
34 $index = PMA_Index::singleton($db, $table, $_REQUEST['index']);
36 } else {
37 $index = new PMA_Index;
40 /**
41 * Process the data from the edit/create index form,
42 * run the query to build the new index
43 * and moves back to "tbl_sql.php"
45 if (isset($_REQUEST['do_save_data'])) {
46 $error = false;
48 // $sql_query is the one displayed in the query box
49 $sql_query = 'ALTER TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table);
51 // Drops the old index
52 if (! empty($_REQUEST['old_index'])) {
53 if ($_REQUEST['old_index'] == 'PRIMARY') {
54 $sql_query .= ' DROP PRIMARY KEY,';
55 } else {
56 $sql_query .= ' DROP INDEX ' . PMA_backquote($_REQUEST['old_index']) . ',';
58 } // end if
60 // Builds the new one
61 switch ($index->getType()) {
62 case 'PRIMARY':
63 if ($index->getName() == '') {
64 $index->setName('PRIMARY');
65 } elseif ($index->getName() != 'PRIMARY') {
66 $error = PMA_Message::error(__('The name of the primary key must be "PRIMARY"!'));
68 $sql_query .= ' ADD PRIMARY KEY';
69 break;
70 case 'FULLTEXT':
71 case 'UNIQUE':
72 case 'INDEX':
73 if ($index->getName() == 'PRIMARY') {
74 $error = PMA_Message::error(__('Can\'t rename index to PRIMARY!'));
76 $sql_query .= ' ADD ' . $index->getType() . ' '
77 . ($index->getName() ? PMA_backquote($index->getName()) : '');
78 break;
79 } // end switch
81 $index_fields = array();
82 foreach ($index->getColumns() as $key => $column) {
83 $index_fields[$key] = PMA_backquote($column->getName());
84 if ($column->getSubPart()) {
85 $index_fields[$key] .= '(' . $column->getSubPart() . ')';
87 } // end while
89 if (empty($index_fields)){
90 $error = PMA_Message::error(__('No index parts defined!'));
91 } else {
92 $sql_query .= ' (' . implode(', ', $index_fields) . ')';
95 if (! $error) {
96 PMA_DBI_query($sql_query);
97 $message = PMA_Message::success(__('Table %1$s has been altered successfully'));
98 $message->addParam($table);
100 $active_page = 'tbl_structure.php';
101 require './tbl_structure.php';
102 exit;
103 } else {
104 $error->display();
106 } // end builds the new index
110 * Display the form to edit/create an index
113 // Displays headers (if needed)
114 $GLOBALS['js_include'][] = 'indexes.js';
116 require_once './libraries/tbl_info.inc.php';
117 require_once './libraries/tbl_links.inc.php';
119 if (isset($_REQUEST['index']) && is_array($_REQUEST['index'])) {
120 // coming already from form
121 $add_fields =
122 count($_REQUEST['index']['columns']['names']) - $index->getColumnCount();
123 if (isset($_REQUEST['add_fields'])) {
124 $add_fields += $_REQUEST['added_fields'];
126 } elseif (isset($_REQUEST['create_index'])) {
127 $add_fields = $_REQUEST['added_fields'];
128 } else {
129 $add_fields = 1;
132 // end preparing form values
135 <form action="./tbl_indexes.php" method="post" name="index_frm"
136 onsubmit="if (typeof(this.elements['index[Key_name]'].disabled) != 'undefined') {
137 this.elements['index[Key_name]'].disabled = false}">
138 <?php
139 $form_params = array(
140 'db' => $db,
141 'table' => $table,
144 if (isset($_REQUEST['create_index'])) {
145 $form_params['create_index'] = 1;
146 } elseif (isset($_REQUEST['old_index'])) {
147 $form_params['old_index'] = $_REQUEST['old_index'];
148 } elseif (isset($_REQUEST['index'])) {
149 $form_params['old_index'] = $_REQUEST['index'];
152 echo PMA_generate_common_hidden_inputs($form_params);
154 <fieldset>
155 <legend>
156 <?php
157 if (isset($_REQUEST['create_index'])) {
158 echo __('Create a new index');
159 } else {
160 echo __('Modify an index');
163 </legend>
164 <?php
165 PMA_Message::notice(__('("PRIMARY" <b>must</b> be the name of and <b>only of</b> a primary key!)'))->display();
167 <div class="formelement">
168 <label for="input_index_name"><?php echo __('Index name:'); ?></label>
169 <input type="text" name="index[Key_name]" id="input_index_name" size="25"
170 value="<?php echo htmlspecialchars($index->getName()); ?>" onfocus="this.select()" />
171 </div>
173 <div class="formelement">
174 <label for="select_index_type"><?php echo __('Index type:'); ?></label>
175 <select name="index[Index_type]" id="select_index_type" onchange="return checkIndexName()">
176 <?php echo $index->generateIndexSelector(); ?>
177 </select>
178 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'ALTER_TABLE'); ?>
179 </div>
181 <br class="clearfloat" /><br />
183 <table>
184 <thead>
185 <tr><th><?php echo __('Column'); ?></th>
186 <th><?php echo __('Size'); ?></th>
187 </tr>
188 </thead>
189 <tbody>
190 <?php
191 $odd_row = true;
192 foreach ($index->getColumns() as $column) {
194 <tr class="<?php echo $odd_row ? 'odd' : 'even'; ?>">
195 <td><select name="index[columns][names][]">
196 <option value="">-- <?php echo __('Ignore'); ?> --</option>
197 <?php
198 foreach ($fields as $field_name => $field_type) {
199 if ($index->getType() != 'FULLTEXT'
200 || preg_match('/(char|text)/i', $field_type)) {
201 echo '<option value="' . htmlspecialchars($field_name) . '"'
202 . (($field_name == $column->getName()) ? ' selected="selected"' : '') . '>'
203 . htmlspecialchars($field_name) . ' [' . $field_type . ']'
204 . '</option>' . "\n";
206 } // end foreach $fields
208 </select>
209 </td>
210 <td><input type="text" size="5" onfocus="this.select()"
211 name="index[columns][sub_parts][]" value="<?php echo $column->getSubPart(); ?>" />
212 </td>
213 </tr>
214 <?php
215 $odd_row = !$odd_row;
216 } // end foreach $edited_index_info['Sequences']
217 for ($i = 0; $i < $add_fields; $i++) {
219 <tr class="<?php echo $odd_row ? 'odd' : 'even'; ?>">
220 <td><select name="index[columns][names][]">
221 <option value="">-- <?php echo __('Ignore'); ?> --</option>
222 <?php
223 foreach ($fields as $field_name => $field_type) {
224 echo '<option value="' . htmlspecialchars($field_name) . '">'
225 . htmlspecialchars($field_name) . ' [' . $field_type . ']'
226 . '</option>' . "\n";
227 } // end foreach $fields
229 </select>
230 </td>
231 <td><input type="text" size="5" onfocus="this.select()"
232 name="index[columns][sub_parts][]" value="" />
233 </td>
234 </tr>
235 <?php
236 $odd_row = !$odd_row;
237 } // end foreach $edited_index_info['Sequences']
239 </tbody>
240 </table>
241 </fieldset>
243 <fieldset class="tblFooters">
244 <input type="submit" name="do_save_data" value="<?php echo __('Save'); ?>" />
245 <?php
246 echo __('Or') . ' ';
247 echo sprintf(__('Add to index &nbsp;%s&nbsp;column(s)'),
248 '<input type="text" name="added_fields" size="2" value="1"'
249 .' onfocus="this.select()" />') . "\n";
250 echo '<input type="submit" name="add_fields" value="' . __('Go') . '"'
251 .' onclick="return checkFormElementInRange(this.form,'
252 ." 'added_fields', '" . PMA_jsFormat(__('Column count has to be larger than zero.')) . "', 1"
253 .')" />' . "\n";
255 </fieldset>
256 </form>
257 <?php
260 * Displays the footer
262 require './libraries/footer.inc.php';