Mention wiki.
[phpmyadmin/crack.git] / tbl_select.php
blob0ab6e10b0e5e665a9ba67905b407b1ae36cb5d48
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
6 /**
7 * Gets some core libraries
8 */
9 require_once('./libraries/common.lib.php');
10 require_once('./libraries/relation.lib.php'); // foreign keys
11 require_once('./libraries/mysql_charsets.lib.php');
13 if ( $GLOBALS['cfg']['PropertiesIconic'] == true ) {
14 $titles['Browse'] =
15 '<img class="icon" width="16" height="16" src="' . $pmaThemeImage
16 .'b_browse.png" alt="' . $strBrowseForeignValues . '" title="'
17 .$strBrowseForeignValues . '" />';
19 if ($GLOBALS['cfg']['PropertiesIconic'] === 'both') {
20 $titles['Browse'] .= $strBrowseForeignValues;
22 } else {
23 $titles['Browse'] = $strBrowseForeignValues;
26 /**
27 * Not selection yet required -> displays the selection form
29 if (!isset($param) || $param[0] == '') {
30 // Gets some core libraries
31 require_once('./libraries/tbl_properties_common.php');
32 //$err_url = 'tbl_select.php' . $err_url;
33 $url_query .= '&amp;goto=tbl_select.php&amp;back=tbl_select.php';
35 /**
36 * Gets tables informations
38 require_once('./libraries/tbl_properties_table_info.inc.php');
40 /**
41 * Displays top menu links
43 require_once('./libraries/tbl_properties_links.inc.php');
45 if (!isset($goto)) {
46 $goto = $GLOBALS['cfg']['DefaultTabTable'];
48 // Defines the url to return to in case of error in the next sql statement
49 $err_url = $goto . '?' . PMA_generate_common_url($db, $table);
51 // Gets the list and number of fields
52 $result = PMA_DBI_query('SHOW' . (PMA_MYSQL_INT_VERSION >= 40100 ? ' FULL' : '') . ' FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE);
53 $fields_cnt = PMA_DBI_num_rows($result);
54 // rabue: we'd better ensure, that all arrays are empty.
55 $fields_list = $fields_null = $fields_type = $fields_collation = array();
56 while ($row = PMA_DBI_fetch_assoc($result)) {
57 $fields_list[] = $row['Field'];
58 $type = $row['Type'];
59 // reformat mysql query output - staybyte - 9. June 2001
60 if (strncasecmp($type, 'set', 3) == 0
61 || strncasecmp($type, 'enum', 4) == 0) {
62 $type = str_replace(',', ', ', $type);
63 } else {
65 // strip the "BINARY" attribute, except if we find "BINARY(" because
66 // this would be a BINARY or VARBINARY field type
67 if (!preg_match('@BINARY[\(]@i', $type)) {
68 $type = preg_replace('@BINARY@i', '', $type);
70 $type = preg_replace('@ZEROFILL@i', '', $type);
71 $type = preg_replace('@UNSIGNED@i', '', $type);
73 $type = strtolower($type);
75 if (empty($type)) {
76 $type = '&nbsp;';
78 $fields_null[] = $row['Null'];
79 $fields_type[] = $type;
80 $fields_collation[] = PMA_MYSQL_INT_VERSION >= 40100 && !empty($row['Collation']) && $row['Collation'] != 'NULL'
81 ? $row['Collation']
82 : '';
83 } // end while
84 PMA_DBI_free_result($result);
85 unset($result, $type);
87 // <markus@noga.de>
88 // retrieve keys into foreign fields, if any
89 $cfgRelation = PMA_getRelationsParam();
90 // check also foreigners even if relwork is FALSE (to get
91 // foreign keys from innodb)
92 //$foreigners = ($cfgRelation['relwork'] ? PMA_getForeigners($db, $table) : FALSE);
93 $foreigners = PMA_getForeigners($db, $table);
95 <script type="text/javascript" language="javascript">
96 // <![CDATA[
97 function PMA_tbl_select_operator(f, index, multiple) {
98 switch (f.elements["func[" + index + "]"].options[f.elements["func[" + index + "]"].selectedIndex].value) {
99 <?php
100 reset( $GLOBALS['cfg']['UnaryOperators'] );
101 while (list($operator) = each($GLOBALS['cfg']['UnaryOperators'])) {
102 echo ' case "' . $operator . "\":\r\n";
105 bDisabled = true;
106 break;
108 default:
109 bDisabled = false;
111 f.elements["fields[" + index + "]" + ((multiple) ? "[]": "")].disabled = bDisabled;
113 // ]]>
114 </script>
115 <form method="post" action="tbl_select.php" name="insertForm">
116 <?php echo PMA_generate_common_hidden_inputs($db, $table); ?>
117 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
118 <input type="hidden" name="back" value="tbl_select.php" />
120 <fieldset id="fieldset_table_search">
122 <fieldset id="fieldset_select_fields">
123 <legend><?php echo $strSelectFields; ?></legend>
124 <select name="param[]" size="<?php echo min($fields_cnt, 10); ?>"
125 multiple="multiple">
126 <?php
127 // Displays the list of the fields
128 foreach ( $fields_list as $each_field ) {
129 echo ' '
130 .'<option value="' . htmlspecialchars( $each_field ) . '"'
131 .' selected="selected">' . htmlspecialchars( $each_field )
132 .'</option>' . "\n";
135 </select>
136 <input type="checkbox" name="distinct" value="DISTINCT" id="oDistinct" />
137 <label for="oDistinct">DISTINCT</label>
138 </fieldset>
140 <fieldset id="fieldset_limit_rows">
141 <legend><?php echo $strLimitNumRows; ?></legend>
142 <input type="text" size="4" name="session_max_rows"
143 value="<?php echo $GLOBALS['cfg']['MaxRows']; ?>" class="textfield" />
144 </fieldset>
146 <fieldset id="fieldset_display_order">
147 <legend><?php echo $strDisplayOrder; ?></legend>
148 <select name="orderField" style="vertical-align: middle">
149 <option value="--nil--"></option>
150 <?php
151 foreach ( $fields_list as $each_field ) {
152 echo ' '
153 .'<option value="' . htmlspecialchars( $each_field ) . '">'
154 .htmlspecialchars( $each_field ) . '</option>' . "\n";
155 } // end for
157 </select>
159 <div class="formelement">
160 <input type="radio" name="order" value="ASC" checked="checked" id="sortASC" />
161 <label for="sortASC"><?php echo $strAscending; ?></label>
162 </div>
164 <div class="formelement">
165 <input type="radio" name="order" value="DESC" id="sortDESC" />
166 <label for="sortDESC"><?php echo $strDescending; ?></label>
167 </div>
168 </fieldset>
170 <br class="clearfloat" />
171 <?php echo $strAddSearchConditions; ?>
172 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'Functions'); ?>
174 <input type="text" name="where" class="textfield" size="64" />
176 </fieldset>
177 <fieldset class="tblFooters">
178 <input type="submit" name="submit" value="<?php echo $strGo; ?>" />
179 </fieldset>
181 <fieldset id="fieldset_table_qbe">
182 <legend><?php echo '<em>' . $strOr . '</em> ' . $strDoAQuery; ?></legend>
183 <table class="data">
184 <thead>
185 <tr><th><?php echo $strField; ?></th>
186 <th><?php echo $strType; ?></th>
187 <?php echo PMA_MYSQL_INT_VERSION >= 40100 ? '<th>' . $strCollation . '</th>' . "\n" : ''; ?>
188 <th><?php echo $strOperator; ?></th>
189 <th><?php echo $strValue; ?></th>
190 </tr>
191 </thead>
192 <tbody>
193 <?php
194 $odd_row = true;
196 <script type="text/javascript" language="javascript" src="./js/tbl_change.js"></script>
197 <?php
198 for ($i = 0; $i < $fields_cnt; $i++) {
200 <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
201 <th><?php echo htmlspecialchars($fields_list[$i]); ?></th>
202 <td><?php echo $fields_type[$i]; ?></td>
203 <?php echo PMA_MYSQL_INT_VERSION >= 40100 ? '<td>'
204 . $fields_collation[$i] . '</td>' . "\n" : ''; ?>
205 <td><select name="func[]">
206 <?php
207 if (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
208 foreach ($GLOBALS['cfg']['EnumOperators'] as $fc) {
209 echo "\n" . ' '
210 . '<option value="' . htmlspecialchars($fc) . '">'
211 . htmlspecialchars($fc) . '</option>';
213 } elseif (preg_match('@char|blob|text|set@i', $fields_type[$i])) {
214 foreach ($GLOBALS['cfg']['TextOperators'] as $fc) {
215 echo "\n" . ' '
216 . '<option value="' . htmlspecialchars($fc) . '">'
217 . htmlspecialchars($fc) . '</option>';
219 } else {
220 foreach ($GLOBALS['cfg']['NumOperators'] as $fc) {
221 echo "\n" . ' '
222 . '<option value="' . htmlspecialchars($fc) . '">'
223 . htmlspecialchars($fc) . '</option>';
225 } // end if... else...
226 if ($fields_null[$i]) {
227 foreach ($GLOBALS['cfg']['NullOperators'] as $fc) {
228 echo "\n" . ' '
229 . '<option value="' . htmlspecialchars($fc) . '">'
230 . htmlspecialchars($fc) . '</option>';
235 </select>
236 </td>
237 <td>
238 <?php
239 // <markus@noga.de>
240 $field = $fields_list[$i];
242 // do not use require_once here
243 require('./libraries/get_foreign.lib.php');
245 // we got a bug report: in some cases, even if $disp is true,
246 // there are no rows, so we add a fetch_array
248 if ($foreigners && isset($foreigners[$field]) && isset($disp_row) && is_array($disp_row)) {
249 // f o r e i g n k e y s
250 echo ' <select name="fields[' . $i . ']">' . "\n";
251 // go back to first row
253 // here, the 4th parameter is empty because there is no current
254 // value of data for the dropdown (the search page initial values
255 // are displayed empty)
256 echo PMA_foreignDropdown($disp_row, $foreign_field, $foreign_display,
257 '', $GLOBALS['cfg']['ForeignKeyMaxLimit']);
258 echo ' </select>' . "\n";
259 } elseif (isset($foreign_link) && $foreign_link == true) {
261 <input type="text" name="fields[<?php echo $i; ?>]"
262 id="field_<?php echo md5($field); ?>[<?php echo $i; ?>]"
263 class="textfield" />
264 <script type="text/javascript" language="javascript">
265 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>');
266 </script>
267 <?php
268 } elseif (strncasecmp($fields_type[$i], 'enum', 4) == 0) {
269 // e n u m s
270 $enum_value=explode(', ', str_replace("'", '', substr($fields_type[$i], 5, -1)));
271 $cnt_enum_value = count($enum_value);
272 echo ' <select name="fields[' . $i . '][]"'
273 .' multiple="multiple" size="' . min(3, $cnt_enum_value) . '">' . "\n";
274 for ($j = 0; $j < $cnt_enum_value; $j++) {
275 echo ' <option value="' . $enum_value[$j] . '">'
276 . $enum_value[$j] . '</option>';
277 } // end for
278 echo ' </select>' . "\n";
279 } else {
280 // o t h e r c a s e s
281 echo ' <input type="text" name="fields[' . $i . ']"'
282 .' size="40" class="textfield" id="field_' . $i . '" />' . "\n";
284 $type = $fields_type[$i];
285 if ($type == 'date' || $type == 'datetime' || substr($type, 0, 9) == 'timestamp') {
287 <script type="text/javascript" language="javascript">
288 //<![CDATA[
289 document.write('<a title="<?php echo $strCalendar;?>" href="javascript:openCalendar(\'<?php echo PMA_generate_common_url();?>\', \'insertForm\', \'field_<?php echo ($i); ?>\', \'<?php echo (PMA_MYSQL_INT_VERSION >= 40100 && 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>');
290 //]]>
291 </script>
292 <?php
295 <input type="hidden" name="names[<?php echo $i; ?>]"
296 value="<?php echo htmlspecialchars($fields_list[$i]); ?>" />
297 <input type="hidden" name="types[<?php echo $i; ?>]"
298 value="<?php echo $fields_type[$i]; ?>" />
299 <input type="hidden" name="collations[<?php echo $i; ?>]"
300 value="<?php echo $fields_collation[$i]; ?>" />
301 </td>
302 </tr>
303 <?php
304 } // end for
306 </tbody>
307 </table>
308 </fieldset>
309 <fieldset class="tblFooters">
310 <input type="hidden" name="max_number_of_fields"
311 value="<?php echo $fields_cnt; ?>" />
312 <input type="submit" name="submit" value="<?php echo $strGo; ?>" />
313 </fieldset>
314 </form>
315 <?php
316 require_once('./libraries/footer.inc.php');
321 * Selection criteria have been submitted -> do the work
323 else {
324 // Builds the query
326 $sql_query = 'SELECT ' . (isset($distinct) ? 'DISTINCT ' : '');
328 // if all fields were selected to display, we do a SELECT *
329 // (more efficient and this helps prevent a problem in IE
330 // if one of the rows is edited and we come back to the Select results)
332 if (count($param) == $max_number_of_fields) {
333 $sql_query .= '* ';
334 } else {
335 $param = PMA_backquote( $param );
336 $sql_query .= implode( ', ', $param );
337 unset( $param );
338 } // end if
340 $sql_query .= ' FROM ' . PMA_backquote($table);
342 // The where clause
343 if (trim($where) != '') {
344 $sql_query .= ' WHERE ' . $where;
345 } else {
346 $w = $charsets = array();
347 $cnt_func = count($func);
348 reset($func);
349 while (list($i, $func_type) = each($func)) {
350 if (PMA_MYSQL_INT_VERSION >= 40100) {
351 list($charsets[$i]) = explode('_', $collations[$i]);
353 if (@$GLOBALS['cfg']['UnaryOperators'][$func_type] == 1) {
354 $fields[$i] = '';
355 $w[] = PMA_backquote(urldecode($names[$i])) . ' ' . $func_type;
357 } elseif (strncasecmp($types[$i], 'enum', 4) == 0) {
358 if (!empty($fields[$i])) {
359 if (!is_array($fields[$i])) {
360 $fields[$i] = explode(',', $fields[$i]);
362 $enum_selected_count = count($fields[$i]);
363 if ($func_type == '=' && $enum_selected_count > 1) {
364 $func_type = $func[$i] = 'IN';
365 $parens_open = '(';
366 $parens_close = ')';
368 } elseif ($func_type == '!=' && $enum_selected_count > 1) {
369 $func_type = $func[$i] = 'NOT IN';
370 $parens_open = '(';
371 $parens_close = ')';
373 } else {
374 $parens_open = '';
375 $parens_close = '';
377 $enum_where = '\'' . PMA_sqlAddslashes($fields[$i][0]) . '\'';
378 if (PMA_MYSQL_INT_VERSION >= 40100 && $charsets[$i] != $charset_connection) {
379 $enum_where = 'CONVERT(_utf8 ' . $enum_where . ' USING ' . $charsets[$i] . ') COLLATE ' . $collations[$i];
381 for ($e = 1; $e < $enum_selected_count; $e++) {
382 $enum_where .= ', ';
383 $tmp_literal = '\'' . PMA_sqlAddslashes($fields[$i][$e]) . '\'';
384 if (PMA_MYSQL_INT_VERSION >= 40100 && $charsets[$i] != $charset_connection) {
385 $tmp_literal = 'CONVERT(_utf8 ' . $tmp_literal . ' USING ' . $charsets[$i] . ') COLLATE ' . $collations[$i];
387 $enum_where .= $tmp_literal;
388 unset($tmp_literal);
391 $w[] = PMA_backquote(urldecode($names[$i])) . ' ' . $func_type . ' ' . $parens_open . $enum_where . $parens_close;
394 } elseif ($fields[$i] != '') {
395 // For these types we quote the value. Even if it's another type (like INT),
396 // for a LIKE we always quote the value. MySQL converts strings to numbers
397 // and numbers to strings as necessary during the comparison
398 if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types[$i]) || strpos(' ' . $func_type, 'LIKE')) {
399 $quot = '\'';
400 } else {
401 $quot = '';
404 // Make query independant from the selected connection charset.
405 // But if the field's type is VARBINARY, it has no charset
406 // and $charsets[$i] is empty, so we cannot generate a CONVERT
408 if (PMA_MYSQL_INT_VERSION >= 40101 && !empty($charsets[$i]) && $charsets[$i] != $charset_connection && preg_match('@char|binary|blob|text|set@i', $types[$i])) {
409 $prefix = 'CONVERT(_utf8 ';
410 $suffix = ' USING ' . $charsets[$i] . ') COLLATE ' . $collations[$i];
411 } else {
412 $prefix = $suffix = '';
415 // LIKE %...%
416 if ($func_type == 'LIKE %...%') {
417 $func_type = 'LIKE';
418 $fields[$i] = '%' . $fields[$i] . '%';
420 $w[] = PMA_backquote(urldecode($names[$i])) . ' ' . $func_type . ' ' . $prefix . $quot . PMA_sqlAddslashes($fields[$i]) . $quot . $suffix;
422 } // end if
423 } // end for
425 if ($w) {
426 $sql_query .= ' WHERE ' . implode(' AND ', $w);
428 } // end if
430 if ($orderField != '--nil--') {
431 $sql_query .= ' ORDER BY ' . PMA_backquote(urldecode($orderField)) . ' ' . $order;
432 } // end if
433 include('./sql.php');