3 // vim: expandtab sw=4 ts=4 sts=4:
6 * Gets some core libraries
8 require('./libraries/grab_globals.lib.php3');
9 require('./libraries/common.lib.php3');
12 * Defines the url to return to in case of error in a sql statement
16 $is_gotofile = ereg_replace('^([^?]+).*$', '\\1', $goto);
17 if (!@file_exists
('./' . $is_gotofile)) {
20 $is_gotofile = ($is_gotofile == $goto);
22 } // end if (security checkings)
25 $goto = (empty($table)) ?
$cfg['DefaultTabDatabase'] : $cfg['DefaultTabTable'];
28 if (!isset($err_url)) {
29 $err_url = (!empty($back) ?
$back : $goto)
30 . '?' . PMA_generate_common_url(isset($db) ?
$db : '')
31 . ((strpos(' ' . $goto, 'db_details') != 1 && isset($table)) ?
'&table=' . urlencode($table) : '');
34 // Coming from a bookmark dialog
35 if (isset($fields['query'])) {
36 $sql_query = $fields['query'];
40 * Check rights in case of DROP DATABASE
42 * This test may be bypassed if $is_js_confirmed = 1 (already checked with js)
43 * but since a malicious user may pass this variable by url/form, we don't take
44 * into account this case.
46 if (!defined('PMA_CHK_DROP')
47 && !$cfg['AllowUserDropDatabase']
48 && eregi('DROP[[:space:]]+(IF EXISTS[[:space:]]+)?DATABASE[[:space:]]', $sql_query)) {
49 // Checks if the user is a Superuser
50 // TODO: set a global variable with this information
51 // loic1: optimized query
52 $result = @PMA_mysql_query
('USE mysql');
53 if (PMA_mysql_error()) {
54 include('./header.inc.php3');
55 PMA_mysqlDie($strNoDropDatabases, '', '', $err_url);
63 if (isset($store_bkm)) {
64 if (get_magic_quotes_gpc()) {
65 $fields['label'] = stripslashes($fields['label']);
67 include('./libraries/bookmark.lib.php3');
68 PMA_addBookmarks($fields, $cfg['Bookmark']);
69 header('Location: ' . $cfg['PmaAbsoluteUri'] . $goto);
74 * Gets the true sql query
76 // $sql_query has been urlencoded in the confirmation form for drop/delete
77 // queries or in the navigation bar for browsing among records
78 if (isset($btnDrop) ||
isset($navig)) {
79 $sql_query = urldecode($sql_query);
86 $parsed_sql = PMA_SQP_parse((get_magic_quotes_gpc() ?
stripslashes($sql_query) : $sql_query));
87 $analyzed_sql = PMA_SQP_analyze($parsed_sql);
88 // Bug #641765 - Robbat2 - 12 January 2003, 10:49PM
89 // Reverted - Robbat2 - 13 January 2003, 2:40PM
90 $sql_query = PMA_SQP_formatHtml($parsed_sql, 'query_only');
92 // here we are sure that SELECT is uppercase
93 $is_select = eregi('^SELECT[[:space:]]+', $sql_query);
95 // If the query is a Select, extract the db and table names and modify
96 // $db and $table, to have correct page headers, links and left frame.
97 // db and table name may be enclosed with backquotes, db is optionnal,
98 // query may contain aliases.
100 // (TODO: if there are more than one table name in the Select:
101 // - do not extract the first table name
102 // - do not show a table name in the page header
103 // - do not display the sub-pages links)
106 // eregi('^SELECT[[:space:]]+(.*)[[:space:]]+FROM[[:space:]]+(`[^`]+`|[A-Za-z0-9_$]+)([\.]*)(`[^`]*`|[A-Za-z0-9_$]*)', $sql_query, $tmp);
108 // if ($tmp[3] == '.') {
110 // $db = str_replace('`', '', $tmp[2]);
111 // $reload = ($db == $prev_db) ? 0 : 1;
112 // $table = str_replace('`', '', $tmp[4]);
115 // $table = str_replace('`', '', $tmp[2]);
121 if (isset($analyzed_sql[0]['table_ref'][0]['table_true_name'])) {
122 $table = $analyzed_sql[0]['table_ref'][0]['table_true_name'];
124 if (isset($analyzed_sql[0]['table_ref'][0]['db'])
125 && !empty($analyzed_sql[0]['table_ref'][0]['db'])) {
126 $db = $analyzed_sql[0]['table_ref'][0]['db'];
131 $reload = ($db == $prev_db) ?
0 : 1;
135 * Sets or modifies the $goto variable if required
137 if ($goto == 'sql.php3') {
139 . PMA_generate_common_url($db, $table)
141 . '&sql_query=' . urlencode($sql_query);
146 * Go back to further page if table should not be dropped
148 if (isset($btnDrop) && $btnDrop == $strNo) {
153 if (strpos(' ' . $goto, 'db_details') == 1 && !empty($table)) {
156 include('./' . ereg_replace('\.\.*', '.', $goto));
158 header('Location: ' . $cfg['PmaAbsoluteUri'] . str_replace('&', '&', $goto));
165 * Displays the confirm page if required
167 * This part of the script is bypassed if $is_js_confirmed = 1 (already checked
168 * with js) because possible security issue is not so important here: at most,
169 * the confirm message isn't displayed.
171 * Also bypassed if only showing php code.or validating a SQL query
174 ||
(isset($is_js_confirmed) && $is_js_confirmed)
176 ||
!empty($GLOBALS['show_as_php'])
177 ||
!empty($GLOBALS['validatequery'])) {
180 //$do_confirm = (eregi('DROP[[:space:]]+(IF[[:space:]]+EXISTS[[:space:]]+)?(TABLE|DATABASE[[:space:]])|ALTER[[:space:]]+TABLE[[:space:]]+((`[^`]+`)|([A-Za-z0-9_$]+))[[:space:]]+DROP[[:space:]]|DELETE[[:space:]]+FROM[[:space:]]', $sql_query));
182 $do_confirm = isset($analyzed_sql[0]['queryflags']['need_confirm']);
186 // already stripped at beginning of script
187 //$stripped_sql_query = (get_magic_quotes_gpc() ? stripslashes($sql_query) : $sql_query);
188 $stripped_sql_query = $sql_query;
189 include('./header.inc.php3');
190 echo $strDoYouReally . ' :<br />' . "\n";
191 echo '<tt>' . htmlspecialchars($stripped_sql_query) . '</tt> ?<br/>' . "\n";
193 <form action
="sql.php3" method
="post">
194 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
195 <input type
="hidden" name
="sql_query" value
="<?php echo urlencode(addslashes($sql_query)); ?>" />
196 <input type
="hidden" name
="zero_rows" value
="<?php echo isset($zero_rows) ? $zero_rows : ''; ?>" />
197 <input type
="hidden" name
="goto" value
="<?php echo $goto; ?>" />
198 <input type
="hidden" name
="back" value
="<?php echo isset($back) ? $back : ''; ?>" />
199 <input type
="hidden" name
="reload" value
="<?php echo isset($reload) ? $reload : 0; ?>" />
200 <input type
="hidden" name
="show_query" value
="<?php echo isset($show_query) ? $show_query : ''; ?>" />
201 <input type
="submit" name
="btnDrop" value
="<?php echo $strYes; ?>" />
202 <input type
="submit" name
="btnDrop" value
="<?php echo $strNo; ?>" />
210 * Executes the query and displays results
213 if (!isset($sql_query)) {
216 // already stripped at beginning of script
217 // else if (get_magic_quotes_gpc()) {
218 // $sql_query = stripslashes($sql_query);
220 // Defines some variables
221 // loic1: A table has to be created -> left frame should be reloaded
222 if ((!isset($reload) ||
$reload == 0)
223 && eregi('^CREATE TABLE[[:space:]]+(.*)', $sql_query)) {
226 // Gets the number of rows per page
227 if (!isset($session_max_rows)) {
228 $session_max_rows = $cfg['MaxRows'];
229 } else if ($session_max_rows != 'all') {
230 $cfg['MaxRows'] = $session_max_rows;
232 // Defines the display mode (horizontal/vertical) and header "frequency"
233 if (empty($disp_direction)) {
234 $disp_direction = $cfg['DefaultDisplay'];
236 if (empty($repeat_cells)) {
237 $repeat_cells = $cfg['RepeatCells'];
240 // SK -- Patch: $is_group added for use in calculation of total number of
242 // $is_count is changed for more correct "LIMIT" clause
243 // appending in queries like
244 // "SELECT COUNT(...) FROM ... GROUP BY ..."
245 $is_explain = $is_count = $is_export = $is_delete = $is_insert = $is_affected = $is_show = $is_maint = $is_analyse = $is_group = $is_func = FALSE;
246 if ($is_select) { // see line 141
247 $is_group = eregi('[[:space:]]+(GROUP[[:space:]]+BY|HAVING|SELECT[[:space:]]+DISTINCT)[[:space:]]+', $sql_query);
249 $is_func = !$is_group && (eregi('[[:space:]]+(SUM|AVG|STD|STDDEV|MIN|MAX|BIT_OR|BIT_AND)\s*\(', $sql_query));
250 $is_count = !$is_group && (eregi('^SELECT[[:space:]]+COUNT\((.*\.+)?.*\)', $sql_query));
251 $is_export = (eregi('[[:space:]]+INTO[[:space:]]+OUTFILE[[:space:]]+', $sql_query));
252 $is_analyse = (eregi('[[:space:]]+PROCEDURE[[:space:]]+ANALYSE\(', $sql_query));
253 } else if (eregi('^EXPLAIN[[:space:]]+', $sql_query)) {
255 } else if (eregi('^DELETE[[:space:]]+', $sql_query)) {
258 } else if (eregi('^(INSERT|LOAD[[:space:]]+DATA|REPLACE)[[:space:]]+', $sql_query)) {
261 } else if (eregi('^UPDATE[[:space:]]+', $sql_query)) {
263 } else if (eregi('^SHOW[[:space:]]+', $sql_query)) {
265 } else if (eregi('^(CHECK|ANALYZE|REPAIR|OPTIMIZE)[[:space:]]+TABLE[[:space:]]+', $sql_query)) {
269 // Do append a "LIMIT" clause?
271 && (!$cfg['ShowAll'] ||
$session_max_rows != 'all')
272 && !($is_count ||
$is_export ||
$is_func ||
$is_analyse)
275 //&& eregi('[[:space:]]FROM[[:space:]]', $sql_query)
277 && isset($analyzed_sql[0]['queryflags']['select_from'])
279 && !eregi('[[:space:]]LIMIT[[:space:]0-9,-]+$', $sql_query)) {
280 $sql_limit_to_append = " LIMIT $pos, ".$cfg['MaxRows'];
281 if (eregi('(.*)([[:space:]](PROCEDURE[[:space:]](.*)|FOR[[:space:]]+UPDATE|LOCK[[:space:]]+IN[[:space:]]+SHARE[[:space:]]+MODE))$', $sql_query, $regs)) {
282 $full_sql_query = $regs[1] . $sql_limit_to_append . $regs[2];
284 $full_sql_query = $sql_query . $sql_limit_to_append;
287 $full_sql_query = $sql_query;
290 PMA_mysql_select_db($db);
292 // If the query is a DELETE query with no WHERE clause, get the number of
293 // rows that will be deleted (mysql_affected_rows will always return 0 in
296 && eregi('^DELETE([[:space:]].+)?([[:space:]]FROM[[:space:]](.+))$', $sql_query, $parts)
297 && !eregi('[[:space:]]WHERE[[:space:]]', $parts[3])) {
298 $cnt_all_result = @PMA_mysql_query
('SELECT COUNT(*) as count' . $parts[2]);
299 if ($cnt_all_result) {
300 $num_rows = PMA_mysql_result($cnt_all_result, 0, 'count');
301 mysql_free_result($cnt_all_result);
307 // E x e c u t e t h e q u e r y
309 // Only if we didn't ask to see the php code (mikebeck)
310 if (!empty($GLOBALS['show_as_php']) ||
!empty($GLOBALS['validatequery'])) {
315 $result = @PMA_mysql_query
($full_sql_query);
317 // Displays an error message if required and stop parsing the script
318 if (PMA_mysql_error()) {
319 $error = PMA_mysql_error();
320 include('./header.inc.php3');
321 $full_err_url = (ereg('^(db_details|tbl_properties)', $err_url))
322 ?
$err_url . '&show_query=1&sql_query=' . urlencode($sql_query)
324 PMA_mysqlDie($error, $full_sql_query, '', $full_err_url);
327 // Gets the number of rows affected/returned
328 // (This must be done immediately after the query because
329 // mysql_affected_rows() reports about the last query done)
332 $num_rows = ($result) ? @mysql_num_rows
($result) : 0;
333 } else if (!isset($num_rows)) {
334 $num_rows = @mysql_affected_rows
();
337 // Checks if the current database has changed
338 // This could happen if the user sends a query like "USE `database`;"
339 $res = PMA_mysql_query('SELECT DATABASE() AS "db";');
340 $row = PMA_mysql_fetch_array($res);
341 if ($db != $row['db']) {
345 @mysql_free_result
($res);
349 // tmpfile remove after convert encoding appended by Y.Kawada
350 if (function_exists('PMA_kanji_file_conv')
351 && (isset($textfile) && file_exists($textfile))) {
355 // Counts the total number of rows for the same 'SELECT' query without the
356 // 'LIMIT' clause that may have been programatically added
357 if (empty($sql_limit_to_append)) {
358 $unlim_num_rows = $num_rows;
360 else if ($is_select) {
361 // SK -- Patch : correct calculations for GROUP BY, HAVING, DISTINCT
363 // Reads only the from-part of the query...
364 // NOTE: here the presence of LIMIT is impossible, HAVING and GROUP BY
365 // are necessary for correct calculation, and extra spaces and
366 // lowercase reserved words are removed, so we have a simple split
369 $array = split('[[:space:]]+(FROM|ORDER BY)[[:space:]]+', $sql_query);
371 // if $array[1] is empty here, there is an error in the query:
372 // "... FROM [ORDER BY ...]", but the query is already executed with
373 // success so this check is redundant???
375 if (!empty($array[1])) {
376 // ... and makes a count(*) to count the entries
377 // Special case: SELECT DISTINCT ... FROM ...
378 // the count of resulting rows can be found as:
379 // SELECT COUNT(DISTINCT ...) FROM ...
380 if (eregi('^SELECT DISTINCT(.*)', $array[0], $array_dist)) {
381 $count_what = 'DISTINCT ' . $array_dist[1];
385 $count_query = 'SELECT COUNT(' . $count_what . ') AS count FROM ' . $array[1];
386 if ($cnt_all_result = mysql_query($count_query)) {
388 $unlim_num_rows = @mysql_num_rows
($cnt_all_result);
390 $unlim_num_rows = mysql_result($cnt_all_result, 0, 'count');
392 mysql_free_result($cnt_all_result);
397 } // end rows total count
398 } // end else "didn't ask to see php code"
401 // No rows returned -> move back to the calling page
402 if ($num_rows < 1 ||
$is_affected) {
404 $message = $strDeletedRows . ' ' . $num_rows;
405 } else if ($is_insert) {
406 $message = $strInsertedRows . ' ' . $num_rows;
407 } else if ($is_affected) {
408 $message = $strAffectedRows . ' ' . $num_rows;
409 } else if (!empty($zero_rows)) {
410 $message = $zero_rows;
411 } else if (!empty($GLOBALS['show_as_php'])) {
413 } else if (!empty($GLOBALS['validatequery'])) {
414 $message = $strValidateSQL;
416 $message = $strEmptyResultSet;
420 $goto = ereg_replace('\.\.*', '.', $goto);
421 // Checks for a valid target script
422 if (isset($table) && $table == '') {
425 if (isset($db) && $db == '') {
428 $is_db = $is_table = FALSE;
429 if (strpos(' ' . $goto, 'tbl_properties') == 1) {
430 if (!isset($table)) {
431 $goto = 'db_details.php3';
433 $is_table = @PMA_mysql_query
('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'');
434 if (!($is_table && @mysql_numrows
($is_table))) {
435 $goto = 'db_details.php3';
438 } // end if... else...
440 if (strpos(' ' . $goto, 'db_details') == 1) {
447 $is_db = @PMA_mysql_select_db
($db);
452 } // end if... else...
454 // Loads to target script
455 if (strpos(' ' . $goto, 'db_details') == 1
456 ||
strpos(' ' . $goto, 'tbl_properties') == 1) {
457 $js_to_run = 'functions.js';
459 if ($goto != 'main.php3') {
460 include('./header.inc.php3');
462 include('./' . $goto);
463 } // end if file_exist
465 header('Location: ' . $cfg['PmaAbsoluteUri'] . str_replace('&', '&', $goto) . '&message=' . urlencode($message));
468 } // end no rows returned
470 // At least one row is returned -> displays a table with results
472 // Displays the headers
473 if (isset($show_query)) {
476 if (isset($printview) && $printview == '1') {
477 include('./header_printview.inc.php3');
479 $js_to_run = 'functions.js';
481 if (!empty($table)) {
482 include('./tbl_properties_common.php3');
483 $url_query .= '&goto=tbl_properties.php3&back=tbl_properties.php3';
484 include('./tbl_properties_table_info.php3');
487 include('./db_details_common.php3');
488 include('./db_details_db_info.php3');
490 include('./libraries/relation.lib.php3');
491 $cfgRelation = PMA_getRelationsParam();
494 // Gets the list of fields properties
495 if (isset($result) && $result) {
496 while ($field = PMA_mysql_fetch_field($result)) {
497 $fields_meta[] = $field;
499 $fields_cnt = count($fields_meta);
502 // Display previous update query (from tbl_replace)
503 if (isset($disp_query) && $cfg['ShowSQL'] == TRUE) {
504 $tmp_sql_query = $GLOBALS['sql_query'];
505 $tmp_sql_limit_to_append = $GLOBALS['sql_limit_to_append'];
506 $GLOBALS['sql_query'] = stripslashes($disp_query);
507 $GLOBALS['sql_limit_to_append'] = '';
508 PMA_showMessage($disp_message);
509 $GLOBALS['sql_query'] = $tmp_sql_query;
510 $GLOBALS['sql_limit_to_append'] = $tmp_sql_limit_to_append;
513 // Displays the results in a table
514 include('./libraries/display_tbl.lib.php3');
515 if (empty($disp_mode)) {
516 // see the "PMA_setDisplayMode()" function in
517 // libraries/display_tbl.lib.php3
518 $disp_mode = 'urdr111101';
520 if (!isset($dontlimitchars)) {
524 PMA_displayTable($result, $disp_mode, $analyzed_sql);
525 mysql_free_result($result);
527 if ($disp_mode[6] == '1' ||
$disp_mode[9] == '1') {
531 // Displays "Insert a new row" link if required
532 if ($disp_mode[6] == '1') {
533 $lnk_goto = 'sql.php3?'
534 . PMA_generate_common_url($db, $table)
536 . '&session_max_rows=' . $session_max_rows
537 . '&disp_direction=' . $disp_direction
538 . '&repeat_cells=' . $repeat_cells
539 . '&dontlimitchars=' . $dontlimitchars
540 . '&sql_query=' . urlencode($sql_query);
542 . PMA_generate_common_url($db, $table)
544 . '&session_max_rows=' . $session_max_rows
545 . '&disp_direction=' . $disp_direction
546 . '&repeat_cells=' . $repeat_cells
547 . '&dontlimitchars=' . $dontlimitchars
548 . '&sql_query=' . urlencode($sql_query)
549 . '&goto=' . urlencode($lnk_goto);
551 echo ' <!-- Insert a new row -->' . "\n"
552 . ' <a href="tbl_change.php3' . $url_query . '">' . $strInsertNewRow . '</a>';
553 if ($disp_mode[9] == '1') {
557 } // end insert new row
559 // Displays "printable view" link if required
560 if ($disp_mode[9] == '1') {
562 . PMA_generate_common_url($db, $table)
564 . '&session_max_rows=' . $session_max_rows
565 . '&disp_direction=' . $disp_direction
566 . '&repeat_cells=' . $repeat_cells
568 . ((isset($dontlimitchars) && $dontlimitchars == '1') ?
'&dontlimitchars=1' : '')
569 . '&sql_query=' . urlencode($sql_query);
570 echo ' <!-- Print view -->' . "\n"
571 . ' <a href="sql.php3' . $url_query . '" target="print_view">' . $strPrintView . '</a>' . "\n";
572 } // end displays "printable view"
577 // Export link, if only one table
578 // (the url_query has extra parameters that won't be used to export)
579 if (!isset($printview)
580 && isset($analyzed_sql[0]['table_ref'][0]['table_true_name'])
581 && !isset($analyzed_sql[0]['table_ref'][1]['table_true_name'])) {
582 echo ' <!-- Export -->' . "\n"
583 . ' <a href="tbl_properties_export.php3' . $url_query . '&unlim_num_rows=' . $unlim_num_rows . '">' . $strExport . '</a>' . "\n";
586 // Bookmark Support if required
587 if ($disp_mode[7] == '1'
588 && ($cfg['Bookmark']['db'] && $cfg['Bookmark']['table'] && empty($id_bookmark))
589 && !empty($sql_query)) {
593 . PMA_generate_common_url($db, $table)
595 . '&session_max_rows=' . $session_max_rows
596 . '&disp_direction=' . $disp_direction
597 . '&repeat_cells=' . $repeat_cells
598 . '&dontlimitchars=' . $dontlimitchars
599 . '&sql_query=' . urlencode($sql_query)
600 . '&id_bookmark=1';
602 <!-- Bookmark the query
-->
603 <form action
="sql.php3" method
="post" onsubmit
="return emptyFormElements(this, 'fields[label]');">
606 if ($disp_mode[3] == '1') {
607 echo ' <i>' . $strOr . '</i>' . "\n";
611 <?php
echo $strBookmarkLabel; ?
> 
;:
612 <?php
echo PMA_generate_common_hidden_inputs(); ?
>
613 <input type
="hidden" name
="goto" value
="<?php echo $goto; ?>" />
614 <input type
="hidden" name
="fields[dbase]" value
="<?php echo htmlspecialchars($db); ?>" />
615 <input type
="hidden" name
="fields[user]" value
="<?php echo $cfg['Bookmark']['user']; ?>" />
616 <input type
="hidden" name
="fields[query]" value
="<?php echo urlencode($sql_query); ?>" />
617 <input type
="text" name
="fields[label]" value
="" />
618 <input type
="submit" name
="store_bkm" value
="<?php echo $strBookmarkThis; ?>" />
621 } // end bookmark support
623 // Do print the page if required
624 if (isset($printview) && $printview == '1') {
627 <script type
="text/javascript" language
="javascript1.2">
630 if (typeof(window
.print) != 'undefined') {
637 } // end rows returned
639 } // end executes the query
644 * Displays the footer
646 require('./footer.inc.php3');