2.5.3-rc2
[phpmyadmin/crack.git] / tbl_relation.php3
blob585c954d22b8ebafea2af1f40407667fa235e037
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
6 /**
7 * Gets some core libraries
8 */
9 require('./libraries/grab_globals.lib.php3');
10 require('./libraries/common.lib.php3');
11 require('./tbl_properties_common.php3');
12 $url_query .= '&amp;goto=tbl_properties.php3';
13 require('./tbl_properties_table_info.php3');
14 require('./libraries/relation.lib.php3');
16 /**
17 * Gets the relation settings
19 $cfgRelation = PMA_getRelationsParam();
22 /**
23 * Updates
26 if ($cfgRelation['relwork']) {
27 $existrel = PMA_getForeigners($db, $table, '', 'internal');
29 if ($cfgRelation['displaywork']) {
30 $disp = PMA_getDisplayField($db, $table);
32 if ($cfgRelation['relwork']
33 && isset($submit_rel) && $submit_rel == 'true') {
35 while (list($key, $value) = each($destination)) {
36 if ($value != 'nix') {
37 $for = explode('.', $value);
38 if (!isset($existrel[$key])) {
39 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['relation'])
40 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
41 . ' values('
42 . '\'' . PMA_sqlAddslashes($db) . '\', '
43 . '\'' . PMA_sqlAddslashes($table) . '\', '
44 . '\'' . PMA_sqlAddslashes($key) . '\', '
45 . '\'' . PMA_sqlAddslashes($for[0]) . '\', '
46 . '\'' . PMA_sqlAddslashes($for[1]) . '\','
47 . '\'' . PMA_sqlAddslashes($for[2]) . '\')';
48 } else if ($existrel[$key] != $value) {
49 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['relation']) . ' SET'
50 . ' foreign_db = \'' . PMA_sqlAddslashes($for[0]) . '\', '
51 . ' foreign_table = \'' . PMA_sqlAddslashes($for[1]) . '\', '
52 . ' foreign_field = \'' . PMA_sqlAddslashes($for[2]) . '\' '
53 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
54 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
55 . ' AND master_field = \'' . PMA_sqlAddslashes($key) . '\'';
56 } // end if... else....
57 } else if (isset($existrel[$key])) {
58 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['relation'])
59 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
60 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
61 . ' AND master_field = \'' . PMA_sqlAddslashes($key) . '\'';
62 } // end if... else....
63 if (isset($upd_query)) {
64 $upd_rs = PMA_query_as_cu($upd_query);
65 unset($upd_query);
67 } // end while
68 } // end if
70 if ($cfgRelation['displaywork']
71 && isset($submit_show) && $submit_show == 'true') {
73 if ($disp) {
74 if ($display_field != '') {
75 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['table_info'])
76 . ' SET display_field = \'' . PMA_sqlAddslashes($display_field) . '\''
77 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
78 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
79 } else {
80 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['table_info'])
81 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
82 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
84 } elseif ($display_field != '') {
85 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['table_info'])
86 . '(db_name, table_name, display_field) '
87 . ' VALUES('
88 . '\'' . PMA_sqlAddslashes($db) . '\','
89 . '\'' . PMA_sqlAddslashes($table) . '\','
90 . '\'' . PMA_sqlAddslashes($display_field) . '\')';
93 if (isset($upd_query)) {
94 $upd_rs = PMA_query_as_cu($upd_query);
96 } // end if
98 if ($cfgRelation['commwork']
99 && isset($submit_comm) && $submit_comm == 'true') {
100 while (list($key, $value) = each($comment)) {
101 // garvin: I exported the snippet here to a function (relation.lib.php3) , so it can be used multiple times throughout other pages where you can set comments.
102 PMA_setComment($db, $table, $key, $value);
103 } // end while (transferred data)
104 } // end if (commwork)
106 // Now that we might have changed we have to see again
107 if ($cfgRelation['relwork']) {
108 $existrel = PMA_getForeigners($db, $table, '', 'internal');
110 if ($cfgRelation['displaywork']) {
111 $disp = PMA_getDisplayField($db, $table);
113 if ($cfgRelation['commwork']) {
114 $comments = PMA_getComments($db, $table);
119 * Dialog
121 if ($cfgRelation['relwork']) {
123 // To choose relations we first need all tables names in current db
124 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
125 $tab_rs = PMA_mysql_query($tab_query) or PMA_mysqlDie('', $tab_query, '', $err_url_0);
126 $selectboxall['nix'] = '--';
127 while ($curr_table = @PMA_mysql_fetch_array($tab_rs)) {
128 if (($curr_table[0] != $table) && ($curr_table[0] != $cfg['Server']['relation'])) {
129 $fi_query = 'SHOW KEYS FROM ' . PMA_backquote($curr_table[0]);
130 $fi_rs = PMA_mysql_query($fi_query) or PMA_mysqlDie('', $fi_query, '', $err_url_0);
131 if ($fi_rs && mysql_num_rows($fi_rs) > 0) {
132 $seen_a_primary=FALSE;
133 while ($curr_field = PMA_mysql_fetch_array($fi_rs)) {
134 if (isset($curr_field['Key_name']) && $curr_field['Key_name'] == 'PRIMARY') {
135 $seen_a_primary=TRUE;
136 $field_full = $db . '.' .$curr_field['Table'] . '.' . $curr_field['Column_name'];
137 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
138 $selectboxall[$field_full] = $field_v;
139 // there could be more than one segment of the primary
140 // so do not break
142 } else if (isset($curr_field['Non_unique']) && $curr_field['Non_unique'] == 0 && $seen_a_primary==FALSE) {
143 // if we can't find a primary key we take any unique one
144 // (in fact, we show all segments of unique keys
145 // and all unique keys)
146 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
147 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
148 $selectboxall[$field_full] = $field_v;
149 } // end if
150 } // end while over keys
151 } // end if (mysql_num_rows)
153 // Mike Beck - 24.07.02: i've been asked to add all keys of the
154 // current table (see bug report #574851)
156 else if ($curr_table[0] == $table) {
157 $fi_query = 'SHOW KEYS FROM ' . PMA_backquote($curr_table[0]);
158 $fi_rs = PMA_mysql_query($fi_query) or PMA_mysqlDie('', $fi_query, '', $err_url_0);
159 if ($fi_rs && mysql_num_rows($fi_rs) > 0) {
160 while ($curr_field = PMA_mysql_fetch_array($fi_rs)) {
161 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
162 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
163 if (isset($field_full) && isset($field_v)) {
164 $selectboxall[$field_full] = $field_v;
166 } // end while
167 } // end if (mysql_num_rows)
169 } // end while over tables
171 // Create array of relations (Mike Beck)
172 $rel_dest = PMA_getForeigners($db, $table, '', 'internal');
173 } // end if
175 // Now find out the columns of our $table
176 $col_query = 'SHOW COLUMNS FROM ' . PMA_backquote($table);
177 $col_rs = PMA_mysql_query($col_query) or PMA_mysqlDie('', $col_query, '', $err_url_0);
179 if ($col_rs && mysql_num_rows($col_rs) > 0) {
180 while ($row = PMA_mysql_fetch_array($col_rs)) {
181 $save_row[] = $row;
183 $saved_row_cnt = count($save_row);
186 <form method="post" action="tbl_relation.php3">
187 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
188 <input type="hidden" name="submit_rel" value="true" />
190 <table>
191 <tr>
192 <th colspan="2" align="center"><b><?php echo $strLinksTo; ?></b></th>
193 </tr>
194 <?php
195 for ($i = 0; $i < $saved_row_cnt; $i++) {
196 echo "\n";
198 <tr>
199 <th><?php echo $save_row[$i]['Field']; ?></th>
200 <td>
201 <input type="hidden" name="src_field" value="<?php echo $save_row[$i]['Field']; ?>" />
202 <select name="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
203 <?php
204 echo "\n";
205 reset($selectboxall);
206 $myfield = $save_row[$i]['Field'];
207 if (isset($existrel[$myfield])) {
208 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
209 . $existrel[$myfield]['foreign_table'] . '.'
210 . $existrel[$myfield]['foreign_field'];
211 } else {
212 $foreign_field = FALSE;
214 $seen_key = FALSE;
215 while (list($key, $value) = each($selectboxall)) {
216 echo ' '
217 . '<option value="' . htmlspecialchars($key) . '"';
218 if ($foreign_field && $key == $foreign_field) {
219 echo ' selected="selected"';
220 $seen_key = TRUE;
222 echo '>' . $value . '</option>'. "\n";
223 } // end while
225 // if the link defined in relationtable points to a foreign field
226 // that is not a key in the foreign table, we show the link
227 // (will not be shown with an arrow)
228 if ($foreign_field && !$seen_key) {
229 echo ' '
230 . '<option value="' . htmlspecialchars($foreign_field) . '"';
231 echo ' selected="selected"';
232 echo '>' . $foreign_field . '</option>'. "\n";
235 </select>
236 </td>
237 </tr>
238 <?php
239 } // end for
241 echo "\n";
243 <tr>
244 <td colspan="2" align="center">
245 <input type="submit" value="<?php echo $strGo; ?>" />
246 </td>
247 </tr>
248 </table>
249 </form>
251 <?php
252 if ($cfgRelation['displaywork']) {
253 // Get "display_filed" infos
254 $disp = PMA_getDisplayField($db, $table);
256 echo "\n";
258 <form method="post" action="tbl_relation.php3">
259 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
260 <input type="hidden" name="submit_show" value="true" />
262 <p><?php echo $strChangeDisplay; ?></p>
263 <select name="display_field" onchange="this.form.submit();">
264 <option value="">---</option>
265 <?php
266 echo "\n";
267 mysql_data_seek($col_rs, 0);
268 while ($row = @PMA_mysql_fetch_array($col_rs)) {
269 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
270 if (isset($disp) && $row['Field'] == $disp) {
271 echo ' selected="selected"';
273 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
274 } // end while
276 </select>
277 <script type="text/javascript" language="javascript">
278 <!--
279 // Fake js to allow the use of the <noscript> tag
280 //-->
281 </script>
282 <noscript>
283 <input type="submit" value="<?php echo $strGo; ?>" />
284 </noscript>
285 </form>
286 <?php
287 } // end if (displayworks)
289 if ($cfgRelation['commwork']) {
291 echo "\n";
293 <form method="post" action="tbl_relation.php3">
294 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
295 <input type="hidden" name="submit_comm" value="true" />
297 <table>
298 <tr>
299 <th colspan="2" align="center"><b><?php echo $strComments; ?></b></th>
300 </tr>
301 <?php
302 for ($i = 0; $i < $saved_row_cnt; $i++) {
303 $field = $save_row[$i]['Field'];
304 echo "\n";
306 <tr>
307 <th><?php echo $field; ?></th>
308 <td>
309 <input type="text" name="comment[<?php echo $field; ?>]" value="<?php echo (isset($comments[$field]) ? htmlspecialchars($comments[$field]) : ''); ?>" />
310 </td>
311 </tr>
312 <?php
313 } // end for
315 echo "\n";
317 <tr>
318 <td colspan="2" align="center">
319 <input type="submit" value="<?php echo $strGo; ?>" />
320 </td>
321 </tr>
322 </table>
323 </form>
324 <?php
325 } // end if (comments work)
326 } // end if (we have columns in this table)
330 * Displays the footer
332 echo "\n";
333 require('./footer.inc.php3');