update
[phpmyadmin/crack.git] / tbl_relation.php3
blob32b288bb3d7483ae39b7e2ba4fcb3b112d7a91fa
1 <?php
2 /* $Id$ */
5 /**
6 * Gets some core libraries
8 * I am including part of the tbl_properties stuff which will complain if it is
9 * not being told what part of the tbl_properties is calling it, so i set this
10 * variable empty:
12 $sub_part='';
14 require('./libraries/grab_globals.lib.php3');
15 require('./libraries/common.lib.php3');
16 require('./tbl_properties_common.php3');
17 require('./tbl_properties_table_info.php3');
18 require('./libraries/relation.lib.php3');
21 /**
22 * Gets the relation settings
24 $cfgRelation = PMA_getRelationsParam();
27 /**
28 * Updates
30 if ($cfgRelation['relwork']) {
31 $existrel = PMA_getForeigners($db, $table);
33 if ($cfgRelation['displaywork']) {
34 $disp = PMA_getDisplayField($db, $table);
36 if ($cfgRelation['relwork']
37 && isset($submit_rel) && $submit_rel == 'true') {
39 while (list($key, $value) = each($destination)) {
40 if ($value != 'nix') {
41 $for = explode('.', $value);
42 if (!isset($existrel[$key])) {
43 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['relation'])
44 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
45 . ' values('
46 . '\'' . PMA_sqlAddslashes($db) . '\', '
47 . '\'' . PMA_sqlAddslashes($table) . '\', '
48 . '\'' . PMA_sqlAddslashes($key) . '\', '
49 . '\'' . PMA_sqlAddslashes($for[0]) . '\', '
50 . '\'' . PMA_sqlAddslashes($for[1]) . '\','
51 . '\'' . PMA_sqlAddslashes($for[2]) . '\')';
52 } else if ($existrel[$key] != $value) {
53 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['relation']) . ' SET'
54 . ' foreign_db = \'' . PMA_sqlAddslashes($for[0]) . '\', '
55 . ' foreign_table = \'' . PMA_sqlAddslashes($for[1]) . '\', '
56 . ' foreign_field = \'' . PMA_sqlAddslashes($for[2]) . '\' '
57 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
58 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
59 . ' AND master_field = \'' . PMA_sqlAddslashes($key) . '\'';
60 } // end if... else....
61 } else if (isset($existrel[$key])) {
62 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['relation'])
63 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
64 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
65 . ' AND master_field = \'' . PMA_sqlAddslashes($key) . '\'';
66 } // end if... else....
67 if (isset($upd_query)) {
68 $upd_rs = PMA_query_as_cu($upd_query);
69 unset($upd_query);
71 } // end while
72 } // end if
74 if ($cfgRelation['displaywork']
75 && isset($submit_show) && $submit_show == 'true') {
77 if ($disp) {
78 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['table_info'])
79 . ' SET display_field = \'' . PMA_sqlAddslashes($display_field) . '\''
80 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
81 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
82 } else {
83 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['table_info'])
84 . '(db_name, table_name, display_field) '
85 . ' VALUES('
86 . '\'' . PMA_sqlAddslashes($db) . '\','
87 . '\'' . PMA_sqlAddslashes($table) . '\','
88 . '\'' . PMA_sqlAddslashes($display_field) . '\')';
90 if (isset($upd_query)) {
91 $upd_rs = PMA_query_as_cu($upd_query);
93 } // end if
95 if ($cfgRelation['commwork']
96 && isset($submit_comm) && $submit_comm == 'true') {
97 while (list($key, $value) = each($comment)) {
98 $test_qry = 'SELECT comment FROM ' . PMA_backquote($cfgRelation['column_comments'])
99 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
100 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
101 . ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\'';
102 $test_rs = PMA_query_as_cu($test_qry);
103 if ($test_rs && mysql_num_rows($test_rs) > 0) {
104 if (strlen($value) > 0) {
105 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['column_comments'])
106 . ' SET comment = \'' . PMA_sqlAddslashes($value) . '\''
107 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
108 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
109 . ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\'';
110 } else {
111 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['column_comments'])
112 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
113 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
114 . ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\'';
116 } else if (strlen($value) > 0) {
117 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['column_comments'])
118 . ' (db_name, table_name, column_name, comment) '
119 . ' VALUES('
120 . '\'' . PMA_sqlAddslashes($db) . '\','
121 . '\'' . PMA_sqlAddslashes($table) . '\','
122 . '\'' . PMA_sqlAddslashes($key) . '\','
123 . '\'' . PMA_sqlAddslashes($value) . '\')';
125 if (isset($upd_query)){
126 $upd_rs = PMA_query_as_cu($upd_query);
128 } // end while (transferred data)
129 } // end if (commwork)
131 // Now that we might have changed we have to see again
132 if ($cfgRelation['relwork']) {
133 $existrel = PMA_getForeigners($db, $table);
135 if ($cfgRelation['displaywork']) {
136 $disp = PMA_getDisplayField($db, $table);
138 if ($cfgRelation['commwork']) {
139 $comments = PMA_getComments($db, $table);
144 * Dialog
146 if ($cfgRelation['relwork']) {
148 // To choose relations we first need all tables names in current db
149 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
150 $tab_rs = PMA_mysql_query($tab_query) or PMA_mysqlDie('', $tab_query, '', $err_url_0);
151 $selectboxall['nix'] = '--';
152 while ($curr_table = @PMA_mysql_fetch_array($tab_rs)) {
153 if (($curr_table[0] != $table) && ($curr_table[0] != $cfg['Server']['relation'])) {
154 $fi_query = 'SHOW KEYS FROM ' . PMA_backquote($curr_table[0]);
155 $fi_rs = PMA_mysql_query($fi_query) or PMA_mysqlDie('', $fi_query, '', $err_url_0);
156 if ($fi_rs && mysql_num_rows($fi_rs) > 0) {
157 while ($curr_field = PMA_mysql_fetch_array($fi_rs)) {
158 if (isset($curr_field['Key_name']) && $curr_field['Key_name'] == 'PRIMARY') {
159 $field_full = $db . '.' .$curr_field['Table'] . '.' . $curr_field['Column_name'];
160 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
161 break;
162 } else if (isset($curr_field['non_unique']) && $curr_field['non_unique'] == 0) {
163 // if we can't find a primary key we take any unique one
164 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
165 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
166 } // end if
167 } // end while over keys
168 if (isset($field_full) && isset($field_v)) {
169 $selectboxall[$field_full] = $field_v;
171 } // end if (mysql_num_rows)
172 // Mike Beck - 24.07.02: i've been asked to add all keys of the
173 // current table (see bug report #574851)
175 else if ($curr_table[0] == $table) {
176 $fi_query = 'SHOW KEYS FROM ' . PMA_backquote($curr_table[0]);
177 $fi_rs = PMA_mysql_query($fi_query) or PMA_mysqlDie('', $fi_query, '', $err_url_0);
178 if ($fi_rs && mysql_num_rows($fi_rs) > 0) {
179 while ($curr_field = PMA_mysql_fetch_array($fi_rs)) {
180 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
181 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
182 if (isset($field_full) && isset($field_v)) {
183 $selectboxall[$field_full] = $field_v;
185 } // end while
186 } // end if (mysql_num_rows)
188 } // end while over tables
190 // Create array of relations (Mike Beck)
191 $rel_dest = PMA_getForeigners($db, $table);
192 } // end if
194 // Now find out the columns of our $table
195 $col_query = 'SHOW COLUMNS FROM ' . PMA_backquote($table);
196 $col_rs = PMA_mysql_query($col_query) or PMA_mysqlDie('', $col_query, '', $err_url_0);
198 if ($col_rs && mysql_num_rows($col_rs) > 0) {
199 while ($row = PMA_mysql_fetch_array($col_rs)) {
200 $save_row[] = $row;
202 $saved_row_cnt = count($save_row);
205 <form method="post" action="tbl_relation.php3">
206 <input type="hidden" name="lang" value="<?php echo $lang; ?>" />
207 <input type="hidden" name="server" value="<?php echo $server; ?>" />
208 <input type="hidden" name="db" value="<?php echo $db; ?>" />
209 <input type="hidden" name="table" value="<?php echo $table; ?>" />
210 <input type="hidden" name="submit_rel" value="true" />
212 <table>
213 <tr>
214 <th colspan="2" align="center"><b><?php echo $strLinksTo; ?></b></th>
215 </tr>
216 <?php
217 for ($i = 0; $i < $saved_row_cnt; $i++) {
218 echo "\n";
220 <tr>
221 <th><?php echo $save_row[$i]['Field']; ?></th>
222 <td>
223 <input type="hidden" name="src_field" value="<?php echo $save_row[$i]['Field']; ?>" />
224 <select name="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
225 <?php
226 echo "\n";
227 reset($selectboxall);
228 while (list($key, $value) = each($selectboxall)) {
229 $myfield = $save_row[$i]['Field'];
230 if (isset($existrel[$myfield])) {
231 $test = $existrel[$myfield]['foreign_db'] . '.'
232 . $existrel[$myfield]['foreign_table'] . '.'
233 . $existrel[$myfield]['foreign_field'];
234 } else {
235 $test = FALSE;
237 echo ' '
238 . '<option value="' . htmlspecialchars($key) . '"';
239 if ($test && $key == $test) {
240 echo ' selected="selected"';
242 echo '>' . $value . '</option>'. "\n";
243 } // end while
245 </select>
246 </td>
247 </tr>
248 <?php
249 } // end for
251 echo "\n";
253 <tr>
254 <td colspan="2" align="center">
255 <input type="submit" value="<?php echo $strGo; ?>" />
256 </td>
257 </tr>
258 </table>
259 </form>
261 <?php
262 if ($cfgRelation['displaywork']) {
263 // Get "display_filed" infos
264 $disp = PMA_getDisplayField($db, $table);
266 echo "\n";
268 <form method="post" action="tbl_relation.php3">
269 <input type="hidden" name="lang" value="<?php echo $lang; ?>" />
270 <input type="hidden" name="server" value="<?php echo $server; ?>" />
271 <input type="hidden" name="db" value="<?php echo $db; ?>" />
272 <input type="hidden" name="table" value="<?php echo $table; ?>" />
273 <input type="hidden" name="submit_show" value="true" />
275 <p><?php echo $strChangeDisplay; ?></p>
276 <select name="display_field" onchange="this.form.submit();">
277 <?php
278 echo "\n";
279 mysql_data_seek($col_rs, 0);
280 while ($row = @PMA_mysql_fetch_array($col_rs)) {
281 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
282 if (isset($disp) && $row['Field'] == $disp) {
283 echo ' selected="selected"';
285 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
286 } // end while
288 </select>
289 <script type="text/javascript" language="javascript">
290 <!--
291 // Fake js to allow the use of the <noscript> tag //-->
292 </script>
293 <noscript>
294 <input type="submit" value="<?php echo $strGo; ?>" />
295 </noscript>
296 </form>
297 <?php
298 } // end if (displayworks)
300 if ($cfgRelation['commwork']) {
302 echo "\n";
304 <form method="post" action="tbl_relation.php3">
305 <input type="hidden" name="lang" value="<?php echo $lang; ?>" />
306 <input type="hidden" name="server" value="<?php echo $server; ?>" />
307 <input type="hidden" name="db" value="<?php echo $db; ?>" />
308 <input type="hidden" name="table" value="<?php echo $table; ?>" />
309 <input type="hidden" name="submit_comm" value="true" />
311 <table>
312 <tr>
313 <th colspan="2" align="center"><b><?php echo $strComments; ?></b></th>
314 </tr>
315 <?php
316 for ($i = 0; $i < $saved_row_cnt; $i++) {
317 $field = $save_row[$i]['Field'];
318 echo "\n";
320 <tr>
321 <th><?php echo $field; ?></th>
322 <td>
323 <input type="text" name="comment[<?php echo $field;?>]" value="<?php echo htmlspecialchars($comments[$field]); ?>" />
324 </td>
325 </tr>
326 <?php
327 } // end for
329 echo "\n";
331 <tr>
332 <td colspan="2" align="center">
333 <input type="submit" value="<?php echo $strGo; ?>" />
334 </td>
335 </tr>
336 </table>
337 </form>
338 <?php
339 } // end if (comments work)
340 } // end if (we have columns in this table)
344 * Displays the footer
346 echo "\n";
347 require('./footer.inc.php3');