Add <div> to allow selecting whole SQL by tripple click (patch #1611591).
[phpmyadmin/crack.git] / libraries / sql_query_form.lib.php
blob7c74b2581905bc7cdcd45d986c37edffcfb30ee9
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
4 /**
5 * functions for displaying the sql query form
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 require_once './libraries/file_listing.php'; // used for file listing
15 require_once './libraries/bookmark.lib.php'; // used for file listing
17 /**
18 * prints the sql query boxes
20 * @usedby server_sql.php
21 * @usedby db_sql.php
22 * @usedby tbl_sql.php
23 * @usedby tbl_structure.php
24 * @usedby querywindow.php
25 * @uses $GLOBALS['table']
26 * @uses $GLOBALS['db']
27 * @uses $GLOBALS['server']
28 * @uses $GLOBALS['goto']
29 * @uses $GLOBALS['is_upload'] from common.lib.php
30 * @uses $GLOBALS['sql_query'] from grab_globals.lib.php
31 * @uses $GLOBALS['cfg']['DefaultQueryTable']
32 * @uses $GLOBALS['cfg']['DefaultQueryDatabase']
33 * @uses $GLOBALS['cfg']['Servers']
34 * @uses $GLOBALS['cfg']['DefaultTabDatabase']
35 * @uses $GLOBALS['cfg']['DefaultQueryDatabase']
36 * @uses $GLOBALS['cfg']['DefaultQueryTable']
37 * @uses $GLOBALS['cfg']['Bookmark']['db']
38 * @uses $GLOBALS['cfg']['Bookmark']['table']
39 * @uses $GLOBALS['strSuccess']
40 * @uses PMA_generate_common_url()
41 * @uses PMA_backquote()
42 * @uses PMA_DBI_fetch_result()
43 * @uses PMA_showMySQLDocu()
44 * @uses PMA_generate_common_hidden_inputs()
45 * @uses PMA_sqlQueryFormBookmark()
46 * @uses PMA_sqlQueryFormInsert()
47 * @uses PMA_sqlQueryFormUpload()
48 * @uses PMA_DBI_QUERY_STORE
49 * @uses PMA_set_enc_form()
50 * @uses sprintf()
51 * @uses htmlspecialchars()
52 * @uses str_replace()
53 * @uses md5()
54 * @uses function_exists()
55 * @param boolean|string $query query to display in the textarea
56 * or true to display last executed
57 * @param boolean|string $display_tab sql|files|history|full|FALSE
58 * what part to display
59 * false if not inside querywindow
61 function PMA_sqlQueryForm($query = true, $display_tab = false)
63 // check tab to display if inside querywindow
64 if (! $display_tab) {
65 $display_tab = 'full';
66 $is_querywindow = false;
67 } else {
68 $is_querywindow = true;
71 // query to show
72 if (true === $query) {
73 $query = empty($GLOBALS['sql_query']) ? '' : $GLOBALS['sql_query'];
76 // set enctype to multipart for file uploads
77 if ($GLOBALS['is_upload']) {
78 $enctype = ' enctype="multipart/form-data"';
79 } else {
80 $enctype = '';
83 $table = '';
84 $db = '';
85 if (! isset($GLOBALS['db']) || ! strlen($GLOBALS['db'])) {
86 // prepare for server related
87 $goto = empty($GLOBALS['goto']) ?
88 'server_sql.php' : $GLOBALS['goto'];
89 } elseif (! isset($GLOBALS['table']) || ! strlen($GLOBALS['table'])) {
90 // prepare for db related
91 $db = $GLOBALS['db'];
92 $goto = empty($GLOBALS['goto']) ?
93 'db_sql.php' : $GLOBALS['goto'];
94 } else {
95 $table = $GLOBALS['table'];
96 $db = $GLOBALS['db'];
97 $goto = empty($GLOBALS['goto']) ?
98 'tbl_sql.php' : $GLOBALS['goto'];
102 // start output
103 if ($is_querywindow) {
105 <form method="post" id="sqlqueryform" target="frame_content"
106 action="import.php"<?php echo $enctype; ?> name="sqlform"
107 onsubmit="var save_name = window.opener.parent.frame_content.name;
108 window.opener.parent.frame_content.name = save_name + '<?php echo time(); ?>';
109 this.target = window.opener.parent.frame_content.name;
110 return checkSqlQuery( this );" >
111 <?php
112 } else {
113 echo '<form method="post" action="import.php" ' . $enctype . ' id="sqlqueryform"'
114 .' onsubmit="return checkSqlQuery(this)" name="sqlform">' . "\n";
117 if ($is_querywindow) {
118 echo '<input type="hidden" name="focus_querywindow" value="true" />'
119 ."\n";
120 if ($display_tab != 'sql' && $display_tab != 'full') {
121 echo '<input type="hidden" name="sql_query" value="" />' . "\n";
122 echo '<input type="hidden" name="show_query" value="1" />' . "\n";
125 echo '<input type="hidden" name="is_js_confirmed" value="0" />' . "\n"
126 .PMA_generate_common_hidden_inputs($db, $table) . "\n"
127 .'<input type="hidden" name="pos" value="0" />' . "\n"
128 .'<input type="hidden" name="goto" value="'
129 .htmlspecialchars($goto) . '" />' . "\n"
130 .'<input type="hidden" name="zero_rows" value="'
131 . htmlspecialchars($GLOBALS['strSuccess']) . '" />' . "\n"
132 .'<input type="hidden" name="prev_sql_query" value="'
133 . htmlspecialchars($query) . '" />' . "\n";
135 // display querybox
136 if ($display_tab === 'full' || $display_tab === 'sql') {
137 PMA_sqlQueryFormInsert($query, $is_querywindow);
140 // display uploads
141 if ($display_tab === 'files' && $GLOBALS['is_upload']) {
142 PMA_sqlQueryFormUpload();
145 // Bookmark Support
146 if ($display_tab === 'full' || $display_tab === 'history') {
147 if (! empty( $GLOBALS['cfg']['Bookmark'])
148 && $GLOBALS['cfg']['Bookmark']['db']
149 && $GLOBALS['cfg']['Bookmark']['table']) {
150 PMA_sqlQueryFormBookmark();
154 // Encoding setting form appended by Y.Kawada
155 if (function_exists('PMA_set_enc_form')) {
156 echo PMA_set_enc_form(' ');
159 echo '</form>' . "\n";
160 if ($is_querywindow) {
162 <script type="text/javascript" language="javascript">
163 //<![CDATA[
164 if (window.opener) {
165 window.opener.parent.insertQuery();
167 //]]>
168 </script>
169 <?php
174 * prints querybox fieldset
176 * @usedby PMA_sqlQueryForm()
177 * @uses $GLOBALS['text_dir']
178 * @uses $GLOBALS['cfg']['TextareaAutoSelect']
179 * @uses $GLOBALS['cfg']['TextareaCols']
180 * @uses $GLOBALS['cfg']['TextareaRows']
181 * @uses $GLOBALS['strShowThisQuery']
182 * @uses $GLOBALS['strGo']
183 * @uses PMA_USR_OS
184 * @uses PMA_USR_BROWSER_AGENT
185 * @uses PMA_USR_BROWSER_VER
186 * @uses htmlspecialchars()
187 * @param string $query query to display in the textarea
188 * @param boolean $is_querywindow if inside querywindow or not
190 function PMA_sqlQueryFormInsert($query = '', $is_querywindow = false)
193 // enable auto select text in textarea
194 if ($GLOBALS['cfg']['TextareaAutoSelect']) {
195 $auto_sel = ' onfocus="selectContent( this, sql_box_locked, true )"';
196 } else {
197 $auto_sel = '';
200 // enable locking if inside query window
201 if ($is_querywindow) {
202 $locking = ' onkeypress="document.sqlform.elements[\'LockFromUpdate\'].'
203 .'checked = true;"';
204 $height = $GLOBALS['cfg']['TextareaRows'] * 1.25;
205 } else {
206 $locking = '';
207 $height = $GLOBALS['cfg']['TextareaRows'] * 2;
210 $table = '';
211 $db = '';
212 $fields_list = array();
213 if (! isset($GLOBALS['db']) || ! strlen($GLOBALS['db'])) {
214 // prepare for server related
215 $legend = sprintf($GLOBALS['strRunSQLQueryOnServer'],
216 htmlspecialchars(
217 $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['host']));
218 } elseif (! isset($GLOBALS['table']) || ! strlen($GLOBALS['table'])) {
219 // prepare for db related
220 $db = $GLOBALS['db'];
221 // if you want navigation:
222 $strDBLink = '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase']
223 . '?' . PMA_generate_common_url($db) . '"';
224 if ($is_querywindow) {
225 $strDBLink .= ' target="_self"'
226 . ' onclick="this.target=window.opener.frame_content.name"';
228 $strDBLink .= '>'
229 . htmlspecialchars($db) . '</a>';
230 // else use
231 // $strDBLink = htmlspecialchars($db);
232 $legend = sprintf($GLOBALS['strRunSQLQuery'], $strDBLink);
233 if (empty($query)) {
234 $query = str_replace('%d',
235 PMA_backquote($db), $GLOBALS['cfg']['DefaultQueryDatabase']);
237 } else {
238 $table = $GLOBALS['table'];
239 $db = $GLOBALS['db'];
240 // Get the list and number of fields
241 // we do a try_query here, because we could be in the query window,
242 // trying to synchonize and the table has not yet been created
243 $fields_list = PMA_DBI_fetch_result(
244 'SHOW FULL COLUMNS FROM ' . PMA_backquote($db)
245 . '.' . PMA_backquote($GLOBALS['table']));
247 $strDBLink = '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase']
248 . '?' . PMA_generate_common_url($db) . '"';
249 if ($is_querywindow) {
250 $strDBLink .= ' target="_self"'
251 . ' onclick="this.target=window.opener.frame_content.name"';
253 $strDBLink .= '>'
254 . htmlspecialchars($db) . '</a>';
255 // else use
256 // $strDBLink = htmlspecialchars($db);
257 $legend = sprintf($GLOBALS['strRunSQLQuery'], $strDBLink);
258 if (empty($query) && count($fields_list)) {
259 $field_names = array();
260 foreach ($fields_list as $field) {
261 $field_names[] = PMA_backquote($field['Field']);
263 $query =
264 str_replace('%d', PMA_backquote($db),
265 str_replace('%t', PMA_backquote($table),
266 str_replace('%f',
267 implode(', ', $field_names ),
268 $GLOBALS['cfg']['DefaultQueryTable'])));
269 unset($field_names);
272 $legend .= ': ' . PMA_showMySQLDocu('SQL-Syntax', 'SELECT');
274 if (count($fields_list)) {
275 $sqlquerycontainer_id = 'sqlquerycontainer';
276 } else {
277 $sqlquerycontainer_id = 'sqlquerycontainerfull';
280 echo '<a name="querybox"></a>' . "\n"
281 .'<div id="queryboxcontainer">' . "\n"
282 .'<fieldset id="querybox">' . "\n";
283 echo '<legend>' . $legend . '</legend>' . "\n";
284 echo '<div id="queryfieldscontainer">' . "\n";
285 echo '<div id="' . $sqlquerycontainer_id . '">' . "\n"
286 .'<textarea name="sql_query" id="sqlquery"'
287 .' cols="' . $GLOBALS['cfg']['TextareaCols'] . '"'
288 .' rows="' . $height . '"'
289 .' dir="' . $GLOBALS['text_dir'] . '"'
290 .$auto_sel . $locking . '>' . htmlspecialchars($query) . '</textarea>' . "\n";
291 echo '</div>' . "\n";
293 if (count($fields_list)) {
294 echo '<div id="tablefieldscontainer">' . "\n"
295 .'<label>' . $GLOBALS['strFields'] . '</label>' . "\n"
296 .'<select id="tablefields" name="dummy" '
297 .'size="' . ($GLOBALS['cfg']['TextareaRows'] - 2) . '" '
298 .'multiple="multiple" ondblclick="insertValueQuery()">' . "\n";
299 foreach ($fields_list as $field) {
300 echo '<option value="'
301 .PMA_backquote(htmlspecialchars($field['Field'])) . '"';
302 if (isset($field['Field']) && strlen($field['Field']) && isset($field['Comment'])) {
303 echo ' title="' . htmlspecialchars($field['Comment']) . '"';
305 echo '>' . htmlspecialchars( $field['Field'] ) . '</option>' . "\n";
307 echo '</select>' . "\n"
308 .'<div id="tablefieldinsertbuttoncontainer">' . "\n";
309 if ( $GLOBALS['cfg']['PropertiesIconic'] ) {
310 echo '<input type="button" name="insert" value="&lt;&lt;"'
311 .' onclick="insertValueQuery()"'
312 .' title="' . $GLOBALS['strInsert'] . '" />' . "\n";
313 } else {
314 echo '<input type="button" name="insert"'
315 .' value="' . $GLOBALS['strInsert'] . '"'
316 .' onclick="insertValueQuery()" />' . "\n";
318 echo '</div>' . "\n"
319 .'</div>' . "\n";
322 echo '<div class="clearfloat"></div>' . "\n";
323 echo '</div>' . "\n";
325 if (! empty($GLOBALS['cfg']['Bookmark'])
326 && $GLOBALS['cfg']['Bookmark']['db']
327 && $GLOBALS['cfg']['Bookmark']['table']) {
329 <div id="bookmarkoptions">
330 <div class="formelement">
331 <label for="bkm_label">
332 <?php echo $GLOBALS['strBookmarkThis']; ?>:</label>
333 <input type="text" name="bkm_label" id="bkm_label" value="" />
334 </div>
335 <div class="formelement">
336 <input type="checkbox" name="bkm_all_users" id="id_bkm_all_users"
337 value="true" />
338 <label for="id_bkm_all_users">
339 <?php echo $GLOBALS['strBookmarkAllUsers']; ?></label>
340 </div>
341 <div class="formelement">
342 <input type="checkbox" name="bkm_replace" id="id_bkm_replace"
343 value="true" />
344 <label for="id_bkm_replace">
345 <?php echo $GLOBALS['strBookmarkReplace']; ?></label>
346 </div>
347 </div>
348 <?php
351 echo '<div class="clearfloat"></div>' . "\n";
352 echo '</fieldset>' . "\n"
353 .'</div>' . "\n";
355 echo '<fieldset id="queryboxfooter" class="tblFooters">' . "\n";
356 echo '<div class="formelement">' . "\n";
357 if ($is_querywindow) {
359 <script type="text/javascript" language="javascript">
360 //<![CDATA[
361 document.writeln(' <input type="checkbox" name="LockFromUpdate" value="1" id="checkbox_lock" /> <label for="checkbox_lock"><?php echo $GLOBALS['strQueryWindowLock']; ?></label> ');
362 //]]>
363 </script>
364 <?php
366 echo '</div>' . "\n";
367 echo '<div class="formelement">' . "\n";
368 if (PMA_MYSQL_INT_VERSION >= 50000) {
369 echo '<label for="id_sql_delimiter">[ ' . $GLOBALS['strDelimiter']
370 .'</label>' . "\n";
371 echo '<input type="text" name="sql_delimiter" size="3" value=";" '
372 .'id="id_sql_delimiter" /> ]' . "\n";
375 echo '<input type="checkbox" name="show_query" value="1" '
376 .'id="checkbox_show_query" checked="checked" />' . "\n"
377 .'<label for="checkbox_show_query">' . $GLOBALS['strShowThisQuery']
378 .'</label>' . "\n";
380 echo '</div>' . "\n";
381 echo '<input type="submit" name="SQL" value="' . $GLOBALS['strGo'] . '" />'
382 ."\n";
383 echo '<div class="clearfloat"></div>' . "\n";
384 echo '</fieldset>' . "\n";
388 * prints bookmark fieldset
390 * @usedby PMA_sqlQueryForm()
391 * @uses PMA_listBookmarks()
392 * @uses $GLOBALS['db']
393 * @uses $GLOBALS['pmaThemeImage']
394 * @uses $GLOBALS['cfg']['Bookmark']
395 * @uses $GLOBALS['cfg']['ReplaceHelpImg']
396 * @uses $GLOBALS['strBookmarkQuery']
397 * @uses $GLOBALS['strBookmarkView']
398 * @uses $GLOBALS['strDelete']
399 * @uses $GLOBALS['strDocu']
400 * @uses $GLOBALS['strGo']
401 * @uses $GLOBALS['strSubmit']
402 * @uses $GLOBALS['strVar']
403 * @uses count()
404 * @uses htmlspecialchars()
406 function PMA_sqlQueryFormBookmark()
408 $bookmark_list = PMA_listBookmarks(isset($GLOBALS['db']) ? $GLOBALS['db'] : '', $GLOBALS['cfg']['Bookmark'] );
409 if (! $bookmark_list || count($bookmark_list) < 1) {
410 return;
413 echo '<fieldset id="bookmarkoptions">';
414 echo '<legend>';
415 echo $GLOBALS['strBookmarkQuery'] . '</legend>' . "\n";
416 echo '<div class="formelement">';
417 echo '<select name="id_bookmark">' . "\n";
418 echo '<option value=""></option>' . "\n";
419 foreach ($bookmark_list as $key => $value) {
420 echo '<option value="' . htmlspecialchars($key) . '">'
421 .htmlspecialchars($value) . '</option>' . "\n";
423 // &nbsp; is required for correct display with styles/line height
424 echo '</select>&nbsp;' . "\n";
425 echo '</div>' . "\n";
426 echo '<div class="formelement">' . "\n";
427 echo $GLOBALS['strVar'];
428 if ($GLOBALS['cfg']['ReplaceHelpImg']) {
429 echo ' <a href="./Documentation.html#faqbookmark"'
430 .' target="documentation">'
431 .'<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_help.png"'
432 .' border="0" width="11" height="11" align="middle"'
433 .' alt="' . $GLOBALS['strDocu'] . '" /></a> ';
434 } else {
435 echo ' (<a href="./Documentation.html#faqbookmark"'
436 .' target="documentation">' . $GLOBALS['strDocu'] . '</a>): ';
438 echo '<input type="text" name="bookmark_variable" class="textfield"'
439 .' size="10" />' . "\n";
440 echo '</div>' . "\n";
441 echo '<div class="formelement">' . "\n";
442 echo '<input type="radio" name="action_bookmark" value="0"'
443 .' id="radio_bookmark_exe" checked="checked" />'
444 .'<label for="radio_bookmark_exe">' . $GLOBALS['strSubmit']
445 .'</label>' . "\n";
446 echo '<input type="radio" name="action_bookmark" value="1"'
447 .' id="radio_bookmark_view" />'
448 .'<label for="radio_bookmark_view">' . $GLOBALS['strBookmarkView']
449 .'</label>' . "\n";
450 echo '<input type="radio" name="action_bookmark" value="2"'
451 .' id="radio_bookmark_del" />'
452 .'<label for="radio_bookmark_del">' . $GLOBALS['strDelete']
453 .'</label>' . "\n";
454 echo '</div>' . "\n";
455 echo '<div class="clearfloat"></div>' . "\n";
456 echo '</fieldset>' . "\n";
458 echo '<fieldset id="bookmarkoptionsfooter" class="tblFooters">' . "\n";
459 echo '<input type="submit" name="SQL" value="' . $GLOBALS['strGo'] . '" />';
460 echo '<div class="clearfloat"></div>' . "\n";
461 echo '</fieldset>' . "\n";
465 * prints bookmark fieldset
467 * @usedby PMA_sqlQueryForm()
468 * @uses $GLOBALS['cfg']['GZipDump']
469 * @uses $GLOBALS['cfg']['BZipDump']
470 * @uses $GLOBALS['cfg']['UploadDir']
471 * @uses $GLOBALS['cfg']['AvailableCharsets']
472 * @uses $GLOBALS['cfg']['AllowAnywhereRecoding']
473 * @uses $GLOBALS['strAutodetect']
474 * @uses $GLOBALS['strBzip']
475 * @uses $GLOBALS['strCharsetOfFile']
476 * @uses $GLOBALS['strCompression']
477 * @uses $GLOBALS['strError']
478 * @uses $GLOBALS['strGo']
479 * @uses $GLOBALS['strGzip']
480 * @uses $GLOBALS['strLocationTextfile']
481 * @uses $GLOBALS['strWebServerUploadDirectory']
482 * @uses $GLOBALS['strWebServerUploadDirectoryError']
483 * @uses $GLOBALS['allow_recoding']
484 * @uses $GLOBALS['charset']
485 * @uses $GLOBALS['max_upload_size']
486 * @uses PMA_supportedDecompressions()
487 * @uses PMA_getFileSelectOptions()
488 * @uses PMA_displayMaximumUploadSize()
489 * @uses PMA_generateCharsetDropdownBox()
490 * @uses PMA_generateHiddenMaxFileSize()
491 * @uses PMA_MYSQL_INT_VERSION
492 * @uses PMA_CSDROPDOWN_CHARSET
493 * @uses empty()
495 function PMA_sqlQueryFormUpload(){
496 $errors = array ();
498 $matcher = '@\.sql(\.(' . PMA_supportedDecompressions() . '))?$@'; // we allow only SQL here
500 if (!empty($GLOBALS['cfg']['UploadDir'])) {
501 $files = PMA_getFileSelectOptions(PMA_userDir($GLOBALS['cfg']['UploadDir']), $matcher, (isset($timeout_passed) && $timeout_passed && isset($local_import_file)) ? $local_import_file : '');
502 } else {
503 $files = '';
506 // start output
507 echo '<fieldset id="">';
508 echo '<legend>';
509 echo $GLOBALS['strLocationTextfile'] . '</legend>';
510 echo '<div class="formelement">';
511 echo '<input type="file" name="sql_file" class="textfield" /> ';
512 echo PMA_displayMaximumUploadSize($GLOBALS['max_upload_size']);
513 // some browsers should respect this :)
514 echo PMA_generateHiddenMaxFileSize($GLOBALS['max_upload_size']) . "\n";
515 echo '</div>';
517 if ($files === FALSE) {
518 $errors[$GLOBALS['strError']] = $GLOBALS['strWebServerUploadDirectoryError'];
519 } elseif (!empty($files)) {
520 echo '<div class="formelement">';
521 echo '<strong>' . $GLOBALS['strWebServerUploadDirectory'] .':</strong>' . "\n";
522 echo '<select size="1" name="sql_localfile">' . "\n";
523 echo '<option value="" selected="selected"></option>' . "\n";
524 echo $files;
525 echo '</select>' . "\n";
526 echo '</div>';
529 echo '<div class="clearfloat"></div>' . "\n";
530 echo '</fieldset>';
533 echo '<fieldset id="" class="tblFooters">';
534 if ( PMA_MYSQL_INT_VERSION < 40100
535 && $GLOBALS['cfg']['AllowAnywhereRecoding']
536 && $GLOBALS['allow_recoding'] ) {
537 echo $GLOBALS['strCharsetOfFile'] . "\n"
538 . '<select name="charset_of_file" size="1">' . "\n";
539 foreach ($GLOBALS['cfg']['AvailableCharsets'] as $temp_charset) {
540 echo '<option value="' . $temp_charset . '"';
541 if ($temp_charset == $GLOBALS['charset']) {
542 echo ' selected="selected"';
544 echo '>' . $temp_charset . '</option>' . "\n";
546 echo '</select>' . "\n";
547 } elseif (PMA_MYSQL_INT_VERSION >= 40100) {
548 echo $GLOBALS['strCharsetOfFile'] . "\n";
549 echo PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_CHARSET,
550 'charset_of_file', null, 'utf8', FALSE);
551 } // end if (recoding)
552 echo '<input type="submit" name="SQL" value="' . $GLOBALS['strGo']
553 .'" />' . "\n";
554 echo '<div class="clearfloat"></div>' . "\n";
555 echo '</fieldset>';
557 foreach ( $errors as $error => $message ) {
558 echo '<div>' . $error . '</div>';
559 echo '<div>' . $message . '</div>';