Add <div> to allow selecting whole SQL by tripple click (patch #1611591).
[phpmyadmin/crack.git] / tbl_relation.php
blobf2a018822fbe0db59f2a4c1820cb3b3fab92ab52
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Gets some core libraries
7 */
8 require_once('./libraries/common.lib.php');
9 require_once('./libraries/tbl_common.php');
10 $url_query .= '&amp;goto=tbl_sql.php';
13 /**
14 * Gets tables informations
16 require_once('./libraries/tbl_info.inc.php');
18 // Note: in libraries/tbl_links.inc.php we get and display the table comment.
19 // For InnoDB, this comment contains the REFER information but any update
20 // has not been done yet (will be done in tbl_relation.php later).
21 $avoid_show_comment = TRUE;
23 /**
24 * Displays top menu links
26 require_once('./libraries/tbl_links.inc.php');
28 require_once('./libraries/relation.lib.php');
30 $options_array = array('CASCADE' => 'CASCADE', 'SET_NULL' => 'SET NULL', 'NO_ACTION' => 'NO ACTION', 'RESTRICT' => 'RESTRICT');
32 /**
33 * Generate dropdown choices
35 * @param string Message to display
36 * @param string Name of the <select> field
37 * @param array Choices for dropdown
38 * @return string The existing value (for selected)
40 * @access public
42 function PMA_generate_dropdown($dropdown_question, $radio_name, $choices, $selected_value)
44 echo $dropdown_question . '&nbsp;&nbsp;';
46 echo '<select name="' . $radio_name . '">' . "\n";
47 echo '<option value="nix">--</option>' . "\n";
49 foreach ($choices AS $one_value => $one_label) {
50 echo '<option value="' . $one_value . '"';
51 if ($selected_value == $one_value) {
52 echo ' selected="selected" ';
54 echo '>' . $one_label . '</option>' . "\n";
56 echo '</select>' . "\n";
57 echo "\n";
61 /**
62 * Gets the relation settings
64 $cfgRelation = PMA_getRelationsParam();
67 /**
68 * Updates
71 // ensure we are positionned to our current db (since the previous reading
72 // of relations makes pmadb the current one, maybe depending on the MySQL version)
73 PMA_DBI_select_db($db);
75 if ($cfgRelation['relwork']) {
76 $existrel = PMA_getForeigners($db, $table, '', 'internal');
78 if ($tbl_type=='INNODB') {
79 $existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
81 if ($cfgRelation['displaywork']) {
82 $disp = PMA_getDisplayField($db, $table);
85 // u p d a t e s f o r I n t e r n a l r e l a t i o n s
86 if (isset($destination) && $cfgRelation['relwork']) {
88 foreach ($destination AS $master_field => $foreign_string) {
89 if ($foreign_string != 'nix') {
90 list($foreign_db, $foreign_table, $foreign_field) = explode('.', $foreign_string);
91 if (!isset($existrel[$master_field])) {
92 $upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
93 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
94 . ' values('
95 . '\'' . PMA_sqlAddslashes($db) . '\', '
96 . '\'' . PMA_sqlAddslashes($table) . '\', '
97 . '\'' . PMA_sqlAddslashes($master_field) . '\', '
98 . '\'' . PMA_sqlAddslashes($foreign_db) . '\', '
99 . '\'' . PMA_sqlAddslashes($foreign_table) . '\','
100 . '\'' . PMA_sqlAddslashes($foreign_field) . '\')';
101 } elseif ($existrel[$master_field]['foreign_db'] . '.' .$existrel[$master_field]['foreign_table'] . '.' . $existrel[$master_field]['foreign_field'] != $foreign_string) {
102 $upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation']) . ' SET'
103 . ' foreign_db = \'' . PMA_sqlAddslashes($foreign_db) . '\', '
104 . ' foreign_table = \'' . PMA_sqlAddslashes($foreign_table) . '\', '
105 . ' foreign_field = \'' . PMA_sqlAddslashes($foreign_field) . '\' '
106 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
107 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
108 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
109 } // end if... else....
110 } elseif (isset($existrel[$master_field])) {
111 $upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
112 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
113 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
114 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
115 } // end if... else....
116 if (isset($upd_query)) {
117 $upd_rs = PMA_query_as_cu($upd_query);
118 unset($upd_query);
120 } // end while
121 } // end if (updates for internal relations)
123 // u p d a t e s f o r I n n o D B
124 // ( for now, one index name only; we keep the definitions if the
125 // foreign db is not the same)
126 // I use $sql_query to be able to display directly the query via
127 // PMA_showMessage()
129 if (isset($_REQUEST['destination_innodb'])) {
130 $display_query = '';
131 $seen_error = false;
132 foreach ($_REQUEST['destination_innodb'] as $master_field => $foreign_string) {
133 if ($foreign_string != 'nix') {
134 list($foreign_db, $foreign_table, $foreign_field) = explode('.', $foreign_string);
135 if (!isset($existrel_innodb[$master_field])) {
136 // no key defined for this field
138 // The next few lines are repeated below, so they
139 // could be put in an include file
140 // Note: I tried to enclose the db and table name with
141 // backquotes but MySQL 4.0.16 did not like the syntax
142 // (for example: `base2`.`table1` )
144 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
145 . ' ADD FOREIGN KEY ('
146 . PMA_backquote($master_field) . ')'
147 . ' REFERENCES '
148 . PMA_backquote($foreign_db) . '.'
149 . PMA_backquote($foreign_table) . '('
150 . PMA_backquote($foreign_field) . ')';
152 if ($_REQUEST['on_delete'][$master_field] != 'nix') {
153 $sql_query .= ' ON DELETE ' . $options_array[$_REQUEST['on_delete'][$master_field]];
155 if ($_REQUEST['on_update'][$master_field] != 'nix') {
156 $sql_query .= ' ON UPDATE ' . $options_array[$_REQUEST['on_update'][$master_field]];
158 $sql_query .= ';';
159 $display_query .= $sql_query . "\n";
160 // end repeated code
162 } elseif (($existrel_innodb[$master_field]['foreign_db'] . '.' .$existrel_innodb[$master_field]['foreign_table'] . '.' . $existrel_innodb[$master_field]['foreign_field'] != $foreign_string)
163 || ( $_REQUEST['on_delete'][$master_field] != (!empty($existrel_innodb[$master_field]['on_delete']) ? $existrel_innodb[$master_field]['on_delete'] : ''))
164 || ( $_REQUEST['on_update'][$master_field] != (!empty($existrel_innodb[$master_field]['on_update']) ? $existrel_innodb[$master_field]['on_update'] : ''))
166 // another foreign key is already defined for this field
167 // or
168 // an option has been changed for ON DELETE or ON UPDATE
170 // remove existing key
171 if (PMA_MYSQL_INT_VERSION >= 40013) {
172 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
173 . ' DROP FOREIGN KEY '
174 . PMA_backquote($existrel_innodb[$master_field]['constraint']) . ';';
176 // I tried to send both in one query but it failed
177 $upd_rs = PMA_DBI_query($sql_query);
178 $display_query .= $sql_query . "\n";
181 // add another
182 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
183 . ' ADD FOREIGN KEY ('
184 . PMA_backquote($master_field) . ')'
185 . ' REFERENCES '
186 . PMA_backquote($foreign_db) . '.'
187 . PMA_backquote($foreign_table) . '('
188 . PMA_backquote($foreign_field) . ')';
190 if ($_REQUEST['on_delete'][$master_field] != 'nix') {
191 $sql_query .= ' ON DELETE '
192 . $options_array[$_REQUEST['on_delete'][$master_field]];
194 if ($_REQUEST['on_update'][$master_field] != 'nix') {
195 $sql_query .= ' ON UPDATE '
196 . $options_array[$_REQUEST['on_update'][$master_field]];
198 $sql_query .= ';';
199 $display_query .= $sql_query . "\n";
201 } // end if... else....
202 } elseif (isset($existrel_innodb[$master_field])) {
203 if (PMA_MYSQL_INT_VERSION >= 40013) {
204 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
205 . ' DROP FOREIGN KEY '
206 . PMA_backquote($existrel_innodb[$master_field]['constraint']);
207 $sql_query .= ';';
208 $display_query .= $sql_query . "\n";
210 } // end if... else....
212 if (isset($sql_query)) {
213 $upd_rs = PMA_DBI_try_query($sql_query);
214 $tmp_error = PMA_DBI_getError();
215 if (! empty($tmp_error)) {
216 $seen_error = true;
218 if (substr($tmp_error, 1, 4) == '1216'
219 || substr($tmp_error, 1, 4) == '1452') {
220 PMA_mysqlDie($tmp_error, $sql_query, FALSE, '', FALSE);
221 echo PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
223 if (substr($tmp_error, 1, 4) == '1005') {
224 echo '<p class="warning">' . $strForeignKeyError . ' : ' . $master_field
225 .'</p>' . PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
227 unset($sql_query, $tmp_error);
229 } // end foreach
230 if (!empty($display_query)) {
231 if ($seen_error) {
232 PMA_showMessage($strError);
233 } else {
234 PMA_showMessage($strSuccess);
237 } // end if isset($destination_innodb)
240 // U p d a t e s f o r d i s p l a y f i e l d
242 if ($cfgRelation['displaywork']
243 && isset($display_field)) {
245 if ($disp) {
246 if ($display_field != '') {
247 $upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
248 . ' SET display_field = \'' . PMA_sqlAddslashes($display_field) . '\''
249 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
250 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
251 } else {
252 $upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
253 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
254 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
256 } elseif ($display_field != '') {
257 $upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
258 . '(db_name, table_name, display_field) '
259 . ' VALUES('
260 . '\'' . PMA_sqlAddslashes($db) . '\','
261 . '\'' . PMA_sqlAddslashes($table) . '\','
262 . '\'' . PMA_sqlAddslashes($display_field) . '\')';
265 if (isset($upd_query)) {
266 $upd_rs = PMA_query_as_cu($upd_query);
268 } // end if
270 // If we did an update, refresh our data
271 if (isset($destination) && $cfgRelation['relwork']) {
272 $existrel = PMA_getForeigners($db, $table, '', 'internal');
274 if (isset($destination_innodb) && $tbl_type=='INNODB') {
275 $existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
278 if ($cfgRelation['displaywork']) {
279 $disp = PMA_getDisplayField($db, $table);
284 * Dialog
287 // common form
288 echo '<form method="post" action="tbl_relation.php">' . "\n";
289 echo PMA_generate_common_hidden_inputs($db, $table);
292 // relations
294 if ($cfgRelation['relwork'] || $tbl_type=='INNODB') {
295 // To choose relations we first need all tables names in current db
296 // and if PMA version permits and the main table is innodb,
297 // we use SHOW TABLE STATUS because we need to find other InnoDB tables
299 if ($tbl_type=='INNODB') {
300 $tab_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
301 // [0] of the row is the name
302 // [1] is the type
303 } else {
304 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
306 // [0] of the row is the name
308 $tab_rs = PMA_DBI_query($tab_query, null, PMA_DBI_QUERY_STORE);
309 $selectboxall['nix'] = '--';
310 $selectboxall_innodb['nix'] = '--';
312 while ($curr_table = @PMA_DBI_fetch_row($tab_rs)) {
313 if (($curr_table[0] != $table) && ($curr_table[0] != $cfg['Server']['relation'])) {
314 PMA_DBI_select_db($db);
316 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
317 $fi_rs = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($curr_table[0]) . ';', null, PMA_DBI_QUERY_STORE);
318 if ($fi_rs && PMA_DBI_num_rows($fi_rs) > 0) {
319 $seen_a_primary = FALSE;
320 while ($curr_field = PMA_DBI_fetch_assoc($fi_rs)) {
321 if (isset($curr_field['Key_name']) && $curr_field['Key_name'] == 'PRIMARY') {
322 $seen_a_primary = TRUE;
323 $field_full = $db . '.' .$curr_field['Table'] . '.' . $curr_field['Column_name'];
324 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
325 $selectboxall[$field_full] = $field_v;
326 // there could be more than one segment of the primary
327 // so do not break
329 // Please watch here, tbl_type is INNODB but the
330 // resulting value of SHOW KEYS is InnoDB
332 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
333 $selectboxall_innodb[$field_full] = $field_v;
336 } elseif (isset($curr_field['Non_unique']) && $curr_field['Non_unique'] == 0 && $seen_a_primary==FALSE) {
337 // if we can't find a primary key we take any unique one
338 // (in fact, we show all segments of unique keys
339 // and all unique keys)
340 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
341 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
342 $selectboxall[$field_full] = $field_v;
343 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
344 $selectboxall_innodb[$field_full] = $field_v;
347 // for InnoDB, any index is allowed
348 } elseif ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
349 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
350 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
351 $selectboxall_innodb[$field_full] = $field_v;
353 } // end if
354 } // end while over keys
355 } // end if (PMA_DBI_num_rows)
356 PMA_DBI_free_result($fi_rs);
357 unset($fi_rs);
358 // Mike Beck - 24.07.02: i've been asked to add all keys of the
359 // current table (see bug report #574851)
360 } elseif ($curr_table[0] == $table) {
361 PMA_DBI_select_db($db);
363 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
364 $fi_rs = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($curr_table[0]) . ';', null, PMA_DBI_QUERY_STORE);
365 if ($fi_rs && PMA_DBI_num_rows($fi_rs) > 0) {
366 while ($curr_field = PMA_DBI_fetch_assoc($fi_rs)) {
367 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
368 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
369 $selectboxall[$field_full] = $field_v;
370 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
371 $selectboxall_innodb[$field_full] = $field_v;
373 } // end while
374 } // end if (PMA_DBI_num_rows)
375 PMA_DBI_free_result($fi_rs);
376 unset($fi_rs);
378 } // end while over tables
380 } // end if
383 // Now find out the columns of our $table
384 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
385 $col_rs = PMA_DBI_try_query('SHOW COLUMNS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE);
387 if ($col_rs && PMA_DBI_num_rows($col_rs) > 0) {
388 while ($row = PMA_DBI_fetch_assoc($col_rs)) {
389 $save_row[] = $row;
391 $saved_row_cnt = count($save_row);
393 <fieldset>
394 <legend><?php echo $strLinksTo; ?></legend>
396 <table>
397 <tr><th></th>
398 <?php
399 if ( $cfgRelation['relwork'] ) {
400 echo '<th>' . $strInternalRelations;
401 if ($tbl_type=='INNODB') {
402 echo PMA_showHint( $strInternalNotNecessary );
404 echo '</th>';
406 if ( $tbl_type=='INNODB' ) {
407 echo '<th colspan="2">InnoDB';
408 if (PMA_MYSQL_INT_VERSION < 40013) {
409 echo '(**)';
411 echo '</th>';
414 </tr>
415 <?php
416 $odd_row = true;
417 for ($i = 0; $i < $saved_row_cnt; $i++) {
418 $myfield = $save_row[$i]['Field'];
420 <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
421 <td align="center">
422 <b><?php echo $save_row[$i]['Field']; ?></b></td>
423 <?php
424 if ($cfgRelation['relwork']) {
426 <td><select name="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
427 <?php
428 // PMA internal relations
429 if (isset($existrel[$myfield])) {
430 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
431 . $existrel[$myfield]['foreign_table'] . '.'
432 . $existrel[$myfield]['foreign_field'];
433 } else {
434 $foreign_field = FALSE;
436 $seen_key = FALSE;
437 foreach ($selectboxall AS $key => $value) {
438 echo ' '
439 . '<option value="' . htmlspecialchars($key) . '"';
440 if ($foreign_field && $key == $foreign_field) {
441 echo ' selected="selected"';
442 $seen_key = TRUE;
444 echo '>' . $value . '</option>'. "\n";
445 } // end while
447 // if the link defined in relationtable points to a foreign field
448 // that is not a key in the foreign table, we show the link
449 // (will not be shown with an arrow)
450 if ($foreign_field && !$seen_key) {
451 echo ' '
452 .'<option value="' . htmlspecialchars($foreign_field) . '"'
453 .' selected="selected"'
454 .'>' . $foreign_field . '</option>'. "\n";
457 </select>
458 </td>
459 <?php
460 } // end if (internal relations)
462 if ($tbl_type=='INNODB') {
463 echo '<td>';
464 if (!empty($save_row[$i]['Key'])) {
466 <span class="formelement">
467 <select name="destination_innodb[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
468 <?php
469 if (isset($existrel_innodb[$myfield])) {
470 $foreign_field = $existrel_innodb[$myfield]['foreign_db'] . '.'
471 . $existrel_innodb[$myfield]['foreign_table'] . '.'
472 . $existrel_innodb[$myfield]['foreign_field'];
473 } else {
474 $foreign_field = FALSE;
477 $found_foreign_field = FALSE;
478 foreach ($selectboxall_innodb AS $key => $value) {
479 echo ' '
480 . '<option value="' . htmlspecialchars($key) . '"';
481 if ($foreign_field && $key == $foreign_field) {
482 echo ' selected="selected"';
483 $found_foreign_field = TRUE;
485 echo '>' . $value . '</option>'. "\n";
486 } // end while
488 // we did not find the foreign field in the tables of current db,
489 // must be defined in another db so show it to avoid erasing it
490 if (!$found_foreign_field && $foreign_field) {
491 echo ' '
492 . '<option value="' . htmlspecialchars($foreign_field) . '"';
493 echo ' selected="selected"';
494 echo '>' . $foreign_field . '</option>' . "\n";
498 </select>
499 </span>
500 <span class="formelement">
501 <?php
502 PMA_generate_dropdown('ON DELETE',
503 'on_delete[' . htmlspecialchars($save_row[$i]['Field']) . ']',
504 $options_array,
505 isset($existrel_innodb[$myfield]['on_delete']) ? $existrel_innodb[$myfield]['on_delete']: '' );
507 echo '</span>' . "\n"
508 .'<span class="formelement">' . "\n";
510 PMA_generate_dropdown('ON UPDATE',
511 'on_update[' . htmlspecialchars($save_row[$i]['Field']) . ']',
512 $options_array,
513 isset($existrel_innodb[$myfield]['on_update']) ? $existrel_innodb[$myfield]['on_update']: '' );
514 echo '</span>' . "\n";
515 } else {
516 echo $strNoIndex;
517 } // end if (a key exists)
518 echo ' </td>';
519 } // end if (InnoDB)
521 </tr>
522 <?php
523 } // end for
525 echo ' </table>' . "\n";
526 echo '</fieldset>' . "\n";
528 if ($cfgRelation['displaywork']) {
529 // Get "display_field" infos
530 $disp = PMA_getDisplayField($db, $table);
532 <fieldset>
533 <label><?php echo $strChangeDisplay . ': '; ?></label>
534 <select name="display_field" style="vertical-align: middle">
535 <option value="">---</option>
536 <?php
537 foreach ($save_row AS $row) {
538 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
539 if (isset($disp) && $row['Field'] == $disp) {
540 echo ' selected="selected"';
542 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
543 } // end while
545 </select>
546 </fieldset>
547 <?php
548 } // end if (displayworks)
550 <fieldset class="tblFooters">
551 <input type="submit" value="<?php echo $strSave; ?>" />
552 </fieldset>
553 </form>
554 <?php
555 } // end if (we have columns in this table)
557 if ( $tbl_type === 'INNODB' && PMA_MYSQL_INT_VERSION < 40013 ) {
558 echo '<div class="warning">'
559 .'** ' . sprintf($strUpgrade, 'MySQL', '4.0.13')
560 .'</div>';
564 * Displays the footer
566 require_once('./libraries/footer.inc.php');