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