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) : '');
38 * Check rights in case of DROP DATABASE
40 * This test may be bypassed if $is_js_confirmed = 1 (already checked with js)
41 * but since a malicious user may pass this variable by url/form, we don't take
42 * into account this case.
44 if (!defined('PMA_CHK_DROP')
45 && !$cfg['AllowUserDropDatabase']
46 && eregi('DROP[[:space:]]+(IF EXISTS[[:space:]]+)?DATABASE[[:space:]]', $sql_query)) {
47 // Checks if the user is a Superuser
48 // TODO: set a global variable with this information
49 // loic1: optimized query
50 $result = @PMA_mysql_query
('USE mysql');
51 if (PMA_mysql_error()) {
52 include('./header.inc.php3');
53 PMA_mysqlDie($strNoDropDatabases, '', '', $err_url);
61 if (isset($store_bkm)) {
62 if (get_magic_quotes_gpc()) {
63 $fields['label'] = stripslashes($fields['label']);
65 include('./libraries/bookmark.lib.php3');
66 PMA_addBookmarks($fields, $cfg['Bookmark']);
67 header('Location: ' . $cfg['PmaAbsoluteUri'] . $goto);
72 * Gets the true sql query
74 // $sql_query has been urlencoded in the confirmation form for drop/delete
75 // queries or in the navigation bar for browsing among records
76 if (isset($btnDrop) ||
isset($navig)) {
77 $sql_query = urldecode($sql_query);
84 $parsed_sql = PMA_SQP_parse((get_magic_quotes_gpc() ?
stripslashes($sql_query) : $sql_query));
85 $analyzed_sql = PMA_SQP_analyze($parsed_sql);
86 $sql_query = PMA_SQP_formatHtml($parsed_sql, 'query_only');
88 // here we are sure that SELECT is uppercase
89 $is_select = eregi('^SELECT[[:space:]]+', $sql_query);
91 // If the query is a Select, extract the db and table names and modify
92 // $db and $table, to have correct page headers, links and left frame.
93 // db and table name may be enclosed with backquotes, db is optionnal,
94 // query may contain aliases.
95 // (todo: check for embedded comments...)
97 // (todo: if there are more than one table name in the Select:
98 // - do not extract the first table name
99 // - do not show a table name in the page header
100 // - do not display the sub-pages links)
103 eregi('^SELECT[[:space:]]+(.*)[[:space:]]+FROM[[:space:]]+(`[^`]+`|[A-Za-z0-9_$]+)([\.]*)(`[^`]*`|[A-Za-z0-9_$]*)', $sql_query, $tmp);
105 if ($tmp[3] == '.') {
107 $db = str_replace('`', '', $tmp[2]);
108 $reload = ($db == $prev_db) ?
0 : 1;
109 $table = str_replace('`', '', $tmp[4]);
112 $table = str_replace('`', '', $tmp[2]);
118 * Sets or modifies the $goto variable if required
120 if ($goto == 'sql.php3') {
123 . '&convcharset=' . $convcharset
124 . '&server=' . $server
125 . '&db=' . urlencode($db)
126 . '&table=' . urlencode($table)
128 . '&sql_query=' . urlencode($sql_query);
133 * Go back to further page if table should not be dropped
135 if (isset($btnDrop) && $btnDrop == $strNo) {
140 if (strpos(' ' . $goto, 'db_details') == 1 && !empty($table)) {
143 include('./' . ereg_replace('\.\.*', '.', $goto));
145 header('Location: ' . $cfg['PmaAbsoluteUri'] . str_replace('&', '&', $goto));
152 * Displays the confirm page if required
154 * This part of the script is bypassed if $is_js_confirmed = 1 (already checked
155 * with js) because possible security issue is not so important here: at most,
156 * the confirm message isn't displayed.
158 * Also bypassed if only showing php code.or validating a SQL query
161 ||
(isset($is_js_confirmed) && $is_js_confirmed)
163 ||
!empty($GLOBALS['show_as_php'])
164 ||
!empty($GLOBALS['validatequery'])) {
167 /* SQL-Parser-Analyzer */
168 $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));
172 // already stripped at beginning of script
173 //$stripped_sql_query = (get_magic_quotes_gpc() ? stripslashes($sql_query) : $sql_query);
174 $stripped_sql_query = $sql_query;
175 include('./header.inc.php3');
176 echo $strDoYouReally . ' :<br />' . "\n";
177 echo '<tt>' . htmlspecialchars($stripped_sql_query) . '</tt> ?<br/>' . "\n";
179 <form action
="sql.php3" method
="post">
180 <input type
="hidden" name
="lang" value
="<?php echo $lang; ?>" />
181 <input type
="hidden" name
="convcharset" value
="<?php echo $convcharset; ?>" />
182 <input type
="hidden" name
="server" value
="<?php echo $server; ?>" />
183 <input type
="hidden" name
="db" value
="<?php echo htmlspecialchars($db); ?>" />
184 <input type
="hidden" name
="table" value
="<?php echo isset($table) ? htmlspecialchars($table) : ''; ?>" />
185 <input type
="hidden" name
="sql_query" value
="<?php echo urlencode(addslashes($sql_query)); ?>" />
186 <input type
="hidden" name
="zero_rows" value
="<?php echo isset($zero_rows) ? $zero_rows : ''; ?>" />
187 <input type
="hidden" name
="goto" value
="<?php echo $goto; ?>" />
188 <input type
="hidden" name
="back" value
="<?php echo isset($back) ? $back : ''; ?>" />
189 <input type
="hidden" name
="reload" value
="<?php echo isset($reload) ? $reload : 0; ?>" />
190 <input type
="hidden" name
="show_query" value
="<?php echo isset($show_query) ? $show_query : ''; ?>" />
191 <input type
="submit" name
="btnDrop" value
="<?php echo $strYes; ?>" />
192 <input type
="submit" name
="btnDrop" value
="<?php echo $strNo; ?>" />
200 * Executes the query and displays results
203 if (!isset($sql_query)) {
206 // already stripped at beginning of script
207 // else if (get_magic_quotes_gpc()) {
208 // $sql_query = stripslashes($sql_query);
210 // Defines some variables
211 // loic1: A table has to be created -> left frame should be reloaded
212 if ((!isset($reload) ||
$reload == 0)
213 && eregi('^CREATE TABLE[[:space:]]+(.*)', $sql_query)) {
216 // Gets the number of rows per page
217 if (!isset($session_max_rows)) {
218 $session_max_rows = $cfg['MaxRows'];
219 } else if ($session_max_rows != 'all') {
220 $cfg['MaxRows'] = $session_max_rows;
222 // Defines the display mode (horizontal/vertical) and header "frequency"
223 if (empty($disp_direction)) {
224 $disp_direction = $cfg['DefaultDisplay'];
226 if (empty($repeat_cells)) {
227 $repeat_cells = $cfg['RepeatCells'];
230 // SK -- Patch: $is_group added for use in calculation of total number of
232 // $is_count is changed for more correct "LIMIT" clause
233 // appending in queries like
234 // "SELECT COUNT(...) FROM ... GROUP BY ..."
235 $is_explain = $is_count = $is_export = $is_delete = $is_insert = $is_affected = $is_show = $is_maint = $is_analyse = $is_group = $is_func = FALSE;
236 if ($is_select) { // see line 141
237 $is_group = eregi('[[:space:]]+(GROUP[[:space:]]+BY|HAVING|SELECT[[:space:]]+DISTINCT)[[:space:]]+', $sql_query);
239 $is_func = !$is_group && (eregi('[[:space:]]+(SUM|AVG|STD|STDDEV|MIN|MAX|BIT_OR|BIT_AND)\s*\(', $sql_query));
240 $is_count = !$is_group && (eregi('^SELECT[[:space:]]+COUNT\((.*\.+)?.*\)', $sql_query));
241 $is_export = (eregi('[[:space:]]+INTO[[:space:]]+OUTFILE[[:space:]]+', $sql_query));
242 $is_analyse = (eregi('[[:space:]]+PROCEDURE[[:space:]]+ANALYSE\(', $sql_query));
243 } else if (eregi('^EXPLAIN[[:space:]]+', $sql_query)) {
245 } else if (eregi('^DELETE[[:space:]]+', $sql_query)) {
248 } else if (eregi('^(INSERT|LOAD[[:space:]]+DATA|REPLACE)[[:space:]]+', $sql_query)) {
251 } else if (eregi('^UPDATE[[:space:]]+', $sql_query)) {
253 } else if (eregi('^SHOW[[:space:]]+', $sql_query)) {
255 } else if (eregi('^(CHECK|ANALYZE|REPAIR|OPTIMIZE)[[:space:]]+TABLE[[:space:]]+', $sql_query)) {
259 // Do append a "LIMIT" clause?
261 && (!$cfg['ShowAll'] ||
$session_max_rows != 'all')
263 && !($is_count ||
$is_export ||
$is_func ||
$is_analyse)
264 && eregi('[[:space:]]FROM[[:space:]]', $sql_query)
265 && !eregi('[[:space:]]LIMIT[[:space:]0-9,-]+$', $sql_query)) {
266 $sql_limit_to_append = " LIMIT $pos, ".$cfg['MaxRows'];
267 if (eregi('(.*)([[:space:]](PROCEDURE[[:space:]](.*)|FOR[[:space:]]+UPDATE|LOCK[[:space:]]+IN[[:space:]]+SHARE[[:space:]]+MODE))$', $sql_query, $regs)) {
268 $full_sql_query = $regs[1] . $sql_limit_to_append . $regs[2];
270 $full_sql_query = $sql_query . $sql_limit_to_append;
273 $full_sql_query = $sql_query;
276 PMA_mysql_select_db($db);
278 // If the query is a DELETE query with no WHERE clause, get the number of
279 // rows that will be deleted (mysql_affected_rows will always return 0 in
282 && eregi('^DELETE([[:space:]].+)?([[:space:]]FROM[[:space:]](.+))$', $sql_query, $parts)
283 && !eregi('[[:space:]]WHERE[[:space:]]', $parts[3])) {
284 $cnt_all_result = @PMA_mysql_query
('SELECT COUNT(*) as count' . $parts[2]);
285 if ($cnt_all_result) {
286 $num_rows = PMA_mysql_result($cnt_all_result, 0, 'count');
287 mysql_free_result($cnt_all_result);
292 // Executes the query
293 // Only if we didn't ask to see the php code (mikebeck)
294 if (!empty($GLOBALS['show_as_php']) ||
!empty($GLOBALS['validatequery'])) {
299 $result = @PMA_mysql_query
($full_sql_query);
300 // Displays an error message if required and stop parsing the script
301 if (PMA_mysql_error()) {
302 $error = PMA_mysql_error();
303 include('./header.inc.php3');
304 $full_err_url = (ereg('^(db_details|tbl_properties)', $err_url))
305 ?
$err_url . '&show_query=1&sql_query=' . urlencode($sql_query)
307 PMA_mysqlDie($error, $full_sql_query, '', $full_err_url);
310 // Checks if the current database has changed
311 // This could happen if the user sends a query like "USE `database`;"
312 $res = PMA_mysql_query('SELECT DATABASE() AS "db";');
313 $row = PMA_mysql_fetch_array($res);
314 if ($db != $row['db']) {
318 @mysql_free_result
($res);
322 // tmpfile remove after convert encoding appended by Y.Kawada
323 if (function_exists('PMA_kanji_file_conv')
324 && (isset($textfile) && file_exists($textfile))) {
328 // Gets the number of rows affected/returned
330 $num_rows = ($result) ? @mysql_num_rows
($result) : 0;
331 } else if (!isset($num_rows)) {
332 $num_rows = @mysql_affected_rows
();
335 // Counts the total number of rows for the same 'SELECT' query without the
336 // 'LIMIT' clause that may have been programatically added
337 if (empty($sql_limit_to_append)) {
338 $unlim_num_rows = $num_rows;
340 else if ($is_select) {
341 // SK -- Patch : correct calculations for GROUP BY, HAVING, DISTINCT
343 // Reads only the from-part of the query...
344 // NOTE: here the presence of LIMIT is impossible, HAVING and GROUP BY
345 // are necessary for correct calculation, and extra spaces and
346 // lowercase reserved words are removed, so we have a simple split
349 $array = split('[[:space:]]+(FROM|ORDER BY)[[:space:]]+', $sql_query);
351 // if $array[1] is empty here, there is an error in the query:
352 // "... FROM [ORDER BY ...]", but the query is already executed with
353 // success so this check is redundant???
355 if (!empty($array[1])) {
356 // ... and makes a count(*) to count the entries
357 // Special case: SELECT DISTINCT ... FROM ...
358 // the count of resulting rows can be found as:
359 // SELECT COUNT(DISTINCT ...) FROM ...
360 if (eregi('^SELECT DISTINCT(.*)', $array[0], $array_dist)) {
361 $count_what = 'DISTINCT ' . $array_dist[1];
365 $count_query = 'SELECT COUNT(' . $count_what . ') AS count FROM ' . $array[1];
366 if ($cnt_all_result = mysql_query($count_query)) {
368 $unlim_num_rows = @mysql_num_rows
($cnt_all_result);
370 $unlim_num_rows = mysql_result($cnt_all_result, 0, 'count');
372 mysql_free_result($cnt_all_result);
377 } // end rows total count
378 } // end else "didn't ask to see php code"
380 // No rows returned -> move back to the calling page
381 if ($num_rows < 1 ||
$is_affected) {
383 $message = $strDeletedRows . ' ' . $num_rows;
384 } else if ($is_insert) {
385 $message = $strInsertedRows . ' ' . $num_rows;
386 } else if ($is_affected) {
387 $message = $strAffectedRows . ' ' . $num_rows;
388 } else if (!empty($zero_rows)) {
389 $message = $zero_rows;
390 } else if (!empty($GLOBALS['show_as_php'])) {
392 } else if (!empty($GLOBALS['validatequery'])) {
393 $message = $strValidateSQL;
395 $message = $strEmptyResultSet;
399 $goto = ereg_replace('\.\.*', '.', $goto);
400 // Checks for a valid target script
401 if (isset($table) && $table == '') {
404 if (isset($db) && $db == '') {
407 $is_db = $is_table = FALSE;
408 if (strpos(' ' . $goto, 'tbl_properties') == 1) {
409 if (!isset($table)) {
410 $goto = 'db_details.php3';
412 $is_table = @PMA_mysql_query
('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'');
413 if (!($is_table && @mysql_numrows
($is_table))) {
414 $goto = 'db_details.php3';
417 } // end if... else...
419 if (strpos(' ' . $goto, 'db_details') == 1) {
426 $is_db = @PMA_mysql_select_db
($db);
431 } // end if... else...
433 // Loads to target script
434 if (strpos(' ' . $goto, 'db_details') == 1
435 ||
strpos(' ' . $goto, 'tbl_properties') == 1) {
436 $js_to_run = 'functions.js';
438 if ($goto != 'main.php3') {
439 include('./header.inc.php3');
441 include('./' . $goto);
442 } // end if file_exist
444 header('Location: ' . $cfg['PmaAbsoluteUri'] . str_replace('&', '&', $goto) . '&message=' . urlencode($message));
447 } // end no rows returned
449 // At least one row is returned -> displays a table with results
451 // Displays the headers
452 if (isset($show_query)) {
455 if (isset($printview) && $printview == '1') {
456 include('./header_printview.inc.php3');
458 $js_to_run = 'functions.js';
460 if (!empty($table)) {
461 include('./tbl_properties_common.php3');
462 $url_query .= '&goto=tbl_properties.php3&back=tbl_properties.php3';
463 include('./tbl_properties_table_info.php3');
466 include('./db_details_common.php3');
467 include('./db_details_db_info.php3');
469 include('./libraries/relation.lib.php3');
470 $cfgRelation = PMA_getRelationsParam();
473 // Gets the list of fields properties
474 if (isset($result) && $result) {
475 while ($field = PMA_mysql_fetch_field($result)) {
476 $fields_meta[] = $field;
478 $fields_cnt = count($fields_meta);
480 // Displays the results in a table
481 include('./libraries/display_tbl.lib.php3');
482 if (empty($disp_mode)) {
483 // see the "PMA_setDisplayMode()" function in
484 // libraries/display_tbl.lib.php3
485 $disp_mode = 'urdr111101';
487 if (!isset($dontlimitchars)) {
491 PMA_displayTable($result, $disp_mode, $analyzed_sql);
492 mysql_free_result($result);
494 if ($disp_mode[6] == '1' ||
$disp_mode[9] == '1') {
498 // Displays "Insert a new row" link if required
499 if ($disp_mode[6] == '1') {
500 $lnk_goto = 'sql.php3'
502 . '&convcharset=' . $convcharset
503 . '&server=' . $server
504 . '&db=' . urlencode($db)
505 . '&table=' . urlencode($table)
507 . '&session_max_rows=' . $session_max_rows
508 . '&disp_direction=' . $disp_direction
509 . '&repeat_cells=' . $repeat_cells
510 . '&dontlimitchars=' . $dontlimitchars
511 . '&sql_query=' . urlencode($sql_query);
512 $url_query = '?lang=' . $lang
513 . '&convcharset=' . $convcharset
514 . '&server=' . $server
515 . '&db=' . urlencode($db)
516 . '&table=' . urlencode($table)
518 . '&session_max_rows=' . $session_max_rows
519 . '&disp_direction=' . $disp_direction
520 . '&repeat_cells=' . $repeat_cells
521 . '&dontlimitchars=' . $dontlimitchars
522 . '&sql_query=' . urlencode($sql_query)
523 . '&goto=' . urlencode($lnk_goto);
525 echo ' <!-- Insert a new row -->' . "\n"
526 . ' <a href="tbl_change.php3' . $url_query . '">' . $strInsertNewRow . '</a>';
527 if ($disp_mode[9] == '1') {
531 } // end insert new row
533 // Displays "printable view" link if required
534 if ($disp_mode[9] == '1') {
535 $url_query = '?lang=' . $lang
536 . '&convcharset=' . $convcharset
537 . '&server=' . $server
538 . '&db=' . urlencode($db)
539 . '&table=' . urlencode($table)
541 . '&session_max_rows=' . $session_max_rows
542 . '&disp_direction=' . $disp_direction
543 . '&repeat_cells=' . $repeat_cells
545 . ((isset($dontlimitchars) && $dontlimitchars == '1') ?
'&dontlimitchars=1' : '')
546 . '&sql_query=' . urlencode($sql_query);
547 echo ' <!-- Print view -->' . "\n"
548 . ' <a href="sql.php3' . $url_query . '" target="print_view">' . $strPrintView . '</a>' . "\n";
549 } // end displays "printable view"
554 // Bookmark Support if required
555 if ($disp_mode[7] == '1'
556 && ($cfg['Bookmark']['db'] && $cfg['Bookmark']['table'] && empty($id_bookmark))
557 && !empty($sql_query)) {
562 . '&convcharset=' . $convcharset
563 . '&server=' . $server
564 . '&db=' . urlencode($db)
565 . '&table=' . urlencode($table)
567 . '&session_max_rows=' . $session_max_rows
568 . '&disp_direction=' . $disp_direction
569 . '&repeat_cells=' . $repeat_cells
570 . '&dontlimitchars=' . $dontlimitchars
571 . '&sql_query=' . urlencode($sql_query)
572 . '&id_bookmark=1';
574 <!-- Bookmark the query
-->
575 <form action
="sql.php3" method
="post" onsubmit
="return emptyFormElements(this, 'fields[label]');">
578 if ($disp_mode[3] == '1') {
579 echo ' <i>' . $strOr . '</i>' . "\n";
583 <?php
echo $strBookmarkLabel; ?
> 
;:
584 <input type
="hidden" name
="server" value
="<?php echo $server; ?>" />
585 <input type
="hidden" name
="goto" value
="<?php echo $goto; ?>" />
586 <input type
="hidden" name
="fields[dbase]" value
="<?php echo htmlspecialchars($db); ?>" />
587 <input type
="hidden" name
="fields[user]" value
="<?php echo $cfg['Bookmark']['user']; ?>" />
588 <input type
="hidden" name
="fields[query]" value
="<?php echo urlencode($sql_query); ?>" />
589 <input type
="text" name
="fields[label]" value
="" />
590 <input type
="submit" name
="store_bkm" value
="<?php echo $strBookmarkThis; ?>" />
593 } // end bookmark support
595 // Do print the page if required
596 if (isset($printview) && $printview == '1') {
599 <script type
="text/javascript" language
="javascript1.2">
602 if (typeof(window
.print) != 'undefined') {
609 } // end rows returned
611 } // end executes the query
616 * Displays the footer
618 require('./footer.inc.php3');