DbTableExists.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin;
  4. use PhpMyAdmin\Controllers\Database\SqlController;
  5. use function defined;
  6. use function strlen;
  7. final class DbTableExists
  8. {
  9. /**
  10. * Ensure the database and the table exist (else move to the "parent" script)
  11. * and display headers
  12. */
  13. public static function check(): void
  14. {
  15. self::checkDatabase();
  16. self::checkTable();
  17. }
  18. private static function checkDatabase(): void
  19. {
  20. global $db, $dbi, $is_db, $message, $show_as_php, $sql_query;
  21. if (! empty($is_db)) {
  22. return;
  23. }
  24. $is_db = false;
  25. if (strlen($db) > 0) {
  26. $is_db = @$dbi->selectDb($db);
  27. }
  28. if ($is_db || defined('IS_TRANSFORMATION_WRAPPER')) {
  29. return;
  30. }
  31. $response = Response::getInstance();
  32. if ($response->isAjax()) {
  33. $response->setRequestStatus(false);
  34. $response->addJSON(
  35. 'message',
  36. Message::error(__('No databases selected.'))
  37. );
  38. exit;
  39. }
  40. $url_params = ['reload' => 1];
  41. if (isset($message)) {
  42. $url_params['message'] = $message;
  43. }
  44. if (! empty($sql_query)) {
  45. $url_params['sql_query'] = $sql_query;
  46. }
  47. if (isset($show_as_php)) {
  48. $url_params['show_as_php'] = $show_as_php;
  49. }
  50. Core::sendHeaderLocation('./index.php?route=/' . Url::getCommonRaw($url_params, '&'));
  51. exit;
  52. }
  53. private static function checkTable(): void
  54. {
  55. global $containerBuilder, $db, $table, $dbi, $is_table;
  56. if (! empty($is_table)
  57. || defined('PMA_SUBMIT_MULT')
  58. || defined('TABLE_MAY_BE_ABSENT')
  59. ) {
  60. return;
  61. }
  62. $is_table = false;
  63. if (strlen($table) > 0) {
  64. $is_table = $dbi->getCache()->getCachedTableContent([$db, $table], false);
  65. if ($is_table) {
  66. return;
  67. }
  68. $_result = $dbi->tryQuery(
  69. 'SHOW TABLES LIKE \'' . $dbi->escapeString($table) . '\';',
  70. DatabaseInterface::CONNECT_USER,
  71. DatabaseInterface::QUERY_STORE
  72. );
  73. $is_table = @$dbi->numRows($_result);
  74. $dbi->freeResult($_result);
  75. }
  76. if ($is_table) {
  77. return;
  78. }
  79. if (defined('IS_TRANSFORMATION_WRAPPER')) {
  80. exit;
  81. }
  82. if (strlen($table) > 0) {
  83. /**
  84. * SHOW TABLES doesn't show temporary tables, so try select
  85. * (as it can happen just in case temporary table, it should be fast):
  86. */
  87. $_result = $dbi->tryQuery(
  88. 'SELECT COUNT(*) FROM ' . Util::backquote($table) . ';',
  89. DatabaseInterface::CONNECT_USER,
  90. DatabaseInterface::QUERY_STORE
  91. );
  92. $is_table = ($_result && @$dbi->numRows($_result));
  93. $dbi->freeResult($_result);
  94. }
  95. if ($is_table) {
  96. return;
  97. }
  98. /** @var SqlController $controller */
  99. $controller = $containerBuilder->get(SqlController::class);
  100. $controller->index();
  101. exit;
  102. }
  103. }