2.3.3
[phpmyadmin/crack.git] / db_details_qbe.php3
blob16f7b7cb054717047e51bf3744d0a39d27d1770e
1 <?php
2 /* $Id$ */
5 /**
6 * Get the values of the variables posted or sent to this script and display
7 * the headers
8 */
9 require('./libraries/grab_globals.lib.php3');
10 require('./libraries/common.lib.php3');
11 require('./libraries/relation.lib.php3');
14 /**
15 * Gets the relation settings
17 $cfgRelation = PMA_getRelationsParam();
20 /**
21 * A query has been submitted -> execute it, else display the headers
23 if (isset($submit_sql) && eregi('^SELECT', $encoded_sql_query)) {
24 $goto = 'db_details.php3';
25 $zero_rows = htmlspecialchars($strSuccess);
26 $sql_query = urldecode($encoded_sql_query);
27 if (get_magic_quotes_gpc()) {
28 $sql_query = addslashes($sql_query);
30 include('./sql.php3');
31 exit();
32 } else {
33 $sub_part = '_qbe';
34 include('./db_details_common.php3');
35 $url_query .= '&amp;goto=db_details_qbe.php3';
36 include('./db_details_db_info.php3');
39 if (isset($submit_sql) && !eregi('^SELECT', $encoded_sql_query)) {
40 echo '<p class="warning">' . $strHaveToShow . '</p>';
44 /**
45 * Initialize some variables
47 if (empty($Columns)) {
48 $Columns = 3; // Initial number of columns
50 if (!isset($Add_Col)) {
51 $Add_Col = '';
53 if (!isset($Add_Row)) {
54 $Add_Row = '';
56 if (!isset($Rows)) {
57 $Rows = '';
59 if (!isset($InsCol)) {
60 $InsCol = '';
62 if (!isset($DelCol)) {
63 $DelCol = '';
65 if (!isset($prev_Criteria)) {
66 $prev_Criteria = '';
68 // workaround for a PHP3 problem
69 if (!isset($Criteria)) {
70 //$Criteria = '';
71 $Criteria = array();
72 for ($i = 0; $i < $Columns; $i++) {
73 $Criteria[$i] = '';
76 if (!isset($InsRow)) {
77 // $InsRow = '';
78 $InsRow = array();
79 for ($i = 0; $i < $Columns; $i++) {
80 $InsRow[$i] = '';
83 if (!isset($DelRow)) {
84 // $DelRow = '';
85 $DelRow = array();
86 for ($i = 0; $i < $Columns; $i++) {
87 $DelRow[$i] = '';
90 if (!isset($AndOrRow)) {
91 // $AndOrRow = '';
92 $AndOrRow = array();
93 for ($i = 0; $i < $Columns; $i++) {
94 $AndOrRow[$i] = '';
97 if (!isset($AndOrCol)) {
98 // $AndOrCol = '';
99 $AndOrCol = array();
100 for ($i = 0; $i < $Columns; $i++) {
101 $AndOrCol[$i] = '';
104 // minimum width
105 $wid = 12;
106 $col = $Columns + $Add_Col;
107 if ($col < 0) {
108 $col = 0;
110 $row = $Rows + $Add_Row;
111 if ($row < 0) {
112 $row = 0;
117 * Prepares the form
119 $tbl_result = PMA_mysql_list_tables($db);
120 $tbl_result_cnt = mysql_num_rows($tbl_result);
121 $i = 0;
122 $k = 0;
124 // The tables list sent by a previously submitted form
125 if (!empty($TableList)) {
126 for ($x = 0; $x < count($TableList); $x++) {
127 $tbl_names[urldecode($TableList[$x])] = ' selected="selected"';
129 } // end if
131 // The tables list gets from MySQL
132 while ($i < $tbl_result_cnt) {
133 $tbl = PMA_mysql_tablename($tbl_result, $i);
134 $fld_results = @PMA_mysql_list_fields($db, $tbl) or PMA_mysqlDie(PMA_mysql_error(), 'PMA_mysql_list_fields(' . $db . ', ' . $tbl . ')', FALSE, $err_url);
135 $fld_results_cnt = ($fld_results) ? mysql_num_fields($fld_results) : 0;
136 $j = 0;
138 if (empty($tbl_names[$tbl]) && !empty($TableList)) {
139 $tbl_names[$tbl] = '';
140 } else {
141 $tbl_names[$tbl] = ' selected="selected"';
142 } // end if
144 // The fields list per selected tables
145 if ($tbl_names[$tbl] == ' selected="selected"') {
146 $fld[$k++] = PMA_backquote($tbl) . '.*';
147 while ($j < $fld_results_cnt) {
148 $fld[$k] = PMA_mysql_field_name($fld_results, $j);
149 $fld[$k] = PMA_backquote($tbl) . '.' . PMA_backquote($fld[$k]);
151 // increase the width if necessary
152 if (strlen($fld[$k]) > $wid) {
153 $wid = strlen($fld[$k]);
154 } //end if
156 $k++;
157 $j++;
158 } // end while
159 } // end if
160 if ($fld_results) {
161 mysql_free_result($fld_results);
164 $i++;
165 } // end if
166 mysql_free_result($tbl_result);
168 // largest width found
169 $realwidth = $wid . 'ex';
173 * Displays the form
177 <!-- Query by example form -->
178 <form action="db_details_qbe.php3" method="post">
179 <table border="<?php echo $cfg['Border']; ?>">
181 <!-- Fields row -->
182 <tr>
183 <td align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $cfg['ThBgcolor']; ?>">
184 <b><?php echo $strField; ?>&nbsp;:&nbsp;</b>
185 </td>
186 <?php
187 $z = 0;
188 for ($x = 0; $x < $col; $x++) {
189 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
191 <td align="center" bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
192 <select style="width: <?php echo $realwidth; ?>" name="Field[<?php echo $z; ?>]" size="1">
193 <option value=""></option>
194 <?php
195 echo "\n";
196 for ($y = 0; $y < sizeof($fld); $y++) {
197 if ($fld[$y] == '') {
198 $sel = ' selected="selected"';
199 } else {
200 $sel = '';
202 echo ' ';
203 echo '<option value="' . urlencode($fld[$y]) . '"' . $sel . '>' . htmlspecialchars($fld[$y]) . '</option>' . "\n";
204 } // end for
206 </select>
207 </td>
208 <?php
209 $z++;
210 } // end if
211 echo "\n";
213 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
214 continue;
217 <td align="center" bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
218 <select style="width: <?php echo $realwidth; ?>" name="Field[<?php echo $z; ?>]" size="1">
219 <option value=""></option>
220 <?php
221 echo "\n";
222 for ($y = 0; $y < sizeof($fld); $y++) {
223 if (isset($Field[$x]) && $fld[$y] == urldecode($Field[$x])) {
224 $curField[$z] = urldecode($Field[$x]);
225 $sel = ' selected="selected"';
226 } else {
227 $sel = '';
228 } // end if
229 echo ' ';
230 echo '<option value="' . urlencode($fld[$y]) . '"' . $sel . '>' . htmlspecialchars($fld[$y]) . '</option>' . "\n";
231 } // end for
233 </select>
234 </td>
235 <?php
236 $z++;
237 echo "\n";
238 } // end for
240 </tr>
242 <!-- Sort row -->
243 <tr>
244 <td align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $cfg['ThBgcolor']; ?>">
245 <b><?php echo $strSort; ?>&nbsp;:&nbsp;</b>
246 </td>
247 <?php
248 $z = 0;
249 for ($x = 0; $x < $col; $x++) {
250 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
252 <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
253 <select style="width: <?php echo $realwidth; ?>" name="Sort[<?php echo $z; ?>]" size="1">
254 <option value=""></option>
255 <option value="ASC"><?php echo $strAscending; ?></option>
256 <option value="DESC"><?php echo $strDescending; ?></option>
257 </select>
258 </td>
259 <?php
260 $z++;
261 } // end if
262 echo "\n";
264 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
265 continue;
268 <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
269 <select style="width: <?php echo $realwidth; ?>" name="Sort[<?php echo $z; ?>]" size="1">
270 <option value=""></option>
271 <?php
272 echo "\n";
274 // If they have chosen all fields using the * selector,
275 // then sorting is not available
276 // Robbat2 - Fix for Bug #570698
277 if (isset($Sort[$x]) && isset($Field[$x]) && (substr(urldecode($Field[$x]),-2) == '.*')) {
278 $Sort[$x] = '';
279 } //end if
281 if (isset($Sort[$x]) && $Sort[$x] == 'ASC') {
282 $curSort[$z] = $Sort[$x];
283 $sel = ' selected="selected"';
284 } else {
285 $sel = '';
286 } // end if
287 echo ' ';
288 echo '<option value="ASC"' . $sel . '>' . $strAscending . '</option>' . "\n";
289 if (isset($Sort[$x]) && $Sort[$x] == 'DESC') {
290 $curSort[$z] = $Sort[$x];
291 $sel = ' selected="selected"';
292 } else {
293 $sel = '';
294 } // end if
295 echo ' ';
296 echo '<option value="DESC"' . $sel . '>' . $strDescending . '</option>' . "\n";
298 </select>
299 </td>
300 <?php
301 $z++;
302 echo "\n";
303 } // end for
305 </tr>
307 <!-- Show row -->
308 <tr>
309 <td align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $cfg['ThBgcolor']; ?>">
310 <b><?php echo $strShow; ?>&nbsp;:&nbsp;</b>
311 </td>
312 <?php
313 $z = 0;
314 for ($x = 0; $x < $col; $x++) {
315 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
317 <td align="center" bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
318 <input type="checkbox" name="Show[<?php echo $z; ?>]" />
319 </td>
320 <?php
321 $z++;
322 } // end if
323 echo "\n";
325 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
326 continue;
328 if (isset($Show[$x])) {
329 $checked = ' checked="checked"';
330 $curShow[$z] = $Show[$x];
331 } else {
332 $checked = '';
335 <td align="center" bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
336 <input type="checkbox" name="Show[<?php echo $z; ?>]"<?php echo $checked; ?> />
337 </td>
338 <?php
339 $z++;
340 echo "\n";
341 } // end for
343 </tr>
345 <!-- Criteria row -->
346 <tr>
347 <td align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $cfg['ThBgcolor']; ?>">
348 <b><?php echo $strCriteria; ?>&nbsp;:&nbsp;</b>
349 </td>
350 <?php
351 $z = 0;
352 for ($x = 0; $x < $col; $x++) {
353 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
355 <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
356 <input type="text" name="Criteria[<?php echo $z; ?>]" value="" class="textfield" style="width: <?php echo $realwidth; ?>" size="20" />
357 </td>
358 <?php
359 $z++;
360 } // end if
361 echo "\n";
363 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
364 continue;
366 if (isset($Criteria[$x])) {
367 if (get_magic_quotes_gpc()) {
368 $stripped_Criteria = stripslashes($Criteria[$x]);
369 } else {
370 $stripped_Criteria = $Criteria[$x];
373 if ((empty($prev_Criteria) || !isset($prev_Criteria[$x]))
374 || urldecode($prev_Criteria[$x]) != htmlspecialchars($stripped_Criteria)) {
375 $curCriteria[$z] = $stripped_Criteria;
376 $encoded_Criteria = urlencode($stripped_Criteria);
377 } else {
378 $curCriteria[$z] = urldecode($prev_Criteria[$x]);
379 $encoded_Criteria = $prev_Criteria[$x];
382 <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
383 <input type="hidden" name="prev_Criteria[<?php echo $z; ?>]" value="<?php echo $encoded_Criteria; ?>" />
384 <input type="text" name="Criteria[<?php echo $z; ?>]" value="<?php echo htmlspecialchars($stripped_Criteria); ?>" class="textfield" style="width: <?php echo $realwidth; ?>" size="20" />
385 </td>
386 <?php
387 $z++;
388 echo "\n";
389 } // end for
391 </tr>
393 <!-- And/Or columns and rows -->
394 <?php
395 $w = 0;
396 for ($y = 0; $y <= $row; $y++) {
397 $bgcolor = ($y % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo'];
398 if (isset($InsRow[$y]) && $InsRow[$y] == 'on') {
399 $chk['or'] = ' checked="checked"';
400 $chk['and'] = '';
402 <tr>
403 <td align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $bgcolor; ?>" nowrap="nowrap">
404 <!-- Row controls -->
405 <table bgcolor="<?php echo $bgcolor; ?>">
406 <tr>
407 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
408 <small><?php echo $strQBEIns; ?>&nbsp;:</small>
409 <input type="checkbox" name="InsRow[<?php echo $w; ?>]" />
410 </td>
411 <td align="<?php echo $cell_align_right; ?>">
412 <b><?php echo $strAnd; ?>&nbsp;:</b>
413 </td>
414 <td>
415 <input type="radio" name="AndOrRow[<?php echo $w; ?>]" value="and"<?php echo $chk['and']; ?> />
416 &nbsp;
417 </td>
418 </tr>
419 <tr>
420 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
421 <small><?php echo $strQBEDel; ?>&nbsp;:</small>
422 <input type="checkbox" name="DelRow[<?php echo $w; ?>]" />
423 </td>
424 <td align="<?php echo $cell_align_right; ?>">
425 <b><?php echo $strOr; ?>&nbsp;:</b>
426 </td>
427 <td>
428 <input type="radio" name="AndOrRow[<?php echo $w; ?>]" value="or"<?php echo $chk['or']; ?> />
429 &nbsp;
430 </td>
431 </tr>
432 </table>
433 </td>
434 <?php
435 $z = 0;
436 for ($x = 0; $x < $col; $x++) {
437 if ($InsCol[$x] == 'on') {
438 echo "\n";
439 $or = 'Or' . $w . '[' . $z . ']';
441 <td align="center" bgcolor="<?php echo $bgcolor; ?>">
442 <textarea cols="20" rows="2" style="width: <?php echo $realwidth; ?>" name="<?php echo $or; ?>" dir="<?php echo $text_dir; ?>"></textarea>
443 </td>
444 <?php
445 $z++;
446 } // end if
447 if ($DelCol[$x] == 'on') {
448 continue;
451 echo "\n";
452 $or = 'Or' . $w . '[' . $z . ']';
454 <td align="center" bgcolor="<?php echo $bgcolor; ?>">
455 <textarea cols="20" rows="2" style="width: <?php echo $realwidth; ?>" name="<?php echo $or; ?>" dir="<?php echo $text_dir; ?>"></textarea>
456 </td>
457 <?php
458 $z++;
459 } // end for
460 $w++;
461 echo "\n";
463 </tr>
464 <?php
465 } // end if
467 if (isset($DelRow[$y]) && $DelRow[$y] == 'on') {
468 continue;
471 if (isset($AndOrRow[$y])) {
472 $curAndOrRow[$w] = $AndOrRow[$y];
474 if (isset($AndOrRow[$y]) && $AndOrRow[$y] == 'and') {
475 $chk['and'] = ' checked="checked"';
476 $chk['or'] = '';
477 } else {
478 $chk['or'] = ' checked="checked"';
479 $chk['and'] = '';
481 echo "\n";
483 <tr>
484 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
485 <!-- Row controls -->
486 <table bgcolor="<?php echo $bgcolor; ?>">
487 <tr>
488 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
489 <small><?php echo $strQBEIns; ?>&nbsp;:</small>
490 <input type="checkbox" name="InsRow[<?php echo $w; ?>]" />
491 </td>
492 <td align="<?php echo $cell_align_right; ?>">
493 <b><?php echo $strAnd; ?>&nbsp;:</b>
494 </td>
495 <td>
496 <input type="radio" name="AndOrRow[<?php echo $w; ?>]" value="and"<?php echo $chk['and']; ?> />
497 </td>
498 </tr>
499 <tr>
500 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
501 <small><?php echo $strQBEDel; ?>&nbsp;:</small>
502 <input type="checkbox" name="DelRow[<?php echo $w; ?>]" />
503 </td>
504 <td align="<?php echo $cell_align_right; ?>">
505 <b><?php echo $strOr; ?>&nbsp;:</b>
506 </td>
507 <td>
508 <input type="radio" name="AndOrRow[<?php echo $w; ?>]" value="or"<?php echo $chk['or']; ?> />
509 </td>
510 </tr>
511 </table>
512 </td>
513 <?php
514 $z = 0;
515 for ($x = 0; $x < $col; $x++) {
516 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
517 echo "\n";
518 $or = 'Or' . $w . '[' . $z . ']';
520 <td align="center" bgcolor="<?php echo $bgcolor; ?>">
521 <textarea cols="20" rows="2" style="width: <?php echo $realwidth; ?>" name="<?php echo $or; ?>" dir="<?php echo $text_dir; ?>"></textarea>
522 </td>
523 <?php
524 $z++;
525 } // end if
526 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
527 continue;
530 echo "\n";
531 $or = 'Or' . $y;
532 if (!isset(${$or})) {
533 ${$or} = '';
535 if (!empty(${$or}) && isset(${$or}[$x])) {
536 if (get_magic_quotes_gpc()) {
537 $stripped_or = stripslashes(${$or}[$x]);
538 } else {
539 $stripped_or = ${$or}[$x];
541 } else {
542 $stripped_or = '';
545 <td align="center" bgcolor="<?php echo $bgcolor; ?>">
546 <textarea cols="20" rows="2" style="width: <?php echo $realwidth; ?>" name="Or<?php echo $w . '[' . $z . ']'; ?>" dir="<?php echo $text_dir; ?>"><?php echo htmlspecialchars($stripped_or); ?></textarea>
547 </td>
548 <?php
549 if (!empty(${$or}) && isset(${$or}[$x])) {
550 ${'cur' . $or}[$z] = ${$or}[$x];
552 $z++;
553 } // end for
554 $w++;
555 echo "\n";
557 </tr>
558 <?php
559 echo "\n";
560 } // end for
563 <!-- Modify columns -->
564 <tr>
565 <td align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $cfg['ThBgcolor']; ?>">
566 <b><?php echo $strModify; ?>&nbsp;:&nbsp;</b>
567 </td>
568 <?php
569 $z = 0;
570 for ($x = 0; $x < $col; $x++) {
571 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
572 $curAndOrCol[$z] = $AndOrCol[$y];
573 if ($AndOrCol[$z] == 'or') {
574 $chk['or'] = ' checked="checked"';
575 $chk['and'] = '';
576 } else {
577 $chk['and'] = ' checked="checked"';
578 $chk['or'] = '';
581 <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
582 <b><?php echo $strOr; ?>&nbsp;:</b>
583 <input type="radio" name="AndOrCol[<?php echo $z; ?>]" value="or"<?php echo $chk['or']; ?> />
584 &nbsp;&nbsp;<b><?php echo $strAnd; ?>&nbsp;:</b>
585 <input type="radio" name="AndOrCol[<?php echo $z; ?>]" value="and"<?php echo $chk['and']; ?> />
586 <br />
587 <?php echo $strQBEIns . "\n"; ?>
588 <input type="checkbox" name="InsCol[<?php echo $z; ?>]" />
589 &nbsp;&nbsp;<?php echo $strQBEDel . "\n"; ?>
590 <input type="checkbox" name="DelCol[<?php echo $z; ?>]" />
591 </td>
592 <?php
593 $z++;
594 } // end if
595 echo "\n";
597 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
598 continue;
601 if (isset($AndOrCol[$y])) {
602 $curAndOrCol[$z] = $AndOrCol[$y];
604 if (isset($AndOrCol[$z]) && $AndOrCol[$z] == 'or') {
605 $chk['or'] = ' checked="checked"';
606 $chk['and'] = '';
607 } else {
608 $chk['and'] = ' checked="checked"';
609 $chk['or'] = '';
612 <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
613 <b><?php echo $strOr; ?>&nbsp;:</b>
614 <input type="radio" name="AndOrCol[<?php echo $z; ?>]" value="or"<?php echo $chk['or']; ?> />
615 &nbsp;&nbsp;<b><?php echo $strAnd; ?>&nbsp;:</b>
616 <input type="radio" name="AndOrCol[<?php echo $z; ?>]" value="and"<?php echo $chk['and']; ?> />
617 <br />
618 <?php echo $strQBEIns . "\n"; ?>
619 <input type="checkbox" name="InsCol[<?php echo $z; ?>]" />
620 &nbsp;&nbsp;<?php echo $strQBEDel . "\n"; ?>
621 <input type="checkbox" name="DelCol[<?php echo $z; ?>]" />
622 </td>
623 <?php
624 $z++;
625 echo "\n";
626 } // end for
628 </tr>
629 </table>
632 <!-- Other controls -->
633 <table border="0">
634 <tr>
635 <td valign="top">
636 <table border="0" align="<?php echo $cell_align_left; ?>">
637 <tr>
638 <td rowspan="4" valign="top">
639 <?php echo $strUseTables; ?>&nbsp;:
640 <br />
641 <select name="TableList[]" size="7" multiple="multiple">
642 <?php
643 while (list($key, $val) = each($tbl_names)) {
644 echo ' ';
645 echo '<option value="' . urlencode($key) . '"' . $val . '>' . htmlspecialchars($key) . '</option>' . "\n";
648 </select>
649 </td>
650 <td align="<?php echo $cell_align_right; ?>" valign="bottom">
651 <input type="hidden" value="<?php echo $db; ?>" name="db" />
652 <input type="hidden" value="<?php echo $z; ?>" name="Columns" />
653 <?php
654 $w--;
656 <input type="hidden" value="<?php echo $w; ?>" name="Rows" />
657 <?php echo $strAddDeleteRow; ?>&nbsp;:
658 <select size="1" name="Add_Row">
659 <option value="-3">-3</option>
660 <option value="-2">-2</option>
661 <option value="-1">-1</option>
662 <option value="0" selected="selected">0</option>
663 <option value="1">1</option>
664 <option value="2">2</option>
665 <option value="3">3</option>
666 </select>
667 </td>
668 </tr>
669 <tr>
670 <td align="<?php echo $cell_align_right; ?>" valign="bottom">
671 <?php echo $strAddDeleteColumn; ?>&nbsp;:
672 <select size="1" name="Add_Col">
673 <option value="-3">-3</option>
674 <option value="-2">-2</option>
675 <option value="-1">-1</option>
676 <option value="0" selected="selected">0</option>
677 <option value="1">1</option>
678 <option value="2">2</option>
679 <option value="3">3</option>
680 </select>
681 </td>
682 </tr>
683 <!-- Generates a query -->
684 <tr align="center" valign="top">
685 <td>
686 <input type="submit" name="modify" value="<?php echo $strUpdateQuery; ?>" />
687 <input type="hidden" name="server" value="<?php echo $server; ?>" />
688 <input type="hidden" name="lang" value="<?php echo $lang; ?>" />
689 <input type="hidden" name="convcharset" value="<?php echo $convcharset; ?>" />
690 </td>
691 </tr>
692 <!-- Executes a query -->
693 <tr align="center" valign="top">
694 <td>
695 <input type="submit" name="submit_sql" value="<?php echo $strRunQuery; ?>" />
696 </td>
697 </tr>
698 </table>
699 </td>
700 <td>
701 <!-- Displays the current query -->
702 <?php echo sprintf($strQueryOnDb, htmlspecialchars($db)); ?><br />
703 <textarea cols="30" rows="7" name="sql_query" dir="<?php echo $text_dir; ?>">
704 <?php
705 // 1. SELECT
706 $last_select = 0;
707 $encoded_qry = '';
708 if (!isset($qry_select)) {
709 $qry_select = '';
711 for ($x = 0; $x < $col; $x++) {
712 if (!empty($curField[$x]) && isset($curShow[$x]) && $curShow[$x] == 'on') {
713 if ($last_select) {
714 $qry_select .= ', ';
716 $qry_select .= $curField[$x];
717 $last_select = 1;
719 } // end for
720 if (!empty($qry_select)) {
721 $encoded_qry .= urlencode('SELECT ' . $qry_select . "\n");
722 echo 'SELECT ' . htmlspecialchars($qry_select) . "\n";
725 // 2. FROM
727 // Create LEFT JOINS out of Relations
728 // Code originally by Mike Beck <mike.beck@ibmiller.de>
729 // If we can use Relations we could make some left joins.
730 // First find out if relations are available in this database.
732 // Debugging:
733 //echo '</textarea><pre style="background-color: white;">';
734 // First we need the really needed Tables - those in TableList might still be
735 // all Tables.
736 if (isset($Field) && count($Field) > 0) {
738 // Initialize some variables
739 $tab_all = array();
740 $col_all = array();
741 $tab_wher = array();
742 $tab_know = array();
743 $tab_left = array();
744 $col_where = array();
745 $fromclause = '';
747 // We only start this if we have fields, otherwise it would be dumb
748 // echo "get everything\n";
749 while (list(, $value) = each($Field)) {
750 $parts = explode('.', $value);
751 if (!empty($parts[0]) && !empty($parts[1])) {
752 $tab_raw = urldecode($parts[0]);
753 $tab = str_replace('`', '', $tab_raw);
754 // echo 'new Tab: ' . $tab . "\n";
755 $tab_all[$tab] = $tab;
756 $col_raw = urldecode($parts[1]);
757 $col_all[] = $tab . '.' . str_replace('`', '', $col_raw);
758 // echo 'new col: ' . $tab . '.' . str_replace('`', '', $col_raw) . "\n";
760 } // end while
762 // echo "check where clauses\n";
763 if ($cfgRelation['relwork'] && count($tab_all) > 0) {
764 // Now we need all tables that we have in the where clause
765 $crit_cnt = count($Criteria);
766 for ($x = 0; $x < $crit_cnt; $x++) {
767 $curr_tab = explode('.', urldecode($Field[$x]));
768 if (!empty($curr_tab[0]) && !empty($curr_tab[1])) {
769 $tab_raw = urldecode($curr_tab[0]);
770 $tab = str_replace('`', '', $tab_raw);
772 $col_raw = urldecode($curr_tab[1]);
773 $col1 = str_replace('`', '', $col_raw);
774 $col1 = $tab . '.' . $col1;
775 // Now we know that our array has the same numbers as $Criteria
776 // we can check which of our columns has a where clause
777 if (!empty($Criteria[$x])) {
778 if (substr($Criteria[$x], 0, 1) == '=' || eregi('is', $Criteria[$x])) {
779 $col_where[$col] = $col1;
780 $tab_wher[$tab] = $tab;
781 // echo 'new where clause: ' . $tab_wher[$tab] . "||\n";
783 } // end if
784 } // end if
785 } // end for
787 // Cleans temp vars w/o further use
788 unset($tab_raw);
789 unset($col_raw);
790 unset($col1);
792 if (count($tab_wher) == 1) {
793 // If there is exactly one column that has a decent where-clause
794 // we will just use this
795 $master = key($tab_wher);
796 // echo 'nur ein where: master = ' .$master . "||\n";
797 } else {
798 // Now let's find out which of the tables has an index
799 // echo "prüfe indexe:\n";
800 while (list(, $tab) = each($tab_all)) {
801 $ind_qry = 'SHOW INDEX FROM ' . PMA_backquote($tab);
802 $ind_rs = PMA_mysql_query($ind_qry);
803 while ($ind = PMA_mysql_fetch_array($ind_rs)) {
804 $col1 = $tab . '.' . $ind['Column_name'];
805 if (isset($col_all[$col1])) {
806 if ($ind['non_unique'] == 0) {
807 if (isset($col_where[$col1])) {
808 $col_unique[$col1] = 'Y';
809 } else {
810 $col_unique[$col1] = 'N';
812 //echo 'neuen unique index gefunden: ' . $col . "\n";
813 } else {
814 if (isset($col_where[$col1])) {
815 $col_index[$col1] = 'Y';
816 } else {
817 $col_index[$col1] = 'N';
819 //echo 'neuen index gefunden: ' . $col . "\n";
822 } // end while (each col of tab)
823 } // end while (each tab)
824 // now we want to find the best.
825 if (isset($col_unique) && count($col_unique) > 0) {
826 $col_cand = $col_unique;
827 //echo "Kandidaten sind jetzt alle mit unique index\n";
828 $needsort = 1;
829 } else if (isset($col_index) && count($col_index) > 0) {
830 $col_cand = $col_index;
831 $needsort = 1;
832 //echo "Kandidaten sind jetzt alle mit index\n";
833 } else if (isset($col_where) && count($col_where) > 0) {
834 $col_cand = $tab_wher;
835 //echo "Kandidaten sind jetzt alle im whereclause\n";
836 $needsort = 0;
837 } else {
838 $col_cand = $tab_all;
839 $needsort = 0;
840 //echo "Kandidaten sind jetzt alle \n";
843 // If we came up with $col_unique (very good) or $col_index (still
844 // good) as $col_cand we want to check if we have any 'Y' there
845 // (that would mean that they were also found in the whereclauses
846 // which would be great). if yes, we take only those
847 if ($needsort == 1) {
848 while (list($col, $is_where) = each($col_cand)) {
849 $tab = explode('.', $col);
850 $tab = $tab[0];
851 if ($is_where == 'Y') {
852 $vg[$col] = $tab;
853 } else {
854 $sg[$col] = $tab;
857 if (isset($vg)) {
858 $col_cand = $vg;
859 //echo "Kandidaten konnten auf index+where beschränkt werden\n";
860 } else {
861 $col_cand = $sg;
862 //echo "keiner der Kandidaten mit Index ist im wherclause\n";
866 // If our array of candidates has more than one member we'll just
867 // find the smallest table.
868 // Of course the actual query would be faster if we check for
869 // the Criteria which gives the smallest result set in its table,
870 // but it would take too much time to check this
871 if (count($col_cand) > 1) {
872 //echo "wir haben immer noch mehr als einen Kandidaten. Prüfe Größe\n";
873 // Of course we only want to check each table once
874 $checked_tables = $col_cand;
875 while (list(, $tab) = each($col_cand)) {
876 if ($checked_tables[$tab] != 1 ) {
877 //echo 'prüfe jetzt: Tabelle ' . $tab . "\n";
878 $rows_qry = 'SELECT COUNT(1) AS anz '
879 . 'FROM ' . PMA_backquote($tab);
880 $rows_rs = PMA_mysql_query($rows_qry);
881 while ($res = PMA_mysql_fetch_array($rows_rs)) {
882 $tsize[$tab] = $res['anz'];
883 //echo "$tab hat: " . $tsize[$tab] . "\n";
885 $checked_tables[$tab] = 1;
887 $csize[$tab] = $tsize[$tab];
888 //echo 'erster csize: ' . $csize[$tab] . "\n";
890 asort($csize);
891 reset($csize);
892 $master = key($csize);
893 //echo 'kleinste Datei: ' . $master . "\n";
894 } else {
895 $master = $col_cand[0];
896 //echo 'master ist der einzige Kandidat: ' . $master . "\n";
898 } // end if (exactly one where clause)
899 //echo 'ich habe mich entschieden: ' . $master;
900 //die;
904 * Removes unwanted entries from an array (PHP3 compliant)
906 * @param array the array to work with
907 * @param array the list of keys to remove
909 * @return array the cleaned up array
911 * @access private
913 function PMA_arrayShort($array, $key)
915 while (list($k, $v) = each($array)) {
916 if ($k != $key) {
917 $reta[$k] = $v;
920 if (!isset($reta)) {
921 $reta = array();
924 return $reta;
925 } // end of the "PMA_arrayShort()" function
929 * Finds all related tables
931 * @param string wether to go from master to foreign or vice versa
933 * @return boolean always TRUE
935 * @global array the list of tables that we still couldn't connect
936 * @global array the list of allready connected tables
937 * @global string the current databse name
938 * @global string the super user connection id
939 * @global array the list of relation settings
941 * @access private
943 function PMA_getRelatives($from) {
944 global $tab_left, $tab_know, $fromclause;
945 global $dbh, $db, $cfgRelation;
947 if ($from == 'master') {
948 $to = 'foreign';
949 } else {
950 $to = 'master';
952 $in_know = '(\'' . implode('\', \'', $tab_know) . '\')';
953 $in_left = '(\'' . implode('\', \'', $tab_left) . '\')';
955 $rel_query = 'SELECT *'
956 . ' FROM ' . PMA_backquote($cfgRelation['relation'])
957 . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($db) . '\''
958 . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($db) . '\''
959 . ' AND ' . $from . '_table IN ' . $in_know
960 . ' AND ' . $to . '_table IN ' . $in_left;
961 if (isset($dbh)) {
962 PMA_mysql_select_db($cfgRelation['db'], $dbh);
963 $relations = @PMA_mysql_query($rel_query, $dbh) or PMA_mysqlDie(PMA_mysql_error($dbh), $rel_query, '', $err_url_0);
964 PMA_mysql_select_db($db, $dbh);
965 } else {
966 PMA_mysql_select_db($cfgRelation['db']);
967 $relations = @PMA_mysql_query($rel_query) or PMA_mysqlDie('', $rel_query, '', $err_url_0);
968 PMA_mysql_select_db($db);
970 while ($row = PMA_mysql_fetch_array($relations)) {
971 $found_table = $row[$to . '_table'];
972 if (isset($tab_left[$found_table])) {
973 $fromclause .= "\n" . ' LEFT JOIN '
974 . PMA_backquote($row[$to . '_table']) . ' ON '
975 . PMA_backquote($row[$from . '_table']) . '.'
976 . PMA_backquote($row[$from . '_field']) . ' = '
977 . PMA_backquote($row[$to . '_table']) . '.'
978 . PMA_backquote($row[$to . '_field']) . ' ';
979 $tab_know[$found_table] = $found_table;
980 $tab_left = PMA_arrayShort($tab_left, $found_table);
982 } // end while
984 return TRUE;
985 } // end of the "PMA_getRelatives()" function
988 $tab_left = PMA_arrayShort($tab_all, $master);
989 $tab_know[$master] = $master;
991 $run = 0;
992 $emerg = '';
993 while (count($tab_left) > 0) {
994 if ($run % 2 == 0) {
995 PMA_getRelatives('master');
996 } else {
997 PMA_getRelatives('foreign');
999 $run++;
1000 if ($run > 5) {
1002 while (list(, $tab) = each($tab_left)) {
1003 $emerg .= ', ' . $tab;
1004 $tab_left = PMA_arrayShort($tab_left, $tab);
1007 } // end while
1008 $qry_from = $master . $emerg . $fromclause;
1009 } // end if ($cfgRelation['relwork'] && count($tab_all) > 0)
1011 } // end count($Field) > 0
1013 // Now let's see what we got
1014 if (!empty($qry_from)) {
1015 $encoded_qry .= urlencode('FROM ' . $qry_from . "\n");
1016 echo 'FROM ' . htmlspecialchars($qry_from) . "\n";
1019 // 3. WHERE
1020 $qry_where = '';
1021 $criteria_cnt = 0;
1022 for ($x = 0; $x < $col; $x++) {
1023 if (!empty($curField[$x]) && !empty($curCriteria[$x]) && $x && isset($last_where)) {
1024 $qry_where .= ' ' . strtoupper($curAndOrCol[$last_where]) . ' ';
1026 if (!empty($curField[$x]) && !empty($curCriteria[$x])) {
1027 $qry_where .= '(' . $curField[$x] . ' ' . $curCriteria[$x] . ')';
1028 $last_where = $x;
1029 $criteria_cnt++;
1031 } // end for
1032 if ($criteria_cnt > 1) {
1033 $qry_where = '(' . $qry_where . ')';
1035 // OR rows ${'cur' . $or}[$x]
1036 if (!isset($curAndOrRow)) {
1037 $curAndOrRow = array();
1039 for ($y = 0; $y <= $row; $y++) {
1040 $criteria_cnt = 0;
1041 $qry_orwhere = '';
1042 $last_orwhere = '';
1043 for ($x = 0; $x < $col; $x++) {
1044 if (!empty($curField[$x]) && !empty(${'curOr' . $y}[$x]) && $x) {
1045 $qry_orwhere .= ' ' . strtoupper($curAndOrCol[$last_orwhere]) . ' ';
1047 if (!empty($curField[$x]) && !empty(${'curOr' . $y}[$x])) {
1048 $qry_orwhere .= '(' . $curField[$x]
1049 . ' '
1050 . (get_magic_quotes_gpc() ? stripslashes(${'curOr' . $y}[$x]) : ${'curOr' . $y}[$x])
1051 . ')';
1052 $last_orwhere = $x;
1053 $criteria_cnt++;
1055 } // end for
1056 if ($criteria_cnt > 1) {
1057 $qry_orwhere = '(' . $qry_orwhere . ')';
1059 if (!empty($qry_orwhere)) {
1060 $qry_where .= "\n"
1061 . strtoupper(isset($curAndOrRow[$y]) ? $curAndOrRow[$y] . ' ' : '')
1062 . $qry_orwhere;
1063 } // end if
1064 } // end for
1066 if (!empty($qry_where) && $qry_where != '()') {
1067 $encoded_qry .= urlencode('WHERE ' . $qry_where . "\n");
1068 echo 'WHERE ' . htmlspecialchars($qry_where) . "\n";
1069 } // end if
1071 // 4. ORDER BY
1072 $last_orderby = 0;
1073 if (!isset($qry_orderby)) {
1074 $qry_orderby = '';
1076 for ($x = 0; $x < $col; $x++) {
1077 if ($last_orderby && $x && !empty($curField[$x]) && !empty($curSort[$x])) {
1078 $qry_orderby .= ', ';
1080 if (!empty($curField[$x]) && !empty($curSort[$x])) {
1081 // if they have chosen all fields using the * selector,
1082 // then sorting is not available
1083 // Robbat2 - Fix for Bug #570698
1084 if (substr($curField[$x], -2) != '.*') {
1085 $qry_orderby .= $curField[$x] . ' ' . $curSort[$x];
1086 $last_orderby = 1;
1089 } // end for
1090 if (!empty($qry_orderby)) {
1091 $encoded_qry .= urlencode('ORDER BY ' . $qry_orderby);
1092 echo 'ORDER BY ' . htmlspecialchars($qry_orderby) . "\n";
1095 </textarea>
1096 <input type="hidden" name="encoded_sql_query" value="<?php echo $encoded_qry; ?>" />
1097 </td>
1098 </tr>
1099 </table>
1101 </form>
1104 <?php
1106 * Displays the footer
1108 require('./footer.inc.php3');