update
[phpmyadmin/crack.git] / tbl_relation.php3
blob2b776f9675e82f84d53e2b298e29993a3c434713
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');
17 /**
18 * Gets the relation settings
20 $cfgRelation = PMA_getRelationsParam();
23 /**
24 * Adds/removes slashes if required
26 * @param string the string to slash
28 * @return string the slashed string
30 * @access public
32 function PMA_handleSlashes($val) {
33 return (get_magic_quotes_gpc() ? str_replace('\\"', '"', $val) : PMA_sqlAddslashes($val));
34 } // end of the "PMA_handleSlashes()" function
37 /**
38 * Updates
40 if (get_magic_quotes_gpc()) {
41 $db = stripslashes($db);
42 $table = stripslashes($table);
45 if ($cfgRelation['relwork']) {
46 $existrel = PMA_getForeigners($db, $table);
48 if ($cfgRelation['displaywork']) {
49 $disp = PMA_getDisplayField($db, $table);
51 if ($cfgRelation['relwork']
52 && isset($submit_rel) && $submit_rel == 'true') {
54 while (list($key, $value) = each($destination)) {
55 if ($value != 'nix') {
56 $for = explode('.', $value);
57 if (!isset($existrel[$key])) {
58 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['relation'])
59 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
60 . ' values('
61 . '\'' . PMA_sqlAddslashes($db) . '\', '
62 . '\'' . PMA_sqlAddslashes($table) . '\', '
63 . '\'' . PMA_handleSlashes($key) . '\', '
64 . '\'' . PMA_handleSlashes($for[0]) . '\', '
65 . '\'' . PMA_handleSlashes($for[1]) . '\','
66 . '\'' . PMA_handleSlashes($for[2]) . '\')';
67 } else if ($existrel[$key] != $value) {
68 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['relation']) . ' SET'
69 . ' foreign_db = \'' . PMA_handleSlashes($for[0]) . '\', '
70 . ' foreign_table = \'' . PMA_handleSlashes($for[1]) . '\', '
71 . ' foreign_field = \'' . PMA_handleSlashes($for[2]) . '\' '
72 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
73 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
74 . ' AND master_field = \'' . PMA_handleSlashes($key) . '\'';
75 } // end if... else....
76 } else if (isset($existrel[$key])) {
77 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['relation'])
78 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
79 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
80 . ' AND master_field = \'' . PMA_handleSlashes($key) . '\'';
81 } // end if... else....
82 if (isset($upd_query)) {
83 $upd_rs = PMA_query_as_cu($upd_query);
84 unset($upd_query);
86 } // end while
87 } // end if
89 if ($cfgRelation['displaywork']
90 && isset($submit_show) && $submit_show == 'true') {
92 if ($disp) {
93 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['table_info'])
94 . ' SET display_field = \'' . PMA_handleSlashes($display_field) . '\''
95 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
96 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
97 } else {
98 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['table_info'])
99 . '(db_name, table_name, display_field) '
100 . ' VALUES('
101 . '\'' . PMA_sqlAddslashes($db) . '\','
102 . '\'' . PMA_sqlAddslashes($table) . '\','
103 . '\'' . PMA_handleSlashes($display_field) . '\')';
105 if (isset($upd_query)) {
106 $upd_rs = PMA_query_as_cu($upd_query);
108 } // end if
110 if ($cfgRelation['commwork']
111 && isset($submit_comm) && $submit_comm == 'true') {
112 while (list($key, $value) = each($comment)) {
113 $test_qry = 'SELECT comment FROM ' . PMA_backquote($cfgRelation['column_comments'])
114 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
115 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
116 . ' AND column_name = \'' . PMA_handleSlashes($key) . '\'';
117 $test_rs = PMA_query_as_cu($test_qry);
118 if ($test_rs && mysql_num_rows($test_rs) > 0) {
119 if (strlen($value) > 0) {
120 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['column_comments'])
121 . ' SET comment = \'' . PMA_handleSlashes($value) . '\''
122 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
123 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
124 . ' AND column_name = \'' . PMA_handleSlashes($key) . '\'';
125 } else {
126 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['column_comments'])
127 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
128 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
129 . ' AND column_name = \'' . PMA_handleSlashes($key) . '\'';
131 } else if (strlen($value) > 0) {
132 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['column_comments'])
133 . ' (db_name, table_name, column_name, comment) '
134 . ' VALUES('
135 . '\'' . PMA_sqlAddslashes($db) . '\','
136 . '\'' . PMA_sqlAddslashes($table) . '\','
137 . '\'' . PMA_handleSlashes($key) . '\','
138 . '\'' . PMA_handleSlashes($value) . '\')';
140 if (isset($upd_query)){
141 $upd_rs = PMA_query_as_cu($upd_query);
143 } // end while (transferred data)
144 } // end if (commwork)
146 // Now that we might have changed we have to see again
147 if ($cfgRelation['relwork']) {
148 $existrel = PMA_getForeigners($db, $table);
150 if ($cfgRelation['displaywork']) {
151 $disp = PMA_getDisplayField($db, $table);
153 if ($cfgRelation['commwork']) {
154 $comments = PMA_getComments($db, $table);
159 * Dialog
161 if ($cfgRelation['relwork']) {
163 // To choose relations we first need all tables names in current db
164 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
165 $tab_rs = PMA_mysql_query($tab_query) or PMA_mysqlDie('', $tab_query, '', $err_url_0);
166 $selectboxall['nix'] = '--';
167 while ($curr_table = @PMA_mysql_fetch_array($tab_rs)) {
168 if (($curr_table[0] != $table) && ($curr_table[0] != $cfg['Server']['relation'])) {
169 $fi_query = 'SHOW KEYS FROM ' . PMA_backquote($curr_table[0]);
170 $fi_rs = PMA_mysql_query($fi_query) or PMA_mysqlDie('', $fi_query, '', $err_url_0);
171 if ($fi_rs && mysql_num_rows($fi_rs) > 0) {
172 while ($curr_field = PMA_mysql_fetch_array($fi_rs)) {
173 if (isset($curr_field['Key_name']) && $curr_field['Key_name'] == 'PRIMARY') {
174 $field_full = $db . '.' .$curr_field['Table'] . '.' . $curr_field['Column_name'];
175 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
176 break;
177 } else if (isset($curr_field['Non_unique']) && $curr_field['Non_unique'] == 0) {
178 // if we can't find a primary key we take any unique one
179 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
180 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
181 } // end if
182 } // end while over keys
183 if (isset($field_full) && isset($field_v)) {
184 $selectboxall[$field_full] = $field_v;
186 } // end if (mysql_num_rows)
187 // Mike Beck - 24.07.02: i've been asked to add all keys of the
188 // current table (see bug report #574851)
190 else if ($curr_table[0] == $table) {
191 $fi_query = 'SHOW KEYS FROM ' . PMA_backquote($curr_table[0]);
192 $fi_rs = PMA_mysql_query($fi_query) or PMA_mysqlDie('', $fi_query, '', $err_url_0);
193 if ($fi_rs && mysql_num_rows($fi_rs) > 0) {
194 while ($curr_field = PMA_mysql_fetch_array($fi_rs)) {
195 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
196 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
197 if (isset($field_full) && isset($field_v)) {
198 $selectboxall[$field_full] = $field_v;
200 } // end while
201 } // end if (mysql_num_rows)
203 } // end while over tables
205 // Create array of relations (Mike Beck)
206 $rel_dest = PMA_getForeigners($db, $table);
207 } // end if
209 // Now find out the columns of our $table
210 $col_query = 'SHOW COLUMNS FROM ' . PMA_backquote($table);
211 $col_rs = PMA_mysql_query($col_query) or PMA_mysqlDie('', $col_query, '', $err_url_0);
213 if ($col_rs && mysql_num_rows($col_rs) > 0) {
214 while ($row = PMA_mysql_fetch_array($col_rs)) {
215 $save_row[] = $row;
217 $saved_row_cnt = count($save_row);
220 <form method="post" action="tbl_relation.php3">
221 <input type="hidden" name="lang" value="<?php echo $lang; ?>" />
222 <input type="hidden" name="server" value="<?php echo $server; ?>" />
223 <input type="hidden" name="db" value="<?php echo $db; ?>" />
224 <input type="hidden" name="table" value="<?php echo $table; ?>" />
225 <input type="hidden" name="submit_rel" value="true" />
227 <table>
228 <tr>
229 <th colspan="2" align="center"><b><?php echo $strLinksTo; ?></b></th>
230 </tr>
231 <?php
232 for ($i = 0; $i < $saved_row_cnt; $i++) {
233 echo "\n";
235 <tr>
236 <th><?php echo $save_row[$i]['Field']; ?></th>
237 <td>
238 <input type="hidden" name="src_field" value="<?php echo $save_row[$i]['Field']; ?>" />
239 <select name="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
240 <?php
241 echo "\n";
242 reset($selectboxall);
243 while (list($key, $value) = each($selectboxall)) {
244 $myfield = $save_row[$i]['Field'];
245 if (isset($existrel[$myfield])) {
246 $test = $existrel[$myfield]['foreign_db'] . '.'
247 . $existrel[$myfield]['foreign_table'] . '.'
248 . $existrel[$myfield]['foreign_field'];
249 } else {
250 $test = FALSE;
252 echo ' '
253 . '<option value="' . htmlspecialchars($key) . '"';
254 if ($test && $key == $test) {
255 echo ' selected="selected"';
257 echo '>' . $value . '</option>'. "\n";
258 } // end while
260 </select>
261 </td>
262 </tr>
263 <?php
264 } // end for
266 echo "\n";
268 <tr>
269 <td colspan="2" align="center">
270 <input type="submit" value="<?php echo $strGo; ?>" />
271 </td>
272 </tr>
273 </table>
274 </form>
276 <?php
277 if ($cfgRelation['displaywork']) {
278 // Get "display_filed" infos
279 $disp = PMA_getDisplayField($db, $table);
281 echo "\n";
283 <form method="post" action="tbl_relation.php3">
284 <input type="hidden" name="lang" value="<?php echo $lang; ?>" />
285 <input type="hidden" name="server" value="<?php echo $server; ?>" />
286 <input type="hidden" name="db" value="<?php echo $db; ?>" />
287 <input type="hidden" name="table" value="<?php echo $table; ?>" />
288 <input type="hidden" name="submit_show" value="true" />
290 <p><?php echo $strChangeDisplay; ?></p>
291 <select name="display_field" onchange="this.form.submit();">
292 <?php
293 echo "\n";
294 mysql_data_seek($col_rs, 0);
295 while ($row = @PMA_mysql_fetch_array($col_rs)) {
296 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
297 if (isset($disp) && $row['Field'] == $disp) {
298 echo ' selected="selected"';
300 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
301 } // end while
303 </select>
304 <script type="text/javascript" language="javascript">
305 <!--
306 // Fake js to allow the use of the <noscript> tag
307 //-->
308 </script>
309 <noscript>
310 <input type="submit" value="<?php echo $strGo; ?>" />
311 </noscript>
312 </form>
313 <?php
314 } // end if (displayworks)
316 if ($cfgRelation['commwork']) {
318 echo "\n";
320 <form method="post" action="tbl_relation.php3">
321 <input type="hidden" name="lang" value="<?php echo $lang; ?>" />
322 <input type="hidden" name="server" value="<?php echo $server; ?>" />
323 <input type="hidden" name="db" value="<?php echo $db; ?>" />
324 <input type="hidden" name="table" value="<?php echo $table; ?>" />
325 <input type="hidden" name="submit_comm" value="true" />
327 <table>
328 <tr>
329 <th colspan="2" align="center"><b><?php echo $strComments; ?></b></th>
330 </tr>
331 <?php
332 for ($i = 0; $i < $saved_row_cnt; $i++) {
333 $field = $save_row[$i]['Field'];
334 echo "\n";
336 <tr>
337 <th><?php echo $field; ?></th>
338 <td>
339 <input type="text" name="comment[<?php echo $field; ?>]" value="<?php echo (isset($comments[$field]) ? htmlspecialchars($comments[$field]) : ''); ?>" />
340 </td>
341 </tr>
342 <?php
343 } // end for
345 echo "\n";
347 <tr>
348 <td colspan="2" align="center">
349 <input type="submit" value="<?php echo $strGo; ?>" />
350 </td>
351 </tr>
352 </table>
353 </form>
354 <?php
355 } // end if (comments work)
356 } // end if (we have columns in this table)
360 * Displays the footer
362 echo "\n";
363 require('./footer.inc.php3');