Add wrapper to show config errors even with no error reporting (RFE #1447173).
[phpmyadmin/crack.git] / tbl_relation.php
blob47feb2655af9263f097e6048a997413ada3f3f76
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_properties_common.php');
10 $url_query .= '&amp;goto=tbl_properties.php';
13 /**
14 * Gets tables informations
16 require_once('./libraries/tbl_properties_table_info.inc.php');
18 // Note: in libraries/tbl_properties_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_properties_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 if (isset($destination_innodb)) {
127 foreach ($destination_innodb AS $master_field => $foreign_string) {
128 if ($foreign_string != 'nix') {
129 list($foreign_db, $foreign_table, $foreign_field) = explode('.', $foreign_string);
130 if (!isset($existrel_innodb[$master_field])) {
131 // no key defined for this field
133 // The next few lines are repeated below, so they
134 // could be put in an include file
135 // Note: I tried to enclose the db and table name with
136 // backquotes but MySQL 4.0.16 did not like the syntax
137 // (for example: `base2`.`table1` )
139 $upd_query = 'ALTER TABLE ' . PMA_backquote($table)
140 . ' ADD FOREIGN KEY ('
141 . PMA_backquote(PMA_sqlAddslashes($master_field)) . ')'
142 . ' REFERENCES '
143 . PMA_backquote(PMA_sqlAddslashes($foreign_db) . '.'
144 . PMA_sqlAddslashes($foreign_table)) . '('
145 . PMA_backquote(PMA_sqlAddslashes($foreign_field)) . ')';
147 if (${$master_field . '_on_delete'} != 'nix') {
148 $upd_query .= ' ON DELETE ' . $options_array[${$master_field . '_on_delete'}];
150 if (${$master_field . '_on_update'} != 'nix') {
151 $upd_query .= ' ON UPDATE ' . $options_array[${$master_field . '_on_update'}];
154 // end repeated code
156 } elseif (($existrel_innodb[$master_field]['foreign_db'] . '.' .$existrel_innodb[$master_field]['foreign_table'] . '.' . $existrel_innodb[$master_field]['foreign_field'] != $foreign_string)
157 || ( ${$master_field . '_on_delete'} != (!empty($existrel_innodb[$master_field]['on_delete']) ? $existrel_innodb[$master_field]['on_delete'] : ''))
158 || ( ${$master_field . '_on_update'} != (!empty($existrel_innodb[$master_field]['on_update']) ? $existrel_innodb[$master_field]['on_update'] : ''))
160 // another foreign key is already defined for this field
161 // or
162 // an option has been changed for ON DELETE or ON UPDATE
164 // remove existing key
165 if (PMA_MYSQL_INT_VERSION >= 40013) {
166 $upd_query = 'ALTER TABLE ' . PMA_backquote($table)
167 . ' DROP FOREIGN KEY '
168 . PMA_backquote($existrel_innodb[$master_field]['constraint']);
170 // I tried to send both in one query but it failed
171 $upd_rs = PMA_DBI_query($upd_query);
174 // add another
175 $upd_query = 'ALTER TABLE ' . PMA_backquote($table)
176 . ' ADD FOREIGN KEY ('
177 . PMA_backquote(PMA_sqlAddslashes($master_field)) . ')'
178 . ' REFERENCES '
179 . PMA_backquote(PMA_sqlAddslashes($foreign_db) . '.'
180 . PMA_sqlAddslashes($foreign_table)) . '('
181 . PMA_backquote(PMA_sqlAddslashes($foreign_field)) . ')';
183 if (${$master_field . '_on_delete'} != 'nix') {
184 $upd_query .= ' ON DELETE ' . $options_array[${$master_field . '_on_delete'}];
186 if (${$master_field . '_on_update'} != 'nix') {
187 $upd_query .= ' ON UPDATE ' . $options_array[${$master_field . '_on_update'}];
190 } // end if... else....
191 } elseif (isset($existrel_innodb[$master_field])) {
192 if (PMA_MYSQL_INT_VERSION >= 40013) {
193 $upd_query = 'ALTER TABLE ' . PMA_backquote($table)
194 . ' DROP FOREIGN KEY '
195 . PMA_backquote($existrel_innodb[$master_field]['constraint']);
197 } // end if... else....
199 if (isset($upd_query)) {
200 $upd_rs = PMA_DBI_try_query($upd_query);
201 $tmp_error = PMA_DBI_getError();
202 if (substr($tmp_error, 1, 4) == '1216') {
203 PMA_mysqlDie($tmp_error, $upd_query, FALSE, '', FALSE);
204 echo PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
206 if (substr($tmp_error, 1, 4) == '1005') {
207 echo '<p class="warning">' . $strNoIndex . ' (' . $master_field .')</p>' . PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
209 unset($upd_query);
210 unset($tmp_error);
212 } // end while
213 } // end if isset($destination_innodb)
216 // U p d a t e s f o r d i s p l a y f i e l d
218 if ($cfgRelation['displaywork']
219 && isset($display_field)) {
221 if ($disp) {
222 if ($display_field != '') {
223 $upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
224 . ' SET display_field = \'' . PMA_sqlAddslashes($display_field) . '\''
225 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
226 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
227 } else {
228 $upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
229 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
230 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
232 } elseif ($display_field != '') {
233 $upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
234 . '(db_name, table_name, display_field) '
235 . ' VALUES('
236 . '\'' . PMA_sqlAddslashes($db) . '\','
237 . '\'' . PMA_sqlAddslashes($table) . '\','
238 . '\'' . PMA_sqlAddslashes($display_field) . '\')';
241 if (isset($upd_query)) {
242 $upd_rs = PMA_query_as_cu($upd_query);
244 } // end if
246 // If we did an update, refresh our data
247 if (isset($destination) && $cfgRelation['relwork']) {
248 $existrel = PMA_getForeigners($db, $table, '', 'internal');
250 if (isset($destination_innodb) && $tbl_type=='INNODB') {
251 $existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
254 if ($cfgRelation['displaywork']) {
255 $disp = PMA_getDisplayField($db, $table);
260 * Dialog
263 // common form
264 echo '<form method="post" action="tbl_relation.php">' . "\n";
265 echo PMA_generate_common_hidden_inputs($db, $table);
268 // relations
270 if ($cfgRelation['relwork'] || $tbl_type=='INNODB') {
271 // To choose relations we first need all tables names in current db
272 // and if PMA version permits and the main table is innodb,
273 // we use SHOW TABLE STATUS because we need to find other InnoDB tables
275 if ($tbl_type=='INNODB') {
276 $tab_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
277 // [0] of the row is the name
278 // [1] is the type
279 } else {
280 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
282 // [0] of the row is the name
284 $tab_rs = PMA_DBI_query($tab_query, null, PMA_DBI_QUERY_STORE);
285 $selectboxall['nix'] = '--';
286 $selectboxall_innodb['nix'] = '--';
288 while ($curr_table = @PMA_DBI_fetch_row($tab_rs)) {
289 if (($curr_table[0] != $table) && ($curr_table[0] != $cfg['Server']['relation'])) {
290 PMA_DBI_select_db($db);
292 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
293 $fi_rs = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($curr_table[0]) . ';', null, PMA_DBI_QUERY_STORE);
294 if ($fi_rs && PMA_DBI_num_rows($fi_rs) > 0) {
295 $seen_a_primary = FALSE;
296 while ($curr_field = PMA_DBI_fetch_assoc($fi_rs)) {
297 if (isset($curr_field['Key_name']) && $curr_field['Key_name'] == 'PRIMARY') {
298 $seen_a_primary = TRUE;
299 $field_full = $db . '.' .$curr_field['Table'] . '.' . $curr_field['Column_name'];
300 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
301 $selectboxall[$field_full] = $field_v;
302 // there could be more than one segment of the primary
303 // so do not break
305 // Please watch here, tbl_type is INNODB but the
306 // resulting value of SHOW KEYS is InnoDB
308 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
309 $selectboxall_innodb[$field_full] = $field_v;
312 } elseif (isset($curr_field['Non_unique']) && $curr_field['Non_unique'] == 0 && $seen_a_primary==FALSE) {
313 // if we can't find a primary key we take any unique one
314 // (in fact, we show all segments of unique keys
315 // and all unique keys)
316 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
317 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
318 $selectboxall[$field_full] = $field_v;
319 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
320 $selectboxall_innodb[$field_full] = $field_v;
323 // for InnoDB, any index is allowed
324 } elseif ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
325 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
326 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
327 $selectboxall_innodb[$field_full] = $field_v;
329 } // end if
330 } // end while over keys
331 } // end if (PMA_DBI_num_rows)
332 PMA_DBI_free_result($fi_rs);
333 unset($fi_rs);
334 // Mike Beck - 24.07.02: i've been asked to add all keys of the
335 // current table (see bug report #574851)
336 } elseif ($curr_table[0] == $table) {
337 PMA_DBI_select_db($db);
339 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
340 $fi_rs = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($curr_table[0]) . ';', null, PMA_DBI_QUERY_STORE);
341 if ($fi_rs && PMA_DBI_num_rows($fi_rs) > 0) {
342 while ($curr_field = PMA_DBI_fetch_assoc($fi_rs)) {
343 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
344 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
345 $selectboxall[$field_full] = $field_v;
346 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
347 $selectboxall_innodb[$field_full] = $field_v;
349 } // end while
350 } // end if (PMA_DBI_num_rows)
351 PMA_DBI_free_result($fi_rs);
352 unset($fi_rs);
354 } // end while over tables
356 } // end if
359 // Now find out the columns of our $table
360 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
361 $col_rs = PMA_DBI_try_query('SHOW COLUMNS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE);
363 if ($col_rs && PMA_DBI_num_rows($col_rs) > 0) {
364 while ($row = PMA_DBI_fetch_assoc($col_rs)) {
365 $save_row[] = $row;
367 $saved_row_cnt = count($save_row);
369 <fieldset>
370 <legend><?php echo $strLinksTo; ?></legend>
372 <table>
373 <tr><th></th>
374 <?php
375 if ( $cfgRelation['relwork'] ) {
376 echo '<th>' . $strInternalRelations;
377 if ($tbl_type=='INNODB') {
378 echo PMA_showHint( $strInternalNotNecessary );
380 echo '</th>';
382 if ( $tbl_type=='INNODB' ) {
383 echo '<th colspan="2">InnoDB';
384 if (PMA_MYSQL_INT_VERSION < 40013) {
385 echo '(**)';
387 echo '</th>';
390 </tr>
391 <?php
392 $odd_row = true;
393 for ($i = 0; $i < $saved_row_cnt; $i++) {
394 $myfield = $save_row[$i]['Field'];
396 <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
397 <td align="center">
398 <b><?php echo $save_row[$i]['Field']; ?></b></td>
399 <?php
400 if ($cfgRelation['relwork']) {
402 <td><select name="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
403 <?php
404 // PMA internal relations
405 if (isset($existrel[$myfield])) {
406 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
407 . $existrel[$myfield]['foreign_table'] . '.'
408 . $existrel[$myfield]['foreign_field'];
409 } else {
410 $foreign_field = FALSE;
412 $seen_key = FALSE;
413 foreach ($selectboxall AS $key => $value) {
414 echo ' '
415 . '<option value="' . htmlspecialchars($key) . '"';
416 if ($foreign_field && $key == $foreign_field) {
417 echo ' selected="selected"';
418 $seen_key = TRUE;
420 echo '>' . $value . '</option>'. "\n";
421 } // end while
423 // if the link defined in relationtable points to a foreign field
424 // that is not a key in the foreign table, we show the link
425 // (will not be shown with an arrow)
426 if ($foreign_field && !$seen_key) {
427 echo ' '
428 .'<option value="' . htmlspecialchars($foreign_field) . '"'
429 .' selected="selected"'
430 .'>' . $foreign_field . '</option>'. "\n";
433 </select>
434 </td>
435 <?php
436 } // end if (internal relations)
438 if ($tbl_type=='INNODB') {
439 echo '<td>';
440 if (!empty($save_row[$i]['Key'])) {
442 <span class="formelement">
443 <select name="destination_innodb[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
444 <?php
445 if (isset($existrel_innodb[$myfield])) {
446 $foreign_field = $existrel_innodb[$myfield]['foreign_db'] . '.'
447 . $existrel_innodb[$myfield]['foreign_table'] . '.'
448 . $existrel_innodb[$myfield]['foreign_field'];
449 } else {
450 $foreign_field = FALSE;
453 $found_foreign_field = FALSE;
454 foreach ($selectboxall_innodb AS $key => $value) {
455 echo ' '
456 . '<option value="' . htmlspecialchars($key) . '"';
457 if ($foreign_field && $key == $foreign_field) {
458 echo ' selected="selected"';
459 $found_foreign_field = TRUE;
461 echo '>' . $value . '</option>'. "\n";
462 } // end while
464 // we did not find the foreign field in the tables of current db,
465 // must be defined in another db so show it to avoid erasing it
466 if (!$found_foreign_field && $foreign_field) {
467 echo ' '
468 . '<option value="' . htmlspecialchars($foreign_field) . '"';
469 echo ' selected="selected"';
470 echo '>' . $foreign_field . '</option>' . "\n";
474 </select>
475 </span>
476 <span class="formelement">
477 <?php
478 PMA_generate_dropdown('ON DELETE',
479 htmlspecialchars($save_row[$i]['Field']) . '_on_delete',
480 $options_array,
481 isset($existrel_innodb[$myfield]['on_delete']) ? $existrel_innodb[$myfield]['on_delete']: '' );
483 echo '</span>' . "\n"
484 .'<span class="formelement">' . "\n";
486 PMA_generate_dropdown('ON UPDATE',
487 htmlspecialchars($save_row[$i]['Field']) . '_on_update',
488 $options_array,
489 isset($existrel_innodb[$myfield]['on_update']) ? $existrel_innodb[$myfield]['on_update']: '' );
490 echo '</span>' . "\n";
491 } else {
492 echo $strNoIndex;
493 } // end if (a key exists)
494 echo ' </td>';
495 } // end if (InnoDB)
497 </tr>
498 <?php
499 } // end for
501 echo ' </table>' . "\n";
502 echo '</fieldset>' . "\n";
504 if ($cfgRelation['displaywork']) {
505 // Get "display_field" infos
506 $disp = PMA_getDisplayField($db, $table);
508 <fieldset>
509 <label><?php echo $strChangeDisplay . ': '; ?></label>
510 <select name="display_field" style="vertical-align: middle">
511 <option value="">---</option>
512 <?php
513 foreach ($save_row AS $row) {
514 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
515 if (isset($disp) && $row['Field'] == $disp) {
516 echo ' selected="selected"';
518 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
519 } // end while
521 </select>
522 </fieldset>
523 <?php
524 } // end if (displayworks)
526 <fieldset class="tblFooters">
527 <input type="submit" value="<?php echo $strSave; ?>" />
528 </fieldset>
529 </form>
530 <?php
531 } // end if (we have columns in this table)
533 if ( $tbl_type === 'INNODB' && PMA_MYSQL_INT_VERSION < 40013 ) {
534 echo '<div class="warning">'
535 .'** ' . sprintf($strUpgrade, 'MySQL', '4.0.13')
536 .'</div>';
540 * Displays the footer
542 require_once('./libraries/footer.inc.php');