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)
31 . '&convcharset=' . $convcharset
32 . '&server=' . $server
33 . (isset($db) ?
'&db=' . urlencode($db) : '')
34 . ((strpos(' ' . $goto, 'db_details') != 1 && isset($table)) ?
'&table=' . urlencode($table) : '');
37 // Coming from a bookmark dialog
38 if (isset($fields['query'])) {
39 $sql_query = $fields['query'];
43 * Check rights in case of DROP DATABASE
45 * This test may be bypassed if $is_js_confirmed = 1 (already checked with js)
46 * but since a malicious user may pass this variable by url/form, we don't take
47 * into account this case.
49 if (!defined('PMA_CHK_DROP')
50 && !$cfg['AllowUserDropDatabase']
51 && eregi('DROP[[:space:]]+(IF EXISTS[[:space:]]+)?DATABASE[[:space:]]', $sql_query)) {
52 // Checks if the user is a Superuser
53 // TODO: set a global variable with this information
54 // loic1: optimized query
55 $result = @PMA_mysql_query
('USE mysql');
56 if (PMA_mysql_error()) {
57 include('./header.inc.php3');
58 PMA_mysqlDie($strNoDropDatabases, '', '', $err_url);
66 if (isset($store_bkm)) {
67 if (get_magic_quotes_gpc()) {
68 $fields['label'] = stripslashes($fields['label']);
70 include('./libraries/bookmark.lib.php3');
71 PMA_addBookmarks($fields, $cfg['Bookmark']);
72 header('Location: ' . $cfg['PmaAbsoluteUri'] . $goto);
77 * Gets the true sql query
79 // $sql_query has been urlencoded in the confirmation form for drop/delete
80 // queries or in the navigation bar for browsing among records
81 if (isset($btnDrop) ||
isset($navig)) {
82 $sql_query = urldecode($sql_query);
89 $parsed_sql = PMA_SQP_parse((get_magic_quotes_gpc() ?
stripslashes($sql_query) : $sql_query));
90 $analyzed_sql = PMA_SQP_analyze($parsed_sql);
91 $sql_query = PMA_SQP_formatHtml($parsed_sql, 'query_only');
93 // here we are sure that SELECT is uppercase
94 $is_select = eregi('^SELECT[[:space:]]+', $sql_query);
96 // If the query is a Select, extract the db and table names and modify
97 // $db and $table, to have correct page headers, links and left frame.
98 // db and table name may be enclosed with backquotes, db is optionnal,
99 // query may contain aliases.
100 // (todo: check for embedded comments...)
102 // (todo: if there are more than one table name in the Select:
103 // - do not extract the first table name
104 // - do not show a table name in the page header
105 // - do not display the sub-pages links)
108 eregi('^SELECT[[:space:]]+(.*)[[:space:]]+FROM[[:space:]]+(`[^`]+`|[A-Za-z0-9_$]+)([\.]*)(`[^`]*`|[A-Za-z0-9_$]*)', $sql_query, $tmp);
110 if ($tmp[3] == '.') {
112 $db = str_replace('`', '', $tmp[2]);
113 $reload = ($db == $prev_db) ?
0 : 1;
114 $table = str_replace('`', '', $tmp[4]);
117 $table = str_replace('`', '', $tmp[2]);
123 * Sets or modifies the $goto variable if required
125 if ($goto == 'sql.php3') {
128 . '&convcharset=' . $convcharset
129 . '&server=' . $server
130 . '&db=' . urlencode($db)
131 . '&table=' . urlencode($table)
133 . '&sql_query=' . urlencode($sql_query);
138 * Go back to further page if table should not be dropped
140 if (isset($btnDrop) && $btnDrop == $strNo) {
145 if (strpos(' ' . $goto, 'db_details') == 1 && !empty($table)) {
148 include('./' . ereg_replace('\.\.*', '.', $goto));
150 header('Location: ' . $cfg['PmaAbsoluteUri'] . str_replace('&', '&', $goto));
157 * Displays the confirm page if required
159 * This part of the script is bypassed if $is_js_confirmed = 1 (already checked
160 * with js) because possible security issue is not so important here: at most,
161 * the confirm message isn't displayed.
163 * Also bypassed if only showing php code.or validating a SQL query
166 ||
(isset($is_js_confirmed) && $is_js_confirmed)
168 ||
!empty($GLOBALS['show_as_php'])
169 ||
!empty($GLOBALS['validatequery'])) {
172 /* SQL-Parser-Analyzer */
173 $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));
177 // already stripped at beginning of script
178 //$stripped_sql_query = (get_magic_quotes_gpc() ? stripslashes($sql_query) : $sql_query);
179 $stripped_sql_query = $sql_query;
180 include('./header.inc.php3');
181 echo $strDoYouReally . ' :<br />' . "\n";
182 echo '<tt>' . htmlspecialchars($stripped_sql_query) . '</tt> ?<br/>' . "\n";
184 <form action
="sql.php3" method
="post">
185 <input type
="hidden" name
="lang" value
="<?php echo $lang; ?>" />
186 <input type
="hidden" name
="convcharset" value
="<?php echo $convcharset; ?>" />
187 <input type
="hidden" name
="server" value
="<?php echo $server; ?>" />
188 <input type
="hidden" name
="db" value
="<?php echo htmlspecialchars($db); ?>" />
189 <input type
="hidden" name
="table" value
="<?php echo isset($table) ? htmlspecialchars($table) : ''; ?>" />
190 <input type
="hidden" name
="sql_query" value
="<?php echo urlencode(addslashes($sql_query)); ?>" />
191 <input type
="hidden" name
="zero_rows" value
="<?php echo isset($zero_rows) ? $zero_rows : ''; ?>" />
192 <input type
="hidden" name
="goto" value
="<?php echo $goto; ?>" />
193 <input type
="hidden" name
="back" value
="<?php echo isset($back) ? $back : ''; ?>" />
194 <input type
="hidden" name
="reload" value
="<?php echo isset($reload) ? $reload : 0; ?>" />
195 <input type
="hidden" name
="show_query" value
="<?php echo isset($show_query) ? $show_query : ''; ?>" />
196 <input type
="submit" name
="btnDrop" value
="<?php echo $strYes; ?>" />
197 <input type
="submit" name
="btnDrop" value
="<?php echo $strNo; ?>" />
205 * Executes the query and displays results
208 if (!isset($sql_query)) {
211 // already stripped at beginning of script
212 // else if (get_magic_quotes_gpc()) {
213 // $sql_query = stripslashes($sql_query);
215 // Defines some variables
216 // loic1: A table has to be created -> left frame should be reloaded
217 if ((!isset($reload) ||
$reload == 0)
218 && eregi('^CREATE TABLE[[:space:]]+(.*)', $sql_query)) {
221 // Gets the number of rows per page
222 if (!isset($session_max_rows)) {
223 $session_max_rows = $cfg['MaxRows'];
224 } else if ($session_max_rows != 'all') {
225 $cfg['MaxRows'] = $session_max_rows;
227 // Defines the display mode (horizontal/vertical) and header "frequency"
228 if (empty($disp_direction)) {
229 $disp_direction = $cfg['DefaultDisplay'];
231 if (empty($repeat_cells)) {
232 $repeat_cells = $cfg['RepeatCells'];
235 // SK -- Patch: $is_group added for use in calculation of total number of
237 // $is_count is changed for more correct "LIMIT" clause
238 // appending in queries like
239 // "SELECT COUNT(...) FROM ... GROUP BY ..."
240 $is_explain = $is_count = $is_export = $is_delete = $is_insert = $is_affected = $is_show = $is_maint = $is_analyse = $is_group = $is_func = FALSE;
241 if ($is_select) { // see line 141
242 $is_group = eregi('[[:space:]]+(GROUP[[:space:]]+BY|HAVING|SELECT[[:space:]]+DISTINCT)[[:space:]]+', $sql_query);
244 $is_func = !$is_group && (eregi('[[:space:]]+(SUM|AVG|STD|STDDEV|MIN|MAX|BIT_OR|BIT_AND)\s*\(', $sql_query));
245 $is_count = !$is_group && (eregi('^SELECT[[:space:]]+COUNT\((.*\.+)?.*\)', $sql_query));
246 $is_export = (eregi('[[:space:]]+INTO[[:space:]]+OUTFILE[[:space:]]+', $sql_query));
247 $is_analyse = (eregi('[[:space:]]+PROCEDURE[[:space:]]+ANALYSE\(', $sql_query));
248 } else if (eregi('^EXPLAIN[[:space:]]+', $sql_query)) {
250 } else if (eregi('^DELETE[[:space:]]+', $sql_query)) {
253 } else if (eregi('^(INSERT|LOAD[[:space:]]+DATA|REPLACE)[[:space:]]+', $sql_query)) {
256 } else if (eregi('^UPDATE[[:space:]]+', $sql_query)) {
258 } else if (eregi('^SHOW[[:space:]]+', $sql_query)) {
260 } else if (eregi('^(CHECK|ANALYZE|REPAIR|OPTIMIZE)[[:space:]]+TABLE[[:space:]]+', $sql_query)) {
264 // Do append a "LIMIT" clause?
266 && (!$cfg['ShowAll'] ||
$session_max_rows != 'all')
268 && !($is_count ||
$is_export ||
$is_func ||
$is_analyse)
269 && eregi('[[:space:]]FROM[[:space:]]', $sql_query)
270 && !eregi('[[:space:]]LIMIT[[:space:]0-9,-]+$', $sql_query)) {
271 $sql_limit_to_append = " LIMIT $pos, ".$cfg['MaxRows'];
272 if (eregi('(.*)([[:space:]](PROCEDURE[[:space:]](.*)|FOR[[:space:]]+UPDATE|LOCK[[:space:]]+IN[[:space:]]+SHARE[[:space:]]+MODE))$', $sql_query, $regs)) {
273 $full_sql_query = $regs[1] . $sql_limit_to_append . $regs[2];
275 $full_sql_query = $sql_query . $sql_limit_to_append;
278 $full_sql_query = $sql_query;
281 PMA_mysql_select_db($db);
283 // If the query is a DELETE query with no WHERE clause, get the number of
284 // rows that will be deleted (mysql_affected_rows will always return 0 in
287 && eregi('^DELETE([[:space:]].+)?([[:space:]]FROM[[:space:]](.+))$', $sql_query, $parts)
288 && !eregi('[[:space:]]WHERE[[:space:]]', $parts[3])) {
289 $cnt_all_result = @PMA_mysql_query
('SELECT COUNT(*) as count' . $parts[2]);
290 if ($cnt_all_result) {
291 $num_rows = PMA_mysql_result($cnt_all_result, 0, 'count');
292 mysql_free_result($cnt_all_result);
297 // Executes the query
298 // Only if we didn't ask to see the php code (mikebeck)
299 if (!empty($GLOBALS['show_as_php']) ||
!empty($GLOBALS['validatequery'])) {
304 $result = @PMA_mysql_query
($full_sql_query);
305 // Displays an error message if required and stop parsing the script
306 if (PMA_mysql_error()) {
307 $error = PMA_mysql_error();
308 include('./header.inc.php3');
309 $full_err_url = (ereg('^(db_details|tbl_properties)', $err_url))
310 ?
$err_url . '&show_query=1&sql_query=' . urlencode($sql_query)
312 PMA_mysqlDie($error, $full_sql_query, '', $full_err_url);
315 // Checks if the current database has changed
316 // This could happen if the user sends a query like "USE `database`;"
317 $res = PMA_mysql_query('SELECT DATABASE() AS "db";');
318 $row = PMA_mysql_fetch_array($res);
319 if ($db != $row['db']) {
323 @mysql_free_result
($res);
327 // tmpfile remove after convert encoding appended by Y.Kawada
328 if (function_exists('PMA_kanji_file_conv')
329 && (isset($textfile) && file_exists($textfile))) {
333 // Gets the number of rows affected/returned
335 $num_rows = ($result) ? @mysql_num_rows
($result) : 0;
336 } else if (!isset($num_rows)) {
337 $num_rows = @mysql_affected_rows
();
340 // Counts the total number of rows for the same 'SELECT' query without the
341 // 'LIMIT' clause that may have been programatically added
342 if (empty($sql_limit_to_append)) {
343 $unlim_num_rows = $num_rows;
345 else if ($is_select) {
346 // SK -- Patch : correct calculations for GROUP BY, HAVING, DISTINCT
348 // Reads only the from-part of the query...
349 // NOTE: here the presence of LIMIT is impossible, HAVING and GROUP BY
350 // are necessary for correct calculation, and extra spaces and
351 // lowercase reserved words are removed, so we have a simple split
354 $array = split('[[:space:]]+(FROM|ORDER BY)[[:space:]]+', $sql_query);
356 // if $array[1] is empty here, there is an error in the query:
357 // "... FROM [ORDER BY ...]", but the query is already executed with
358 // success so this check is redundant???
360 if (!empty($array[1])) {
361 // ... and makes a count(*) to count the entries
362 // Special case: SELECT DISTINCT ... FROM ...
363 // the count of resulting rows can be found as:
364 // SELECT COUNT(DISTINCT ...) FROM ...
365 if (eregi('^SELECT DISTINCT(.*)', $array[0], $array_dist)) {
366 $count_what = 'DISTINCT ' . $array_dist[1];
370 $count_query = 'SELECT COUNT(' . $count_what . ') AS count FROM ' . $array[1];
371 if ($cnt_all_result = mysql_query($count_query)) {
373 $unlim_num_rows = @mysql_num_rows
($cnt_all_result);
375 $unlim_num_rows = mysql_result($cnt_all_result, 0, 'count');
377 mysql_free_result($cnt_all_result);
382 } // end rows total count
383 } // end else "didn't ask to see php code"
385 // No rows returned -> move back to the calling page
386 if ($num_rows < 1 ||
$is_affected) {
388 $message = $strDeletedRows . ' ' . $num_rows;
389 } else if ($is_insert) {
390 $message = $strInsertedRows . ' ' . $num_rows;
391 } else if ($is_affected) {
392 $message = $strAffectedRows . ' ' . $num_rows;
393 } else if (!empty($zero_rows)) {
394 $message = $zero_rows;
395 } else if (!empty($GLOBALS['show_as_php'])) {
397 } else if (!empty($GLOBALS['validatequery'])) {
398 $message = $strValidateSQL;
400 $message = $strEmptyResultSet;
404 $goto = ereg_replace('\.\.*', '.', $goto);
405 // Checks for a valid target script
406 if (isset($table) && $table == '') {
409 if (isset($db) && $db == '') {
412 $is_db = $is_table = FALSE;
413 if (strpos(' ' . $goto, 'tbl_properties') == 1) {
414 if (!isset($table)) {
415 $goto = 'db_details.php3';
417 $is_table = @PMA_mysql_query
('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'');
418 if (!($is_table && @mysql_numrows
($is_table))) {
419 $goto = 'db_details.php3';
422 } // end if... else...
424 if (strpos(' ' . $goto, 'db_details') == 1) {
431 $is_db = @PMA_mysql_select_db
($db);
436 } // end if... else...
438 // Loads to target script
439 if (strpos(' ' . $goto, 'db_details') == 1
440 ||
strpos(' ' . $goto, 'tbl_properties') == 1) {
441 $js_to_run = 'functions.js';
443 if ($goto != 'main.php3') {
444 include('./header.inc.php3');
446 include('./' . $goto);
447 } // end if file_exist
449 header('Location: ' . $cfg['PmaAbsoluteUri'] . str_replace('&', '&', $goto) . '&message=' . urlencode($message));
452 } // end no rows returned
454 // At least one row is returned -> displays a table with results
456 // Displays the headers
457 if (isset($show_query)) {
460 if (isset($printview) && $printview == '1') {
461 include('./header_printview.inc.php3');
463 $js_to_run = 'functions.js';
465 if (!empty($table)) {
466 include('./tbl_properties_common.php3');
467 $url_query .= '&goto=tbl_properties.php3&back=tbl_properties.php3';
468 include('./tbl_properties_table_info.php3');
471 include('./db_details_common.php3');
472 include('./db_details_db_info.php3');
474 include('./libraries/relation.lib.php3');
475 $cfgRelation = PMA_getRelationsParam();
478 // Gets the list of fields properties
479 if (isset($result) && $result) {
480 while ($field = PMA_mysql_fetch_field($result)) {
481 $fields_meta[] = $field;
483 $fields_cnt = count($fields_meta);
485 // Displays the results in a table
486 include('./libraries/display_tbl.lib.php3');
487 if (empty($disp_mode)) {
488 // see the "PMA_setDisplayMode()" function in
489 // libraries/display_tbl.lib.php3
490 $disp_mode = 'urdr111101';
492 if (!isset($dontlimitchars)) {
496 PMA_displayTable($result, $disp_mode, $analyzed_sql);
497 mysql_free_result($result);
499 if ($disp_mode[6] == '1' ||
$disp_mode[9] == '1') {
503 // Displays "Insert a new row" link if required
504 if ($disp_mode[6] == '1') {
505 $lnk_goto = 'sql.php3'
507 . '&convcharset=' . $convcharset
508 . '&server=' . $server
509 . '&db=' . urlencode($db)
510 . '&table=' . urlencode($table)
512 . '&session_max_rows=' . $session_max_rows
513 . '&disp_direction=' . $disp_direction
514 . '&repeat_cells=' . $repeat_cells
515 . '&dontlimitchars=' . $dontlimitchars
516 . '&sql_query=' . urlencode($sql_query);
517 $url_query = '?lang=' . $lang
518 . '&convcharset=' . $convcharset
519 . '&server=' . $server
520 . '&db=' . urlencode($db)
521 . '&table=' . urlencode($table)
523 . '&session_max_rows=' . $session_max_rows
524 . '&disp_direction=' . $disp_direction
525 . '&repeat_cells=' . $repeat_cells
526 . '&dontlimitchars=' . $dontlimitchars
527 . '&sql_query=' . urlencode($sql_query)
528 . '&goto=' . urlencode($lnk_goto);
530 echo ' <!-- Insert a new row -->' . "\n"
531 . ' <a href="tbl_change.php3' . $url_query . '">' . $strInsertNewRow . '</a>';
532 if ($disp_mode[9] == '1') {
536 } // end insert new row
538 // Displays "printable view" link if required
539 if ($disp_mode[9] == '1') {
540 $url_query = '?lang=' . $lang
541 . '&convcharset=' . $convcharset
542 . '&server=' . $server
543 . '&db=' . urlencode($db)
544 . '&table=' . urlencode($table)
546 . '&session_max_rows=' . $session_max_rows
547 . '&disp_direction=' . $disp_direction
548 . '&repeat_cells=' . $repeat_cells
550 . ((isset($dontlimitchars) && $dontlimitchars == '1') ?
'&dontlimitchars=1' : '')
551 . '&sql_query=' . urlencode($sql_query);
552 echo ' <!-- Print view -->' . "\n"
553 . ' <a href="sql.php3' . $url_query . '" target="print_view">' . $strPrintView . '</a>' . "\n";
554 } // end displays "printable view"
559 // Bookmark Support if required
560 if ($disp_mode[7] == '1'
561 && ($cfg['Bookmark']['db'] && $cfg['Bookmark']['table'] && empty($id_bookmark))
562 && !empty($sql_query)) {
567 . '&convcharset=' . $convcharset
568 . '&server=' . $server
569 . '&db=' . urlencode($db)
570 . '&table=' . urlencode($table)
572 . '&session_max_rows=' . $session_max_rows
573 . '&disp_direction=' . $disp_direction
574 . '&repeat_cells=' . $repeat_cells
575 . '&dontlimitchars=' . $dontlimitchars
576 . '&sql_query=' . urlencode($sql_query)
577 . '&id_bookmark=1';
579 <!-- Bookmark the query
-->
580 <form action
="sql.php3" method
="post" onsubmit
="return emptyFormElements(this, 'fields[label]');">
583 if ($disp_mode[3] == '1') {
584 echo ' <i>' . $strOr . '</i>' . "\n";
588 <?php
echo $strBookmarkLabel; ?
> 
;:
589 <input type
="hidden" name
="server" value
="<?php echo $server; ?>" />
590 <input type
="hidden" name
="goto" value
="<?php echo $goto; ?>" />
591 <input type
="hidden" name
="fields[dbase]" value
="<?php echo htmlspecialchars($db); ?>" />
592 <input type
="hidden" name
="fields[user]" value
="<?php echo $cfg['Bookmark']['user']; ?>" />
593 <input type
="hidden" name
="fields[query]" value
="<?php echo urlencode($sql_query); ?>" />
594 <input type
="text" name
="fields[label]" value
="" />
595 <input type
="submit" name
="store_bkm" value
="<?php echo $strBookmarkThis; ?>" />
598 } // end bookmark support
600 // Do print the page if required
601 if (isset($printview) && $printview == '1') {
604 <script type
="text/javascript" language
="javascript1.2">
607 if (typeof(window
.print) != 'undefined') {
614 } // end rows returned
616 } // end executes the query
621 * Displays the footer
623 require('./footer.inc.php3');