Merge branch 'blinky/master' into gsoc2010-blinky
[phpmyadmin.git] / libraries / sql_query_form.lib.php
blobd9386993341b7e3ea71ff2fd2ad5049ee8c6aec8
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 . ' id="sqlqueryform"'
119 .' onsubmit="return checkSqlQuery(this)" name="sqlform">' . "\n";
122 if ($is_querywindow) {
123 echo '<input type="hidden" name="focus_querywindow" value="true" />'
124 ."\n";
125 if ($display_tab != 'sql' && $display_tab != 'full') {
126 echo '<input type="hidden" name="sql_query" value="" />' . "\n";
127 echo '<input type="hidden" name="show_query" value="1" />' . "\n";
130 echo '<input type="hidden" name="is_js_confirmed" value="0" />' . "\n"
131 .PMA_generate_common_hidden_inputs($db, $table) . "\n"
132 .'<input type="hidden" name="pos" value="0" />' . "\n"
133 .'<input type="hidden" name="goto" value="'
134 .htmlspecialchars($goto) . '" />' . "\n"
135 .'<input type="hidden" name="zero_rows" value="'
136 . htmlspecialchars(__('Your SQL query has been executed successfully')) . '" />' . "\n"
137 .'<input type="hidden" name="prev_sql_query" value="'
138 . htmlspecialchars($query) . '" />' . "\n";
140 // display querybox
141 if ($display_tab === 'full' || $display_tab === 'sql') {
142 PMA_sqlQueryFormInsert($query, $is_querywindow, $delimiter);
145 // display uploads
146 if ($display_tab === 'files' && $GLOBALS['is_upload']) {
147 PMA_sqlQueryFormUpload();
150 // Bookmark Support
151 if ($display_tab === 'full' || $display_tab === 'history') {
152 if (! empty($GLOBALS['cfg']['Bookmark'])) {
153 PMA_sqlQueryFormBookmark();
157 // Encoding setting form appended by Y.Kawada
158 if (function_exists('PMA_set_enc_form')) {
159 echo PMA_set_enc_form(' ');
162 echo '</form>' . "\n";
163 if ($is_querywindow) {
165 <script type="text/javascript">
166 //<![CDATA[
167 if (window.opener) {
168 window.opener.parent.insertQuery();
170 //]]>
171 </script>
172 <?php
177 * prints querybox fieldset
179 * @usedby PMA_sqlQueryForm()
180 * @uses $GLOBALS['text_dir']
181 * @uses $GLOBALS['cfg']['TextareaAutoSelect']
182 * @uses $GLOBALS['cfg']['TextareaCols']
183 * @uses $GLOBALS['cfg']['TextareaRows']
184 * @uses PMA_USR_OS
185 * @uses PMA_USR_BROWSER_AGENT
186 * @uses PMA_USR_BROWSER_VER
187 * @uses htmlspecialchars()
188 * @param string $query query to display in the textarea
189 * @param boolean $is_querywindow if inside querywindow or not
190 * @param string $delimiter default delimiter to use
192 function PMA_sqlQueryFormInsert($query = '', $is_querywindow = false, $delimiter = ';')
195 // enable auto select text in textarea
196 if ($GLOBALS['cfg']['TextareaAutoSelect']) {
197 $auto_sel = ' onfocus="selectContent(this, sql_box_locked, true)"';
198 } else {
199 $auto_sel = '';
202 // enable locking if inside query window
203 if ($is_querywindow) {
204 $locking = ' onkeypress="document.sqlform.elements[\'LockFromUpdate\'].'
205 .'checked = true;"';
206 $height = $GLOBALS['cfg']['TextareaRows'] * 1.25;
207 } else {
208 $locking = '';
209 $height = $GLOBALS['cfg']['TextareaRows'] * 2;
212 $table = '';
213 $db = '';
214 $fields_list = array();
215 if (! strlen($GLOBALS['db'])) {
216 // prepare for server related
217 $legend = sprintf(__('Run SQL query/queries on server %s'),
218 '&quot;' . htmlspecialchars(
219 ! empty($GLOBALS['cfg']['Servers'][$GLOBALS['server']]['verbose']) ? $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['verbose'] : $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['host']) . '&quot;');
220 } elseif (! strlen($GLOBALS['table'])) {
221 // prepare for db related
222 $db = $GLOBALS['db'];
223 // if you want navigation:
224 $tmp_db_link = '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase']
225 . '?' . PMA_generate_common_url($db) . '"';
226 if ($is_querywindow) {
227 $tmp_db_link .= ' target="_self"'
228 . ' onclick="this.target=window.opener.frame_content.name"';
230 $tmp_db_link .= '>'
231 . htmlspecialchars($db) . '</a>';
232 // else use
233 // $tmp_db_link = htmlspecialchars($db);
234 $legend = sprintf(__('Run SQL query/queries on database %s'), $tmp_db_link);
235 if (empty($query)) {
236 $query = PMA_expandUserString($GLOBALS['cfg']['DefaultQueryDatabase'], 'PMA_backquote');
238 } else {
239 $table = $GLOBALS['table'];
240 $db = $GLOBALS['db'];
241 // Get the list and number of fields
242 // we do a try_query here, because we could be in the query window,
243 // trying to synchonize and the table has not yet been created
244 $fields_list = PMA_DBI_fetch_result(
245 'SHOW FULL COLUMNS FROM ' . PMA_backquote($db)
246 . '.' . PMA_backquote($GLOBALS['table']));
248 $tmp_db_link = '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase']
249 . '?' . PMA_generate_common_url($db) . '"';
250 if ($is_querywindow) {
251 $tmp_db_link .= ' target="_self"'
252 . ' onclick="this.target=window.opener.frame_content.name"';
254 $tmp_db_link .= '>'
255 . htmlspecialchars($db) . '</a>';
256 // else use
257 // $tmp_db_link = htmlspecialchars($db);
258 $legend = sprintf(__('Run SQL query/queries on database %s'), $tmp_db_link);
259 if (empty($query)) {
260 $query = PMA_expandUserString($GLOBALS['cfg']['DefaultQueryTable'], 'PMA_backquote');
263 $legend .= ': ' . PMA_showMySQLDocu('SQL-Syntax', 'SELECT');
265 if (count($fields_list)) {
266 $sqlquerycontainer_id = 'sqlquerycontainer';
267 } else {
268 $sqlquerycontainer_id = 'sqlquerycontainerfull';
271 echo '<a name="querybox"></a>' . "\n"
272 .'<div id="queryboxcontainer">' . "\n"
273 .'<fieldset id="querybox">' . "\n";
274 echo '<legend>' . $legend . '</legend>' . "\n";
275 echo '<div id="queryfieldscontainer">' . "\n";
276 echo '<div id="' . $sqlquerycontainer_id . '">' . "\n"
277 .'<textarea tabindex="100" name="sql_query" id="sqlquery"'
278 .' cols="' . $GLOBALS['cfg']['TextareaCols'] . '"'
279 .' rows="' . $height . '"'
280 .' dir="' . $GLOBALS['text_dir'] . '"'
281 .$auto_sel . $locking . '>' . htmlspecialchars($query) . '</textarea>' . "\n";
282 // Add buttons to generate query easily for select all,single select,insert,update and delete
283 if(count($fields_list)) {
284 echo '<input type="button" value="SELECT *" id="selectall" class="sqlbutton" />';
285 echo '<input type="button" value="SELECT" id="select" class="sqlbutton" />';
286 echo '<input type="button" value="INSERT" id="insert" class="sqlbutton" />';
287 echo '<input type="button" value="UPDATE" id="update" class="sqlbutton" />';
288 echo '<input type="button" value="DELETE" id="delete" class="sqlbutton" />';
290 echo '</div>' . "\n";
292 if (count($fields_list)) {
293 echo '<div id="tablefieldscontainer">' . "\n"
294 .'<label>' . __('Columns') . '</label>' . "\n"
295 .'<select id="tablefields" name="dummy" '
296 .'size="' . ($GLOBALS['cfg']['TextareaRows'] - 2) . '" '
297 .'multiple="multiple" ondblclick="insertValueQuery()">' . "\n";
298 foreach ($fields_list as $field) {
299 echo '<option value="'
300 .PMA_backquote(htmlspecialchars($field['Field'])) . '"';
301 if (isset($field['Field']) && strlen($field['Field']) && isset($field['Comment'])) {
302 echo ' title="' . htmlspecialchars($field['Comment']) . '"';
304 echo '>' . htmlspecialchars($field['Field']) . '</option>' . "\n";
306 echo '</select>' . "\n"
307 .'<div id="tablefieldinsertbuttoncontainer">' . "\n";
308 if ($GLOBALS['cfg']['PropertiesIconic']) {
309 echo '<input type="button" name="insert" value="&lt;&lt;"'
310 .' onclick="insertValueQuery()"'
311 .' title="' . __('Insert') . '" />' . "\n";
312 } else {
313 echo '<input type="button" name="insert"'
314 .' value="' . __('Insert') . '"'
315 .' onclick="insertValueQuery()" />' . "\n";
317 echo '</div>' . "\n"
318 .'</div>' . "\n";
321 echo '<div class="clearfloat"></div>' . "\n";
322 echo '</div>' . "\n";
324 if (! empty($GLOBALS['cfg']['Bookmark'])) {
326 <div id="bookmarkoptions">
327 <div class="formelement">
328 <label for="bkm_label">
329 <?php echo __('Bookmark this SQL query'); ?>:</label>
330 <input type="text" name="bkm_label" id="bkm_label" tabindex="110" value="" />
331 </div>
332 <div class="formelement">
333 <input type="checkbox" name="bkm_all_users" tabindex="111" id="id_bkm_all_users"
334 value="true" />
335 <label for="id_bkm_all_users">
336 <?php echo __('Let every user access this bookmark'); ?></label>
337 </div>
338 <div class="formelement">
339 <input type="checkbox" name="bkm_replace" tabindex="112" id="id_bkm_replace"
340 value="true" />
341 <label for="id_bkm_replace">
342 <?php echo __('Replace existing bookmark of same name'); ?></label>
343 </div>
344 </div>
345 <?php
348 echo '<div class="clearfloat"></div>' . "\n";
349 echo '</fieldset>' . "\n"
350 .'</div>' . "\n";
352 echo '<fieldset id="queryboxfooter" class="tblFooters">' . "\n";
353 echo '<div class="formelement">' . "\n";
354 if ($is_querywindow) {
356 <script type="text/javascript">
357 //<![CDATA[
358 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> ');
359 //]]>
360 </script>
361 <?php
363 echo '</div>' . "\n";
364 echo '<div class="formelement">' . "\n";
365 echo '<label for="id_sql_delimiter">[ ' . __('Delimiter')
366 .'</label>' . "\n";
367 echo '<input type="text" name="sql_delimiter" tabindex="131" size="3" '
368 .'value="' . $delimiter . '" '
369 .'id="id_sql_delimiter" /> ]' . "\n";
371 echo '<input type="checkbox" name="show_query" value="1" '
372 .'id="checkbox_show_query" tabindex="132" checked="checked" />' . "\n"
373 .'<label for="checkbox_show_query">' . __(' Show this query here again ')
374 .'</label>' . "\n";
376 echo '</div>' . "\n";
377 echo '<input type="submit" name="SQL" tabindex="200" value="' . __('Go') . '" />'
378 ."\n";
379 echo '<div class="clearfloat"></div>' . "\n";
380 echo '</fieldset>' . "\n";
384 * prints bookmark fieldset
386 * @usedby PMA_sqlQueryForm()
387 * @uses PMA_Bookmark_getList()
388 * @uses $GLOBALS['db']
389 * @uses $GLOBALS['pmaThemeImage']
390 * @uses $GLOBALS['cfg']['ReplaceHelpImg']
391 * @uses count()
392 * @uses htmlspecialchars()
394 function PMA_sqlQueryFormBookmark()
396 $bookmark_list = PMA_Bookmark_getList($GLOBALS['db']);
397 if (! $bookmark_list || count($bookmark_list) < 1) {
398 return;
401 echo '<fieldset id="bookmarkoptions">';
402 echo '<legend>';
403 echo __('Bookmarked SQL query') . '</legend>' . "\n";
404 echo '<div class="formelement">';
405 echo '<select name="id_bookmark">' . "\n";
406 echo '<option value="">&nbsp;</option>' . "\n";
407 foreach ($bookmark_list as $key => $value) {
408 echo '<option value="' . htmlspecialchars($key) . '">'
409 .htmlspecialchars($value) . '</option>' . "\n";
411 // &nbsp; is required for correct display with styles/line height
412 echo '</select>&nbsp;' . "\n";
413 echo '</div>' . "\n";
414 echo '<div class="formelement">' . "\n";
415 echo __('Variable');
416 if ($GLOBALS['cfg']['ReplaceHelpImg']) {
417 echo ' <a href="./Documentation.html#faqbookmark"'
418 .' target="documentation">'
419 .'<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_help.png"'
420 .' border="0" width="11" height="11" align="middle"'
421 .' alt="' . __('Documentation') . '" /></a> ';
422 } else {
423 echo ' (<a href="./Documentation.html#faqbookmark"'
424 .' target="documentation">' . __('Documentation') . '</a>): ';
426 echo '<input type="text" name="bookmark_variable" class="textfield"'
427 .' size="10" />' . "\n";
428 echo '</div>' . "\n";
429 echo '<div class="formelement">' . "\n";
430 echo '<input type="radio" name="action_bookmark" value="0"'
431 .' id="radio_bookmark_exe" checked="checked" />'
432 .'<label for="radio_bookmark_exe">' . __('Submit')
433 .'</label>' . "\n";
434 echo '<input type="radio" name="action_bookmark" value="1"'
435 .' id="radio_bookmark_view" />'
436 .'<label for="radio_bookmark_view">' . __('View only')
437 .'</label>' . "\n";
438 echo '<input type="radio" name="action_bookmark" value="2"'
439 .' id="radio_bookmark_del" />'
440 .'<label for="radio_bookmark_del">' . __('Delete')
441 .'</label>' . "\n";
442 echo '</div>' . "\n";
443 echo '<div class="clearfloat"></div>' . "\n";
444 echo '</fieldset>' . "\n";
446 echo '<fieldset id="bookmarkoptionsfooter" class="tblFooters">' . "\n";
447 echo '<input type="submit" name="SQL" value="' . __('Go') . '" />';
448 echo '<div class="clearfloat"></div>' . "\n";
449 echo '</fieldset>' . "\n";
453 * prints bookmark fieldset
455 * @usedby PMA_sqlQueryForm()
456 * @uses $GLOBALS['cfg']['GZipDump']
457 * @uses $GLOBALS['cfg']['BZipDump']
458 * @uses $GLOBALS['cfg']['UploadDir']
459 * @uses $GLOBALS['cfg']['AvailableCharsets']
460 * @uses $GLOBALS['charset']
461 * @uses $GLOBALS['max_upload_size']
462 * @uses PMA_supportedDecompressions()
463 * @uses PMA_getFileSelectOptions()
464 * @uses PMA_displayMaximumUploadSize()
465 * @uses PMA_generateCharsetDropdownBox()
466 * @uses PMA_generateHiddenMaxFileSize()
467 * @uses PMA_CSDROPDOWN_CHARSET
468 * @uses empty()
470 function PMA_sqlQueryFormUpload(){
471 $errors = array ();
473 $matcher = '@\.sql(\.(' . PMA_supportedDecompressions() . '))?$@'; // we allow only SQL here
475 if (!empty($GLOBALS['cfg']['UploadDir'])) {
476 $files = PMA_getFileSelectOptions(PMA_userDir($GLOBALS['cfg']['UploadDir']), $matcher, (isset($timeout_passed) && $timeout_passed && isset($local_import_file)) ? $local_import_file : '');
477 } else {
478 $files = '';
481 // start output
482 echo '<fieldset id="">';
483 echo '<legend>';
484 echo __('Location of the text file') . '</legend>';
485 echo '<div class="formelement">';
486 echo '<input type="file" name="sql_file" class="textfield" /> ';
487 echo PMA_displayMaximumUploadSize($GLOBALS['max_upload_size']);
488 // some browsers should respect this :)
489 echo PMA_generateHiddenMaxFileSize($GLOBALS['max_upload_size']) . "\n";
490 echo '</div>';
492 if ($files === FALSE) {
493 $errors[] = PMA_Message::error( __('The directory you set for upload work cannot be reached'));
494 } elseif (!empty($files)) {
495 echo '<div class="formelement">';
496 echo '<strong>' . __('web server upload directory') .':</strong>' . "\n";
497 echo '<select size="1" name="sql_localfile">' . "\n";
498 echo '<option value="" selected="selected"></option>' . "\n";
499 echo $files;
500 echo '</select>' . "\n";
501 echo '</div>';
504 echo '<div class="clearfloat"></div>' . "\n";
505 echo '</fieldset>';
508 echo '<fieldset id="" class="tblFooters">';
509 echo __('Character set of the file:') . "\n";
510 echo PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_CHARSET,
511 'charset_of_file', null, 'utf8', FALSE);
512 echo '<input type="submit" name="SQL" value="' . __('Go')
513 .'" />' . "\n";
514 echo '<div class="clearfloat"></div>' . "\n";
515 echo '</fieldset>';
517 foreach ($errors as $error) {
518 $error->display();