Feature Request #3187076
[phpmyadmin-regexreplace.git] / libraries / sql_query_form.lib.php
blob9852e0aec8c4589fb2b149ddec90af9ef820ed1d
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * functions for displaying the sql query form
6 * @usedby server_sql.php
7 * @usedby db_sql.php
8 * @usedby tbl_sql.php
9 * @usedby tbl_structure.php
10 * @usedby tbl_tracking.php
11 * @usedby querywindow.php
12 * @package phpMyAdmin
14 if (! defined('PHPMYADMIN')) {
15 exit;
18 /**
21 require_once './libraries/file_listing.php'; // used for file listing
22 require_once './libraries/bookmark.lib.php'; // used for file listing
24 /**
25 * prints the sql query boxes
27 * @usedby server_sql.php
28 * @usedby db_sql.php
29 * @usedby tbl_sql.php
30 * @usedby tbl_structure.php
31 * @usedby tbl_tracking.php
32 * @usedby querywindow.php
33 * @uses $GLOBALS['table']
34 * @uses $GLOBALS['db']
35 * @uses $GLOBALS['server']
36 * @uses $GLOBALS['goto']
37 * @uses $GLOBALS['is_upload'] from common.inc.php
38 * @uses $GLOBALS['sql_query'] from grab_globals.lib.php
39 * @uses $GLOBALS['cfg']['DefaultQueryTable']
40 * @uses $GLOBALS['cfg']['DefaultQueryDatabase']
41 * @uses $GLOBALS['cfg']['Servers']
42 * @uses $GLOBALS['cfg']['DefaultTabDatabase']
43 * @uses $GLOBALS['cfg']['Bookmark']
44 * @uses PMA_generate_common_url()
45 * @uses PMA_backquote()
46 * @uses PMA_DBI_fetch_result()
47 * @uses PMA_showMySQLDocu()
48 * @uses PMA_generate_common_hidden_inputs()
49 * @uses PMA_sqlQueryFormBookmark()
50 * @uses PMA_sqlQueryFormInsert()
51 * @uses PMA_sqlQueryFormUpload()
52 * @uses PMA_DBI_QUERY_STORE
53 * @uses PMA_set_enc_form()
54 * @uses sprintf()
55 * @uses htmlspecialchars()
56 * @uses str_replace()
57 * @uses md5()
58 * @uses function_exists()
59 * @param boolean|string $query query to display in the textarea
60 * or true to display last executed
61 * @param boolean|string $display_tab sql|files|history|full|FALSE
62 * what part to display
63 * false if not inside querywindow
64 * @param string $delimiter
66 function PMA_sqlQueryForm($query = true, $display_tab = false, $delimiter = ';')
68 // check tab to display if inside querywindow
69 if (! $display_tab) {
70 $display_tab = 'full';
71 $is_querywindow = false;
72 } else {
73 $is_querywindow = true;
76 // query to show
77 if (true === $query) {
78 $query = $GLOBALS['sql_query'];
81 // set enctype to multipart for file uploads
82 if ($GLOBALS['is_upload']) {
83 $enctype = ' enctype="multipart/form-data"';
84 } else {
85 $enctype = '';
88 $table = '';
89 $db = '';
90 if (! strlen($GLOBALS['db'])) {
91 // prepare for server related
92 $goto = empty($GLOBALS['goto']) ?
93 'server_sql.php' : $GLOBALS['goto'];
94 } elseif (! strlen($GLOBALS['table'])) {
95 // prepare for db related
96 $db = $GLOBALS['db'];
97 $goto = empty($GLOBALS['goto']) ?
98 'db_sql.php' : $GLOBALS['goto'];
99 } else {
100 $table = $GLOBALS['table'];
101 $db = $GLOBALS['db'];
102 $goto = empty($GLOBALS['goto']) ?
103 'tbl_sql.php' : $GLOBALS['goto'];
107 // start output
108 if ($is_querywindow) {
110 <form method="post" id="sqlqueryform" target="frame_content"
111 action="import.php"<?php echo $enctype; ?> name="sqlform"
112 onsubmit="var save_name = window.opener.parent.frame_content.name;
113 window.opener.parent.frame_content.name = save_name + '<?php echo time(); ?>';
114 this.target = window.opener.parent.frame_content.name;
115 return checkSqlQuery(this)">
116 <?php
117 } else {
118 echo '<form method="post" action="import.php" ' . $enctype;
119 if ($GLOBALS['cfg']['AjaxEnable']) {
120 echo ' class="ajax"';
122 echo ' 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="message_to_show" 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
179 // print an empty div, which will be later filled with the sql query results by ajax
180 echo '<div id="sqlqueryresults"></div>';
184 * prints querybox fieldset
186 * @usedby PMA_sqlQueryForm()
187 * @uses $GLOBALS['text_dir']
188 * @uses $GLOBALS['cfg']['TextareaAutoSelect']
189 * @uses $GLOBALS['cfg']['TextareaCols']
190 * @uses $GLOBALS['cfg']['TextareaRows']
191 * @uses PMA_USR_OS
192 * @uses PMA_USR_BROWSER_AGENT
193 * @uses PMA_USR_BROWSER_VER
194 * @uses htmlspecialchars()
195 * @param string $query query to display in the textarea
196 * @param boolean $is_querywindow if inside querywindow or not
197 * @param string $delimiter default delimiter to use
199 function PMA_sqlQueryFormInsert($query = '', $is_querywindow = false, $delimiter = ';')
202 // enable auto select text in textarea
203 if ($GLOBALS['cfg']['TextareaAutoSelect']) {
204 $auto_sel = ' onfocus="selectContent(this, sql_box_locked, true)"';
205 } else {
206 $auto_sel = '';
209 // enable locking if inside query window
210 if ($is_querywindow) {
211 $locking = ' onkeypress="document.sqlform.elements[\'LockFromUpdate\'].'
212 .'checked = true;"';
213 $height = $GLOBALS['cfg']['TextareaRows'] * 1.25;
214 } else {
215 $locking = '';
216 $height = $GLOBALS['cfg']['TextareaRows'] * 2;
219 $table = '';
220 $db = '';
221 $fields_list = array();
222 if (! strlen($GLOBALS['db'])) {
223 // prepare for server related
224 $legend = sprintf(__('Run SQL query/queries on server %s'),
225 '&quot;' . htmlspecialchars(
226 ! empty($GLOBALS['cfg']['Servers'][$GLOBALS['server']]['verbose']) ? $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['verbose'] : $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['host']) . '&quot;');
227 } elseif (! strlen($GLOBALS['table'])) {
228 // prepare for db related
229 $db = $GLOBALS['db'];
230 // if you want navigation:
231 $tmp_db_link = '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase']
232 . '?' . PMA_generate_common_url($db) . '"';
233 if ($is_querywindow) {
234 $tmp_db_link .= ' target="_self"'
235 . ' onclick="this.target=window.opener.frame_content.name"';
237 $tmp_db_link .= '>'
238 . htmlspecialchars($db) . '</a>';
239 // else use
240 // $tmp_db_link = htmlspecialchars($db);
241 $legend = sprintf(__('Run SQL query/queries on database %s'), $tmp_db_link);
242 if (empty($query)) {
243 $query = PMA_expandUserString($GLOBALS['cfg']['DefaultQueryDatabase'], 'PMA_backquote');
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)) {
267 $query = PMA_expandUserString($GLOBALS['cfg']['DefaultQueryTable'], 'PMA_backquote');
270 $legend .= ': ' . PMA_showMySQLDocu('SQL-Syntax', 'SELECT');
272 if (count($fields_list)) {
273 $sqlquerycontainer_id = 'sqlquerycontainer';
274 } else {
275 $sqlquerycontainer_id = 'sqlquerycontainerfull';
278 echo '<a name="querybox"></a>' . "\n"
279 .'<div id="queryboxcontainer">' . "\n"
280 .'<fieldset id="querybox">' . "\n";
281 echo '<legend>' . $legend . '</legend>' . "\n";
282 echo '<div id="queryfieldscontainer">' . "\n";
283 echo '<div id="' . $sqlquerycontainer_id . '">' . "\n"
284 .'<textarea tabindex="100" name="sql_query" id="sqlquery"'
285 .' cols="' . $GLOBALS['cfg']['TextareaCols'] . '"'
286 .' rows="' . $height . '"'
287 .' dir="' . $GLOBALS['text_dir'] . '"'
288 .$auto_sel . $locking . '>' . htmlspecialchars($query) . '</textarea>' . "\n";
289 // Add buttons to generate query easily for select all,single select,insert,update and delete
290 if(count($fields_list)) {
291 echo '<input type="button" value="SELECT *" id="selectall" class="sqlbutton" />';
292 echo '<input type="button" value="SELECT" id="select" class="sqlbutton" />';
293 echo '<input type="button" value="INSERT" id="insert" class="sqlbutton" />';
294 echo '<input type="button" value="UPDATE" id="update" class="sqlbutton" />';
295 echo '<input type="button" value="DELETE" id="delete" class="sqlbutton" />';
297 echo '<input type="button" value="' . __('Clear') . '" id="clear" class="sqlbutton" />';
298 echo '</div>' . "\n";
300 if (count($fields_list)) {
301 echo '<div id="tablefieldscontainer">' . "\n"
302 .'<label>' . __('Columns') . '</label>' . "\n"
303 .'<select id="tablefields" name="dummy" '
304 .'size="' . ($GLOBALS['cfg']['TextareaRows'] - 2) . '" '
305 .'multiple="multiple" ondblclick="insertValueQuery()">' . "\n";
306 foreach ($fields_list as $field) {
307 echo '<option value="'
308 .PMA_backquote(htmlspecialchars($field['Field'])) . '"';
309 if (isset($field['Field']) && strlen($field['Field']) && isset($field['Comment'])) {
310 echo ' title="' . htmlspecialchars($field['Comment']) . '"';
312 echo '>' . htmlspecialchars($field['Field']) . '</option>' . "\n";
314 echo '</select>' . "\n"
315 .'<div id="tablefieldinsertbuttoncontainer">' . "\n";
316 if ($GLOBALS['cfg']['PropertiesIconic']) {
317 echo '<input type="button" name="insert" value="&lt;&lt;"'
318 .' onclick="insertValueQuery()"'
319 .' title="' . __('Insert') . '" />' . "\n";
320 } else {
321 echo '<input type="button" name="insert"'
322 .' value="' . __('Insert') . '"'
323 .' onclick="insertValueQuery()" />' . "\n";
325 echo '</div>' . "\n"
326 .'</div>' . "\n";
329 echo '<div class="clearfloat"></div>' . "\n";
330 echo '</div>' . "\n";
332 if (! empty($GLOBALS['cfg']['Bookmark'])) {
334 <div id="bookmarkoptions">
335 <div class="formelement">
336 <label for="bkm_label">
337 <?php echo __('Bookmark this SQL query'); ?>:</label>
338 <input type="text" name="bkm_label" id="bkm_label" tabindex="110" value="" />
339 </div>
340 <div class="formelement">
341 <input type="checkbox" name="bkm_all_users" tabindex="111" id="id_bkm_all_users"
342 value="true" />
343 <label for="id_bkm_all_users">
344 <?php echo __('Let every user access this bookmark'); ?></label>
345 </div>
346 <div class="formelement">
347 <input type="checkbox" name="bkm_replace" tabindex="112" id="id_bkm_replace"
348 value="true" />
349 <label for="id_bkm_replace">
350 <?php echo __('Replace existing bookmark of same name'); ?></label>
351 </div>
352 </div>
353 <?php
356 echo '<div class="clearfloat"></div>' . "\n";
357 echo '</fieldset>' . "\n"
358 .'</div>' . "\n";
360 echo '<fieldset id="queryboxfooter" class="tblFooters">' . "\n";
361 echo '<div class="formelement">' . "\n";
362 if ($is_querywindow) {
364 <script type="text/javascript">
365 //<![CDATA[
366 document.writeln(' <input type="checkbox" name="LockFromUpdate" checked="checked" tabindex="120" id="checkbox_lock" /> <label for="checkbox_lock"><?php echo __('Do not overwrite this query from outside the window'); ?></label> ');
367 //]]>
368 </script>
369 <?php
371 echo '</div>' . "\n";
372 echo '<div class="formelement">' . "\n";
373 echo '<label for="id_sql_delimiter">[ ' . __('Delimiter')
374 .'</label>' . "\n";
375 echo '<input type="text" name="sql_delimiter" tabindex="131" size="3" '
376 .'value="' . $delimiter . '" '
377 .'id="id_sql_delimiter" /> ]' . "\n";
379 echo '<input type="checkbox" name="show_query" value="1" '
380 .'id="checkbox_show_query" tabindex="132" checked="checked" />' . "\n"
381 .'<label for="checkbox_show_query">' . __(' Show this query here again ')
382 .'</label>' . "\n";
384 echo '</div>' . "\n";
385 echo '<input type="submit" name="SQL" tabindex="200" value="' . __('Go') . '" />'
386 ."\n";
387 echo '<div class="clearfloat"></div>' . "\n";
388 echo '</fieldset>' . "\n";
392 * prints bookmark fieldset
394 * @usedby PMA_sqlQueryForm()
395 * @uses PMA_Bookmark_getList()
396 * @uses $GLOBALS['db']
397 * @uses $GLOBALS['pmaThemeImage']
398 * @uses $GLOBALS['cfg']['ReplaceHelpImg']
399 * @uses count()
400 * @uses htmlspecialchars()
402 function PMA_sqlQueryFormBookmark()
404 $bookmark_list = PMA_Bookmark_getList($GLOBALS['db']);
405 if (! $bookmark_list || count($bookmark_list) < 1) {
406 return;
409 echo '<fieldset id="bookmarkoptions">';
410 echo '<legend>';
411 echo __('Bookmarked SQL query') . '</legend>' . "\n";
412 echo '<div class="formelement">';
413 echo '<select name="id_bookmark">' . "\n";
414 echo '<option value="">&nbsp;</option>' . "\n";
415 foreach ($bookmark_list as $key => $value) {
416 echo '<option value="' . htmlspecialchars($key) . '">'
417 .htmlspecialchars($value) . '</option>' . "\n";
419 // &nbsp; is required for correct display with styles/line height
420 echo '</select>&nbsp;' . "\n";
421 echo '</div>' . "\n";
422 echo '<div class="formelement">' . "\n";
423 echo __('Variable');
424 if ($GLOBALS['cfg']['ReplaceHelpImg']) {
425 echo ' <a href="./Documentation.html#faqbookmark"'
426 .' target="documentation">'
427 .'<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_help.png"'
428 .' border="0" width="11" height="11" align="middle"'
429 .' alt="' . __('Documentation') . '" /></a> ';
430 } else {
431 echo ' (<a href="./Documentation.html#faqbookmark"'
432 .' target="documentation">' . __('Documentation') . '</a>): ';
434 echo '<input type="text" name="bookmark_variable" class="textfield"'
435 .' size="10" />' . "\n";
436 echo '</div>' . "\n";
437 echo '<div class="formelement">' . "\n";
438 echo '<input type="radio" name="action_bookmark" value="0"'
439 .' id="radio_bookmark_exe" checked="checked" />'
440 .'<label for="radio_bookmark_exe">' . __('Submit')
441 .'</label>' . "\n";
442 echo '<input type="radio" name="action_bookmark" value="1"'
443 .' id="radio_bookmark_view" />'
444 .'<label for="radio_bookmark_view">' . __('View only')
445 .'</label>' . "\n";
446 echo '<input type="radio" name="action_bookmark" value="2"'
447 .' id="radio_bookmark_del" />'
448 .'<label for="radio_bookmark_del">' . __('Delete')
449 .'</label>' . "\n";
450 echo '</div>' . "\n";
451 echo '<div class="clearfloat"></div>' . "\n";
452 echo '</fieldset>' . "\n";
454 echo '<fieldset id="bookmarkoptionsfooter" class="tblFooters">' . "\n";
455 echo '<input type="submit" name="SQL" value="' . __('Go') . '" />';
456 echo '<div class="clearfloat"></div>' . "\n";
457 echo '</fieldset>' . "\n";
461 * prints bookmark fieldset
463 * @usedby PMA_sqlQueryForm()
464 * @uses $GLOBALS['cfg']['GZipDump']
465 * @uses $GLOBALS['cfg']['BZipDump']
466 * @uses $GLOBALS['cfg']['UploadDir']
467 * @uses $GLOBALS['cfg']['AvailableCharsets']
468 * @uses $GLOBALS['charset']
469 * @uses $GLOBALS['max_upload_size']
470 * @uses PMA_supportedDecompressions()
471 * @uses PMA_getFileSelectOptions()
472 * @uses PMA_displayMaximumUploadSize()
473 * @uses PMA_generateCharsetDropdownBox()
474 * @uses PMA_generateHiddenMaxFileSize()
475 * @uses PMA_CSDROPDOWN_CHARSET
476 * @uses empty()
478 function PMA_sqlQueryFormUpload(){
479 $errors = array ();
481 $matcher = '@\.sql(\.(' . PMA_supportedDecompressions() . '))?$@'; // we allow only SQL here
483 if (!empty($GLOBALS['cfg']['UploadDir'])) {
484 $files = PMA_getFileSelectOptions(PMA_userDir($GLOBALS['cfg']['UploadDir']), $matcher, (isset($timeout_passed) && $timeout_passed && isset($local_import_file)) ? $local_import_file : '');
485 } else {
486 $files = '';
489 // start output
490 echo '<fieldset id="">';
491 echo '<legend>';
492 echo __('Location of the text file') . '</legend>';
493 echo '<div class="formelement">';
494 echo '<input type="file" name="sql_file" class="textfield" /> ';
495 echo PMA_displayMaximumUploadSize($GLOBALS['max_upload_size']);
496 // some browsers should respect this :)
497 echo PMA_generateHiddenMaxFileSize($GLOBALS['max_upload_size']) . "\n";
498 echo '</div>';
500 if ($files === FALSE) {
501 $errors[] = PMA_Message::error( __('The directory you set for upload work cannot be reached'));
502 } elseif (!empty($files)) {
503 echo '<div class="formelement">';
504 echo '<strong>' . __('web server upload directory') .':</strong>' . "\n";
505 echo '<select size="1" name="sql_localfile">' . "\n";
506 echo '<option value="" selected="selected"></option>' . "\n";
507 echo $files;
508 echo '</select>' . "\n";
509 echo '</div>';
512 echo '<div class="clearfloat"></div>' . "\n";
513 echo '</fieldset>';
516 echo '<fieldset id="" class="tblFooters">';
517 echo __('Character set of the file:') . "\n";
518 echo PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_CHARSET,
519 'charset_of_file', null, 'utf8', FALSE);
520 echo '<input type="submit" name="SQL" value="' . __('Go')
521 .'" />' . "\n";
522 echo '<div class="clearfloat"></div>' . "\n";
523 echo '</fieldset>';
525 foreach ($errors as $error) {
526 $error->display();