bugfixes: allow to work with multiple servers, fix boolean values in select fields
[phpmyadmin.git] / libraries / sql_query_form.lib.php
blob6a4b6e9a1c27e995a7b601dd9b90dfd011df27d7
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * functions for displaying the sql query form
6 * @version $Id$
7 * @usedby server_sql.php
8 * @usedby db_sql.php
9 * @usedby tbl_sql.php
10 * @usedby tbl_structure.php
11 * @usedby tbl_tracking.php
12 * @usedby querywindow.php
13 * @package phpMyAdmin
15 if (! defined('PHPMYADMIN')) {
16 exit;
19 /**
22 require_once './libraries/file_listing.php'; // used for file listing
23 require_once './libraries/bookmark.lib.php'; // used for file listing
25 /**
26 * prints the sql query boxes
28 * @usedby server_sql.php
29 * @usedby db_sql.php
30 * @usedby tbl_sql.php
31 * @usedby tbl_structure.php
32 * @usedby tbl_tracking.php
33 * @usedby querywindow.php
34 * @uses $GLOBALS['table']
35 * @uses $GLOBALS['db']
36 * @uses $GLOBALS['server']
37 * @uses $GLOBALS['goto']
38 * @uses $GLOBALS['is_upload'] from common.inc.php
39 * @uses $GLOBALS['sql_query'] from grab_globals.lib.php
40 * @uses $GLOBALS['cfg']['DefaultQueryTable']
41 * @uses $GLOBALS['cfg']['DefaultQueryDatabase']
42 * @uses $GLOBALS['cfg']['Servers']
43 * @uses $GLOBALS['cfg']['DefaultTabDatabase']
44 * @uses $GLOBALS['cfg']['DefaultQueryDatabase']
45 * @uses $GLOBALS['cfg']['DefaultQueryTable']
46 * @uses $GLOBALS['cfg']['Bookmark']
47 * @uses __('Your SQL query has been executed successfully')
48 * @uses PMA_generate_common_url()
49 * @uses PMA_backquote()
50 * @uses PMA_DBI_fetch_result()
51 * @uses PMA_showMySQLDocu()
52 * @uses PMA_generate_common_hidden_inputs()
53 * @uses PMA_sqlQueryFormBookmark()
54 * @uses PMA_sqlQueryFormInsert()
55 * @uses PMA_sqlQueryFormUpload()
56 * @uses PMA_DBI_QUERY_STORE
57 * @uses PMA_set_enc_form()
58 * @uses sprintf()
59 * @uses htmlspecialchars()
60 * @uses str_replace()
61 * @uses md5()
62 * @uses function_exists()
63 * @param boolean|string $query query to display in the textarea
64 * or true to display last executed
65 * @param boolean|string $display_tab sql|files|history|full|FALSE
66 * what part to display
67 * false if not inside querywindow
68 * @param string $delimiter
70 function PMA_sqlQueryForm($query = true, $display_tab = false, $delimiter = ';')
72 // check tab to display if inside querywindow
73 if (! $display_tab) {
74 $display_tab = 'full';
75 $is_querywindow = false;
76 } else {
77 $is_querywindow = true;
80 // query to show
81 if (true === $query) {
82 $query = $GLOBALS['sql_query'];
85 // set enctype to multipart for file uploads
86 if ($GLOBALS['is_upload']) {
87 $enctype = ' enctype="multipart/form-data"';
88 } else {
89 $enctype = '';
92 $table = '';
93 $db = '';
94 if (! strlen($GLOBALS['db'])) {
95 // prepare for server related
96 $goto = empty($GLOBALS['goto']) ?
97 'server_sql.php' : $GLOBALS['goto'];
98 } elseif (! strlen($GLOBALS['table'])) {
99 // prepare for db related
100 $db = $GLOBALS['db'];
101 $goto = empty($GLOBALS['goto']) ?
102 'db_sql.php' : $GLOBALS['goto'];
103 } else {
104 $table = $GLOBALS['table'];
105 $db = $GLOBALS['db'];
106 $goto = empty($GLOBALS['goto']) ?
107 'tbl_sql.php' : $GLOBALS['goto'];
111 // start output
112 if ($is_querywindow) {
114 <form method="post" id="sqlqueryform" target="frame_content"
115 action="import.php"<?php echo $enctype; ?> name="sqlform"
116 onsubmit="var save_name = window.opener.parent.frame_content.name;
117 window.opener.parent.frame_content.name = save_name + '<?php echo time(); ?>';
118 this.target = window.opener.parent.frame_content.name;
119 return checkSqlQuery(this)">
120 <?php
121 } else {
122 echo '<form method="post" action="import.php" ' . $enctype . ' id="sqlqueryform"'
123 .' onsubmit="return checkSqlQuery(this)" name="sqlform">' . "\n";
126 if ($is_querywindow) {
127 echo '<input type="hidden" name="focus_querywindow" value="true" />'
128 ."\n";
129 if ($display_tab != 'sql' && $display_tab != 'full') {
130 echo '<input type="hidden" name="sql_query" value="" />' . "\n";
131 echo '<input type="hidden" name="show_query" value="1" />' . "\n";
134 echo '<input type="hidden" name="is_js_confirmed" value="0" />' . "\n"
135 .PMA_generate_common_hidden_inputs($db, $table) . "\n"
136 .'<input type="hidden" name="pos" value="0" />' . "\n"
137 .'<input type="hidden" name="goto" value="'
138 .htmlspecialchars($goto) . '" />' . "\n"
139 .'<input type="hidden" name="zero_rows" value="'
140 . htmlspecialchars(__('Your SQL query has been executed successfully')) . '" />' . "\n"
141 .'<input type="hidden" name="prev_sql_query" value="'
142 . htmlspecialchars($query) . '" />' . "\n";
144 // display querybox
145 if ($display_tab === 'full' || $display_tab === 'sql') {
146 PMA_sqlQueryFormInsert($query, $is_querywindow, $delimiter);
149 // display uploads
150 if ($display_tab === 'files' && $GLOBALS['is_upload']) {
151 PMA_sqlQueryFormUpload();
154 // Bookmark Support
155 if ($display_tab === 'full' || $display_tab === 'history') {
156 if (! empty($GLOBALS['cfg']['Bookmark'])) {
157 PMA_sqlQueryFormBookmark();
161 // Encoding setting form appended by Y.Kawada
162 if (function_exists('PMA_set_enc_form')) {
163 echo PMA_set_enc_form(' ');
166 echo '</form>' . "\n";
167 if ($is_querywindow) {
169 <script type="text/javascript">
170 //<![CDATA[
171 if (window.opener) {
172 window.opener.parent.insertQuery();
174 //]]>
175 </script>
176 <?php
181 * prints querybox fieldset
183 * @usedby PMA_sqlQueryForm()
184 * @uses $GLOBALS['text_dir']
185 * @uses $GLOBALS['cfg']['TextareaAutoSelect']
186 * @uses $GLOBALS['cfg']['TextareaCols']
187 * @uses $GLOBALS['cfg']['TextareaRows']
188 * @uses __(' Show this query here again ')
189 * @uses __('Go')
190 * @uses PMA_USR_OS
191 * @uses PMA_USR_BROWSER_AGENT
192 * @uses PMA_USR_BROWSER_VER
193 * @uses htmlspecialchars()
194 * @param string $query query to display in the textarea
195 * @param boolean $is_querywindow if inside querywindow or not
196 * @param string $delimiter default delimiter to use
198 function PMA_sqlQueryFormInsert($query = '', $is_querywindow = false, $delimiter = ';')
201 // enable auto select text in textarea
202 if ($GLOBALS['cfg']['TextareaAutoSelect']) {
203 $auto_sel = ' onfocus="selectContent(this, sql_box_locked, true)"';
204 } else {
205 $auto_sel = '';
208 // enable locking if inside query window
209 if ($is_querywindow) {
210 $locking = ' onkeypress="document.sqlform.elements[\'LockFromUpdate\'].'
211 .'checked = true;"';
212 $height = $GLOBALS['cfg']['TextareaRows'] * 1.25;
213 } else {
214 $locking = '';
215 $height = $GLOBALS['cfg']['TextareaRows'] * 2;
218 $table = '';
219 $db = '';
220 $fields_list = array();
221 if (! strlen($GLOBALS['db'])) {
222 // prepare for server related
223 $legend = sprintf(__('Run SQL query/queries on server %s'),
224 '&quot;' . htmlspecialchars(
225 ! empty($GLOBALS['cfg']['Servers'][$GLOBALS['server']]['verbose']) ? $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['verbose'] : $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['host']) . '&quot;');
226 } elseif (! strlen($GLOBALS['table'])) {
227 // prepare for db related
228 $db = $GLOBALS['db'];
229 // if you want navigation:
230 $tmp_db_link = '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase']
231 . '?' . PMA_generate_common_url($db) . '"';
232 if ($is_querywindow) {
233 $tmp_db_link .= ' target="_self"'
234 . ' onclick="this.target=window.opener.frame_content.name"';
236 $tmp_db_link .= '>'
237 . htmlspecialchars($db) . '</a>';
238 // else use
239 // $tmp_db_link = htmlspecialchars($db);
240 $legend = sprintf(__('Run SQL query/queries on database %s'), $tmp_db_link);
241 if (empty($query)) {
242 $query = str_replace('%d',
243 PMA_backquote($db), $GLOBALS['cfg']['DefaultQueryDatabase']);
245 } else {
246 $table = $GLOBALS['table'];
247 $db = $GLOBALS['db'];
248 // Get the list and number of fields
249 // we do a try_query here, because we could be in the query window,
250 // trying to synchonize and the table has not yet been created
251 $fields_list = PMA_DBI_fetch_result(
252 'SHOW FULL COLUMNS FROM ' . PMA_backquote($db)
253 . '.' . PMA_backquote($GLOBALS['table']));
255 $tmp_db_link = '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase']
256 . '?' . PMA_generate_common_url($db) . '"';
257 if ($is_querywindow) {
258 $tmp_db_link .= ' target="_self"'
259 . ' onclick="this.target=window.opener.frame_content.name"';
261 $tmp_db_link .= '>'
262 . htmlspecialchars($db) . '</a>';
263 // else use
264 // $tmp_db_link = htmlspecialchars($db);
265 $legend = sprintf(__('Run SQL query/queries on database %s'), $tmp_db_link);
266 if (empty($query) && count($fields_list)) {
267 $field_names = array();
268 foreach ($fields_list as $field) {
269 $field_names[] = PMA_backquote($field['Field']);
271 $query =
272 str_replace('%d', PMA_backquote($db),
273 str_replace('%t', PMA_backquote($table),
274 str_replace('%f',
275 implode(', ', $field_names),
276 $GLOBALS['cfg']['DefaultQueryTable'])));
277 unset($field_names);
280 $legend .= ': ' . PMA_showMySQLDocu('SQL-Syntax', 'SELECT');
282 if (count($fields_list)) {
283 $sqlquerycontainer_id = 'sqlquerycontainer';
284 } else {
285 $sqlquerycontainer_id = 'sqlquerycontainerfull';
288 echo '<a name="querybox"></a>' . "\n"
289 .'<div id="queryboxcontainer">' . "\n"
290 .'<fieldset id="querybox">' . "\n";
291 echo '<legend>' . $legend . '</legend>' . "\n";
292 echo '<div id="queryfieldscontainer">' . "\n";
293 echo '<div id="' . $sqlquerycontainer_id . '">' . "\n"
294 .'<textarea name="sql_query" id="sqlquery"'
295 .' cols="' . $GLOBALS['cfg']['TextareaCols'] . '"'
296 .' rows="' . $height . '"'
297 .' dir="' . $GLOBALS['text_dir'] . '"'
298 .$auto_sel . $locking . '>' . htmlspecialchars($query) . '</textarea>' . "\n";
299 // Add buttons to generate query easily for select all,single select,insert,update and delete
300 if(count($fields_list)) {
301 echo '<input type="button" value="SELECT *" id="selectall" class="sqlbutton" />';
302 echo '<input type="button" value="SELECT" id="select" class="sqlbutton" />';
303 echo '<input type="button" value="INSERT" id="insert" class="sqlbutton" />';
304 echo '<input type="button" value="UPDATE" id="update" class="sqlbutton" />';
305 echo '<input type="button" value="DELETE" id="delete" class="sqlbutton" />';
307 echo '</div>' . "\n";
309 echo '<script type="text/javascript">' . "\n"
310 .'//<![CDATA[' . "\n"
311 .'document.getElementById("sqlquery").focus();' . "\n"
312 .'//]]>' . "\n"
313 .'</script>' . "\n";
315 if (count($fields_list)) {
316 echo '<div id="tablefieldscontainer">' . "\n"
317 .'<label>' . __('Columns') . '</label>' . "\n"
318 .'<select id="tablefields" name="dummy" '
319 .'size="' . ($GLOBALS['cfg']['TextareaRows'] - 2) . '" '
320 .'multiple="multiple" ondblclick="insertValueQuery()">' . "\n";
321 foreach ($fields_list as $field) {
322 echo '<option value="'
323 .PMA_backquote(htmlspecialchars($field['Field'])) . '"';
324 if (isset($field['Field']) && strlen($field['Field']) && isset($field['Comment'])) {
325 echo ' title="' . htmlspecialchars($field['Comment']) . '"';
327 echo '>' . htmlspecialchars($field['Field']) . '</option>' . "\n";
329 echo '</select>' . "\n"
330 .'<div id="tablefieldinsertbuttoncontainer">' . "\n";
331 if ($GLOBALS['cfg']['PropertiesIconic']) {
332 echo '<input type="button" name="insert" value="&lt;&lt;"'
333 .' onclick="insertValueQuery()"'
334 .' title="' . __('Insert') . '" />' . "\n";
335 } else {
336 echo '<input type="button" name="insert"'
337 .' value="' . __('Insert') . '"'
338 .' onclick="insertValueQuery()" />' . "\n";
340 echo '</div>' . "\n"
341 .'</div>' . "\n";
344 echo '<div class="clearfloat"></div>' . "\n";
345 echo '</div>' . "\n";
347 if (! empty($GLOBALS['cfg']['Bookmark'])) {
349 <div id="bookmarkoptions">
350 <div class="formelement">
351 <label for="bkm_label">
352 <?php echo __('Bookmark this SQL query'); ?>:</label>
353 <input type="text" name="bkm_label" id="bkm_label" value="" />
354 </div>
355 <div class="formelement">
356 <input type="checkbox" name="bkm_all_users" id="id_bkm_all_users"
357 value="true" />
358 <label for="id_bkm_all_users">
359 <?php echo __('Let every user access this bookmark'); ?></label>
360 </div>
361 <div class="formelement">
362 <input type="checkbox" name="bkm_replace" id="id_bkm_replace"
363 value="true" />
364 <label for="id_bkm_replace">
365 <?php echo __('Replace existing bookmark of same name'); ?></label>
366 </div>
367 </div>
368 <?php
371 echo '<div class="clearfloat"></div>' . "\n";
372 echo '</fieldset>' . "\n"
373 .'</div>' . "\n";
375 echo '<fieldset id="queryboxfooter" class="tblFooters">' . "\n";
376 echo '<div class="formelement">' . "\n";
377 if ($is_querywindow) {
379 <script type="text/javascript">
380 //<![CDATA[
381 document.writeln(' <input type="checkbox" name="LockFromUpdate" value="1" id="checkbox_lock" /> <label for="checkbox_lock"><?php echo __('Do not overwrite this query from outside the window'); ?></label> ');
382 //]]>
383 </script>
384 <?php
386 echo '</div>' . "\n";
387 echo '<div class="formelement">' . "\n";
388 echo '<label for="id_sql_delimiter">[ ' . __('Delimiter')
389 .'</label>' . "\n";
390 echo '<input type="text" name="sql_delimiter" size="3" '
391 .'value="' . $delimiter . '" '
392 .'id="id_sql_delimiter" /> ]' . "\n";
394 echo '<input type="checkbox" name="show_query" value="1" '
395 .'id="checkbox_show_query" checked="checked" />' . "\n"
396 .'<label for="checkbox_show_query">' . __(' Show this query here again ')
397 .'</label>' . "\n";
399 echo '</div>' . "\n";
400 echo '<input type="submit" name="SQL" value="' . __('Go') . '" />'
401 ."\n";
402 echo '<div class="clearfloat"></div>' . "\n";
403 echo '</fieldset>' . "\n";
407 * prints bookmark fieldset
409 * @usedby PMA_sqlQueryForm()
410 * @uses PMA_Bookmark_getList()
411 * @uses $GLOBALS['db']
412 * @uses $GLOBALS['pmaThemeImage']
413 * @uses $GLOBALS['cfg']['ReplaceHelpImg']
414 * @uses __('Bookmarked SQL query')
415 * @uses __('View only')
416 * @uses __('Delete')
417 * @uses __('Documentation')
418 * @uses __('Go')
419 * @uses __('Submit')
420 * @uses __('Variable')
421 * @uses count()
422 * @uses htmlspecialchars()
424 function PMA_sqlQueryFormBookmark()
426 $bookmark_list = PMA_Bookmark_getList($GLOBALS['db']);
427 if (! $bookmark_list || count($bookmark_list) < 1) {
428 return;
431 echo '<fieldset id="bookmarkoptions">';
432 echo '<legend>';
433 echo __('Bookmarked SQL query') . '</legend>' . "\n";
434 echo '<div class="formelement">';
435 echo '<select name="id_bookmark">' . "\n";
436 echo '<option value="">&nbsp;</option>' . "\n";
437 foreach ($bookmark_list as $key => $value) {
438 echo '<option value="' . htmlspecialchars($key) . '">'
439 .htmlspecialchars($value) . '</option>' . "\n";
441 // &nbsp; is required for correct display with styles/line height
442 echo '</select>&nbsp;' . "\n";
443 echo '</div>' . "\n";
444 echo '<div class="formelement">' . "\n";
445 echo __('Variable');
446 if ($GLOBALS['cfg']['ReplaceHelpImg']) {
447 echo ' <a href="./Documentation.html#faqbookmark"'
448 .' target="documentation">'
449 .'<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_help.png"'
450 .' border="0" width="11" height="11" align="middle"'
451 .' alt="' . __('Documentation') . '" /></a> ';
452 } else {
453 echo ' (<a href="./Documentation.html#faqbookmark"'
454 .' target="documentation">' . __('Documentation') . '</a>): ';
456 echo '<input type="text" name="bookmark_variable" class="textfield"'
457 .' size="10" />' . "\n";
458 echo '</div>' . "\n";
459 echo '<div class="formelement">' . "\n";
460 echo '<input type="radio" name="action_bookmark" value="0"'
461 .' id="radio_bookmark_exe" checked="checked" />'
462 .'<label for="radio_bookmark_exe">' . __('Submit')
463 .'</label>' . "\n";
464 echo '<input type="radio" name="action_bookmark" value="1"'
465 .' id="radio_bookmark_view" />'
466 .'<label for="radio_bookmark_view">' . __('View only')
467 .'</label>' . "\n";
468 echo '<input type="radio" name="action_bookmark" value="2"'
469 .' id="radio_bookmark_del" />'
470 .'<label for="radio_bookmark_del">' . __('Delete')
471 .'</label>' . "\n";
472 echo '</div>' . "\n";
473 echo '<div class="clearfloat"></div>' . "\n";
474 echo '</fieldset>' . "\n";
476 echo '<fieldset id="bookmarkoptionsfooter" class="tblFooters">' . "\n";
477 echo '<input type="submit" name="SQL" value="' . __('Go') . '" />';
478 echo '<div class="clearfloat"></div>' . "\n";
479 echo '</fieldset>' . "\n";
483 * prints bookmark fieldset
485 * @usedby PMA_sqlQueryForm()
486 * @uses $GLOBALS['cfg']['GZipDump']
487 * @uses $GLOBALS['cfg']['BZipDump']
488 * @uses $GLOBALS['cfg']['UploadDir']
489 * @uses $GLOBALS['cfg']['AvailableCharsets']
490 * @uses $GLOBALS['cfg']['AllowAnywhereRecoding']
491 * @uses __('bzipped')
492 * @uses __('Character set of the file:')
493 * @uses __('Compression')
494 * @uses __('Error')
495 * @uses __('Go')
496 * @uses __('gzipped')
497 * @uses __('Location of the text file')
498 * @uses __('web server upload directory')
499 * @uses __('The directory you set for upload work cannot be reached')
500 * @uses $GLOBALS['charset']
501 * @uses $GLOBALS['max_upload_size']
502 * @uses PMA_supportedDecompressions()
503 * @uses PMA_getFileSelectOptions()
504 * @uses PMA_displayMaximumUploadSize()
505 * @uses PMA_generateCharsetDropdownBox()
506 * @uses PMA_generateHiddenMaxFileSize()
507 * @uses PMA_CSDROPDOWN_CHARSET
508 * @uses empty()
510 function PMA_sqlQueryFormUpload(){
511 $errors = array ();
513 $matcher = '@\.sql(\.(' . PMA_supportedDecompressions() . '))?$@'; // we allow only SQL here
515 if (!empty($GLOBALS['cfg']['UploadDir'])) {
516 $files = PMA_getFileSelectOptions(PMA_userDir($GLOBALS['cfg']['UploadDir']), $matcher, (isset($timeout_passed) && $timeout_passed && isset($local_import_file)) ? $local_import_file : '');
517 } else {
518 $files = '';
521 // start output
522 echo '<fieldset id="">';
523 echo '<legend>';
524 echo __('Location of the text file') . '</legend>';
525 echo '<div class="formelement">';
526 echo '<input type="file" name="sql_file" class="textfield" /> ';
527 echo PMA_displayMaximumUploadSize($GLOBALS['max_upload_size']);
528 // some browsers should respect this :)
529 echo PMA_generateHiddenMaxFileSize($GLOBALS['max_upload_size']) . "\n";
530 echo '</div>';
532 if ($files === FALSE) {
533 $errors[] = PMA_Message::error( __('The directory you set for upload work cannot be reached'));
534 } elseif (!empty($files)) {
535 echo '<div class="formelement">';
536 echo '<strong>' . __('web server upload directory') .':</strong>' . "\n";
537 echo '<select size="1" name="sql_localfile">' . "\n";
538 echo '<option value="" selected="selected"></option>' . "\n";
539 echo $files;
540 echo '</select>' . "\n";
541 echo '</div>';
544 echo '<div class="clearfloat"></div>' . "\n";
545 echo '</fieldset>';
548 echo '<fieldset id="" class="tblFooters">';
549 echo __('Character set of the file:') . "\n";
550 echo PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_CHARSET,
551 'charset_of_file', null, 'utf8', FALSE);
552 echo '<input type="submit" name="SQL" value="' . __('Go')
553 .'" />' . "\n";
554 echo '<div class="clearfloat"></div>' . "\n";
555 echo '</fieldset>';
557 foreach ($errors as $error) {
558 $error->display();