more typos
[phpmyadmin/crack.git] / sql.php3
blobf9444e4066aa77f18826f30b27f8581aa49bbbba
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Gets some core libraries
7 */
8 require('./libraries/grab_globals.lib.php3');
9 require('./libraries/common.lib.php3');
11 /**
12 * Defines the url to return to in case of error in a sql statement
14 // Security checkings
15 if (!empty($goto)) {
16 $is_gotofile = ereg_replace('^([^?]+).*$', '\\1', $goto);
17 if (!@file_exists('./' . $is_gotofile)) {
18 unset($goto);
19 } else {
20 $is_gotofile = ($is_gotofile == $goto);
22 } // end if (security checkings)
24 if (empty($goto)) {
25 $goto = (empty($table)) ? $cfg['DefaultTabDatabase'] : $cfg['DefaultTabTable'];
26 $is_gotofile = TRUE;
27 } // end if
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)) ? '&amp;table=' . urlencode($table) : '');
32 } // end if
34 // Coming from a bookmark dialog
35 if (isset($fields['query'])) {
36 $sql_query = $fields['query'];
39 /**
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);
56 } // end if
57 } // end if
60 /**
61 * Bookmark add
63 if (isset($store_bkm)) {
64 include('./libraries/bookmark.lib.php3');
65 PMA_addBookmarks($fields, $cfg['Bookmark']);
66 header('Location: ' . $cfg['PmaAbsoluteUri'] . $goto);
67 } // end if
70 /**
71 * Gets the true sql query
73 // $sql_query has been urlencoded in the confirmation form for drop/delete
74 // queries or in the navigation bar for browsing among records
75 if (isset($btnDrop) || isset($navig)) {
76 $sql_query = urldecode($sql_query);
79 /**
80 * Reformat the query
83 $parsed_sql = PMA_SQP_parse($sql_query);
84 $analyzed_sql = PMA_SQP_analyze($parsed_sql);
85 // Bug #641765 - Robbat2 - 12 January 2003, 10:49PM
86 // Reverted - Robbat2 - 13 January 2003, 2:40PM
87 $sql_query = PMA_SQP_formatHtml($parsed_sql, 'query_only');
89 // old code did not work, for example, when there is a bracket
90 // before the SELECT
91 // so I guess it's ok to check for a real SELECT ... FROM
92 //$is_select = eregi('^SELECT[[:space:]]+', $sql_query);
93 $is_select = isset($analyzed_sql[0]['queryflags']['select_from']);
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)
105 if ($is_select) {
106 $prev_db = $db;
107 if (isset($analyzed_sql[0]['table_ref'][0]['table_true_name'])) {
108 $table = $analyzed_sql[0]['table_ref'][0]['table_true_name'];
110 if (isset($analyzed_sql[0]['table_ref'][0]['db'])
111 && !empty($analyzed_sql[0]['table_ref'][0]['db'])) {
112 $db = $analyzed_sql[0]['table_ref'][0]['db'];
114 else {
115 $db = $prev_db;
117 $reload = ($db == $prev_db) ? 0 : 1;
121 * Sets or modifies the $goto variable if required
123 if ($goto == 'sql.php3') {
124 $goto = 'sql.php3?'
125 . PMA_generate_common_url($db, $table)
126 . '&amp;pos=' . $pos
127 . '&amp;sql_query=' . urlencode($sql_query);
128 } // end if
132 * Go back to further page if table should not be dropped
134 if (isset($btnDrop) && $btnDrop == $strNo) {
135 if (!empty($back)) {
136 $goto = $back;
138 if ($is_gotofile) {
139 if (strpos(' ' . $goto, 'db_details') == 1 && !empty($table)) {
140 unset($table);
142 include('./' . ereg_replace('\.\.*', '.', $goto));
143 } else {
144 header('Location: ' . $cfg['PmaAbsoluteUri'] . str_replace('&amp;', '&', $goto));
146 exit();
147 } // end if
151 * Displays the confirm page if required
153 * This part of the script is bypassed if $is_js_confirmed = 1 (already checked
154 * with js) because possible security issue is not so important here: at most,
155 * the confirm message isn't displayed.
157 * Also bypassed if only showing php code.or validating a SQL query
159 if (!$cfg['Confirm']
160 || (isset($is_js_confirmed) && $is_js_confirmed)
161 || isset($btnDrop)
162 || !empty($GLOBALS['show_as_php'])
163 || !empty($GLOBALS['validatequery'])) {
164 $do_confirm = FALSE;
165 } else {
166 //$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));
168 $do_confirm = isset($analyzed_sql[0]['queryflags']['need_confirm']);
171 if ($do_confirm) {
172 $stripped_sql_query = $sql_query;
173 include('./header.inc.php3');
174 echo $strDoYouReally . '&nbsp;:<br />' . "\n";
175 echo '<tt>' . htmlspecialchars($stripped_sql_query) . '</tt>&nbsp;?<br/>' . "\n";
177 <form action="sql.php3" method="post">
178 <?php echo PMA_generate_common_hidden_inputs($db, (isset($table)?$table:'')); ?>
179 <input type="hidden" name="sql_query" value="<?php echo urlencode($sql_query); ?>" />
180 <input type="hidden" name="zero_rows" value="<?php echo isset($zero_rows) ? $zero_rows : ''; ?>" />
181 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
182 <input type="hidden" name="back" value="<?php echo isset($back) ? $back : ''; ?>" />
183 <input type="hidden" name="reload" value="<?php echo isset($reload) ? $reload : 0; ?>" />
184 <input type="hidden" name="purge" value="<?php echo isset($purge) ? $purge : ''; ?>" />
185 <input type="hidden" name="cpurge" value="<?php echo isset($cpurge) ? $cpurge : ''; ?>" />
186 <input type="hidden" name="purgekey" value="<?php echo isset($purgekey) ? $purgekey : ''; ?>" />
187 <input type="hidden" name="show_query" value="<?php echo isset($show_query) ? $show_query : ''; ?>" />
188 <input type="submit" name="btnDrop" value="<?php echo $strYes; ?>" />
189 <input type="submit" name="btnDrop" value="<?php echo $strNo; ?>" />
190 </form>
191 <?php
192 echo "\n";
193 } // end if
197 * Executes the query and displays results
199 else {
200 if (!isset($sql_query)) {
201 $sql_query = '';
203 // Defines some variables
204 // loic1: A table has to be created -> left frame should be reloaded
205 if ((!isset($reload) || $reload == 0)
206 && eregi('^CREATE TABLE[[:space:]]+(.*)', $sql_query)) {
207 $reload = 1;
209 // Gets the number of rows per page
210 if (!isset($session_max_rows)) {
211 $session_max_rows = $cfg['MaxRows'];
212 } else if ($session_max_rows != 'all') {
213 $cfg['MaxRows'] = $session_max_rows;
215 // Defines the display mode (horizontal/vertical) and header "frequency"
216 if (empty($disp_direction)) {
217 $disp_direction = $cfg['DefaultDisplay'];
219 if (empty($repeat_cells)) {
220 $repeat_cells = $cfg['RepeatCells'];
223 // SK -- Patch: $is_group added for use in calculation of total number of
224 // rows.
225 // $is_count is changed for more correct "LIMIT" clause
226 // appending in queries like
227 // "SELECT COUNT(...) FROM ... GROUP BY ..."
228 $is_explain = $is_count = $is_export = $is_delete = $is_insert = $is_affected = $is_show = $is_maint = $is_analyse = $is_group = $is_func = FALSE;
229 if ($is_select) { // see line 141
230 $is_group = eregi('[[:space:]]+(GROUP[[:space:]]+BY|HAVING|SELECT[[:space:]]+DISTINCT)[[:space:]]+', $sql_query);
232 $is_func = !$is_group && (eregi('[[:space:]]+(SUM|AVG|STD|STDDEV|MIN|MAX|BIT_OR|BIT_AND)\s*\(', $sql_query));
233 $is_count = !$is_group && (eregi('^SELECT[[:space:]]+COUNT\((.*\.+)?.*\)', $sql_query));
234 $is_export = (eregi('[[:space:]]+INTO[[:space:]]+OUTFILE[[:space:]]+', $sql_query));
235 $is_analyse = (eregi('[[:space:]]+PROCEDURE[[:space:]]+ANALYSE\(', $sql_query));
236 } else if (eregi('^EXPLAIN[[:space:]]+', $sql_query)) {
237 $is_explain = TRUE;
238 } else if (eregi('^DELETE[[:space:]]+', $sql_query)) {
239 $is_delete = TRUE;
240 $is_affected = TRUE;
241 } else if (eregi('^(INSERT|LOAD[[:space:]]+DATA|REPLACE)[[:space:]]+', $sql_query)) {
242 $is_insert = TRUE;
243 $is_affected = TRUE;
244 } else if (eregi('^UPDATE[[:space:]]+', $sql_query)) {
245 $is_affected = TRUE;
246 } else if (eregi('^SHOW[[:space:]]+', $sql_query)) {
247 $is_show = TRUE;
248 } else if (eregi('^(CHECK|ANALYZE|REPAIR|OPTIMIZE)[[:space:]]+TABLE[[:space:]]+', $sql_query)) {
249 $is_maint = TRUE;
252 // Do append a "LIMIT" clause?
253 if (isset($pos)
254 && (!$cfg['ShowAll'] || $session_max_rows != 'all')
255 && !($is_count || $is_export || $is_func || $is_analyse)
256 && isset($analyzed_sql[0]['queryflags']['select_from'])
257 && !eregi('[[:space:]]LIMIT[[:space:]0-9,-]+$', $sql_query)) {
258 $sql_limit_to_append = " LIMIT $pos, ".$cfg['MaxRows'];
259 if (eregi('(.*)([[:space:]](PROCEDURE[[:space:]](.*)|FOR[[:space:]]+UPDATE|LOCK[[:space:]]+IN[[:space:]]+SHARE[[:space:]]+MODE))$', $sql_query, $regs)) {
260 $full_sql_query = $regs[1] . $sql_limit_to_append . $regs[2];
261 } else {
262 $full_sql_query = $sql_query . $sql_limit_to_append;
264 } else {
265 $full_sql_query = $sql_query;
266 } // end if...else
268 PMA_mysql_select_db($db);
270 // If the query is a DELETE query with no WHERE clause, get the number of
271 // rows that will be deleted (mysql_affected_rows will always return 0 in
272 // this case)
273 if ($is_delete
274 && eregi('^DELETE([[:space:]].+)?([[:space:]]FROM[[:space:]](.+))$', $sql_query, $parts)
275 && !eregi('[[:space:]]WHERE[[:space:]]', $parts[3])) {
276 $cnt_all_result = @PMA_mysql_query('SELECT COUNT(*) as count' . $parts[2]);
277 if ($cnt_all_result) {
278 $num_rows = PMA_mysql_result($cnt_all_result, 0, 'count');
279 mysql_free_result($cnt_all_result);
280 } else {
281 $num_rows = 0;
285 // E x e c u t e t h e q u e r y
287 // Only if we didn't ask to see the php code (mikebeck)
288 if (isset($GLOBALS['show_as_php']) || !empty($GLOBALS['validatequery'])) {
289 unset($result);
290 $num_rows = 0;
292 else {
293 // garvin: Measure query time. TODO-Item http://sourceforge.net/tracker/index.php?func=detail&aid=571934&group_id=23067&atid=377411
294 list($usec, $sec) = explode(' ',microtime());
295 $querytime_before = ((float)$usec + (float)$sec);
297 $result = @PMA_mysql_query($full_sql_query);
299 list($usec, $sec) = explode(' ',microtime());
300 $querytime_after = ((float)$usec + (float)$sec);
302 $GLOBALS['querytime'] = $querytime_after - $querytime_before;
304 // Displays an error message if required and stop parsing the script
305 if (PMA_mysql_error()) {
306 $error = PMA_mysql_error();
307 include('./header.inc.php3');
308 $full_err_url = (ereg('^(db_details|tbl_properties)', $err_url))
309 ? $err_url . '&amp;show_query=1&amp;sql_query=' . urlencode($sql_query)
310 : $err_url;
311 PMA_mysqlDie($error, $full_sql_query, '', $full_err_url);
314 // Gets the number of rows affected/returned
315 // (This must be done immediately after the query because
316 // mysql_affected_rows() reports about the last query done)
318 if (!$is_affected) {
319 $num_rows = ($result) ? @mysql_num_rows($result) : 0;
320 } else if (!isset($num_rows)) {
321 $num_rows = @mysql_affected_rows();
324 // Checks if the current database has changed
325 // This could happen if the user sends a query like "USE `database`;"
326 $res = PMA_mysql_query('SELECT DATABASE() AS "db";');
327 $row = PMA_mysql_fetch_array($res);
328 if ($db != $row['db']) {
329 $db = $row['db'];
330 $reload = 1;
332 @mysql_free_result($res);
333 unset($res);
334 unset($row);
336 // tmpfile remove after convert encoding appended by Y.Kawada
337 if (function_exists('PMA_kanji_file_conv')
338 && (isset($textfile) && file_exists($textfile))) {
339 unlink($textfile);
342 // Counts the total number of rows for the same 'SELECT' query without the
343 // 'LIMIT' clause that may have been programatically added
345 if (empty($sql_limit_to_append)) {
346 $unlim_num_rows = $num_rows;
348 else if ($is_select) {
350 // c o u n t q u e r y
352 // If we are just browsing, there is only one table,
353 // and no where clause (or just 'WHERE 1 '),
354 // so we do a quick count (which uses MaxExactCount)
355 // because SQL_CALC_FOUND_ROWS
356 // is not quick on large InnoDB tables
358 if (!isset($analyzed_sql[0]['table_ref'][1]['table_name'])
359 && (empty($analyzed_sql[0]['where_clause'])
360 || $analyzed_sql[0]['where_clause'] == '1 ')) {
362 // "just browsing"
363 $unlim_num_rows = PMA_countRecords($db, $table, TRUE);
365 } else { // not "just browsing"
367 if (PMA_MYSQL_INT_VERSION < 40000) {
368 if (eregi('DISTINCT(.*)', $sql_query)) {
369 $count_what = 'DISTINCT ' . $analyzed_sql[0]['select_expr_clause'];
370 } else {
371 $count_what = '*';
374 $count_query = 'SELECT COUNT(' . $count_what . ') AS count';
376 else {
377 $count_query = 'SELECT SQL_CALC_FOUND_ROWS ';
380 // add the remaining of select expression if there is
381 // a GROUP BY or HAVING clause
382 if (PMA_MYSQL_INT_VERSION < 40000
383 && $count_what =='*'
384 && (!empty($analyzed_sql[0]['group_by_clause'])
385 || !empty($analyzed_sql[0]['having_clause']))) {
386 $count_query .= ' ,' . $analyzed_sql[0]['select_expr_clause'];
389 // add select expression after the SQL_CALC_FOUND_ROWS
390 if (PMA_MYSQL_INT_VERSION >= 40000) {
391 $count_query .= $analyzed_sql[0]['select_expr_clause'];
395 if (!empty($analyzed_sql[0]['from_clause'])) {
396 $count_query .= ' FROM ' . $analyzed_sql[0]['from_clause'];
399 if (!empty($analyzed_sql[0]['where_clause'])) {
400 $count_query .= ' WHERE ' . $analyzed_sql[0]['where_clause'];
402 if (!empty($analyzed_sql[0]['group_by_clause'])) {
403 $count_query .= ' GROUP BY ' . $analyzed_sql[0]['group_by_clause'];
406 if (!empty($analyzed_sql[0]['having_clause'])) {
407 $count_query .= ' HAVING ' . $analyzed_sql[0]['having_clause'];
410 // if using SQL_CALC_FOUND_ROWS, add a LIMIT to avoid
411 // long delays. Returned count will be complete anyway.
413 if (PMA_MYSQL_INT_VERSION >= 40000) {
414 $count_query .= ' LIMIT 1';
417 // do not put the order_by_clause, it interferes
418 // run the count query
419 if (PMA_MYSQL_INT_VERSION < 40000) {
420 if ($cnt_all_result = mysql_query($count_query)) {
421 if ($is_group) {
422 $unlim_num_rows = @mysql_num_rows($cnt_all_result);
423 } else {
424 $unlim_num_rows = mysql_result($cnt_all_result, 0, 'count');
426 mysql_free_result($cnt_all_result);
427 } else {
428 if (mysql_error()) {
429 // there are some cases where the generated
430 // count_query (for MySQL 3) is wrong,
431 // so we get here.
432 //TODO: use a big unlimited query to get
433 // the correct number of rows (depending
434 // on a config variable?)
435 $unlim_num_rows = 0;
438 } else {
439 mysql_query($count_query);
440 if (mysql_error()) {
441 // void. I tried the case
442 // (SELECT `User`, `Host`, `Db`, `Select_priv` FROM `db`)
443 // UNION (SELECT `User`, `Host`, "%" AS "Db",
444 // `Select_priv`
445 // FROM `user`) ORDER BY `User`, `Host`, `Db`;
446 // and although the generated count_query is wrong
447 // the SELECT FOUND_ROWS() work!
449 $cnt_all_result = mysql_query('SELECT FOUND_ROWS() as count');
450 $unlim_num_rows = mysql_result($cnt_all_result,0,'count');
452 } // end else "just browsing"
454 } else { // not $is_select
455 $unlim_num_rows = 0;
456 } // end rows total count
458 // garvin: if a table or database gets dropped, check column comments.
459 if (isset($purge) && $purge == '1') {
460 include('./libraries/relation.lib.php3');
461 $cfgRelation = PMA_getRelationsParam();
463 if (isset($table) && isset($db) && !empty($table) && !empty($db)) {
464 // garvin: Only a table is deleted. Remove all references in PMA_*
465 if ($cfgRelation['commwork']) {
466 $remove_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['column_info'])
467 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
468 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
469 $rmv_rs = PMA_query_as_cu($remove_query);
470 unset($rmv_query);
473 if ($cfgRelation['displaywork']) {
474 $remove_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['table_info'])
475 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
476 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
477 $rmv_rs = PMA_query_as_cu($remove_query);
478 unset($rmv_query);
481 if ($cfgRelation['pdfwork']) {
482 $remove_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['table_coords'])
483 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
484 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
485 $rmv_rs = PMA_query_as_cu($remove_query);
486 unset($rmv_query);
489 if ($cfgRelation['relwork']) {
490 $remove_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['relation'])
491 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
492 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\'';
493 $rmv_rs = PMA_query_as_cu($remove_query);
494 unset($rmv_query);
496 $remove_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['relation'])
497 . ' WHERE foreign_db = \'' . PMA_sqlAddslashes($db) . '\''
498 . ' AND foreign_table = \'' . PMA_sqlAddslashes($table) . '\'';
499 $rmv_rs = PMA_query_as_cu($remove_query);
500 unset($rmv_query);
502 } elseif (isset($db) && !empty($db)) {
503 // garvin: A whole DB gets deleted. Remove all references in PMA_*
504 if ($cfgRelation['commwork']) {
505 $remove_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['column_info'])
506 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
507 $rmv_rs = PMA_query_as_cu($remove_query);
508 unset($rmv_query);
511 if ($cfgRelation['bookmarkwork']) {
512 $remove_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['bookmark'])
513 . ' WHERE dbase = \'' . PMA_sqlAddslashes($db) . '\'';
514 $rmv_rs = PMA_query_as_cu($remove_query);
515 unset($rmv_query);
518 if ($cfgRelation['displaywork']) {
519 $remove_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['table_info'])
520 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
521 $rmv_rs = PMA_query_as_cu($remove_query);
522 unset($rmv_query);
525 if ($cfgRelation['pdfwork']) {
526 $remove_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['pdf_pages'])
527 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
528 $rmv_rs = PMA_query_as_cu($remove_query);
529 unset($rmv_query);
531 $remove_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['table_coords'])
532 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
533 $rmv_rs = PMA_query_as_cu($remove_query);
534 unset($rmv_query);
537 if ($cfgRelation['relwork']) {
538 $remove_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['relation'])
539 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\'';
540 $rmv_rs = PMA_query_as_cu($remove_query);
541 unset($rmv_query);
543 $remove_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['relation'])
544 . ' WHERE foreign_db = \'' . PMA_sqlAddslashes($db) . '\'';
545 $rmv_rs = PMA_query_as_cu($remove_query);
546 unset($rmv_query);
549 } else {
550 // garvin: VOID. No DB/Table gets deleted.
551 } // end if relation-stuff
552 } // end if ($purge)
554 // garvin: If a column gets dropped, do relation magic.
555 if (isset($cpurge) && $cpurge == '1' && isset($purgekey)
556 && isset($db) && isset($table)
557 && !empty($db) && !empty($table) && !empty($purgekey)) {
558 include('./libraries/relation.lib.php3');
559 $cfgRelation = PMA_getRelationsParam();
561 if ($cfgRelation['commwork']) {
562 $remove_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['column_info'])
563 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
564 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
565 . ' AND column_name = \'' . PMA_sqlAddslashes(urldecode($purgekey)) . '\'';
566 $rmv_rs = PMA_query_as_cu($remove_query);
567 unset($rmv_query);
570 if ($cfgRelation['displaywork']) {
571 $remove_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['table_info'])
572 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
573 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
574 . ' AND display_field = \'' . PMA_sqlAddslashes(urldecode($purgekey)) . '\'';
575 $rmv_rs = PMA_query_as_cu($remove_query);
576 unset($rmv_query);
579 if ($cfgRelation['relwork']) {
580 $remove_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['relation'])
581 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
582 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
583 . ' AND master_field = \'' . PMA_sqlAddslashes(urldecode($purgekey)) . '\'';
584 $rmv_rs = PMA_query_as_cu($remove_query);
585 unset($rmv_query);
587 $remove_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['relation'])
588 . ' WHERE foreign_db = \'' . PMA_sqlAddslashes($db) . '\''
589 . ' AND foreign_table = \'' . PMA_sqlAddslashes($table) . '\''
590 . ' AND foreign_field = \'' . PMA_sqlAddslashes(urldecode($purgekey)) . '\'';
591 $rmv_rs = PMA_query_as_cu($remove_query);
592 unset($rmv_query);
594 } // end if column PMA_* purge
595 } // end else "didn't ask to see php code"
598 // No rows returned -> move back to the calling page
599 if ($num_rows < 1 || $is_affected) {
600 if ($is_delete) {
601 $message = $strDeletedRows . '&nbsp;' . $num_rows;
602 } else if ($is_insert) {
603 $message = $strInsertedRows . '&nbsp;' . $num_rows;
604 $insert_id = mysql_insert_id();
605 if ($insert_id != 0) {
606 $message .= '<br />'.$strInsertedRowId . '&nbsp;' . $insert_id;
608 } else if ($is_affected) {
609 $message = $strAffectedRows . '&nbsp;' . $num_rows;
610 } else if (!empty($zero_rows)) {
611 $message = $zero_rows;
612 } else if (!empty($GLOBALS['show_as_php'])) {
613 $message = $strPhp;
614 } else if (!empty($GLOBALS['validatequery'])) {
615 $message = $strValidateSQL;
616 } else {
617 $message = $strEmptyResultSet;
620 $message .= ' ' . (isset($GLOBALS['querytime']) ? '(' . sprintf($strQueryTime, $GLOBALS['querytime']) . ')' : '');
622 if ($is_gotofile) {
623 $goto = ereg_replace('\.\.*', '.', $goto);
624 // Checks for a valid target script
625 if (isset($table) && $table == '') {
626 unset($table);
628 if (isset($db) && $db == '') {
629 unset($db);
631 $is_db = $is_table = FALSE;
632 if (strpos(' ' . $goto, 'tbl_properties') == 1) {
633 if (!isset($table)) {
634 $goto = 'db_details.php3';
635 } else {
636 $is_table = @PMA_mysql_query('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'');
637 if (!($is_table && @mysql_numrows($is_table))) {
638 $goto = 'db_details.php3';
639 unset($table);
641 } // end if... else...
643 if (strpos(' ' . $goto, 'db_details') == 1) {
644 if (isset($table)) {
645 unset($table);
647 if (!isset($db)) {
648 $goto = 'main.php3';
649 } else {
650 $is_db = @PMA_mysql_select_db($db);
651 if (!$is_db) {
652 $goto = 'main.php3';
653 unset($db);
655 } // end if... else...
657 // Loads to target script
658 if (strpos(' ' . $goto, 'db_details') == 1
659 || strpos(' ' . $goto, 'tbl_properties') == 1) {
660 $js_to_run = 'functions.js';
662 if ($goto != 'main.php3') {
663 include('./header.inc.php3');
665 $active_page = $goto;
666 include('./' . $goto);
667 } // end if file_exist
668 else {
669 header('Location: ' . $cfg['PmaAbsoluteUri'] . str_replace('&amp;', '&', $goto) . '&message=' . urlencode($message));
670 } // end else
671 exit();
672 } // end no rows returned
674 // At least one row is returned -> displays a table with results
675 else {
676 // Displays the headers
677 if (isset($show_query)) {
678 unset($show_query);
680 if (isset($printview) && $printview == '1') {
681 include('./header_printview.inc.php3');
682 } else {
683 $js_to_run = 'functions.js';
684 unset($message);
685 if (!empty($table)) {
686 include('./tbl_properties_common.php3');
687 $url_query .= '&amp;goto=tbl_properties.php3&amp;back=tbl_properties.php3';
688 include('./tbl_properties_table_info.php3');
690 else {
691 include('./db_details_common.php3');
692 include('./db_details_db_info.php3');
694 include('./libraries/relation.lib.php3');
695 $cfgRelation = PMA_getRelationsParam();
698 // Gets the list of fields properties
699 if (isset($result) && $result) {
700 while ($field = PMA_mysql_fetch_field($result)) {
701 $fields_meta[] = $field;
703 $fields_cnt = count($fields_meta);
706 // Display previous update query (from tbl_replace)
707 if (isset($disp_query) && $cfg['ShowSQL'] == TRUE) {
708 $tmp_sql_query = $GLOBALS['sql_query'];
709 $tmp_sql_limit_to_append = (isset($GLOBALS['sql_limit_to_append'])?$GLOBALS['sql_limit_to_append']:'');
710 $GLOBALS['sql_query'] = $disp_query;
711 $GLOBALS['sql_limit_to_append'] = '';
712 PMA_showMessage($disp_message);
713 $GLOBALS['sql_query'] = $tmp_sql_query;
714 $GLOBALS['sql_limit_to_append'] = $tmp_sql_limit_to_append;
717 // Displays the results in a table
718 include('./libraries/display_tbl.lib.php3');
719 if (empty($disp_mode)) {
720 // see the "PMA_setDisplayMode()" function in
721 // libraries/display_tbl.lib.php3
722 $disp_mode = 'urdr111101';
724 if (!isset($dontlimitchars)) {
725 $dontlimitchars = 0;
728 PMA_displayTable($result, $disp_mode, $analyzed_sql);
729 mysql_free_result($result);
731 if ($disp_mode[6] == '1' || $disp_mode[9] == '1') {
732 echo "\n";
733 echo '<p>' . "\n";
735 // Displays "Insert a new row" link if required
736 if ($disp_mode[6] == '1') {
737 $lnk_goto = 'sql.php3?'
738 . PMA_generate_common_url($db, $table)
739 . '&amp;pos=' . $pos
740 . '&amp;session_max_rows=' . $session_max_rows
741 . '&amp;disp_direction=' . $disp_direction
742 . '&amp;repeat_cells=' . $repeat_cells
743 . '&amp;dontlimitchars=' . $dontlimitchars
744 . '&amp;sql_query=' . urlencode($sql_query);
745 $url_query = '?'
746 . PMA_generate_common_url($db, $table)
747 . '&amp;pos=' . $pos
748 . '&amp;session_max_rows=' . $session_max_rows
749 . '&amp;disp_direction=' . $disp_direction
750 . '&amp;repeat_cells=' . $repeat_cells
751 . '&amp;dontlimitchars=' . $dontlimitchars
752 . '&amp;sql_query=' . urlencode($sql_query)
753 . '&amp;goto=' . urlencode($lnk_goto);
755 echo ' <!-- Insert a new row -->' . "\n"
756 . ' <a href="tbl_change.php3' . $url_query . '">' . $strInsertNewRow . '</a>';
757 if ($disp_mode[9] == '1') {
758 echo '<br />';
760 echo "\n";
761 } // end insert new row
763 // Displays "printable view" link if required
764 if ($disp_mode[9] == '1') {
765 $url_query = '?'
766 . PMA_generate_common_url($db, $table)
767 . '&amp;pos=' . $pos
768 . '&amp;session_max_rows=' . $session_max_rows
769 . '&amp;disp_direction=' . $disp_direction
770 . '&amp;repeat_cells=' . $repeat_cells
771 . '&amp;printview=1'
772 . ((isset($dontlimitchars) && $dontlimitchars == '1') ? '&amp;dontlimitchars=1' : '')
773 . '&amp;sql_query=' . urlencode($sql_query);
774 echo ' <!-- Print view -->' . "\n"
775 . ' <a href="sql.php3' . $url_query . '" target="print_view">' . $strPrintView . '</a>' . "\n";
776 } // end displays "printable view"
778 echo '</p>' . "\n";
781 // Export link, if only one table
782 // (the url_query has extra parameters that won't be used to export)
783 if (!isset($printview)
784 && isset($analyzed_sql[0]['table_ref'][0]['table_true_name'])
785 && !isset($analyzed_sql[0]['table_ref'][1]['table_true_name'])) {
786 echo ' <!-- Export -->' . "\n"
787 . ' <a href="tbl_properties_export.php3' . $url_query . '&amp;unlim_num_rows=' . $unlim_num_rows . '">' . $strExport . '</a>' . "\n";
790 // Bookmark Support if required
791 if ($disp_mode[7] == '1'
792 && ($cfg['Bookmark']['db'] && $cfg['Bookmark']['table'] && empty($id_bookmark))
793 && !empty($sql_query)) {
794 echo "\n";
796 $goto = 'sql.php3?'
797 . PMA_generate_common_url($db, $table)
798 . '&amp;pos=' . $pos
799 . '&amp;session_max_rows=' . $session_max_rows
800 . '&amp;disp_direction=' . $disp_direction
801 . '&amp;repeat_cells=' . $repeat_cells
802 . '&amp;dontlimitchars=' . $dontlimitchars
803 . '&amp;sql_query=' . urlencode($sql_query)
804 . '&amp;id_bookmark=1';
806 <!-- Bookmark the query -->
807 <form action="sql.php3" method="post" onsubmit="return emptyFormElements(this, 'fields[label]');">
808 <?php
809 echo "\n";
810 if ($disp_mode[3] == '1') {
811 echo ' <i>' . $strOr . '</i>' . "\n";
814 <br /><br />
815 <?php echo $strBookmarkLabel; ?>&nbsp;:
816 <?php echo PMA_generate_common_hidden_inputs(); ?>
817 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
818 <input type="hidden" name="fields[dbase]" value="<?php echo htmlspecialchars($db); ?>" />
819 <input type="hidden" name="fields[user]" value="<?php echo $cfg['Bookmark']['user']; ?>" />
820 <input type="hidden" name="fields[query]" value="<?php echo urlencode($sql_query); ?>" />
821 <input type="text" name="fields[label]" value="" />
822 <input type="submit" name="store_bkm" value="<?php echo $strBookmarkThis; ?>" />
823 </form>
824 <?php
825 } // end bookmark support
827 // Do print the page if required
828 if (isset($printview) && $printview == '1') {
829 echo "\n";
831 <script type="text/javascript" language="javascript1.2">
832 <!--
833 // Do print the page
834 if (typeof(window.print) != 'undefined') {
835 window.print();
837 //-->
838 </script>
839 <?php
840 } // end print case
841 } // end rows returned
843 } // end executes the query
844 echo "\n\n";
848 * Displays the footer
850 require('./footer.inc.php3');