Rev Author Line No. Line
250 kaklik 1 <?php
2 /* $Id: sql_query_form.lib.php,v 1.29.2.1 2006/02/18 13:54:37 cybot_tm Exp $ */
3 // vim: expandtab sw=4 ts=4 sts=4:
4 /**
5 * functions for displaying the sql query form
6 *
7 * @usedby server_sql.php
8 * @usedby db_details.php
9 * @usedby tbl_properties.php
10 * @usedby tbl_properties_structure.php
11 * @usedby querywindow.php
12 */
13  
14 require_once './libraries/file_listing.php'; // used for file listing
15 require_once './libraries/bookmark.lib.php'; // used for file listing
16  
17 /**
18 * prints the sql query boxes
19 *
20 * @usedby server_sql.php
21 * @usedby db_details.php
22 * @usedby tbl_properties.php
23 * @usedby tbl_properties_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
60 */
61 function PMA_sqlQueryForm($query = true, $display_tab = false)
62 {
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;
69 }
70  
71 // query to show
72 if (true === $query) {
73 $query = empty($GLOBALS['sql_query']) ? '' : $GLOBALS['sql_query'];
74 }
75  
76 // set enctype to multipart for file uploads
77 if ($GLOBALS['is_upload']) {
78 $enctype = ' enctype="multipart/form-data"';
79 } else {
80 $enctype = '';
81 }
82  
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_details.php' : $GLOBALS['goto'];
94 } else {
95 $table = $GLOBALS['table'];
96 $db = $GLOBALS['db'];
97 $goto = empty($GLOBALS['goto']) ?
98 'tbl_properties.php' : $GLOBALS['goto'];
99 }
100  
101  
102 // start output
103 if ($is_querywindow) {
104 ?>
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.frames[1].name;
108 window.opener.parent.frames[1].name = save_name + '<?php echo time(); ?>';
109 this.target = window.opener.parent.frames[1].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";
115 }
116  
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";
123 }
124 }
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";
134  
135 // display querybox
136 if ($display_tab === 'full' || $display_tab === 'sql') {
137 PMA_sqlQueryFormInsert($query, $is_querywindow);
138 }
139  
140 // display uploads
141 if ($display_tab === 'files' && $GLOBALS['is_upload']) {
142 PMA_sqlQueryFormUpload();
143 }
144  
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();
151 }
152 }
153  
154 // Encoding setting form appended by Y.Kawada
155 if (function_exists('PMA_set_enc_form')) {
156 echo PMA_set_enc_form(' ');
157 }
158  
159 echo '</form>' . "\n";
160 }
161  
162 /**
163 * prints querybox fieldset
164 *
165 * @usedby PMA_sqlQueryForm()
166 * @uses $GLOBALS['text_dir']
167 * @uses $GLOBALS['cfg']['TextareaAutoSelect']
168 * @uses $GLOBALS['cfg']['TextareaCols']
169 * @uses $GLOBALS['cfg']['TextareaRows']
170 * @uses $GLOBALS['strShowThisQuery']
171 * @uses $GLOBALS['strGo']
172 * @uses PMA_availableDatabases()
173 * @uses PMA_USR_OS
174 * @uses PMA_USR_BROWSER_AGENT
175 * @uses PMA_USR_BROWSER_VER
176 * @uses PMA_availableDatabases()
177 * @uses htmlspecialchars()
178 * @param string $query query to display in the textarea
179 * @param boolean $is_querywindow if inside querywindow or not
180 */
181 function PMA_sqlQueryFormInsert($query = '', $is_querywindow = false)
182 {
183  
184 // enable auto select text in textarea
185 if ($GLOBALS['cfg']['TextareaAutoSelect']) {
186 $auto_sel = ' onfocus="selectContent( this, sql_box_locked, true )"';
187 } else {
188 $auto_sel = '';
189 }
190  
191 // enable locking if inside query window
192 if ($is_querywindow) {
193 $locking = ' onkeypress="document.sqlform.elements[\'LockFromUpdate\'].'
194 .'checked = true;"';
195 } else {
196 $locking = '';
197 }
198  
199 $table = '';
200 $db = '';
201 $fields_list = array();
202 if (! isset($GLOBALS['db']) || ! strlen($GLOBALS['db'])) {
203 // prepare for server related
204 $legend = sprintf($GLOBALS['strRunSQLQueryOnServer'],
205 htmlspecialchars(
206 $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['host']));
207 } elseif (! isset($GLOBALS['table']) || ! strlen($GLOBALS['table'])) {
208 // prepare for db related
209 $db = $GLOBALS['db'];
210 // if you want navigation:
211 $strDBLink = '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase']
212 . '?' . PMA_generate_common_url($db) . '"';
213 if ($is_querywindow) {
214 $strDBLink .= ' target="_self"'
215 . ' onclick="this.target=window.opener.frames[1].name"';
216 }
217 $strDBLink .= '>'
218 . htmlspecialchars($db) . '</a>';
219 // else use
220 // $strDBLink = htmlspecialchars($db);
221 $legend = sprintf($GLOBALS['strRunSQLQuery'], $strDBLink);
222 if (empty($query)) {
223 $query = str_replace('%d',
224 PMA_backquote($db), $GLOBALS['cfg']['DefaultQueryDatabase']);
225 }
226 } else {
227 $table = $GLOBALS['table'];
228 $db = $GLOBALS['db'];
229 // Get the list and number of fields
230 // we do a try_query here, because we could be in the query window,
231 // trying to synchonize and the table has not yet been created
232 $fields_list = PMA_DBI_fetch_result(
233 'SHOW FULL COLUMNS FROM ' . PMA_backquote($db)
234 . '.' . PMA_backquote($GLOBALS['table']));
235  
236 $strDBLink = '<a href="' . $GLOBALS['cfg']['DefaultTabDatabase']
237 . '?' . PMA_generate_common_url($db) . '"';
238 if ($is_querywindow) {
239 $strDBLink .= ' target="_self"'
240 . ' onclick="this.target=window.opener.frames[1].name"';
241 }
242 $strDBLink .= '>'
243 . htmlspecialchars($db) . '</a>';
244 // else use
245 // $strDBLink = htmlspecialchars($db);
246 $legend = sprintf($GLOBALS['strRunSQLQuery'], $strDBLink);
247 if (empty($query) && count($fields_list)) {
248 $field_names = array();
249 foreach ($fields_list as $field) {
250 $field_names[] = PMA_backquote($field['Field']);
251 }
252 $query =
253 str_replace('%d', PMA_backquote($db),
254 str_replace('%t', PMA_backquote($table),
255 str_replace('%f',
256 implode(', ', $field_names ),
257 $GLOBALS['cfg']['DefaultQueryTable'])));
258 unset($field_names);
259 }
260 }
261 $legend .= ': ' . PMA_showMySQLDocu('SQL-Syntax', 'SELECT');
262  
263 if (count($fields_list)) {
264 $sqlquerycontainer_id = 'sqlquerycontainer';
265 } else {
266 $sqlquerycontainer_id = 'sqlquerycontainerfull';
267 }
268  
269 echo '<a name="querybox"></a>' . "\n"
270 .'<div id="queryboxcontainer">' . "\n"
271 .'<fieldset id="querybox">' . "\n";
272 echo '<legend>' . $legend . '</legend>' . "\n";
273 echo '<div id="queryfieldscontainer">' . "\n";
274 echo '<div id="' . $sqlquerycontainer_id . '">' . "\n"
275 .'<textarea name="sql_query" id="sqlquery"'
276 .' cols="' . $GLOBALS['cfg']['TextareaCols'] . '"'
277 .' rows="' . $GLOBALS['cfg']['TextareaRows'] . '"'
278 .' dir="' . $GLOBALS['text_dir'] . '"'
279 .$auto_sel . $locking . '>' . htmlspecialchars($query) . '</textarea>' . "\n";
280 echo '</div>' . "\n";
281  
282 if (count($fields_list)) {
283 echo '<div id="tablefieldscontainer">' . "\n"
284 .'<label>' . $GLOBALS['strFields'] . '</label>' . "\n"
285 .'<select id="tablefields" name="dummy" '
286 .'size="' . ($GLOBALS['cfg']['TextareaRows'] - 2) . '" '
287 .'multiple="multiple" ondblclick="insertValueQuery()">' . "\n";
288 foreach ($fields_list as $field) {
289 echo '<option value="'
290 .PMA_backquote(htmlspecialchars($field['Field'])) . '"';
291 if (isset($field['Field']) && strlen($field['Field']) && isset($field['Comment'])) {
292 echo ' title="' . htmlspecialchars($field['Comment']) . '"';
293 }
294 echo '>' . htmlspecialchars( $field['Field'] ) . '</option>' . "\n";
295 }
296 echo '</select>' . "\n"
297 .'<div id="tablefieldinsertbuttoncontainer">' . "\n";
298 if ( $GLOBALS['cfg']['PropertiesIconic'] ) {
299 echo '<input type="button" name="insert" value="&lt;&lt;"'
300 .' onclick="insertValueQuery()"'
301 .' title="' . $GLOBALS['strInsert'] . '" />' . "\n";
302 } else {
303 echo '<input type="button" name="insert"'
304 .' value="' . $GLOBALS['strInsert'] . '"'
305 .' onclick="insertValueQuery()" />' . "\n";
306 }
307 echo '</div>' . "\n"
308 .'</div>' . "\n";
309 }
310  
311 echo '<div class="clearfloat"></div>' . "\n";
312 echo '</div>' . "\n";
313  
314 if (! empty($GLOBALS['cfg']['Bookmark'])
315 && $GLOBALS['cfg']['Bookmark']['db']
316 && $GLOBALS['cfg']['Bookmark']['table']) {
317 ?>
318 <div id="bookmarkoptions">
319 <div class="formelement">
320 <label for="bkm_label">
321 <?php echo $GLOBALS['strBookmarkThis']; ?>:</label>
322 <input type="text" name="bkm_label" id="bkm_label" value="" />
323 </div>
324 <div class="formelement">
325 <input type="checkbox" name="bkm_all_users" id="id_bkm_all_users"
326 value="true" />
327 <label for="id_bkm_all_users">
328 <?php echo $GLOBALS['strBookmarkAllUsers']; ?></label>
329 </div>
330 <div class="formelement">
331 <input type="checkbox" name="bkm_replace" id="id_bkm_replace"
332 value="true" />
333 <label for="id_bkm_replace">
334 <?php echo $GLOBALS['strBookmarkReplace']; ?></label>
335 </div>
336 </div>
337 <?php
338 }
339  
340 echo '<div class="clearfloat"></div>' . "\n";
341 echo '</fieldset>' . "\n"
342 .'</div>' . "\n";
343  
344 echo '<fieldset id="queryboxfooter" class="tblFooters">' . "\n";
345 echo '<div class="formelement">' . "\n";
346 if ($is_querywindow) {
347 ?>
348 <script type="text/javascript" language="javascript">
349 //<![CDATA[
350 document.writeln(' <input type="checkbox" name="LockFromUpdate" value="1" id="checkbox_lock" /> <label for="checkbox_lock"><?php echo $GLOBALS['strQueryWindowLock']; ?></label> ');
351 //]]>
352 </script>
353 <?php
354 }
355 echo '</div>' . "\n";
356 echo '<div class="formelement">' . "\n";
357 echo '<input type="checkbox" name="show_query" value="1" '
358 .'id="checkbox_show_query" checked="checked" />' . "\n"
359 .'<label for="checkbox_show_query">' . $GLOBALS['strShowThisQuery']
360 .'</label>' . "\n";
361 echo '</div>' . "\n";
362 echo '<input type="submit" name="SQL" value="' . $GLOBALS['strGo'] . '" />'
363 ."\n";
364 echo '<div class="clearfloat"></div>' . "\n";
365 echo '</fieldset>' . "\n";
366 }
367  
368 /**
369 * prints bookmark fieldset
370 *
371 * @usedby PMA_sqlQueryForm()
372 * @uses PMA_listBookmarks()
373 * @uses $GLOBALS['db']
374 * @uses $GLOBALS['pmaThemeImage']
375 * @uses $GLOBALS['cfg']['Bookmark']
376 * @uses $GLOBALS['cfg']['ReplaceHelpImg']
377 * @uses $GLOBALS['strBookmarkQuery']
378 * @uses $GLOBALS['strBookmarkView']
379 * @uses $GLOBALS['strDelete']
380 * @uses $GLOBALS['strDocu']
381 * @uses $GLOBALS['strGo']
382 * @uses $GLOBALS['strSubmit']
383 * @uses $GLOBALS['strVar']
384 * @uses count()
385 * @uses htmlspecialchars()
386 */
387 function PMA_sqlQueryFormBookmark()
388 {
389 $bookmark_list = PMA_listBookmarks(isset($GLOBALS['db']) ? $GLOBALS['db'] : '', $GLOBALS['cfg']['Bookmark'] );
390 if (! $bookmark_list || count($bookmark_list) < 1) {
391 return;
392 }
393  
394 echo '<fieldset id="bookmarkoptions">';
395 echo '<legend>';
396 echo $GLOBALS['strBookmarkQuery'] . '</legend>' . "\n";
397 echo '<div class="formelement">';
398 echo '<select name="id_bookmark">' . "\n";
399 echo '<option value=""></option>' . "\n";
400 foreach ($bookmark_list as $key => $value) {
401 echo '<option value="' . htmlspecialchars($key) . '">'
402 .htmlspecialchars($value) . '</option>' . "\n";
403 }
404 // &nbsp; is required for correct display with styles/line height
405 echo '</select>&nbsp;' . "\n";
406 echo '</div>' . "\n";
407 echo '<div class="formelement">' . "\n";
408 echo $GLOBALS['strVar'];
409 if ($GLOBALS['cfg']['ReplaceHelpImg']) {
410 echo ' <a href="./Documentation.html#faqbookmark"'
411 .' target="documentation">'
412 .'<img class="icon" src="' . $GLOBALS['pmaThemeImage'] . 'b_help.png"'
413 .' border="0" width="11" height="11" align="middle"'
414 .' alt="' . $GLOBALS['strDocu'] . '" /></a> ';
415 } else {
416 echo ' (<a href="./Documentation.html#faqbookmark"'
417 .' target="documentation">' . $GLOBALS['strDocu'] . '</a>): ';
418 }
419 echo '<input type="text" name="bookmark_variable" class="textfield"'
420 .' size="10" />' . "\n";
421 echo '</div>' . "\n";
422 echo '<div class="formelement">' . "\n";
423 echo '<input type="radio" name="action_bookmark" value="0"'
424 .' id="radio_bookmark_exe" checked="checked" />'
425 .'<label for="radio_bookmark_exe">' . $GLOBALS['strSubmit']
426 .'</label>' . "\n";
427 echo '<input type="radio" name="action_bookmark" value="1"'
428 .' id="radio_bookmark_view" />'
429 .'<label for="radio_bookmark_view">' . $GLOBALS['strBookmarkView']
430 .'</label>' . "\n";
431 echo '<input type="radio" name="action_bookmark" value="2"'
432 .' id="radio_bookmark_del" />'
433 .'<label for="radio_bookmark_del">' . $GLOBALS['strDelete']
434 .'</label>' . "\n";
435 echo '</div>' . "\n";
436 echo '<div class="clearfloat"></div>' . "\n";
437 echo '</fieldset>' . "\n";
438  
439 echo '<fieldset id="bookmarkoptionsfooter" class="tblFooters">' . "\n";
440 echo '<input type="submit" name="SQL" value="' . $GLOBALS['strGo'] . '" />';
441 echo '<div class="clearfloat"></div>' . "\n";
442 echo '</fieldset>' . "\n";
443 }
444  
445 /**
446 * prints bookmark fieldset
447 *
448 * @usedby PMA_sqlQueryForm()
449 * @uses $GLOBALS['cfg']['GZipDump']
450 * @uses $GLOBALS['cfg']['BZipDump']
451 * @uses $GLOBALS['cfg']['UploadDir']
452 * @uses $GLOBALS['cfg']['AvailableCharsets']
453 * @uses $GLOBALS['cfg']['AllowAnywhereRecoding']
454 * @uses $GLOBALS['strAutodetect']
455 * @uses $GLOBALS['strBzip']
456 * @uses $GLOBALS['strCharsetOfFile']
457 * @uses $GLOBALS['strCompression']
458 * @uses $GLOBALS['strError']
459 * @uses $GLOBALS['strGo']
460 * @uses $GLOBALS['strGzip']
461 * @uses $GLOBALS['strLocationTextfile']
462 * @uses $GLOBALS['strWebServerUploadDirectory']
463 * @uses $GLOBALS['strWebServerUploadDirectoryError']
464 * @uses $GLOBALS['allow_recoding']
465 * @uses $GLOBALS['charset']
466 * @uses $GLOBALS['max_upload_size']
467 * @uses PMA_supportedDecompressions()
468 * @uses PMA_getFileSelectOptions()
469 * @uses PMA_displayMaximumUploadSize()
470 * @uses PMA_generateCharsetDropdownBox()
471 * @uses PMA_generateHiddenMaxFileSize()
472 * @uses PMA_MYSQL_INT_VERSION
473 * @uses PMA_CSDROPDOWN_CHARSET
474 * @uses empty()
475 */
476 function PMA_sqlQueryFormUpload(){
477 $errors = array ();
478  
479 $matcher = '@\.sql(\.(' . PMA_supportedDecompressions() . '))?$@'; // we allow only SQL here
480  
481 if (!empty($GLOBALS['cfg']['UploadDir'])) {
482 $files = PMA_getFileSelectOptions(PMA_userDir($GLOBALS['cfg']['UploadDir']), $matcher, (isset($timeout_passed) && $timeout_passed && isset($local_import_file)) ? $local_import_file : '');
483 } else {
484 $files = '';
485 }
486  
487 // start output
488 echo '<fieldset id="">';
489 echo '<legend>';
490 echo $GLOBALS['strLocationTextfile'] . '</legend>';
491 echo '<div class="formelement">';
492 echo '<input type="file" name="sql_file" class="textfield" /> ';
493 echo PMA_displayMaximumUploadSize($GLOBALS['max_upload_size']);
494 // some browsers should respect this :)
495 echo PMA_generateHiddenMaxFileSize($GLOBALS['max_upload_size']) . "\n";
496 echo '</div>';
497  
498 if ($files === FALSE) {
499 $errors[$GLOBALS['strError']] = $GLOBALS['strWebServerUploadDirectoryError'];
500 } elseif (!empty($files)) {
501 echo '<div class="formelement">';
502 echo '<strong>' . $GLOBALS['strWebServerUploadDirectory'] .':</strong>' . "\n";
503 echo '<select size="1" name="sql_localfile">' . "\n";
504 echo '<option value="" selected="selected"></option>' . "\n";
505 echo $files;
506 echo '</select>' . "\n";
507 echo '</div>';
508 }
509  
510 echo '<div class="clearfloat"></div>' . "\n";
511 echo '</fieldset>';
512  
513  
514 echo '<fieldset id="" class="tblFooters">';
515 if ( PMA_MYSQL_INT_VERSION < 40100
516 && $GLOBALS['cfg']['AllowAnywhereRecoding']
517 && $GLOBALS['allow_recoding'] ) {
518 echo $GLOBALS['strCharsetOfFile'] . "\n"
519 . '<select name="charset_of_file" size="1">' . "\n";
520 foreach ($GLOBALS['cfg']['AvailableCharsets'] as $temp_charset) {
521 echo '<option value="' . $temp_charset . '"';
522 if ($temp_charset == $GLOBALS['charset']) {
523 echo ' selected="selected"';
524 }
525 echo '>' . $temp_charset . '</option>' . "\n";
526 }
527 echo '</select>' . "\n";
528 } elseif (PMA_MYSQL_INT_VERSION >= 40100) {
529 echo $GLOBALS['strCharsetOfFile'] . "\n";
530 echo PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_CHARSET,
531 'charset_of_file', null, 'utf8', FALSE);
532 } // end if (recoding)
533 echo '<input type="submit" name="SQL" value="' . $GLOBALS['strGo']
534 .'" />' . "\n";
535 echo '<div class="clearfloat"></div>' . "\n";
536 echo '</fieldset>';
537  
538 foreach ( $errors as $error => $message ) {
539 echo '<div>' . $error . '</div>';
540 echo '<div>' . $message . '</div>';
541 }
542 }
543 ?>