2.11.1.2 release
[phpmyadmin/arisferyanto.git] / libraries / sql_query_form.lib.php
blobc4af277c340df30779d81278c7fc49f0ed12ac99
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 querywindow.php
14 /**
17 require_once './libraries/file_listing.php'; // used for file listing
18 require_once './libraries/bookmark.lib.php'; // used for file listing
20 /**
21 * prints the sql query boxes
23 * @usedby server_sql.php
24 * @usedby db_sql.php
25 * @usedby tbl_sql.php
26 * @usedby tbl_structure.php
27 * @usedby querywindow.php
28 * @uses $GLOBALS['table']
29 * @uses $GLOBALS['db']
30 * @uses $GLOBALS['server']
31 * @uses $GLOBALS['goto']
32 * @uses $GLOBALS['is_upload'] from common.inc.php
33 * @uses $GLOBALS['sql_query'] from grab_globals.lib.php
34 * @uses $GLOBALS['cfg']['DefaultQueryTable']
35 * @uses $GLOBALS['cfg']['DefaultQueryDatabase']
36 * @uses $GLOBALS['cfg']['Servers']
37 * @uses $GLOBALS['cfg']['DefaultTabDatabase']
38 * @uses $GLOBALS['cfg']['DefaultQueryDatabase']
39 * @uses $GLOBALS['cfg']['DefaultQueryTable']
40 * @uses $GLOBALS['cfg']['Bookmark']['db']
41 * @uses $GLOBALS['cfg']['Bookmark']['table']
42 * @uses $GLOBALS['strSuccess']
43 * @uses PMA_generate_common_url()
44 * @uses PMA_backquote()
45 * @uses PMA_DBI_fetch_result()
46 * @uses PMA_showMySQLDocu()
47 * @uses PMA_generate_common_hidden_inputs()
48 * @uses PMA_sqlQueryFormBookmark()
49 * @uses PMA_sqlQueryFormInsert()
50 * @uses PMA_sqlQueryFormUpload()
51 * @uses PMA_DBI_QUERY_STORE
52 * @uses PMA_set_enc_form()
53 * @uses sprintf()
54 * @uses htmlspecialchars()
55 * @uses str_replace()
56 * @uses md5()
57 * @uses function_exists()
58 * @param boolean|string $query query to display in the textarea
59 * or true to display last executed
60 * @param boolean|string $display_tab sql|files|history|full|FALSE
61 * what part to display
62 * false if not inside querywindow
63 * @param string $delimiter
65 function PMA_sqlQueryForm($query = true, $display_tab = false, $delimiter = ';')
67 // check tab to display if inside querywindow
68 if (! $display_tab) {
69 $display_tab = 'full';
70 $is_querywindow = false;
71 } else {
72 $is_querywindow = true;
75 // query to show
76 if (true === $query) {
77 $query = $GLOBALS['sql_query'];
80 // set enctype to multipart for file uploads
81 if ($GLOBALS['is_upload']) {
82 $enctype = ' enctype="multipart/form-data"';
83 } else {
84 $enctype = '';
87 $table = '';
88 $db = '';
89 if (! strlen($GLOBALS['db'])) {
90 // prepare for server related
91 $goto = empty($GLOBALS['goto']) ?
92 'server_sql.php' : $GLOBALS['goto'];
93 } elseif (! strlen($GLOBALS['table'])) {
94 // prepare for db related
95 $db = $GLOBALS['db'];
96 $goto = empty($GLOBALS['goto']) ?
97 'db_sql.php' : $GLOBALS['goto'];
98 } else {
99 $table = $GLOBALS['table'];
100 $db = $GLOBALS['db'];
101 $goto = empty($GLOBALS['goto']) ?
102 'tbl_sql.php' : $GLOBALS['goto'];
106 // start output
107 if ($is_querywindow) {
109 <form method="post" id="sqlqueryform" target="frame_content"
110 action="import.php"<?php echo $enctype; ?> name="sqlform"
111 onsubmit="
112 // <![CDATA[
113 var save_name = window.opener.parent.frame_content.name;
114 window.opener.parent.frame_content.name = save_name + '<?php echo time(); ?>';
115 this.target = window.opener.parent.frame_content.name;
116 return checkSqlQuery(this);
117 // ]]" >
118 <?php
119 } else {
120 echo '<form method="post" action="import.php" ' . $enctype . ' id="sqlqueryform"'
121 .' onsubmit="return checkSqlQuery(this)" name="sqlform">' . "\n";
124 if ($is_querywindow) {
125 echo '<input type="hidden" name="focus_querywindow" value="true" />'
126 ."\n";
127 if ($display_tab != 'sql' && $display_tab != 'full') {
128 echo '<input type="hidden" name="sql_query" value="" />' . "\n";
129 echo '<input type="hidden" name="show_query" value="1" />' . "\n";
132 echo '<input type="hidden" name="is_js_confirmed" value="0" />' . "\n"
133 .PMA_generate_common_hidden_inputs($db, $table) . "\n"
134 .'<input type="hidden" name="pos" value="0" />' . "\n"
135 .'<input type="hidden" name="goto" value="'
136 .htmlspecialchars($goto) . '" />' . "\n"
137 .'<input type="hidden" name="zero_rows" value="'
138 . htmlspecialchars($GLOBALS['strSuccess']) . '" />' . "\n"
139 .'<input type="hidden" name="prev_sql_query" value="'
140 . htmlspecialchars($query) . '" />' . "\n";
142 // display querybox
143 if ($display_tab === 'full' || $display_tab === 'sql') {
144 PMA_sqlQueryFormInsert($query, $is_querywindow, $delimiter);
147 // display uploads
148 if ($display_tab === 'files' && $GLOBALS['is_upload']) {
149 PMA_sqlQueryFormUpload();
152 // Bookmark Support
153 if ($display_tab === 'full' || $display_tab === 'history') {
154 if (! empty($GLOBALS['cfg']['Bookmark'])
155 && $GLOBALS['cfg']['Bookmark']['db']
156 && $GLOBALS['cfg']['Bookmark']['table']) {
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 $GLOBALS['strShowThisQuery']
189 * @uses $GLOBALS['strGo']
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($GLOBALS['strRunSQLQueryOnServer'],
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 $strDBLink = '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase']
231 . '?' . PMA_generate_common_url($db) . '"';
232 if ($is_querywindow) {
233 $strDBLink .= ' target="_self"'
234 . ' onclick="this.target=window.opener.frame_content.name"';
236 $strDBLink .= '>'
237 . htmlspecialchars($db) . '</a>';
238 // else use
239 // $strDBLink = htmlspecialchars($db);
240 $legend = sprintf($GLOBALS['strRunSQLQuery'], $strDBLink);
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 $strDBLink = '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase']
256 . '?' . PMA_generate_common_url($db) . '"';
257 if ($is_querywindow) {
258 $strDBLink .= ' target="_self"'
259 . ' onclick="this.target=window.opener.frame_content.name"';
261 $strDBLink .= '>'
262 . htmlspecialchars($db) . '</a>';
263 // else use
264 // $strDBLink = htmlspecialchars($db);
265 $legend = sprintf($GLOBALS['strRunSQLQuery'], $strDBLink);
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 echo '</div>' . "\n";
301 if (count($fields_list)) {
302 echo '<div id="tablefieldscontainer">' . "\n"
303 .'<label>' . $GLOBALS['strFields'] . '</label>' . "\n"
304 .'<select id="tablefields" name="dummy" '
305 .'size="' . ($GLOBALS['cfg']['TextareaRows'] - 2) . '" '
306 .'multiple="multiple" ondblclick="insertValueQuery()">' . "\n";
307 foreach ($fields_list as $field) {
308 echo '<option value="'
309 .PMA_backquote(htmlspecialchars($field['Field'])) . '"';
310 if (isset($field['Field']) && strlen($field['Field']) && isset($field['Comment'])) {
311 echo ' title="' . htmlspecialchars($field['Comment']) . '"';
313 echo '>' . htmlspecialchars($field['Field']) . '</option>' . "\n";
315 echo '</select>' . "\n"
316 .'<div id="tablefieldinsertbuttoncontainer">' . "\n";
317 if ($GLOBALS['cfg']['PropertiesIconic']) {
318 echo '<input type="button" name="insert" value="&lt;&lt;"'
319 .' onclick="insertValueQuery()"'
320 .' title="' . $GLOBALS['strInsert'] . '" />' . "\n";
321 } else {
322 echo '<input type="button" name="insert"'
323 .' value="' . $GLOBALS['strInsert'] . '"'
324 .' onclick="insertValueQuery()" />' . "\n";
326 echo '</div>' . "\n"
327 .'</div>' . "\n";
330 echo '<div class="clearfloat"></div>' . "\n";
331 echo '</div>' . "\n";
333 if (! empty($GLOBALS['cfg']['Bookmark'])
334 && $GLOBALS['cfg']['Bookmark']['db']
335 && $GLOBALS['cfg']['Bookmark']['table']) {
337 <div id="bookmarkoptions">
338 <div class="formelement">
339 <label for="bkm_label">
340 <?php echo $GLOBALS['strBookmarkThis']; ?>:</label>
341 <input type="text" name="bkm_label" id="bkm_label" value="" />
342 </div>
343 <div class="formelement">
344 <input type="checkbox" name="bkm_all_users" id="id_bkm_all_users"
345 value="true" />
346 <label for="id_bkm_all_users">
347 <?php echo $GLOBALS['strBookmarkAllUsers']; ?></label>
348 </div>
349 <div class="formelement">
350 <input type="checkbox" name="bkm_replace" id="id_bkm_replace"
351 value="true" />
352 <label for="id_bkm_replace">
353 <?php echo $GLOBALS['strBookmarkReplace']; ?></label>
354 </div>
355 </div>
356 <?php
359 echo '<div class="clearfloat"></div>' . "\n";
360 echo '</fieldset>' . "\n"
361 .'</div>' . "\n";
363 echo '<fieldset id="queryboxfooter" class="tblFooters">' . "\n";
364 echo '<div class="formelement">' . "\n";
365 if ($is_querywindow) {
367 <script type="text/javascript">
368 //<![CDATA[
369 document.writeln(' <input type="checkbox" name="LockFromUpdate" value="1" id="checkbox_lock" /> <label for="checkbox_lock"><?php echo $GLOBALS['strQueryWindowLock']; ?></label> ');
370 //]]>
371 </script>
372 <?php
374 echo '</div>' . "\n";
375 echo '<div class="formelement">' . "\n";
376 if (PMA_MYSQL_INT_VERSION >= 50000) {
377 echo '<label for="id_sql_delimiter">[ ' . $GLOBALS['strDelimiter']
378 .'</label>' . "\n";
379 echo '<input type="text" name="sql_delimiter" size="3" '
380 .'value="' . $delimiter . '" '
381 .'id="id_sql_delimiter" /> ]' . "\n";
384 echo '<input type="checkbox" name="show_query" value="1" '
385 .'id="checkbox_show_query" checked="checked" />' . "\n"
386 .'<label for="checkbox_show_query">' . $GLOBALS['strShowThisQuery']
387 .'</label>' . "\n";
389 echo '</div>' . "\n";
390 echo '<input type="submit" name="SQL" value="' . $GLOBALS['strGo'] . '" />'
391 ."\n";
392 echo '<div class="clearfloat"></div>' . "\n";
393 echo '</fieldset>' . "\n";
397 * prints bookmark fieldset
399 * @usedby PMA_sqlQueryForm()
400 * @uses PMA_listBookmarks()
401 * @uses $GLOBALS['db']
402 * @uses $GLOBALS['pmaThemeImage']
403 * @uses $GLOBALS['cfg']['Bookmark']
404 * @uses $GLOBALS['cfg']['ReplaceHelpImg']
405 * @uses $GLOBALS['strBookmarkQuery']
406 * @uses $GLOBALS['strBookmarkView']
407 * @uses $GLOBALS['strDelete']
408 * @uses $GLOBALS['strDocu']
409 * @uses $GLOBALS['strGo']
410 * @uses $GLOBALS['strSubmit']
411 * @uses $GLOBALS['strVar']
412 * @uses count()
413 * @uses htmlspecialchars()
415 function PMA_sqlQueryFormBookmark()
417 $bookmark_list = PMA_listBookmarks($GLOBALS['db'], $GLOBALS['cfg']['Bookmark']);
418 if (! $bookmark_list || count($bookmark_list) < 1) {
419 return;
422 echo '<fieldset id="bookmarkoptions">';
423 echo '<legend>';
424 echo $GLOBALS['strBookmarkQuery'] . '</legend>' . "\n";
425 echo '<div class="formelement">';
426 echo '<select name="id_bookmark">' . "\n";
427 echo '<option value="">&nbsp;</option>' . "\n";
428 foreach ($bookmark_list as $key => $value) {
429 echo '<option value="' . htmlspecialchars($key) . '">'
430 .htmlspecialchars($value) . '</option>' . "\n";
432 // &nbsp; is required for correct display with styles/line height
433 echo '</select>&nbsp;' . "\n";
434 echo '</div>' . "\n";
435 echo '<div class="formelement">' . "\n";
436 echo $GLOBALS['strVar'];
437 if ($GLOBALS['cfg']['ReplaceHelpImg']) {
438 echo ' <a href="./Documentation.html#faqbookmark"'
439 .' target="documentation">'
440 .'<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_help.png"'
441 .' border="0" width="11" height="11" align="middle"'
442 .' alt="' . $GLOBALS['strDocu'] . '" /></a> ';
443 } else {
444 echo ' (<a href="./Documentation.html#faqbookmark"'
445 .' target="documentation">' . $GLOBALS['strDocu'] . '</a>): ';
447 echo '<input type="text" name="bookmark_variable" class="textfield"'
448 .' size="10" />' . "\n";
449 echo '</div>' . "\n";
450 echo '<div class="formelement">' . "\n";
451 echo '<input type="radio" name="action_bookmark" value="0"'
452 .' id="radio_bookmark_exe" checked="checked" />'
453 .'<label for="radio_bookmark_exe">' . $GLOBALS['strSubmit']
454 .'</label>' . "\n";
455 echo '<input type="radio" name="action_bookmark" value="1"'
456 .' id="radio_bookmark_view" />'
457 .'<label for="radio_bookmark_view">' . $GLOBALS['strBookmarkView']
458 .'</label>' . "\n";
459 echo '<input type="radio" name="action_bookmark" value="2"'
460 .' id="radio_bookmark_del" />'
461 .'<label for="radio_bookmark_del">' . $GLOBALS['strDelete']
462 .'</label>' . "\n";
463 echo '</div>' . "\n";
464 echo '<div class="clearfloat"></div>' . "\n";
465 echo '</fieldset>' . "\n";
467 echo '<fieldset id="bookmarkoptionsfooter" class="tblFooters">' . "\n";
468 echo '<input type="submit" name="SQL" value="' . $GLOBALS['strGo'] . '" />';
469 echo '<div class="clearfloat"></div>' . "\n";
470 echo '</fieldset>' . "\n";
474 * prints bookmark fieldset
476 * @usedby PMA_sqlQueryForm()
477 * @uses $GLOBALS['cfg']['GZipDump']
478 * @uses $GLOBALS['cfg']['BZipDump']
479 * @uses $GLOBALS['cfg']['UploadDir']
480 * @uses $GLOBALS['cfg']['AvailableCharsets']
481 * @uses $GLOBALS['cfg']['AllowAnywhereRecoding']
482 * @uses $GLOBALS['strBzip']
483 * @uses $GLOBALS['strCharsetOfFile']
484 * @uses $GLOBALS['strCompression']
485 * @uses $GLOBALS['strError']
486 * @uses $GLOBALS['strGo']
487 * @uses $GLOBALS['strGzip']
488 * @uses $GLOBALS['strLocationTextfile']
489 * @uses $GLOBALS['strWebServerUploadDirectory']
490 * @uses $GLOBALS['strWebServerUploadDirectoryError']
491 * @uses $GLOBALS['allow_recoding']
492 * @uses $GLOBALS['charset']
493 * @uses $GLOBALS['max_upload_size']
494 * @uses PMA_supportedDecompressions()
495 * @uses PMA_getFileSelectOptions()
496 * @uses PMA_displayMaximumUploadSize()
497 * @uses PMA_generateCharsetDropdownBox()
498 * @uses PMA_generateHiddenMaxFileSize()
499 * @uses PMA_MYSQL_INT_VERSION
500 * @uses PMA_CSDROPDOWN_CHARSET
501 * @uses empty()
503 function PMA_sqlQueryFormUpload(){
504 $errors = array ();
506 $matcher = '@\.sql(\.(' . PMA_supportedDecompressions() . '))?$@'; // we allow only SQL here
508 if (!empty($GLOBALS['cfg']['UploadDir'])) {
509 $files = PMA_getFileSelectOptions(PMA_userDir($GLOBALS['cfg']['UploadDir']), $matcher, (isset($timeout_passed) && $timeout_passed && isset($local_import_file)) ? $local_import_file : '');
510 } else {
511 $files = '';
514 // start output
515 echo '<fieldset id="">';
516 echo '<legend>';
517 echo $GLOBALS['strLocationTextfile'] . '</legend>';
518 echo '<div class="formelement">';
519 echo '<input type="file" name="sql_file" class="textfield" /> ';
520 echo PMA_displayMaximumUploadSize($GLOBALS['max_upload_size']);
521 // some browsers should respect this :)
522 echo PMA_generateHiddenMaxFileSize($GLOBALS['max_upload_size']) . "\n";
523 echo '</div>';
525 if ($files === FALSE) {
526 $errors[$GLOBALS['strError']] = $GLOBALS['strWebServerUploadDirectoryError'];
527 } elseif (!empty($files)) {
528 echo '<div class="formelement">';
529 echo '<strong>' . $GLOBALS['strWebServerUploadDirectory'] .':</strong>' . "\n";
530 echo '<select size="1" name="sql_localfile">' . "\n";
531 echo '<option value="" selected="selected"></option>' . "\n";
532 echo $files;
533 echo '</select>' . "\n";
534 echo '</div>';
537 echo '<div class="clearfloat"></div>' . "\n";
538 echo '</fieldset>';
541 echo '<fieldset id="" class="tblFooters">';
542 if (PMA_MYSQL_INT_VERSION < 40100
543 && $GLOBALS['cfg']['AllowAnywhereRecoding']
544 && $GLOBALS['allow_recoding']) {
545 echo $GLOBALS['strCharsetOfFile'] . "\n"
546 . '<select name="charset_of_file" size="1">' . "\n";
547 foreach ($GLOBALS['cfg']['AvailableCharsets'] as $temp_charset) {
548 echo '<option value="' . $temp_charset . '"';
549 if ($temp_charset == $GLOBALS['charset']) {
550 echo ' selected="selected"';
552 echo '>' . $temp_charset . '</option>' . "\n";
554 echo '</select>' . "\n";
555 } elseif (PMA_MYSQL_INT_VERSION >= 40100) {
556 echo $GLOBALS['strCharsetOfFile'] . "\n";
557 echo PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_CHARSET,
558 'charset_of_file', null, 'utf8', FALSE);
559 } // end if (recoding)
560 echo '<input type="submit" name="SQL" value="' . $GLOBALS['strGo']
561 .'" />' . "\n";
562 echo '<div class="clearfloat"></div>' . "\n";
563 echo '</fieldset>';
565 foreach ($errors as $error => $message) {
566 echo '<div>' . $error . '</div>';
567 echo '<div>' . $message . '</div>';