1. Check existence of mb_string, mysql and xml extensions before installation.
[openemr.git] / phpmyadmin / libraries / sql_query_form.lib.php
blob19a177acab2c3402001c6fa15e931240bb20ffc1
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 * @package PhpMyAdmin
13 if (! defined('PHPMYADMIN')) {
14 exit;
17 /**
20 require_once './libraries/file_listing.lib.php'; // used for file listing
21 require_once './libraries/bookmark.lib.php'; // used for bookmarks
23 /**
24 * return HTML for the sql query boxes
26 * @param boolean|string $query query to display in the textarea
27 * or true to display last executed
28 * @param boolean|string $display_tab sql|full|false
29 * what part to display
30 * false if not inside querywindow
31 * @param string $delimiter delimiter
33 * @return string
35 * @usedby server_sql.php
36 * @usedby db_sql.php
37 * @usedby tbl_sql.php
38 * @usedby tbl_structure.php
39 * @usedby tbl_tracking.php
41 function PMA_getHtmlForSqlQueryForm(
42 $query = true, $display_tab = false, $delimiter = ';'
43 ) {
44 $html = '';
45 if (! $display_tab) {
46 $display_tab = 'full';
48 // query to show
49 if (true === $query) {
50 $query = $GLOBALS['sql_query'];
53 // set enctype to multipart for file uploads
54 if ($GLOBALS['is_upload']) {
55 $enctype = ' enctype="multipart/form-data"';
56 } else {
57 $enctype = '';
60 $table = '';
61 $db = '';
62 if (! /*overload*/mb_strlen($GLOBALS['db'])) {
63 // prepare for server related
64 $goto = empty($GLOBALS['goto']) ?
65 'server_sql.php' : $GLOBALS['goto'];
66 } elseif (! /*overload*/mb_strlen($GLOBALS['table'])) {
67 // prepare for db related
68 $db = $GLOBALS['db'];
69 $goto = empty($GLOBALS['goto']) ?
70 'db_sql.php' : $GLOBALS['goto'];
71 } else {
72 $table = $GLOBALS['table'];
73 $db = $GLOBALS['db'];
74 $goto = empty($GLOBALS['goto']) ?
75 'tbl_sql.php' : $GLOBALS['goto'];
78 // start output
79 $html .= '<form method="post" action="import.php" ' . $enctype;
80 $html .= ' class="ajax lock-page"';
81 $html .= ' id="sqlqueryform" name="sqlform">' . "\n";
83 $html .= '<input type="hidden" name="is_js_confirmed" value="0" />'
84 . "\n" . PMA_URL_getHiddenInputs($db, $table) . "\n"
85 . '<input type="hidden" name="pos" value="0" />' . "\n"
86 . '<input type="hidden" name="goto" value="'
87 . htmlspecialchars($goto) . '" />' . "\n"
88 . '<input type="hidden" name="message_to_show" value="'
89 . __('Your SQL query has been executed successfully.') . '" />'
90 . "\n" . '<input type="hidden" name="prev_sql_query" value="'
91 . htmlspecialchars($query) . '" />' . "\n";
93 // display querybox
94 if ($display_tab === 'full' || $display_tab === 'sql') {
95 $html .= PMA_getHtmlForSqlQueryFormInsert(
96 $query, $delimiter
100 // Bookmark Support
101 if ($display_tab === 'full') {
102 $cfgBookmark = PMA_Bookmark_getParams();
103 if ($cfgBookmark) {
104 $html .= PMA_getHtmlForSqlQueryFormBookmark();
108 // Encoding setting form appended by Y.Kawada
109 if (function_exists('PMA_Kanji_encodingForm')) {
110 $html .= PMA_Kanji_encodingForm();
113 $html .= '</form>' . "\n";
114 // print an empty div, which will be later filled with
115 // the sql query results by ajax
116 $html .= '<div id="sqlqueryresultsouter"></div>';
118 return $html;
122 * Get initial values for Sql Query Form Insert
124 * @param string $query query to display in the textarea
126 * @return array ($legend, $query, $columns_list)
128 * @usedby PMA_getHtmlForSqlQueryFormInsert()
130 function PMA_initQueryForm($query)
132 $columns_list = array();
133 if (! /*overload*/mb_strlen($GLOBALS['db'])) {
134 // prepare for server related
135 $legend = sprintf(
136 __('Run SQL query/queries on server %s'),
137 '&quot;' . htmlspecialchars(
138 ! empty($GLOBALS['cfg']['Servers'][$GLOBALS['server']]['verbose'])
139 ? $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['verbose']
140 : $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['host']
141 ) . '&quot;'
143 } elseif (! /*overload*/mb_strlen($GLOBALS['table'])) {
144 // prepare for db related
145 $db = $GLOBALS['db'];
146 // if you want navigation:
147 $tmp_db_link = '<a href="' . PMA_Util::getScriptNameForOption(
148 $GLOBALS['cfg']['DefaultTabDatabase'], 'database'
150 . PMA_URL_getCommon(array('db' => $db)) . '"';
151 $tmp_db_link .= '>'
152 . htmlspecialchars($db) . '</a>';
153 $legend = sprintf(__('Run SQL query/queries on database %s'), $tmp_db_link);
154 if (empty($query)) {
155 $query = PMA_Util::expandUserString(
156 $GLOBALS['cfg']['DefaultQueryDatabase'], 'backquote'
159 } else {
160 $db = $GLOBALS['db'];
161 $table = $GLOBALS['table'];
162 // Get the list and number of fields
163 // we do a try_query here, because we could be in the query window,
164 // trying to synchronize and the table has not yet been created
165 $columns_list = $GLOBALS['dbi']->getColumns(
166 $db, $GLOBALS['table'], null, true
169 $tmp_tbl_link = '<a href="' . PMA_Util::getScriptNameForOption(
170 $GLOBALS['cfg']['DefaultTabTable'], 'table'
171 ) . PMA_URL_getCommon(array('db' => $db, 'table' => $table)) . '" >';
172 $tmp_tbl_link .= htmlspecialchars($db)
173 . '.' . htmlspecialchars($table) . '</a>';
174 $legend = sprintf(__('Run SQL query/queries on table %s'), $tmp_tbl_link);
175 if (empty($query)) {
176 $query = PMA_Util::expandUserString(
177 $GLOBALS['cfg']['DefaultQueryTable'], 'backquote'
181 $legend .= ': ' . PMA_Util::showMySQLDocu('SELECT');
183 return array($legend, $query, $columns_list);
187 * return HTML for Sql Query Form Insert
189 * @param string $query query to display in the textarea
190 * @param string $delimiter default delimiter to use
192 * @return string
194 * @usedby PMA_getHtmlForSqlQueryForm()
196 function PMA_getHtmlForSqlQueryFormInsert(
197 $query = '', $delimiter = ';'
199 // enable auto select text in textarea
200 if ($GLOBALS['cfg']['TextareaAutoSelect']) {
201 $auto_sel = ' onclick="selectContent(this, sql_box_locked, true);"';
202 } else {
203 $auto_sel = '';
206 $locking = '';
207 $height = $GLOBALS['cfg']['TextareaRows'] * 2;
209 list($legend, $query, $columns_list) = PMA_initQueryForm($query);
211 if (! empty($columns_list)) {
212 $sqlquerycontainer_id = 'sqlquerycontainer';
213 } else {
214 $sqlquerycontainer_id = 'sqlquerycontainerfull';
217 $html = '<a id="querybox"></a>'
218 . '<div id="queryboxcontainer">'
219 . '<fieldset id="queryboxf">';
220 $html .= '<legend>' . $legend . '</legend>';
221 $html .= '<div id="queryfieldscontainer">';
222 $html .= '<div id="' . $sqlquerycontainer_id . '">'
223 . '<textarea tabindex="100" name="sql_query" id="sqlquery"'
224 . ' cols="' . $GLOBALS['cfg']['TextareaCols'] . '"'
225 . ' rows="' . $height . '"'
226 . $auto_sel . $locking . '>'
227 . htmlspecialchars($query)
228 . '</textarea>';
229 $html .= '<div id="querymessage"></div>';
230 // Add buttons to generate query easily for
231 // select all, single select, insert, update and delete
232 if (! empty($columns_list)) {
233 $html .= '<input type="button" value="SELECT *" id="selectall"'
234 . ' class="button sqlbutton" />';
235 $html .= '<input type="button" value="SELECT" id="select"'
236 . ' class="button sqlbutton" />';
237 $html .= '<input type="button" value="INSERT" id="insert"'
238 . ' class="button sqlbutton" />';
239 $html .= '<input type="button" value="UPDATE" id="update"'
240 . ' class="button sqlbutton" />';
241 $html .= '<input type="button" value="DELETE" id="delete"'
242 . ' class="button sqlbutton" />';
244 $html .= '<input type="button" value="' . __('Clear') . '" id="clear"'
245 . ' class="button sqlbutton" />';
246 if ($GLOBALS['cfg']['CodemirrorEnable']) {
247 $html .= '<input type="button" value="' . __('Format') . '" id="format"'
248 . ' class="button sqlbutton" />';
250 $html .= '<input type="button" value="' . __('Get auto-saved query')
251 . '" id="saved" class="button sqlbutton" />';
253 // parameter binding
254 $html .= '<div>';
255 $html .= '<input type="checkbox" name="parameterized" id="parameterized" />';
256 $html .= '<label for="parameterized">' . __('Bind parameters') . '</label>';
257 $html .= PMA_Util::showDocu('faq', 'faq6-40');
258 $html .= '<div id="parametersDiv"></div>';
259 $html .= '</div>';
261 $html .= '</div>' . "\n";
263 if (! empty($columns_list)) {
264 $html .= '<div id="tablefieldscontainer">'
265 . '<label>' . __('Columns') . '</label>'
266 . '<select id="tablefields" name="dummy" '
267 . 'size="' . ($GLOBALS['cfg']['TextareaRows'] - 2) . '" '
268 . 'multiple="multiple" ondblclick="insertValueQuery()">';
269 foreach ($columns_list as $field) {
270 $html .= '<option value="'
271 . PMA_Util::backquote(htmlspecialchars($field['Field'])) . '"';
272 if (isset($field['Field'])
273 && /*overload*/mb_strlen($field['Field'])
274 && isset($field['Comment'])
276 $html .= ' title="' . htmlspecialchars($field['Comment']) . '"';
278 $html .= '>' . htmlspecialchars($field['Field']) . '</option>' . "\n";
280 $html .= '</select>'
281 . '<div id="tablefieldinsertbuttoncontainer">';
282 if (PMA_Util::showIcons('ActionLinksMode')) {
283 $html .= '<input type="button" class="button" name="insert"'
284 . ' value="&lt;&lt;" onclick="insertValueQuery()"'
285 . ' title="' . __('Insert') . '" />';
286 } else {
287 $html .= '<input type="button" class="button" name="insert"'
288 . ' value="' . __('Insert') . '"'
289 . ' onclick="insertValueQuery()" />';
291 $html .= '</div>' . "\n"
292 . '</div>' . "\n";
295 $html .= '<div class="clearfloat"></div>' . "\n";
296 $html .= '</div>' . "\n";
298 $cfgBookmark = PMA_Bookmark_getParams();
299 if ($cfgBookmark) {
300 $html .= '<div id="bookmarkoptions">';
301 $html .= '<div class="formelement">';
302 $html .= '<label for="bkm_label">'
303 . __('Bookmark this SQL query:') . '</label>';
304 $html .= '<input type="text" name="bkm_label" id="bkm_label"'
305 . ' tabindex="110" value="" />';
306 $html .= '</div>';
307 $html .= '<div class="formelement">';
308 $html .= '<input type="checkbox" name="bkm_all_users" tabindex="111"'
309 . ' id="id_bkm_all_users" value="true" />';
310 $html .= '<label for="id_bkm_all_users">'
311 . __('Let every user access this bookmark') . '</label>';
312 $html .= '</div>';
313 $html .= '<div class="formelement">';
314 $html .= '<input type="checkbox" name="bkm_replace" tabindex="112"'
315 . ' id="id_bkm_replace" value="true" />';
316 $html .= '<label for="id_bkm_replace">'
317 . __('Replace existing bookmark of same name') . '</label>';
318 $html .= '</div>';
319 $html .= '</div>';
322 $html .= '<div class="clearfloat"></div>' . "\n";
323 $html .= '</fieldset>' . "\n"
324 . '</div>' . "\n";
326 $html .= '<fieldset id="queryboxfooter" class="tblFooters">' . "\n";
327 $html .= '<div class="formelement">' . "\n";
328 $html .= '</div>' . "\n";
330 $html .= '<div class="formelement">';
331 $html .= '<label for="id_sql_delimiter">[ ' . __('Delimiter')
332 . '</label>' . "\n";
333 $html .= '<input type="text" name="sql_delimiter" tabindex="131" size="3" '
334 . 'value="' . $delimiter . '" '
335 . 'id="id_sql_delimiter" /> ]';
336 $html .= '</div>';
338 $html .= '<div class="formelement">';
339 $html .= '<input type="checkbox" name="show_query" value="1" '
340 . 'id="checkbox_show_query" tabindex="132" checked="checked" />'
341 . '<label for="checkbox_show_query">' . __('Show this query here again')
342 . '</label>';
343 $html .= '</div>';
345 $html .= '<div class="formelement">';
346 $html .= '<input type="checkbox" name="retain_query_box" value="1" '
347 . 'id="retain_query_box" tabindex="133" '
348 . ($GLOBALS['cfg']['RetainQueryBox'] === false
349 ? '' : ' checked="checked"')
350 . ' />'
351 . '<label for="retain_query_box">' . __('Retain query box')
352 . '</label>';
353 $html .= '</div>';
355 $html .= '<div class="formelement">';
356 $html .= '<input type="checkbox" name="rollback_query" value="1" '
357 . 'id="rollback_query" tabindex="134" />'
358 . '<label for="rollback_query">' . __('Rollback when finished')
359 . '</label>';
360 $html .= '</div>';
362 // Disable/Enable foreign key checks
363 $html .= '<div class="formelement">';
364 $html .= PMA_Util::getFKCheckbox();
365 $html .= '</div>';
367 $html .= '<input type="submit" id="button_submit_query" name="SQL"';
369 $html .= ' tabindex="200" value="' . __('Go') . '" />' . "\n";
370 $html .= '<div class="clearfloat"></div>' . "\n";
371 $html .= '</fieldset>' . "\n";
373 return $html;
377 * return HTML for sql Query Form Bookmark
379 * @return string|null
381 * @usedby PMA_getHtmlForSqlQueryForm()
383 function PMA_getHtmlForSqlQueryFormBookmark()
385 $bookmark_list = PMA_Bookmark_getList($GLOBALS['db']);
386 if (empty($bookmark_list) || count($bookmark_list) < 1) {
387 return null;
390 $html = '<fieldset id="fieldsetBookmarkOptions">';
391 $html .= '<legend>';
392 $html .= __('Bookmarked SQL query') . '</legend>' . "\n";
393 $html .= '<div class="formelement">';
394 $html .= '<select name="id_bookmark" id="id_bookmark">' . "\n";
395 $html .= '<option value="">&nbsp;</option>' . "\n";
396 foreach ($bookmark_list as $key => $value) {
397 $html .= '<option value="' . htmlspecialchars($key) . '"'
398 . ' data-varcount="' . PMA_Bookmark_getVariableCount($value['query'])
399 . '">'
400 . htmlspecialchars($value['label']) . '</option>' . "\n";
402 // &nbsp; is required for correct display with styles/line height
403 $html .= '</select>&nbsp;' . "\n";
404 $html .= '</div>' . "\n";
405 $html .= '<div class="formelement">' . "\n";
406 $html .= '<input type="radio" name="action_bookmark" value="0"'
407 . ' id="radio_bookmark_exe" checked="checked" />'
408 . '<label for="radio_bookmark_exe">' . __('Submit')
409 . '</label>' . "\n";
410 $html .= '<input type="radio" name="action_bookmark" value="1"'
411 . ' id="radio_bookmark_view" />'
412 . '<label for="radio_bookmark_view">' . __('View only')
413 . '</label>' . "\n";
414 $html .= '<input type="radio" name="action_bookmark" value="2"'
415 . ' id="radio_bookmark_del" />'
416 . '<label for="radio_bookmark_del">' . __('Delete')
417 . '</label>' . "\n";
418 $html .= '</div>' . "\n";
419 $html .= '<div class="clearfloat"></div>' . "\n";
420 $html .= '<div class="formelement hide">' . "\n";
421 $html .= __('Variables');
422 $html .= PMA_Util::showDocu('faq', 'faqbookmark');
423 $html .= '<div id="bookmark_variables"></div>';
424 $html .= '</div>' . "\n";
425 $html .= '</fieldset>' . "\n";
427 $html .= '<fieldset id="fieldsetBookmarkOptionsFooter" class="tblFooters">';
428 $html .= '<input type="submit" name="SQL" id="button_submit_bookmark" value="'
429 . __('Go') . '" />';
430 $html .= '<div class="clearfloat"></div>' . "\n";
431 $html .= '</fieldset>' . "\n";
433 return $html;