3.3.0-rc2
[phpmyadmin/madhuracj.git] / libraries / sql_query_form.lib.php
blob97a15e310fb7a77962e61d75b967df510acee478
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 $GLOBALS['strSuccess']
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($GLOBALS['strSuccess']) . '" />' . "\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 $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'])) {
335 <div id="bookmarkoptions">
336 <div class="formelement">
337 <label for="bkm_label">
338 <?php echo $GLOBALS['strBookmarkThis']; ?>:</label>
339 <input type="text" name="bkm_label" id="bkm_label" value="" />
340 </div>
341 <div class="formelement">
342 <input type="checkbox" name="bkm_all_users" id="id_bkm_all_users"
343 value="true" />
344 <label for="id_bkm_all_users">
345 <?php echo $GLOBALS['strBookmarkAllUsers']; ?></label>
346 </div>
347 <div class="formelement">
348 <input type="checkbox" name="bkm_replace" id="id_bkm_replace"
349 value="true" />
350 <label for="id_bkm_replace">
351 <?php echo $GLOBALS['strBookmarkReplace']; ?></label>
352 </div>
353 </div>
354 <?php
357 echo '<div class="clearfloat"></div>' . "\n";
358 echo '</fieldset>' . "\n"
359 .'</div>' . "\n";
361 echo '<fieldset id="queryboxfooter" class="tblFooters">' . "\n";
362 echo '<div class="formelement">' . "\n";
363 if ($is_querywindow) {
365 <script type="text/javascript">
366 //<![CDATA[
367 document.writeln(' <input type="checkbox" name="LockFromUpdate" value="1" id="checkbox_lock" /> <label for="checkbox_lock"><?php echo $GLOBALS['strQueryWindowLock']; ?></label> ');
368 //]]>
369 </script>
370 <?php
372 echo '</div>' . "\n";
373 echo '<div class="formelement">' . "\n";
374 echo '<label for="id_sql_delimiter">[ ' . $GLOBALS['strDelimiter']
375 .'</label>' . "\n";
376 echo '<input type="text" name="sql_delimiter" size="3" '
377 .'value="' . $delimiter . '" '
378 .'id="id_sql_delimiter" /> ]' . "\n";
380 echo '<input type="checkbox" name="show_query" value="1" '
381 .'id="checkbox_show_query" checked="checked" />' . "\n"
382 .'<label for="checkbox_show_query">' . $GLOBALS['strShowThisQuery']
383 .'</label>' . "\n";
385 echo '</div>' . "\n";
386 echo '<input type="submit" name="SQL" value="' . $GLOBALS['strGo'] . '" />'
387 ."\n";
388 echo '<div class="clearfloat"></div>' . "\n";
389 echo '</fieldset>' . "\n";
393 * prints bookmark fieldset
395 * @usedby PMA_sqlQueryForm()
396 * @uses PMA_Bookmark_getList()
397 * @uses $GLOBALS['db']
398 * @uses $GLOBALS['pmaThemeImage']
399 * @uses $GLOBALS['cfg']['ReplaceHelpImg']
400 * @uses $GLOBALS['strBookmarkQuery']
401 * @uses $GLOBALS['strBookmarkView']
402 * @uses $GLOBALS['strDelete']
403 * @uses $GLOBALS['strDocu']
404 * @uses $GLOBALS['strGo']
405 * @uses $GLOBALS['strSubmit']
406 * @uses $GLOBALS['strVar']
407 * @uses count()
408 * @uses htmlspecialchars()
410 function PMA_sqlQueryFormBookmark()
412 $bookmark_list = PMA_Bookmark_getList($GLOBALS['db']);
413 if (! $bookmark_list || count($bookmark_list) < 1) {
414 return;
417 echo '<fieldset id="bookmarkoptions">';
418 echo '<legend>';
419 echo $GLOBALS['strBookmarkQuery'] . '</legend>' . "\n";
420 echo '<div class="formelement">';
421 echo '<select name="id_bookmark">' . "\n";
422 echo '<option value="">&nbsp;</option>' . "\n";
423 foreach ($bookmark_list as $key => $value) {
424 echo '<option value="' . htmlspecialchars($key) . '">'
425 .htmlspecialchars($value) . '</option>' . "\n";
427 // &nbsp; is required for correct display with styles/line height
428 echo '</select>&nbsp;' . "\n";
429 echo '</div>' . "\n";
430 echo '<div class="formelement">' . "\n";
431 echo $GLOBALS['strVar'];
432 if ($GLOBALS['cfg']['ReplaceHelpImg']) {
433 echo ' <a href="./Documentation.html#faqbookmark"'
434 .' target="documentation">'
435 .'<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_help.png"'
436 .' border="0" width="11" height="11" align="middle"'
437 .' alt="' . $GLOBALS['strDocu'] . '" /></a> ';
438 } else {
439 echo ' (<a href="./Documentation.html#faqbookmark"'
440 .' target="documentation">' . $GLOBALS['strDocu'] . '</a>): ';
442 echo '<input type="text" name="bookmark_variable" class="textfield"'
443 .' size="10" />' . "\n";
444 echo '</div>' . "\n";
445 echo '<div class="formelement">' . "\n";
446 echo '<input type="radio" name="action_bookmark" value="0"'
447 .' id="radio_bookmark_exe" checked="checked" />'
448 .'<label for="radio_bookmark_exe">' . $GLOBALS['strSubmit']
449 .'</label>' . "\n";
450 echo '<input type="radio" name="action_bookmark" value="1"'
451 .' id="radio_bookmark_view" />'
452 .'<label for="radio_bookmark_view">' . $GLOBALS['strBookmarkView']
453 .'</label>' . "\n";
454 echo '<input type="radio" name="action_bookmark" value="2"'
455 .' id="radio_bookmark_del" />'
456 .'<label for="radio_bookmark_del">' . $GLOBALS['strDelete']
457 .'</label>' . "\n";
458 echo '</div>' . "\n";
459 echo '<div class="clearfloat"></div>' . "\n";
460 echo '</fieldset>' . "\n";
462 echo '<fieldset id="bookmarkoptionsfooter" class="tblFooters">' . "\n";
463 echo '<input type="submit" name="SQL" value="' . $GLOBALS['strGo'] . '" />';
464 echo '<div class="clearfloat"></div>' . "\n";
465 echo '</fieldset>' . "\n";
469 * prints bookmark fieldset
471 * @usedby PMA_sqlQueryForm()
472 * @uses $GLOBALS['cfg']['GZipDump']
473 * @uses $GLOBALS['cfg']['BZipDump']
474 * @uses $GLOBALS['cfg']['UploadDir']
475 * @uses $GLOBALS['cfg']['AvailableCharsets']
476 * @uses $GLOBALS['cfg']['AllowAnywhereRecoding']
477 * @uses $GLOBALS['strBzip']
478 * @uses $GLOBALS['strCharsetOfFile']
479 * @uses $GLOBALS['strCompression']
480 * @uses $GLOBALS['strError']
481 * @uses $GLOBALS['strGo']
482 * @uses $GLOBALS['strGzip']
483 * @uses $GLOBALS['strLocationTextfile']
484 * @uses $GLOBALS['strWebServerUploadDirectory']
485 * @uses $GLOBALS['strWebServerUploadDirectoryError']
486 * @uses $GLOBALS['charset']
487 * @uses $GLOBALS['max_upload_size']
488 * @uses PMA_supportedDecompressions()
489 * @uses PMA_getFileSelectOptions()
490 * @uses PMA_displayMaximumUploadSize()
491 * @uses PMA_generateCharsetDropdownBox()
492 * @uses PMA_generateHiddenMaxFileSize()
493 * @uses PMA_CSDROPDOWN_CHARSET
494 * @uses empty()
496 function PMA_sqlQueryFormUpload(){
497 $errors = array ();
499 $matcher = '@\.sql(\.(' . PMA_supportedDecompressions() . '))?$@'; // we allow only SQL here
501 if (!empty($GLOBALS['cfg']['UploadDir'])) {
502 $files = PMA_getFileSelectOptions(PMA_userDir($GLOBALS['cfg']['UploadDir']), $matcher, (isset($timeout_passed) && $timeout_passed && isset($local_import_file)) ? $local_import_file : '');
503 } else {
504 $files = '';
507 // start output
508 echo '<fieldset id="">';
509 echo '<legend>';
510 echo $GLOBALS['strLocationTextfile'] . '</legend>';
511 echo '<div class="formelement">';
512 echo '<input type="file" name="sql_file" class="textfield" /> ';
513 echo PMA_displayMaximumUploadSize($GLOBALS['max_upload_size']);
514 // some browsers should respect this :)
515 echo PMA_generateHiddenMaxFileSize($GLOBALS['max_upload_size']) . "\n";
516 echo '</div>';
518 if ($files === FALSE) {
519 $errors[] = PMA_Message::error('strWebServerUploadDirectoryError');
520 } elseif (!empty($files)) {
521 echo '<div class="formelement">';
522 echo '<strong>' . $GLOBALS['strWebServerUploadDirectory'] .':</strong>' . "\n";
523 echo '<select size="1" name="sql_localfile">' . "\n";
524 echo '<option value="" selected="selected"></option>' . "\n";
525 echo $files;
526 echo '</select>' . "\n";
527 echo '</div>';
530 echo '<div class="clearfloat"></div>' . "\n";
531 echo '</fieldset>';
534 echo '<fieldset id="" class="tblFooters">';
535 echo $GLOBALS['strCharsetOfFile'] . "\n";
536 echo PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_CHARSET,
537 'charset_of_file', null, 'utf8', FALSE);
538 echo '<input type="submit" name="SQL" value="' . $GLOBALS['strGo']
539 .'" />' . "\n";
540 echo '<div class="clearfloat"></div>' . "\n";
541 echo '</fieldset>';
543 foreach ($errors as $error) {
544 $error->display();