wrong path
[phpmyadmin/crack.git] / sql.php
blob74d3754914f08c27b26c6f3ea0448c31cd4cc61b
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Gets some core libraries
7 */
8 require_once './libraries/common.lib.php';
9 require_once './libraries/Table.class.php';
10 require_once './libraries/tbl_indexes.lib.php';
11 require_once './libraries/check_user_privileges.lib.php';
12 require_once './libraries/bookmark.lib.php';
14 /**
15 * Could be coming from a subform ("T" column expander)
17 if (isset($_REQUEST['dontlimitchars'])) {
18 $dontlimitchars = $_REQUEST['dontlimitchars'];
21 /**
22 * Defines the url to return to in case of error in a sql statement
24 // Security checkings
25 if (!empty($goto)) {
26 $is_gotofile = preg_replace('@^([^?]+).*$@s', '\\1', $goto);
27 if (!@file_exists('./' . $is_gotofile)) {
28 unset($goto);
29 } else {
30 $is_gotofile = ($is_gotofile == $goto);
32 } // end if (security checkings)
34 if (empty($goto)) {
35 $goto = (! isset($table) || ! strlen($table)) ? $cfg['DefaultTabDatabase'] : $cfg['DefaultTabTable'];
36 $is_gotofile = true;
37 } // end if
38 if (!isset($err_url)) {
39 $err_url = (!empty($back) ? $back : $goto)
40 . '?' . PMA_generate_common_url(isset($db) ? $db : '')
41 . ((strpos(' ' . $goto, 'db_details') != 1 && isset($table)) ? '&amp;table=' . urlencode($table) : '');
42 } // end if
44 // Coming from a bookmark dialog
45 if (isset($fields['query'])) {
46 $sql_query = $fields['query'];
49 // This one is just to fill $db
50 if (isset($fields['dbase'])) {
51 $db = $fields['dbase'];
54 // Default to browse if no query set an we have table (needed for browsing from DefaultTabTable)
55 if (!isset($sql_query) && isset($table) && isset($db)) {
56 require_once './libraries/bookmark.lib.php';
57 $book_sql_query = PMA_queryBookmarks($db, $GLOBALS['cfg']['Bookmark'], '\'' . PMA_sqlAddslashes($table) . '\'', 'label');
58 if (!empty($book_sql_query)) {
59 $sql_query = $book_sql_query;
60 } else {
61 $sql_query = 'SELECT * FROM ' . PMA_backquote($table);
63 unset($book_sql_query);
64 } else {
65 // Now we can check the parameters
66 PMA_checkParameters(array('sql_query'));
69 // instead of doing the test twice
70 $is_drop_database = preg_match('/DROP[[:space:]]+(DATABASE|SCHEMA)[[:space:]]+/i',
71 $sql_query);
73 /**
74 * Check rights in case of DROP DATABASE
76 * This test may be bypassed if $is_js_confirmed = 1 (already checked with js)
77 * but since a malicious user may pass this variable by url/form, we don't take
78 * into account this case.
80 if (!defined('PMA_CHK_DROP')
81 && !$cfg['AllowUserDropDatabase']
82 && $is_drop_database
83 && !$is_superuser) {
84 require_once './libraries/header.inc.php';
85 PMA_mysqlDie($strNoDropDatabases, '', '', $err_url);
86 } // end if
89 /**
90 * Need to find the real end of rows?
93 if (isset($find_real_end) && $find_real_end) {
94 $unlim_num_rows = PMA_Table::countRecords($db, $table, true, true);
95 $pos = @((ceil($unlim_num_rows / $session_max_rows) - 1) * $session_max_rows);
97 /**
98 * Avoids undefined variables
100 elseif (!isset($pos)) {
101 $pos = 0;
105 * Bookmark add
107 if (isset($store_bkm)) {
108 PMA_addBookmarks($fields, $cfg['Bookmark'], (isset($bkm_all_users) && $bkm_all_users == 'true' ? true : false));
109 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . $goto);
110 } // end if
114 * Gets the true sql query
116 // $sql_query has been urlencoded in the confirmation form for drop/delete
117 // queries or in the navigation bar for browsing among records
118 if (isset($btnDrop) || isset($navig)) {
119 $sql_query = urldecode($sql_query);
123 * Reformat the query
126 $GLOBALS['unparsed_sql'] = $sql_query;
127 $parsed_sql = PMA_SQP_parse($sql_query);
128 $analyzed_sql = PMA_SQP_analyze($parsed_sql);
130 // Bug #641765 - Robbat2 - 12 January 2003, 10:49PM
131 // Reverted - Robbat2 - 13 January 2003, 2:40PM
133 // lem9: for bug 780516: now that we use case insensitive preg_match
134 // or flags from the analyser, do not put back the reformatted query
135 // into $sql_query, to make this kind of query work without
136 // capitalizing keywords:
138 // CREATE TABLE SG_Persons (
139 // id int(10) unsigned NOT NULL auto_increment,
140 // first varchar(64) NOT NULL default '',
141 // PRIMARY KEY (`id`)
142 // )
144 // Note: now we probably do not need to fill and use $GLOBALS['unparsed_sql']
145 // but I let this intact for now.
147 //$sql_query = PMA_SQP_formatHtml($parsed_sql, 'query_only');
150 // check for a real SELECT ... FROM
151 $is_select = isset($analyzed_sql[0]['queryflags']['select_from']);
153 // If the query is a Select, extract the db and table names and modify
154 // $db and $table, to have correct page headers, links and left frame.
155 // db and table name may be enclosed with backquotes, db is optionnal,
156 // query may contain aliases.
158 // (TODO: if there are more than one table name in the Select:
159 // - do not extract the first table name
160 // - do not show a table name in the page header
161 // - do not display the sub-pages links)
163 if ($is_select) {
164 $prev_db = $db;
165 if (isset($analyzed_sql[0]['table_ref'][0]['table_true_name'])) {
166 $table = $analyzed_sql[0]['table_ref'][0]['table_true_name'];
168 if (isset($analyzed_sql[0]['table_ref'][0]['db'])
169 && strlen($analyzed_sql[0]['table_ref'][0]['db'])) {
170 $db = $analyzed_sql[0]['table_ref'][0]['db'];
171 } else {
172 $db = $prev_db;
174 // Nijel: don't change reload, if we already decided to reload in import
175 if (empty($reload)) {
176 $reload = ($db == $prev_db) ? 0 : 1;
181 * Sets or modifies the $goto variable if required
183 if ($goto == 'sql.php') {
184 $is_gotofile = false;
185 $goto = 'sql.php?'
186 . PMA_generate_common_url($db, $table)
187 . '&amp;pos=' . $pos
188 . '&amp;sql_query=' . urlencode($sql_query);
189 } // end if
193 * Go back to further page if table should not be dropped
195 if (isset($btnDrop) && $btnDrop == $strNo) {
196 if (!empty($back)) {
197 $goto = $back;
199 if ($is_gotofile) {
200 if (strpos(' ' . $goto, 'db_details') == 1 && isset($table) && strlen($table)) {
201 unset($table);
203 $active_page = $goto;
204 require './' . PMA_securePath($goto);
205 } else {
206 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . str_replace('&amp;', '&', $goto));
208 exit();
209 } // end if
213 * Displays the confirm page if required
215 * This part of the script is bypassed if $is_js_confirmed = 1 (already checked
216 * with js) because possible security issue is not so important here: at most,
217 * the confirm message isn't displayed.
219 * Also bypassed if only showing php code.or validating a SQL query
221 if (!$cfg['Confirm']
222 || (isset($is_js_confirmed) && $is_js_confirmed)
223 || isset($btnDrop)
225 // if we are coming from a "Create PHP code" or a "Without PHP Code"
226 // dialog, we won't execute the query anyway, so don't confirm
227 //|| !empty($GLOBALS['show_as_php'])
228 || isset($GLOBALS['show_as_php'])
230 || !empty($GLOBALS['validatequery'])) {
231 $do_confirm = false;
232 } else {
233 $do_confirm = isset($analyzed_sql[0]['queryflags']['need_confirm']);
236 if ($do_confirm) {
237 $stripped_sql_query = $sql_query;
238 require_once './libraries/header.inc.php';
239 if ($is_drop_database) {
240 echo '<h1 class="warning">' . $strDropDatabaseStrongWarning . '</h1>';
242 echo '<form action="sql.php" method="post">' . "\n"
243 .PMA_generate_common_hidden_inputs($db, (isset($table)?$table:''));
245 <input type="hidden" name="sql_query" value="<?php echo urlencode($sql_query); ?>" />
246 <input type="hidden" name="zero_rows" value="<?php echo isset($zero_rows) ? PMA_sanitize($zero_rows) : ''; ?>" />
247 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
248 <input type="hidden" name="back" value="<?php echo isset($back) ? PMA_sanitize($back) : ''; ?>" />
249 <input type="hidden" name="reload" value="<?php echo isset($reload) ? PMA_sanitize($reload) : 0; ?>" />
250 <input type="hidden" name="purge" value="<?php echo isset($purge) ? PMA_sanitize($purge) : ''; ?>" />
251 <input type="hidden" name="cpurge" value="<?php echo isset($cpurge) ? PMA_sanitize($cpurge) : ''; ?>" />
252 <input type="hidden" name="purgekey" value="<?php echo isset($purgekey) ? PMA_sanitize($purgekey) : ''; ?>" />
253 <input type="hidden" name="show_query" value="<?php echo isset($show_query) ? PMA_sanitize($show_query) : ''; ?>" />
254 <?php
255 echo '<fieldset class="confirmation">' . "\n"
256 .' <legend>' . $strDoYouReally . '</legend>'
257 .' <tt>' . htmlspecialchars($stripped_sql_query) . '</tt>' . "\n"
258 .'</fieldset>' . "\n"
259 .'<fieldset class="tblFooters">' . "\n";
261 <input type="submit" name="btnDrop" value="<?php echo $strYes; ?>" id="buttonYes" />
262 <input type="submit" name="btnDrop" value="<?php echo $strNo; ?>" id="buttonNo" />
263 <?php
264 echo '</fieldset>' . "\n"
265 . '</form>' . "\n";
266 } // end if $do_confirm
270 * Executes the query and displays results
272 else {
273 if (!isset($sql_query)) {
274 $sql_query = '';
276 // Defines some variables
277 // A table has to be created or renamed -> left frame should be reloaded
278 // TODO: use the parser/analyzer
280 if (empty($reload)
281 && preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $sql_query)) {
282 $reload = 1;
284 // Gets the number of rows per page
285 if (empty($session_max_rows)) {
286 $session_max_rows = $cfg['MaxRows'];
287 } elseif ($session_max_rows != 'all') {
288 $cfg['MaxRows'] = $session_max_rows;
290 // Defines the display mode (horizontal/vertical) and header "frequency"
291 if (empty($disp_direction)) {
292 $disp_direction = $cfg['DefaultDisplay'];
294 if (empty($repeat_cells)) {
295 $repeat_cells = $cfg['RepeatCells'];
298 // SK -- Patch: $is_group added for use in calculation of total number of
299 // rows.
300 // $is_count is changed for more correct "LIMIT" clause
301 // appending in queries like
302 // "SELECT COUNT(...) FROM ... GROUP BY ..."
304 // TODO: detect all this with the parser, to avoid problems finding
305 // those strings in comments or backquoted identifiers
307 $is_explain = $is_count = $is_export = $is_delete = $is_insert = $is_affected = $is_show = $is_maint = $is_analyse = $is_group = $is_func = false;
308 if ($is_select) { // see line 141
309 $is_group = preg_match('@(GROUP[[:space:]]+BY|HAVING|SELECT[[:space:]]+DISTINCT)[[:space:]]+@i', $sql_query);
310 $is_func = !$is_group && (preg_match('@[[:space:]]+(SUM|AVG|STD|STDDEV|MIN|MAX|BIT_OR|BIT_AND)\s*\(@i', $sql_query));
311 $is_count = !$is_group && (preg_match('@^SELECT[[:space:]]+COUNT\((.*\.+)?.*\)@i', $sql_query));
312 $is_export = (preg_match('@[[:space:]]+INTO[[:space:]]+OUTFILE[[:space:]]+@i', $sql_query));
313 $is_analyse = (preg_match('@[[:space:]]+PROCEDURE[[:space:]]+ANALYSE@i', $sql_query));
314 } elseif (preg_match('@^EXPLAIN[[:space:]]+@i', $sql_query)) {
315 $is_explain = true;
316 } elseif (preg_match('@^DELETE[[:space:]]+@i', $sql_query)) {
317 $is_delete = true;
318 $is_affected = true;
319 } elseif (preg_match('@^(INSERT|LOAD[[:space:]]+DATA|REPLACE)[[:space:]]+@i', $sql_query)) {
320 $is_insert = true;
321 $is_affected = true;
322 } elseif (preg_match('@^UPDATE[[:space:]]+@i', $sql_query)) {
323 $is_affected = true;
324 } elseif (preg_match('@^SHOW[[:space:]]+@i', $sql_query)) {
325 $is_show = true;
326 } elseif (preg_match('@^(CHECK|ANALYZE|REPAIR|OPTIMIZE)[[:space:]]+TABLE[[:space:]]+@i', $sql_query)) {
327 $is_maint = true;
330 // Do append a "LIMIT" clause?
331 if (isset($pos)
332 && (!$cfg['ShowAll'] || $session_max_rows != 'all')
333 && !($is_count || $is_export || $is_func || $is_analyse)
334 && isset($analyzed_sql[0]['queryflags']['select_from'])
335 && !isset($analyzed_sql[0]['queryflags']['offset'])
336 && !preg_match('@[[:space:]]LIMIT[[:space:]0-9,-]+(;)?$@i', $sql_query)) {
337 $sql_limit_to_append = " LIMIT $pos, ".$cfg['MaxRows'] . " ";
339 $full_sql_query = $analyzed_sql[0]['section_before_limit'] . "\n" . $sql_limit_to_append . $analyzed_sql[0]['section_after_limit'];
340 // FIXME: pretty printing of this modified query
342 if (isset($display_query)) {
343 // if the analysis of the original query revealed that we found
344 // a section_after_limit, we now have to analyze $display_query
345 // to display it correctly
347 if (!empty($analyzed_sql[0]['section_after_limit']) && trim($analyzed_sql[0]['section_after_limit']) != ';') {
348 $analyzed_display_query = PMA_SQP_analyze(PMA_SQP_parse($display_query));
349 $display_query = $analyzed_display_query[0]['section_before_limit'] . "\n" . $sql_limit_to_append . $analyzed_display_query[0]['section_after_limit'];
353 } else {
354 $full_sql_query = $sql_query;
355 } // end if...else
357 if (isset($db)) {
358 PMA_DBI_select_db($db);
361 // If the query is a DELETE query with no WHERE clause, get the number of
362 // rows that will be deleted (mysql_affected_rows will always return 0 in
363 // this case)
364 // Note: testing shows that this no longer applies since MySQL 4.0.x
366 if (PMA_MYSQL_INT_VERSION < 40000) {
367 if ($is_delete
368 && preg_match('@^DELETE([[:space:]].+)?(FROM[[:space:]](.+))$@i', $sql_query, $parts)
369 && !preg_match('@[[:space:]]WHERE[[:space:]]@i', $parts[3])) {
370 $cnt_all_result = @PMA_DBI_try_query('SELECT COUNT(*) as count ' . $parts[2]);
371 if ($cnt_all_result) {
372 list($num_rows) = PMA_DBI_fetch_row($cnt_all_result);
373 PMA_DBI_free_result($cnt_all_result);
374 } else {
375 $num_rows = 0;
380 // E x e c u t e t h e q u e r y
382 // Only if we didn't ask to see the php code (mikebeck)
383 if (isset($GLOBALS['show_as_php']) || !empty($GLOBALS['validatequery'])) {
384 unset($result);
385 $num_rows = 0;
386 } else {
387 // garvin: Measure query time.
388 // TODO-Item http://sourceforge.net/tracker/index.php?func=detail&aid=571934&group_id=23067&atid=377411
389 $querytime_before = array_sum(explode(' ', microtime()));
391 $result = @PMA_DBI_try_query($full_sql_query, null, PMA_DBI_QUERY_STORE);
393 $querytime_after = array_sum(explode(' ', microtime()));
395 $GLOBALS['querytime'] = $querytime_after - $querytime_before;
397 // Displays an error message if required and stop parsing the script
398 if ($error = PMA_DBI_getError()) {
399 require_once './libraries/header.inc.php';
400 $full_err_url = (preg_match('@^(db_details|tbl_properties)@', $err_url))
401 ? $err_url . '&amp;show_query=1&amp;sql_query=' . urlencode($sql_query)
402 : $err_url;
403 PMA_mysqlDie($error, $full_sql_query, '', $full_err_url);
405 unset($error);
407 // Gets the number of rows affected/returned
408 // (This must be done immediately after the query because
409 // mysql_affected_rows() reports about the last query done)
411 if (!$is_affected) {
412 $num_rows = ($result) ? @PMA_DBI_num_rows($result) : 0;
413 } elseif (!isset($num_rows)) {
414 $num_rows = @PMA_DBI_affected_rows();
417 // Checks if the current database has changed
418 // This could happen if the user sends a query like "USE `database`;"
419 $res = PMA_DBI_query('SELECT DATABASE() AS \'db\';');
420 $row = PMA_DBI_fetch_row($res);
421 if (is_array($row) && isset($row[0]) && (strcasecmp($db, $row[0]) != 0)) {
422 $db = $row[0];
423 $reload = 1;
425 @PMA_DBI_free_result($res);
426 unset($res, $row);
428 // tmpfile remove after convert encoding appended by Y.Kawada
429 if (function_exists('PMA_kanji_file_conv')
430 && (isset($textfile) && file_exists($textfile))) {
431 unlink($textfile);
434 // Counts the total number of rows for the same 'SELECT' query without the
435 // 'LIMIT' clause that may have been programatically added
437 if (empty($sql_limit_to_append)) {
438 $unlim_num_rows = $num_rows;
439 // if we did not append a limit, set this to get a correct
440 // "Showing rows..." message
441 $GLOBALS['session_max_rows'] = 'all';
442 } elseif ($is_select) {
444 // c o u n t q u e r y
446 // If we are "just browsing", there is only one table,
447 // and no where clause (or just 'WHERE 1 '),
448 // so we do a quick count (which uses MaxExactCount)
449 // because SQL_CALC_FOUND_ROWS
450 // is not quick on large InnoDB tables
452 // but do not count again if we did it previously
453 // due to $find_real_end == true
455 if (!$is_group
456 && !isset($analyzed_sql[0]['queryflags']['union'])
457 && !isset($analyzed_sql[0]['table_ref'][1]['table_name'])
458 && (empty($analyzed_sql[0]['where_clause'])
459 || $analyzed_sql[0]['where_clause'] == '1 ')
460 && !isset($find_real_end)
463 // "j u s t b r o w s i n g"
464 $unlim_num_rows = PMA_Table::countRecords($db, $table, true);
466 } else { // n o t " j u s t b r o w s i n g "
468 if (PMA_MYSQL_INT_VERSION < 40000) {
470 // detect this case:
471 // SELECT DISTINCT x AS foo, y AS bar FROM sometable
473 if (isset($analyzed_sql[0]['queryflags']['distinct'])) {
474 $count_what = 'DISTINCT ';
475 $first_expr = true;
476 foreach ($analyzed_sql[0]['select_expr'] as $part) {
477 $count_what .= (!$first_expr ? ', ' : '') . $part['expr'];
478 $first_expr = false;
480 } else {
481 $count_what = '*';
483 // this one does not apply to VIEWs
484 $count_query = 'SELECT COUNT(' . $count_what . ') AS count';
487 // add the remaining of select expression if there is
488 // a GROUP BY or HAVING clause
489 if (PMA_MYSQL_INT_VERSION < 40000
490 && $count_what =='*'
491 && (!empty($analyzed_sql[0]['group_by_clause'])
492 || !empty($analyzed_sql[0]['having_clause']))) {
493 $count_query .= ' ,' . $analyzed_sql[0]['select_expr_clause'];
496 if (PMA_MYSQL_INT_VERSION >= 40000) {
497 // add select expression after the SQL_CALC_FOUND_ROWS
499 // for UNION, just adding SQL_CALC_FOUND_ROWS
500 // after the first SELECT works.
502 // take the left part, could be:
503 // SELECT
504 // (SELECT
505 $count_query = PMA_SQP_formatHtml($parsed_sql, 'query_only', 0, $analyzed_sql[0]['position_of_first_select'] + 1);
506 $count_query .= ' SQL_CALC_FOUND_ROWS ';
507 // add everything that was after the first SELECT
508 $count_query .= PMA_SQP_formatHtml($parsed_sql, 'query_only', $analyzed_sql[0]['position_of_first_select']+1);
509 } else { // PMA_MYSQL_INT_VERSION < 40000
511 if (!empty($analyzed_sql[0]['from_clause'])) {
512 $count_query .= ' FROM ' . $analyzed_sql[0]['from_clause'];
514 if (!empty($analyzed_sql[0]['where_clause'])) {
515 $count_query .= ' WHERE ' . $analyzed_sql[0]['where_clause'];
517 if (!empty($analyzed_sql[0]['group_by_clause'])) {
518 $count_query .= ' GROUP BY ' . $analyzed_sql[0]['group_by_clause'];
520 if (!empty($analyzed_sql[0]['having_clause'])) {
521 $count_query .= ' HAVING ' . $analyzed_sql[0]['having_clause'];
523 } // end if
525 // if using SQL_CALC_FOUND_ROWS, add a LIMIT to avoid
526 // long delays. Returned count will be complete anyway.
527 // (but a LIMIT would disrupt results in an UNION)
529 if (PMA_MYSQL_INT_VERSION >= 40000
530 && !isset($analyzed_sql[0]['queryflags']['union'])) {
531 $count_query .= ' LIMIT 1';
534 // run the count query
536 if (PMA_MYSQL_INT_VERSION < 40000) {
537 if ($cnt_all_result = PMA_DBI_try_query($count_query)) {
538 if ($is_group && $count_what == '*') {
539 $unlim_num_rows = @PMA_DBI_num_rows($cnt_all_result);
540 } else {
541 $unlim_num_rows = PMA_DBI_fetch_assoc($cnt_all_result);
542 $unlim_num_rows = $unlim_num_rows['count'];
544 PMA_DBI_free_result($cnt_all_result);
545 } else {
546 if (PMA_DBI_getError()) {
548 // there are some cases where the generated
549 // count_query (for MySQL 3) is wrong,
550 // so we get here.
551 //TODO: use a big unlimited query to get
552 // the correct number of rows (depending
553 // on a config variable?)
554 $unlim_num_rows = 0;
557 } else {
558 PMA_DBI_try_query($count_query);
559 // if (mysql_error()) {
560 // void.
561 // I tried the case
562 // (SELECT `User`, `Host`, `Db`, `Select_priv` FROM `db`)
563 // UNION (SELECT `User`, `Host`, "%" AS "Db",
564 // `Select_priv`
565 // FROM `user`) ORDER BY `User`, `Host`, `Db`;
566 // and although the generated count_query is wrong
567 // the SELECT FOUND_ROWS() work! (maybe it gets the
568 // count from the latest query that worked)
570 // another case where the count_query is wrong:
571 // SELECT COUNT(*), f1 from t1 group by f1
572 // and you click to sort on count(*)
573 // }
574 $cnt_all_result = PMA_DBI_query('SELECT FOUND_ROWS() as count;');
575 list($unlim_num_rows) = PMA_DBI_fetch_row($cnt_all_result);
576 @PMA_DBI_free_result($cnt_all_result);
578 } // end else "just browsing"
580 } else { // not $is_select
581 $unlim_num_rows = 0;
582 } // end rows total count
584 // garvin: if a table or database gets dropped, check column comments.
585 if (isset($purge) && $purge == '1') {
586 require_once './libraries/relation_cleanup.lib.php';
588 if (isset($table) && isset($db) && strlen($table) && strlen($db)) {
589 PMA_relationsCleanupTable($db, $table);
590 } elseif (isset($db) && strlen($db)) {
591 PMA_relationsCleanupDatabase($db);
592 } else {
593 // garvin: VOID. No DB/Table gets deleted.
594 } // end if relation-stuff
595 } // end if ($purge)
597 // garvin: If a column gets dropped, do relation magic.
598 if (isset($cpurge) && $cpurge == '1' && isset($purgekey)
599 && isset($db) && isset($table)
600 && strlen($db) && strlen($table) && !empty($purgekey)) {
601 require_once './libraries/relation_cleanup.lib.php';
602 PMA_relationsCleanupColumn($db, $table, $purgekey);
604 } // end if column PMA_* purge
605 } // end else "didn't ask to see php code"
607 // No rows returned -> move back to the calling page
608 if ($num_rows < 1 || $is_affected) {
609 if ($is_delete) {
610 $message = $strDeletedRows . '&nbsp;' . $num_rows;
611 } elseif ($is_insert) {
612 $message = $strInsertedRows . '&nbsp;' . $num_rows;
613 $insert_id = PMA_DBI_insert_id();
614 if ($insert_id != 0) {
615 // insert_id is id of FIRST record inserted in one insert, so if we inserted multiple rows, we had to increment this
616 $message .= '[br]'.$strInsertedRowId . '&nbsp;' . ($insert_id + $num_rows - 1);
618 } elseif ($is_affected) {
619 $message = $strAffectedRows . '&nbsp;' . $num_rows;
621 // Ok, here is an explanation for the !$is_select.
622 // The form generated by sql_query_form.lib.php
623 // and db_details.php has many submit buttons
624 // on the same form, and some confusion arises from the
625 // fact that $zero_rows is sent for every case.
626 // The $zero_rows containing $strSuccess and sent with
627 // the form should not have priority over
628 // errors like $strEmptyResultSet
629 } elseif (!empty($zero_rows) && !$is_select) {
630 $message = $zero_rows;
631 } elseif (!empty($GLOBALS['show_as_php'])) {
632 $message = $strPhp;
633 } elseif (!empty($GLOBALS['validatequery'])) {
634 $message = $strValidateSQL;
635 } else {
636 $message = $strEmptyResultSet;
639 $message .= ' ' . (isset($GLOBALS['querytime']) ? '(' . sprintf($strQueryTime, $GLOBALS['querytime']) . ')' : '');
641 if ($is_gotofile) {
642 $goto = PMA_securePath($goto);
643 // Checks for a valid target script
644 if (isset($table) && $table == '') {
645 unset($table);
647 if (isset($db) && $db == '') {
648 unset($db);
650 $is_db = $is_table = false;
651 if (strpos(' ' . $goto, 'tbl_properties') == 1) {
652 if (!isset($table)) {
653 $goto = 'db_details.php';
654 } else {
655 $is_table = @PMA_DBI_query('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, true) . '\';', null, PMA_DBI_QUERY_STORE);
656 if (!($is_table && @PMA_DBI_num_rows($is_table))) {
657 $goto = 'db_details.php';
658 unset($table);
660 @PMA_DBI_free_result($is_table);
661 } // end if... else...
663 if (strpos(' ' . $goto, 'db_details') == 1) {
664 if (isset($table)) {
665 unset($table);
667 if (!isset($db)) {
668 $goto = 'main.php';
669 } else {
670 $is_db = @PMA_DBI_select_db($db);
671 if (!$is_db) {
672 $goto = 'main.php';
673 unset($db);
675 } // end if... else...
677 // Loads to target script
678 if (strpos(' ' . $goto, 'db_details') == 1
679 || strpos(' ' . $goto, 'tbl_properties') == 1) {
680 $js_to_run = 'functions.js';
682 if ($goto != 'main.php') {
683 require_once './libraries/header.inc.php';
685 $active_page = $goto;
686 require './' . $goto;
687 } else {
688 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . str_replace('&amp;', '&', $goto) . '&message=' . urlencode($message));
689 } // end else
690 exit();
691 } // end no rows returned
693 // At least one row is returned -> displays a table with results
694 else {
695 // Displays the headers
696 if (isset($show_query)) {
697 unset($show_query);
699 if (isset($printview) && $printview == '1') {
700 require_once './libraries/header_printview.inc.php';
701 } else {
702 $js_to_run = 'functions.js';
703 unset($message);
704 if (isset($table) && strlen($table)) {
705 require './libraries/tbl_properties_common.php';
706 $url_query .= '&amp;goto=tbl_properties.php&amp;back=tbl_properties.php';
707 require './libraries/tbl_properties_table_info.inc.php';
708 require './libraries/tbl_properties_links.inc.php';
709 } elseif (isset($db) && strlen($db)) {
710 require './libraries/db_details_common.inc.php';
711 require './libraries/db_details_db_info.inc.php';
712 } else {
713 require './libraries/server_common.inc.php';
714 require './libraries/server_links.inc.php';
718 if (isset($db) && strlen($db)) {
719 require_once './libraries/relation.lib.php';
720 $cfgRelation = PMA_getRelationsParam();
723 // Gets the list of fields properties
724 if (isset($result) && $result) {
725 $fields_meta = PMA_DBI_get_fields_meta($result);
726 $fields_cnt = count($fields_meta);
729 // Display previous update query (from tbl_replace)
730 if (isset($disp_query) && $cfg['ShowSQL'] == true) {
731 $tmp_sql_query = $GLOBALS['sql_query'];
732 $GLOBALS['sql_query'] = $disp_query;
733 PMA_showMessage($disp_message);
734 $GLOBALS['sql_query'] = $tmp_sql_query;
737 // Displays the results in a table
738 require_once './libraries/display_tbl.lib.php';
739 if (empty($disp_mode)) {
740 // see the "PMA_setDisplayMode()" function in
741 // libraries/display_tbl.lib.php
742 $disp_mode = 'urdr111101';
744 if (!isset($dontlimitchars)) {
745 $dontlimitchars = 0;
748 // hide edit and delete links for information_schema
749 if (PMA_MYSQL_INT_VERSION >= 50002 && isset($db) && $db == 'information_schema') {
750 $disp_mode = 'nnnn110111';
753 PMA_displayTable($result, $disp_mode, $analyzed_sql);
754 PMA_DBI_free_result($result);
756 // BEGIN INDEX CHECK See if indexes should be checked.
757 if (isset($query_type) && $query_type == 'check_tbl' && isset($selected) && is_array($selected)) {
758 foreach ($selected AS $idx => $tbl_name) {
759 $indexes = $indexes_info = $indexes_data = array();
760 $tbl_ret_keys = PMA_get_indexes(urldecode($tbl_name), $err_url_0);
762 PMA_extract_indexes($tbl_ret_keys, $indexes, $indexes_info, $indexes_data);
764 $idx_collection = PMA_show_indexes(urldecode($tbl_name), $indexes, $indexes_info, $indexes_data, false);
765 $check = PMA_check_indexes($idx_collection);
766 if (!empty($check)) {
768 <table border="0" cellpadding="2" cellspacing="0">
769 <tr>
770 <td class="tblHeaders" colspan="7"><?php printf($strIndexWarningTable, urldecode($tbl_name)); ?></td>
771 </tr>
772 <?php echo $check; ?>
773 </table>
774 <?php
777 } // End INDEX CHECK
779 // Bookmark Support if required
780 if ($disp_mode[7] == '1'
781 && (isset($cfg['Bookmark']) && $cfg['Bookmark']['db'] && $cfg['Bookmark']['table'] && empty($id_bookmark))
782 && !empty($sql_query)) {
783 echo "\n";
785 $goto = 'sql.php?'
786 . PMA_generate_common_url($db, $table)
787 . '&amp;pos=' . $pos
788 . '&amp;session_max_rows=' . $session_max_rows
789 . '&amp;disp_direction=' . $disp_direction
790 . '&amp;repeat_cells=' . $repeat_cells
791 . '&amp;dontlimitchars=' . $dontlimitchars
792 . '&amp;sql_query=' . urlencode($sql_query)
793 . '&amp;id_bookmark=1';
796 <form action="sql.php" method="post" onsubmit="return emptyFormElements(this, 'fields[label]');">
797 <?php echo PMA_generate_common_hidden_inputs(); ?>
798 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
799 <input type="hidden" name="fields[dbase]" value="<?php echo htmlspecialchars($db); ?>" />
800 <input type="hidden" name="fields[user]" value="<?php echo $cfg['Bookmark']['user']; ?>" />
801 <input type="hidden" name="fields[query]" value="<?php echo urlencode(isset($complete_query) ? $complete_query : $sql_query); ?>" />
802 <fieldset>
803 <legend><?php
804 echo ($cfg['PropertiesIconic'] ? '<img class="icon" src="' . $pmaThemeImage . 'b_bookmark.png" width="16" height="16" alt="' . $strBookmarkThis . '" />' : '')
805 . $strBookmarkThis;
807 </legend>
809 <div class="formelement">
810 <label for="fields_label_"><?php echo $strBookmarkLabel; ?>:</label>
811 <input type="text" id="fields_label_" name="fields[label]" value="" />
812 </div>
814 <div class="formelement">
815 <input type="checkbox" name="bkm_all_users" id="bkm_all_users" value="true" />
816 <label for="bkm_all_users"><?php echo $strBookmarkAllUsers; ?></label>
817 </div>
819 <div class="clearfloat"></div>
820 </fieldset>
821 <fieldset class="tblFooters">
822 <input type="submit" name="store_bkm" value="<?php echo $strBookmarkThis; ?>" />
823 </fieldset>
824 </form>
825 <?php
826 } // end bookmark support
828 // Do print the page if required
829 if (isset($printview) && $printview == '1') {
831 <script type="text/javascript" language="javascript">
832 //<![CDATA[
833 // Do print the page
834 window.onload = function()
836 if (typeof(window.print) != 'undefined') {
837 window.print();
840 //]]>
841 </script>
842 <?php
843 } // end print case
844 } // end rows returned
846 } // end executes the query
849 * Displays the footer
851 require_once './libraries/footer.inc.php';