PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/pma/sql.php

https://bitbucket.org/StasPiv/playzone
PHP | 717 lines | 436 code | 88 blank | 193 comment | 167 complexity | 3ac7585effd27d57dc1bce856c9b55bf MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-2.0, LGPL-2.1
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * @todo we must handle the case if sql.php is called directly with a query
  5. * that returns 0 rows - to prevent cyclic redirects or includes
  6. * @version $Id: sql.php 12608 2009-06-30 10:48:08Z lem9 $
  7. * @package phpMyAdmin
  8. */
  9. /**
  10. * Gets some core libraries
  11. */
  12. require_once './libraries/common.inc.php';
  13. require_once './libraries/Table.class.php';
  14. require_once './libraries/check_user_privileges.lib.php';
  15. require_once './libraries/bookmark.lib.php';
  16. $GLOBALS['js_include'][] = 'mootools.js';
  17. /**
  18. * Defines the url to return to in case of error in a sql statement
  19. */
  20. // Security checkings
  21. if (! empty($goto)) {
  22. $is_gotofile = preg_replace('@^([^?]+).*$@s', '\\1', $goto);
  23. if (! @file_exists('./' . $is_gotofile)) {
  24. unset($goto);
  25. } else {
  26. $is_gotofile = ($is_gotofile == $goto);
  27. }
  28. } else {
  29. $goto = (! strlen($table)) ? $cfg['DefaultTabDatabase'] : $cfg['DefaultTabTable'];
  30. $is_gotofile = true;
  31. } // end if
  32. if (!isset($err_url)) {
  33. $err_url = (!empty($back) ? $back : $goto)
  34. . '?' . PMA_generate_common_url($db)
  35. . ((strpos(' ' . $goto, 'db_') != 1 && strlen($table)) ? '&amp;table=' . urlencode($table) : '');
  36. } // end if
  37. // Coming from a bookmark dialog
  38. if (isset($fields['query'])) {
  39. $sql_query = $fields['query'];
  40. }
  41. // This one is just to fill $db
  42. if (isset($fields['dbase'])) {
  43. $db = $fields['dbase'];
  44. }
  45. // Default to browse if no query set and we have table
  46. // (needed for browsing from DefaultTabTable)
  47. if (empty($sql_query) && strlen($table) && strlen($db)) {
  48. require_once './libraries/bookmark.lib.php';
  49. $book_sql_query = PMA_Bookmark_get($db, '\'' . PMA_sqlAddslashes($table) . '\'',
  50. 'label');
  51. if (! empty($book_sql_query)) {
  52. $sql_query = $book_sql_query;
  53. } else {
  54. $sql_query = 'SELECT * FROM ' . PMA_backquote($table);
  55. }
  56. unset($book_sql_query);
  57. // set $goto to what will be displayed if query returns 0 rows
  58. $goto = 'tbl_structure.php';
  59. } else {
  60. // Now we can check the parameters
  61. PMA_checkParameters(array('sql_query'));
  62. }
  63. // instead of doing the test twice
  64. $is_drop_database = preg_match('/DROP[[:space:]]+(DATABASE|SCHEMA)[[:space:]]+/i',
  65. $sql_query);
  66. /**
  67. * Check rights in case of DROP DATABASE
  68. *
  69. * This test may be bypassed if $is_js_confirmed = 1 (already checked with js)
  70. * but since a malicious user may pass this variable by url/form, we don't take
  71. * into account this case.
  72. */
  73. if (!defined('PMA_CHK_DROP')
  74. && !$cfg['AllowUserDropDatabase']
  75. && $is_drop_database
  76. && !$is_superuser) {
  77. require_once './libraries/header.inc.php';
  78. PMA_mysqlDie($strNoDropDatabases, '', '', $err_url);
  79. } // end if
  80. require_once './libraries/display_tbl.lib.php';
  81. PMA_displayTable_checkConfigParams();
  82. /**
  83. * Need to find the real end of rows?
  84. */
  85. if (isset($find_real_end) && $find_real_end) {
  86. $unlim_num_rows = PMA_Table::countRecords($db, $table, true, true);
  87. $_SESSION['userconf']['pos'] = @((ceil($unlim_num_rows / $_SESSION['userconf']['max_rows']) - 1) * $_SESSION['userconf']['max_rows']);
  88. }
  89. /**
  90. * Bookmark add
  91. */
  92. if (isset($store_bkm)) {
  93. PMA_Bookmark_save($fields, (isset($bkm_all_users) && $bkm_all_users == 'true' ? true : false));
  94. // go back to sql.php to redisplay query; do not use &amp; in this case:
  95. PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . $goto . '&label=' . $fields['label']);
  96. } // end if
  97. /**
  98. * Parse and analyze the query
  99. */
  100. require_once './libraries/parse_analyze.lib.php';
  101. /**
  102. * Sets or modifies the $goto variable if required
  103. */
  104. if ($goto == 'sql.php') {
  105. $is_gotofile = false;
  106. $goto = 'sql.php?'
  107. . PMA_generate_common_url($db, $table)
  108. . '&amp;sql_query=' . urlencode($sql_query);
  109. } // end if
  110. /**
  111. * Go back to further page if table should not be dropped
  112. */
  113. if (isset($btnDrop) && $btnDrop == $strNo) {
  114. if (!empty($back)) {
  115. $goto = $back;
  116. }
  117. if ($is_gotofile) {
  118. if (strpos($goto, 'db_') === 0 && strlen($table)) {
  119. $table = '';
  120. }
  121. $active_page = $goto;
  122. require './' . PMA_securePath($goto);
  123. } else {
  124. PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . str_replace('&amp;', '&', $goto));
  125. }
  126. exit();
  127. } // end if
  128. /**
  129. * Displays the confirm page if required
  130. *
  131. * This part of the script is bypassed if $is_js_confirmed = 1 (already checked
  132. * with js) because possible security issue is not so important here: at most,
  133. * the confirm message isn't displayed.
  134. *
  135. * Also bypassed if only showing php code.or validating a SQL query
  136. */
  137. if (! $cfg['Confirm'] || isset($_REQUEST['is_js_confirmed']) || isset($btnDrop)
  138. // if we are coming from a "Create PHP code" or a "Without PHP Code"
  139. // dialog, we won't execute the query anyway, so don't confirm
  140. || isset($GLOBALS['show_as_php'])
  141. || !empty($GLOBALS['validatequery'])) {
  142. $do_confirm = false;
  143. } else {
  144. $do_confirm = isset($analyzed_sql[0]['queryflags']['need_confirm']);
  145. }
  146. if ($do_confirm) {
  147. $stripped_sql_query = $sql_query;
  148. require_once './libraries/header.inc.php';
  149. if ($is_drop_database) {
  150. echo '<h1 class="warning">' . $strDropDatabaseStrongWarning . '</h1>';
  151. }
  152. echo '<form action="sql.php" method="post">' . "\n"
  153. .PMA_generate_common_hidden_inputs($db, $table);
  154. ?>
  155. <input type="hidden" name="sql_query" value="<?php echo htmlspecialchars($sql_query); ?>" />
  156. <input type="hidden" name="zero_rows" value="<?php echo isset($zero_rows) ? PMA_sanitize($zero_rows) : ''; ?>" />
  157. <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
  158. <input type="hidden" name="back" value="<?php echo isset($back) ? PMA_sanitize($back) : ''; ?>" />
  159. <input type="hidden" name="reload" value="<?php echo isset($reload) ? PMA_sanitize($reload) : 0; ?>" />
  160. <input type="hidden" name="purge" value="<?php echo isset($purge) ? PMA_sanitize($purge) : ''; ?>" />
  161. <input type="hidden" name="cpurge" value="<?php echo isset($cpurge) ? PMA_sanitize($cpurge) : ''; ?>" />
  162. <input type="hidden" name="purgekey" value="<?php echo isset($purgekey) ? PMA_sanitize($purgekey) : ''; ?>" />
  163. <input type="hidden" name="show_query" value="<?php echo isset($show_query) ? PMA_sanitize($show_query) : ''; ?>" />
  164. <?php
  165. echo '<fieldset class="confirmation">' . "\n"
  166. .' <legend>' . $strDoYouReally . '</legend>'
  167. .' <tt>' . htmlspecialchars($stripped_sql_query) . '</tt>' . "\n"
  168. .'</fieldset>' . "\n"
  169. .'<fieldset class="tblFooters">' . "\n";
  170. ?>
  171. <input type="submit" name="btnDrop" value="<?php echo $strYes; ?>" id="buttonYes" />
  172. <input type="submit" name="btnDrop" value="<?php echo $strNo; ?>" id="buttonNo" />
  173. <?php
  174. echo '</fieldset>' . "\n"
  175. . '</form>' . "\n";
  176. /**
  177. * Displays the footer and exit
  178. */
  179. require_once './libraries/footer.inc.php';
  180. } // end if $do_confirm
  181. // Defines some variables
  182. // A table has to be created, renamed, dropped -> navi frame should be reloaded
  183. /**
  184. * @todo use the parser/analyzer
  185. */
  186. if (empty($reload)
  187. && preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $sql_query)) {
  188. $reload = 1;
  189. }
  190. // SK -- Patch: $is_group added for use in calculation of total number of
  191. // rows.
  192. // $is_count is changed for more correct "LIMIT" clause
  193. // appending in queries like
  194. // "SELECT COUNT(...) FROM ... GROUP BY ..."
  195. /**
  196. * @todo detect all this with the parser, to avoid problems finding
  197. * those strings in comments or backquoted identifiers
  198. */
  199. $is_explain = $is_count = $is_export = $is_delete = $is_insert = $is_affected = $is_show = $is_maint = $is_analyse = $is_group = $is_func = $is_replace = false;
  200. if ($is_select) { // see line 141
  201. $is_group = preg_match('@(GROUP[[:space:]]+BY|HAVING|SELECT[[:space:]]+DISTINCT)[[:space:]]+@i', $sql_query);
  202. $is_func = !$is_group && (preg_match('@[[:space:]]+(SUM|AVG|STD|STDDEV|MIN|MAX|BIT_OR|BIT_AND)\s*\(@i', $sql_query));
  203. $is_count = !$is_group && (preg_match('@^SELECT[[:space:]]+COUNT\((.*\.+)?.*\)@i', $sql_query));
  204. $is_export = (preg_match('@[[:space:]]+INTO[[:space:]]+OUTFILE[[:space:]]+@i', $sql_query));
  205. $is_analyse = (preg_match('@[[:space:]]+PROCEDURE[[:space:]]+ANALYSE@i', $sql_query));
  206. } elseif (preg_match('@^EXPLAIN[[:space:]]+@i', $sql_query)) {
  207. $is_explain = true;
  208. } elseif (preg_match('@^DELETE[[:space:]]+@i', $sql_query)) {
  209. $is_delete = true;
  210. $is_affected = true;
  211. } elseif (preg_match('@^(INSERT|LOAD[[:space:]]+DATA|REPLACE)[[:space:]]+@i', $sql_query)) {
  212. $is_insert = true;
  213. $is_affected = true;
  214. if (preg_match('@^(REPLACE)[[:space:]]+@i', $sql_query)) {
  215. $is_replace = true;
  216. }
  217. } elseif (preg_match('@^UPDATE[[:space:]]+@i', $sql_query)) {
  218. $is_affected = true;
  219. } elseif (preg_match('@^[[:space:]]*SHOW[[:space:]]+@i', $sql_query)) {
  220. $is_show = true;
  221. } elseif (preg_match('@^(CHECK|ANALYZE|REPAIR|OPTIMIZE)[[:space:]]+TABLE[[:space:]]+@i', $sql_query)) {
  222. $is_maint = true;
  223. }
  224. // Do append a "LIMIT" clause?
  225. if ((! $cfg['ShowAll'] || $_SESSION['userconf']['max_rows'] != 'all')
  226. && ! ($is_count || $is_export || $is_func || $is_analyse)
  227. && isset($analyzed_sql[0]['queryflags']['select_from'])
  228. && ! isset($analyzed_sql[0]['queryflags']['offset'])
  229. && empty($analyzed_sql[0]['limit_clause'])
  230. ) {
  231. $sql_limit_to_append = ' LIMIT ' . $_SESSION['userconf']['pos'] . ', ' . $_SESSION['userconf']['max_rows'] . " ";
  232. $full_sql_query = $analyzed_sql[0]['section_before_limit'] . "\n" . $sql_limit_to_append . $analyzed_sql[0]['section_after_limit'];
  233. /**
  234. * @todo pretty printing of this modified query
  235. */
  236. if (isset($display_query)) {
  237. // if the analysis of the original query revealed that we found
  238. // a section_after_limit, we now have to analyze $display_query
  239. // to display it correctly
  240. if (!empty($analyzed_sql[0]['section_after_limit']) && trim($analyzed_sql[0]['section_after_limit']) != ';') {
  241. $analyzed_display_query = PMA_SQP_analyze(PMA_SQP_parse($display_query));
  242. $display_query = $analyzed_display_query[0]['section_before_limit'] . "\n" . $sql_limit_to_append . $analyzed_display_query[0]['section_after_limit'];
  243. }
  244. }
  245. } else {
  246. $full_sql_query = $sql_query;
  247. } // end if...else
  248. if (strlen($db)) {
  249. PMA_DBI_select_db($db);
  250. }
  251. // E x e c u t e t h e q u e r y
  252. // Only if we didn't ask to see the php code (mikebeck)
  253. if (isset($GLOBALS['show_as_php']) || !empty($GLOBALS['validatequery'])) {
  254. unset($result);
  255. $num_rows = 0;
  256. } else {
  257. if (isset($_SESSION['profiling']) && PMA_profilingSupported()) {
  258. PMA_DBI_query('SET PROFILING=1;');
  259. }
  260. // garvin: Measure query time.
  261. // TODO-Item http://sourceforge.net/tracker/index.php?func=detail&aid=571934&group_id=23067&atid=377411
  262. $querytime_before = array_sum(explode(' ', microtime()));
  263. $result = @PMA_DBI_try_query($full_sql_query, null, PMA_DBI_QUERY_STORE);
  264. $querytime_after = array_sum(explode(' ', microtime()));
  265. $GLOBALS['querytime'] = $querytime_after - $querytime_before;
  266. // Displays an error message if required and stop parsing the script
  267. if ($error = PMA_DBI_getError()) {
  268. if ($is_gotofile) {
  269. if (strpos($goto, 'db_') === 0 && strlen($table)) {
  270. $table = '';
  271. }
  272. $active_page = $goto;
  273. $message = htmlspecialchars(PMA_Message::rawError($error));
  274. /**
  275. * Go to target path.
  276. */
  277. require './' . PMA_securePath($goto);
  278. } else {
  279. /**
  280. * HTML header.
  281. */
  282. require_once './libraries/header.inc.php';
  283. $full_err_url = (preg_match('@^(db|tbl)_@', $err_url))
  284. ? $err_url . '&amp;show_query=1&amp;sql_query=' . urlencode($sql_query)
  285. : $err_url;
  286. PMA_mysqlDie($error, $full_sql_query, '', $full_err_url);
  287. }
  288. exit;
  289. }
  290. unset($error);
  291. // Gets the number of rows affected/returned
  292. // (This must be done immediately after the query because
  293. // mysql_affected_rows() reports about the last query done)
  294. if (!$is_affected) {
  295. $num_rows = ($result) ? @PMA_DBI_num_rows($result) : 0;
  296. } elseif (!isset($num_rows)) {
  297. $num_rows = @PMA_DBI_affected_rows();
  298. }
  299. // Grabs the profiling results
  300. if (isset($_SESSION['profiling']) && PMA_profilingSupported()) {
  301. $profiling_results = PMA_DBI_fetch_result('SHOW PROFILE;');
  302. }
  303. // Checks if the current database has changed
  304. // This could happen if the user sends a query like "USE `database`;"
  305. /**
  306. * commented out auto-switching to active database - really required?
  307. * bug #1814718 win: table list disappears (mixed case db names)
  308. * https://sourceforge.net/support/tracker.php?aid=1814718
  309. * @todo RELEASE test and comit or rollback before release
  310. $current_db = PMA_DBI_fetch_value('SELECT DATABASE()');
  311. if ($db !== $current_db) {
  312. $db = $current_db;
  313. $reload = 1;
  314. }
  315. unset($current_db);
  316. */
  317. // tmpfile remove after convert encoding appended by Y.Kawada
  318. if (function_exists('PMA_kanji_file_conv')
  319. && (isset($textfile) && file_exists($textfile))) {
  320. unlink($textfile);
  321. }
  322. // Counts the total number of rows for the same 'SELECT' query without the
  323. // 'LIMIT' clause that may have been programatically added
  324. if (empty($sql_limit_to_append)) {
  325. $unlim_num_rows = $num_rows;
  326. // if we did not append a limit, set this to get a correct
  327. // "Showing rows..." message
  328. //$_SESSION['userconf']['max_rows'] = 'all';
  329. } elseif ($is_select) {
  330. // c o u n t q u e r y
  331. // If we are "just browsing", there is only one table,
  332. // and no where clause (or just 'WHERE 1 '),
  333. // so we do a quick count (which uses MaxExactCount)
  334. // because SQL_CALC_FOUND_ROWS
  335. // is not quick on large InnoDB tables
  336. // but do not count again if we did it previously
  337. // due to $find_real_end == true
  338. if (!$is_group
  339. && !isset($analyzed_sql[0]['queryflags']['union'])
  340. && !isset($analyzed_sql[0]['table_ref'][1]['table_name'])
  341. && (empty($analyzed_sql[0]['where_clause'])
  342. || $analyzed_sql[0]['where_clause'] == '1 ')
  343. && !isset($find_real_end)
  344. ) {
  345. // "j u s t b r o w s i n g"
  346. $unlim_num_rows = PMA_Table::countRecords($db, $table, true);
  347. } else { // n o t " j u s t b r o w s i n g "
  348. // add select expression after the SQL_CALC_FOUND_ROWS
  349. // for UNION, just adding SQL_CALC_FOUND_ROWS
  350. // after the first SELECT works.
  351. // take the left part, could be:
  352. // SELECT
  353. // (SELECT
  354. $count_query = PMA_SQP_formatHtml($parsed_sql, 'query_only', 0, $analyzed_sql[0]['position_of_first_select'] + 1);
  355. $count_query .= ' SQL_CALC_FOUND_ROWS ';
  356. // add everything that was after the first SELECT
  357. $count_query .= PMA_SQP_formatHtml($parsed_sql, 'query_only', $analyzed_sql[0]['position_of_first_select']+1);
  358. // ensure there is no semicolon at the end of the
  359. // count query because we'll probably add
  360. // a LIMIT 1 clause after it
  361. $count_query = rtrim($count_query);
  362. $count_query = rtrim($count_query, ';');
  363. // if using SQL_CALC_FOUND_ROWS, add a LIMIT to avoid
  364. // long delays. Returned count will be complete anyway.
  365. // (but a LIMIT would disrupt results in an UNION)
  366. if (!isset($analyzed_sql[0]['queryflags']['union'])) {
  367. $count_query .= ' LIMIT 1';
  368. }
  369. // run the count query
  370. PMA_DBI_try_query($count_query);
  371. // if (mysql_error()) {
  372. // void.
  373. // I tried the case
  374. // (SELECT `User`, `Host`, `Db`, `Select_priv` FROM `db`)
  375. // UNION (SELECT `User`, `Host`, "%" AS "Db",
  376. // `Select_priv`
  377. // FROM `user`) ORDER BY `User`, `Host`, `Db`;
  378. // and although the generated count_query is wrong
  379. // the SELECT FOUND_ROWS() work! (maybe it gets the
  380. // count from the latest query that worked)
  381. //
  382. // another case where the count_query is wrong:
  383. // SELECT COUNT(*), f1 from t1 group by f1
  384. // and you click to sort on count(*)
  385. // }
  386. $unlim_num_rows = PMA_DBI_fetch_value('SELECT FOUND_ROWS()');
  387. } // end else "just browsing"
  388. } else { // not $is_select
  389. $unlim_num_rows = 0;
  390. } // end rows total count
  391. // garvin: if a table or database gets dropped, check column comments.
  392. if (isset($purge) && $purge == '1') {
  393. /**
  394. * Cleanup relations.
  395. */
  396. require_once './libraries/relation_cleanup.lib.php';
  397. if (strlen($table) && strlen($db)) {
  398. PMA_relationsCleanupTable($db, $table);
  399. } elseif (strlen($db)) {
  400. PMA_relationsCleanupDatabase($db);
  401. } else {
  402. // garvin: VOID. No DB/Table gets deleted.
  403. } // end if relation-stuff
  404. } // end if ($purge)
  405. // garvin: If a column gets dropped, do relation magic.
  406. if (isset($cpurge) && $cpurge == '1' && isset($purgekey)
  407. && strlen($db) && strlen($table) && !empty($purgekey)) {
  408. require_once './libraries/relation_cleanup.lib.php';
  409. PMA_relationsCleanupColumn($db, $table, $purgekey);
  410. } // end if column PMA_* purge
  411. } // end else "didn't ask to see php code"
  412. // No rows returned -> move back to the calling page
  413. if (0 == $num_rows || $is_affected) {
  414. if ($is_delete) {
  415. $message = PMA_Message::success('strRowsDeleted');
  416. $message->addParam($num_rows);
  417. } elseif ($is_insert) {
  418. if ($is_replace) {
  419. /* For replace we get DELETED + INSERTED row count, so we have to call it affected */
  420. $message = PMA_Message::success('strRowsAffected');
  421. $message->addParam($num_rows);
  422. } else {
  423. $message = PMA_Message::success('strRowsInserted');
  424. $message->addParam($num_rows);
  425. }
  426. $insert_id = PMA_DBI_insert_id();
  427. if ($insert_id != 0) {
  428. // insert_id is id of FIRST record inserted in one insert, so if we inserted multiple rows, we had to increment this
  429. $message->addMessage('[br]');
  430. // need to use a temporary because the Message class
  431. // currently supports adding parameters only to the first
  432. // message
  433. $_inserted = PMA_Message::notice('strInsertedRowId');
  434. $_inserted->addParam($insert_id + $num_rows - 1);
  435. $message->addMessage($_inserted);
  436. }
  437. } elseif ($is_affected) {
  438. $message = PMA_Message::success('strRowsAffected');
  439. $message->addParam($num_rows);
  440. // Ok, here is an explanation for the !$is_select.
  441. // The form generated by sql_query_form.lib.php
  442. // and db_sql.php has many submit buttons
  443. // on the same form, and some confusion arises from the
  444. // fact that $zero_rows is sent for every case.
  445. // The $zero_rows containing $strSuccess and sent with
  446. // the form should not have priority over
  447. // errors like $strEmptyResultSet
  448. } elseif (!empty($zero_rows) && !$is_select) {
  449. $message = PMA_Message::rawSuccess(htmlspecialchars($zero_rows));
  450. } elseif (!empty($GLOBALS['show_as_php'])) {
  451. $message = PMA_Message::success('strShowingPhp');
  452. } elseif (isset($GLOBALS['show_as_php'])) {
  453. /* User disable showing as PHP, query is only displayed */
  454. $message = PMA_Message::notice('strShowingSQL');
  455. } elseif (!empty($GLOBALS['validatequery'])) {
  456. $message = PMA_Message::notice('strValidateSQL');
  457. } else {
  458. $message = PMA_Message::success('strEmptyResultSet');
  459. }
  460. if (isset($GLOBALS['querytime'])) {
  461. $_querytime = PMA_Message::notice('strQueryTime');
  462. $_querytime->addParam($GLOBALS['querytime']);
  463. $message->addMessage('(');
  464. $message->addMessage($_querytime);
  465. $message->addMessage(')');
  466. }
  467. if ($is_gotofile) {
  468. $goto = PMA_securePath($goto);
  469. // Checks for a valid target script
  470. $is_db = $is_table = false;
  471. include 'libraries/db_table_exists.lib.php';
  472. if (strpos($goto, 'tbl_') === 0 && ! $is_table) {
  473. if (strlen($table)) {
  474. $table = '';
  475. }
  476. $goto = 'db_sql.php';
  477. }
  478. if (strpos($goto, 'db_') === 0 && ! $is_db) {
  479. if (strlen($db)) {
  480. $db = '';
  481. }
  482. $goto = 'main.php';
  483. }
  484. // Loads to target script
  485. if (strpos($goto, 'db_') === 0
  486. || strpos($goto, 'tbl_') === 0) {
  487. $GLOBALS['js_include'][] = 'functions.js';
  488. }
  489. if ($goto != 'main.php') {
  490. require_once './libraries/header.inc.php';
  491. }
  492. $active_page = $goto;
  493. require './' . $goto;
  494. } else {
  495. // avoid a redirect loop when last record was deleted
  496. if (0 == $num_rows && 'sql.php' == $cfg['DefaultTabTable']) {
  497. $goto = str_replace('sql.php','tbl_structure.php',$goto);
  498. }
  499. PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . str_replace('&amp;', '&', $goto) . '&message=' . urlencode($message));
  500. } // end else
  501. exit();
  502. } // end no rows returned
  503. // At least one row is returned -> displays a table with results
  504. else {
  505. // Displays the headers
  506. if (isset($show_query)) {
  507. unset($show_query);
  508. }
  509. if (isset($printview) && $printview == '1') {
  510. require_once './libraries/header_printview.inc.php';
  511. } else {
  512. $GLOBALS['js_include'][] = 'functions.js';
  513. unset($message);
  514. if (strlen($table)) {
  515. require './libraries/tbl_common.php';
  516. $url_query .= '&amp;goto=tbl_sql.php&amp;back=tbl_sql.php';
  517. require './libraries/tbl_info.inc.php';
  518. require './libraries/tbl_links.inc.php';
  519. } elseif (strlen($db)) {
  520. require './libraries/db_common.inc.php';
  521. require './libraries/db_info.inc.php';
  522. } else {
  523. require './libraries/server_common.inc.php';
  524. require './libraries/server_links.inc.php';
  525. }
  526. }
  527. if (strlen($db)) {
  528. require_once './libraries/relation.lib.php';
  529. $cfgRelation = PMA_getRelationsParam();
  530. }
  531. // Gets the list of fields properties
  532. if (isset($result) && $result) {
  533. $fields_meta = PMA_DBI_get_fields_meta($result);
  534. $fields_cnt = count($fields_meta);
  535. }
  536. // Display previous update query (from tbl_replace)
  537. if (isset($disp_query) && $cfg['ShowSQL'] == true) {
  538. PMA_showMessage($disp_message, $disp_query, 'success');
  539. }
  540. if (isset($profiling_results)) {
  541. PMA_profilingResults($profiling_results);
  542. }
  543. // Displays the results in a table
  544. if (empty($disp_mode)) {
  545. // see the "PMA_setDisplayMode()" function in
  546. // libraries/display_tbl.lib.php
  547. $disp_mode = 'urdr111101';
  548. }
  549. // hide edit and delete links for information_schema
  550. if ($db == 'information_schema') {
  551. $disp_mode = 'nnnn110111';
  552. }
  553. if (isset($label)) {
  554. $message = PMA_message::success('strBookmarkCreated');
  555. $message->addParam($label);
  556. $message->display();
  557. }
  558. PMA_displayTable($result, $disp_mode, $analyzed_sql);
  559. PMA_DBI_free_result($result);
  560. // BEGIN INDEX CHECK See if indexes should be checked.
  561. if (isset($query_type) && $query_type == 'check_tbl' && isset($selected) && is_array($selected)) {
  562. foreach ($selected as $idx => $tbl_name) {
  563. $check = PMA_Index::findDuplicates($tbl_name, $db);
  564. if (! empty($check)) {
  565. printf($strIndexWarningTable, $tbl_name);
  566. echo $check;
  567. }
  568. }
  569. } // End INDEX CHECK
  570. // Bookmark support if required
  571. if ($disp_mode[7] == '1'
  572. && (! empty($cfg['Bookmark']) && empty($id_bookmark))
  573. && !empty($sql_query)) {
  574. echo "\n";
  575. $goto = 'sql.php?'
  576. . PMA_generate_common_url($db, $table)
  577. . '&amp;sql_query=' . urlencode($sql_query)
  578. . '&amp;id_bookmark=1';
  579. ?>
  580. <form action="sql.php" method="post" onsubmit="return emptyFormElements(this, 'fields[label]');">
  581. <?php echo PMA_generate_common_hidden_inputs(); ?>
  582. <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
  583. <input type="hidden" name="fields[dbase]" value="<?php echo htmlspecialchars($db); ?>" />
  584. <input type="hidden" name="fields[user]" value="<?php echo $cfg['Bookmark']['user']; ?>" />
  585. <input type="hidden" name="fields[query]" value="<?php echo urlencode(isset($complete_query) ? $complete_query : $sql_query); ?>" />
  586. <fieldset>
  587. <legend><?php
  588. echo ($cfg['PropertiesIconic'] ? '<img class="icon" src="' . $pmaThemeImage . 'b_bookmark.png" width="16" height="16" alt="' . $strBookmarkThis . '" />' : '')
  589. . $strBookmarkThis;
  590. ?>
  591. </legend>
  592. <div class="formelement">
  593. <label for="fields_label_"><?php echo $strBookmarkLabel; ?>:</label>
  594. <input type="text" id="fields_label_" name="fields[label]" value="" />
  595. </div>
  596. <div class="formelement">
  597. <input type="checkbox" name="bkm_all_users" id="bkm_all_users" value="true" />
  598. <label for="bkm_all_users"><?php echo $strBookmarkAllUsers; ?></label>
  599. </div>
  600. <div class="clearfloat"></div>
  601. </fieldset>
  602. <fieldset class="tblFooters">
  603. <input type="submit" name="store_bkm" value="<?php echo $strBookmarkThis; ?>" />
  604. </fieldset>
  605. </form>
  606. <?php
  607. } // end bookmark support
  608. // Do print the page if required
  609. if (isset($printview) && $printview == '1') {
  610. ?>
  611. <script type="text/javascript">
  612. //<![CDATA[
  613. // Do print the page
  614. window.onload = function()
  615. {
  616. if (typeof(window.print) != 'undefined') {
  617. window.print();
  618. }
  619. }
  620. //]]>
  621. </script>
  622. <?php
  623. } // end print case
  624. } // end rows returned
  625. /**
  626. * Displays the footer
  627. */
  628. require_once './libraries/footer.inc.php';
  629. ?>