3.1.3.1
[phpmyadmin/crack.git] / tbl_select.php
blobafac61b4bc7dc9cc53c722a3f507c837d0e742d0
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Handles table search tab
6 * display table search form, create SQL query from form data
7 * and include sql.php to execute it
9 * @todo display search form again if no results from previous search
10 * @version $Id$
13 /**
14 * Gets some core libraries
16 require_once './libraries/common.inc.php';
17 require_once './libraries/relation.lib.php'; // foreign keys
18 require_once './libraries/mysql_charsets.lib.php';
20 $GLOBALS['js_include'][] = 'tbl_change.js';
21 $GLOBALS['js_include'][] = 'mootools.js';
23 if ($GLOBALS['cfg']['PropertiesIconic'] == true) {
24 $titles['Browse'] =
25 '<img class="icon" width="16" height="16" src="' . $pmaThemeImage
26 .'b_browse.png" alt="' . $strBrowseForeignValues . '" title="'
27 .$strBrowseForeignValues . '" />';
29 if ($GLOBALS['cfg']['PropertiesIconic'] === 'both') {
30 $titles['Browse'] .= $strBrowseForeignValues;
32 } else {
33 $titles['Browse'] = $strBrowseForeignValues;
36 /**
37 * Not selection yet required -> displays the selection form
39 if (!isset($param) || $param[0] == '') {
40 // Gets some core libraries
41 require_once './libraries/tbl_common.php';
42 //$err_url = 'tbl_select.php' . $err_url;
43 $url_query .= '&amp;goto=tbl_select.php&amp;back=tbl_select.php';
45 /**
46 * Gets tables informations
48 require_once './libraries/tbl_info.inc.php';
50 /**
51 * Displays top menu links
53 require_once './libraries/tbl_links.inc.php';
55 if (!isset($goto)) {
56 $goto = $GLOBALS['cfg']['DefaultTabTable'];
58 // Defines the url to return to in case of error in the next sql statement
59 $err_url = $goto . '?' . PMA_generate_common_url($db, $table);
61 // Gets the list and number of fields
62 $result = PMA_DBI_query('SHOW FULL FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE);
63 $fields_cnt = PMA_DBI_num_rows($result);
64 // rabue: we'd better ensure, that all arrays are empty.
65 $fields_list = $fields_null = $fields_type = $fields_collation = array();
66 while ($row = PMA_DBI_fetch_assoc($result)) {
67 $fields_list[] = $row['Field'];
68 $type = $row['Type'];
69 // reformat mysql query output - staybyte - 9. June 2001
70 if (strncasecmp($type, 'set', 3) == 0
71 || strncasecmp($type, 'enum', 4) == 0) {
72 $type = str_replace(',', ', ', $type);
73 } else {
75 // strip the "BINARY" attribute, except if we find "BINARY(" because
76 // this would be a BINARY or VARBINARY field type
77 if (!preg_match('@BINARY[\(]@i', $type)) {
78 $type = preg_replace('@BINARY@i', '', $type);
80 $type = preg_replace('@ZEROFILL@i', '', $type);
81 $type = preg_replace('@UNSIGNED@i', '', $type);
83 $type = strtolower($type);
85 if (empty($type)) {
86 $type = '&nbsp;';
88 $fields_null[] = $row['Null'];
89 $fields_type[] = $type;
90 $fields_collation[] = !empty($row['Collation']) && $row['Collation'] != 'NULL'
91 ? $row['Collation']
92 : '';
93 } // end while
94 PMA_DBI_free_result($result);
95 unset($result, $type);
97 // <markus@noga.de>
98 // retrieve keys into foreign fields, if any
99 // check also foreigners even if relwork is FALSE (to get
100 // foreign keys from innodb)
101 $foreigners = PMA_getForeigners($db, $table);
103 <script type="text/javascript">
104 // <![CDATA[
105 function PMA_tbl_select_operator(f, index, multiple) {
106 switch (f.elements["func[" + index + "]"].options[f.elements["func[" + index + "]"].selectedIndex].value) {
107 <?php
108 reset($GLOBALS['cfg']['UnaryOperators']);
109 while (list($operator) = each($GLOBALS['cfg']['UnaryOperators'])) {
110 echo ' case "' . $operator . "\":\r\n";
113 bDisabled = true;
114 break;
116 default:
117 bDisabled = false;
119 f.elements["fields[" + index + "]" + ((multiple) ? "[]": "")].disabled = bDisabled;
121 // ]]>
122 </script>
123 <form method="post" action="tbl_select.php" name="insertForm">
124 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
125 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
126 <input type="hidden" name="back" value="tbl_select.php" />
128 <fieldset id="fieldset_table_search">
130 <fieldset id="fieldset_table_qbe">
131 <legend><?php echo $strDoAQuery; ?></legend>
132 <table class="data">
133 <thead>
134 <tr><th><?php echo $strField; ?></th>
135 <th><?php echo $strType; ?></th>
136 <th><?php echo $strCollation; ?></th>
137 <th><?php echo $strOperator; ?></th>
138 <th><?php echo $strValue; ?></th>
139 </tr>
140 </thead>
141 <tbody>
142 <?php
143 $odd_row = true;
145 for ($i = 0; $i < $fields_cnt; $i++) {
147 <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
148 <th><?php echo htmlspecialchars($fields_list[$i]); ?></th>
149 <td><?php echo $fields_type[$i]; ?></td>
150 <td><?php echo $fields_collation[$i]; ?></td>
151 <td><select name="func[]">
152 <?php
153 if (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
154 foreach ($GLOBALS['cfg']['EnumOperators'] as $fc) {
155 echo "\n" . ' '
156 . '<option value="' . htmlspecialchars($fc) . '">'
157 . htmlspecialchars($fc) . '</option>';
159 } elseif (preg_match('@char|blob|text|set@i', $fields_type[$i])) {
160 foreach ($GLOBALS['cfg']['TextOperators'] as $fc) {
161 echo "\n" . ' '
162 . '<option value="' . htmlspecialchars($fc) . '">'
163 . htmlspecialchars($fc) . '</option>';
165 } else {
166 foreach ($GLOBALS['cfg']['NumOperators'] as $fc) {
167 echo "\n" . ' '
168 . '<option value="' . htmlspecialchars($fc) . '">'
169 . htmlspecialchars($fc) . '</option>';
171 } // end if... else...
172 if ($fields_null[$i]) {
173 foreach ($GLOBALS['cfg']['NullOperators'] as $fc) {
174 echo "\n" . ' '
175 . '<option value="' . htmlspecialchars($fc) . '">'
176 . htmlspecialchars($fc) . '</option>';
181 </select>
182 </td>
183 <td>
184 <?php
185 // <markus@noga.de>
186 $field = $fields_list[$i];
188 $foreignData = PMA_getForeignData($foreigners, $field, false, '', '');
190 if ($foreigners && isset($foreigners[$field]) && is_array($foreignData['disp_row'])) {
191 // f o r e i g n k e y s
192 echo ' <select name="fields[' . $i . ']">' . "\n";
193 // go back to first row
195 // here, the 4th parameter is empty because there is no current
196 // value of data for the dropdown (the search page initial values
197 // are displayed empty)
198 echo PMA_foreignDropdown($foreignData['disp_row'],
199 $foreignData['foreign_field'],
200 $foreignData['foreign_display'],
201 '', $GLOBALS['cfg']['ForeignKeyMaxLimit']);
202 echo ' </select>' . "\n";
203 } elseif ($foreignData['foreign_link'] == true) {
205 <input type="text" name="fields[<?php echo $i; ?>]"
206 id="field_<?php echo md5($field); ?>[<?php echo $i; ?>]"
207 class="textfield" />
208 <script type="text/javascript">
209 // <![CDATA[
210 document.writeln('<a target="_blank" onclick="window.open(this.href, \'foreigners\', \'width=640,height=240,scrollbars=yes\'); return false" href="browse_foreigners.php?<?php echo PMA_generate_common_url($db, $table); ?>&amp;field=<?php echo urlencode($field); ?>&amp;fieldkey=<?php echo $i; ?>"><?php echo str_replace("'", "\'", $titles['Browse']); ?></a>');
211 // ]]>
212 </script>
213 <?php
214 } elseif (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
215 // e n u m s
216 $enum_value=explode(', ', str_replace("'", '', substr($fields_type[$i], 5, -1)));
217 $cnt_enum_value = count($enum_value);
218 echo ' <select name="fields[' . $i . '][]"'
219 .' multiple="multiple" size="' . min(3, $cnt_enum_value) . '">' . "\n";
220 for ($j = 0; $j < $cnt_enum_value; $j++) {
221 echo ' <option value="' . $enum_value[$j] . '">'
222 . $enum_value[$j] . '</option>';
223 } // end for
224 echo ' </select>' . "\n";
225 } else {
226 // o t h e r c a s e s
227 echo ' <input type="text" name="fields[' . $i . ']"'
228 .' size="40" class="textfield" id="field_' . $i . '" />' . "\n";
230 $type = $fields_type[$i];
231 if ($type == 'date' || $type == 'datetime' || substr($type, 0, 9) == 'timestamp') {
233 <script type="text/javascript">
234 //<![CDATA[
235 document.write('<a title="<?php echo $strCalendar;?>" href="javascript:openCalendar(\'<?php echo PMA_generate_common_url();?>\', \'insertForm\', \'field_<?php echo ($i); ?>\', \'<?php echo (substr($type, 0, 9) == 'timestamp') ? 'datetime' : substr($type, 0, 9); ?>\')"><img class="calendar" src="<?php echo $pmaThemeImage; ?>b_calendar.png" alt="<?php echo $strCalendar; ?>"/></a>');
236 //]]>
237 </script>
238 <?php
241 <input type="hidden" name="names[<?php echo $i; ?>]"
242 value="<?php echo htmlspecialchars($fields_list[$i]); ?>" />
243 <input type="hidden" name="types[<?php echo $i; ?>]"
244 value="<?php echo $fields_type[$i]; ?>" />
245 <input type="hidden" name="collations[<?php echo $i; ?>]"
246 value="<?php echo $fields_collation[$i]; ?>" />
247 </td>
248 </tr>
249 <?php
250 } // end for
252 </tbody>
253 </table>
254 </fieldset>
255 <?php
256 PMA_generate_slider_effect('searchoptions', $strOptions);
258 <fieldset id="fieldset_select_fields">
259 <legend><?php echo $strSelectFields; ?></legend>
260 <select name="param[]" size="<?php echo min($fields_cnt, 10); ?>"
261 multiple="multiple">
262 <?php
263 // Displays the list of the fields
264 foreach ($fields_list as $each_field) {
265 echo ' '
266 .'<option value="' . htmlspecialchars($each_field) . '"'
267 .' selected="selected">' . htmlspecialchars($each_field)
268 .'</option>' . "\n";
271 </select>
272 <input type="checkbox" name="distinct" value="DISTINCT" id="oDistinct" />
273 <label for="oDistinct">DISTINCT</label>
274 </fieldset>
276 <fieldset id="fieldset_search_conditions">
277 <legend><?php echo '<em>' . $strOr . '</em> ' .$strAddSearchConditions; ?></legend>
278 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'Functions'); ?>
280 <input type="text" name="where" class="textfield" size="64" />
281 </fieldset>
283 <fieldset id="fieldset_limit_rows">
284 <legend><?php echo $strLimitNumRows; ?></legend>
285 <input type="text" size="4" name="session_max_rows"
286 value="<?php echo $GLOBALS['cfg']['MaxRows']; ?>" class="textfield" />
287 </fieldset>
289 <fieldset id="fieldset_display_order">
290 <legend><?php echo $strDisplayOrder; ?></legend>
291 <select name="orderField" style="vertical-align: middle">
292 <option value="--nil--"></option>
293 <?php
294 foreach ($fields_list as $each_field) {
295 echo ' '
296 .'<option value="' . htmlspecialchars($each_field) . '">'
297 .htmlspecialchars($each_field) . '</option>' . "\n";
298 } // end for
300 </select>
301 <?php
302 $choices = array(
303 'ASC' => $strAscending,
304 'DESC' => $strDescending
306 PMA_generate_html_radio('order', $choices, 'ASC', false, true, "formelement");
307 unset($choices);
309 </fieldset>
310 </div>
311 </fieldset>
312 <fieldset class="tblFooters">
313 <input type="hidden" name="max_number_of_fields"
314 value="<?php echo $fields_cnt; ?>" />
315 <input type="submit" name="submit" value="<?php echo $strGo; ?>" />
316 </fieldset>
317 </form>
318 <?php
319 require_once './libraries/footer.inc.php';
324 * Selection criteria have been submitted -> do the work
326 else {
327 // Builds the query
329 $sql_query = 'SELECT ' . (isset($distinct) ? 'DISTINCT ' : '');
331 // if all fields were selected to display, we do a SELECT *
332 // (more efficient and this helps prevent a problem in IE
333 // if one of the rows is edited and we come back to the Select results)
335 if (count($param) == $max_number_of_fields) {
336 $sql_query .= '* ';
337 } else {
338 $param = PMA_backquote($param);
339 $sql_query .= implode(', ', $param);
340 unset($param);
341 } // end if
343 $sql_query .= ' FROM ' . PMA_backquote($table);
345 // The where clause
346 if (trim($where) != '') {
347 $sql_query .= ' WHERE ' . $where;
348 } else {
349 $w = $charsets = array();
350 $cnt_func = count($func);
351 reset($func);
352 while (list($i, $func_type) = each($func)) {
353 list($charsets[$i]) = explode('_', $collations[$i]);
354 if (isset($GLOBALS['cfg']['UnaryOperators'][$func_type]) && $GLOBALS['cfg']['UnaryOperators'][$func_type] == 1) {
355 $fields[$i] = '';
356 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type;
358 } elseif (strncasecmp($types[$i], 'enum', 4) == 0) {
359 if (!empty($fields[$i])) {
360 if (!is_array($fields[$i])) {
361 $fields[$i] = explode(',', $fields[$i]);
363 $enum_selected_count = count($fields[$i]);
364 if ($func_type == '=' && $enum_selected_count > 1) {
365 $func_type = $func[$i] = 'IN';
366 $parens_open = '(';
367 $parens_close = ')';
369 } elseif ($func_type == '!=' && $enum_selected_count > 1) {
370 $func_type = $func[$i] = 'NOT IN';
371 $parens_open = '(';
372 $parens_close = ')';
374 } else {
375 $parens_open = '';
376 $parens_close = '';
378 $enum_where = '\'' . PMA_sqlAddslashes($fields[$i][0]) . '\'';
379 for ($e = 1; $e < $enum_selected_count; $e++) {
380 $enum_where .= ', \'' . PMA_sqlAddslashes($fields[$i][$e]) . '\'';
383 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close;
386 } elseif ($fields[$i] != '') {
387 // For these types we quote the value. Even if it's another type (like INT),
388 // for a LIKE we always quote the value. MySQL converts strings to numbers
389 // and numbers to strings as necessary during the comparison
390 if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types[$i]) || strpos(' ' . $func_type, 'LIKE')) {
391 $quot = '\'';
392 } else {
393 $quot = '';
396 // LIKE %...%
397 if ($func_type == 'LIKE %...%') {
398 $func_type = 'LIKE';
399 $fields[$i] = '%' . $fields[$i] . '%';
401 $w[] = PMA_backquote($names[$i]) . ' ' . $func_type . ' ' . $quot . PMA_sqlAddslashes($fields[$i]) . $quot;
403 } // end if
404 } // end for
406 if ($w) {
407 $sql_query .= ' WHERE ' . implode(' AND ', $w);
409 } // end if
411 if ($orderField != '--nil--') {
412 $sql_query .= ' ORDER BY ' . PMA_backquote($orderField) . ' ' . $order;
413 } // end if
415 require './sql.php';