db_table_exists.inc.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Ensure the database and the table exist (else move to the "parent" script)
  5. * and display headers
  6. *
  7. * @package PhpMyAdmin
  8. */
  9. use PhpMyAdmin\Core;
  10. use PhpMyAdmin\Message;
  11. use PhpMyAdmin\Response;
  12. use PhpMyAdmin\Url;
  13. if (! defined('PHPMYADMIN')) {
  14. exit;
  15. }
  16. if (empty($is_db)) {
  17. if (strlen($db) > 0) {
  18. $is_db = @$GLOBALS['dbi']->selectDb($db);
  19. } else {
  20. $is_db = false;
  21. }
  22. if (! $is_db) {
  23. // not a valid db name -> back to the welcome page
  24. if (! defined('IS_TRANSFORMATION_WRAPPER')) {
  25. $response = Response::getInstance();
  26. if ($response->isAjax()) {
  27. $response->setRequestStatus(false);
  28. $response->addJSON(
  29. 'message',
  30. Message::error(__('No databases selected.'))
  31. );
  32. } else {
  33. $url_params = array('reload' => 1);
  34. if (isset($message)) {
  35. $url_params['message'] = $message;
  36. }
  37. if (! empty($sql_query)) {
  38. $url_params['sql_query'] = $sql_query;
  39. }
  40. if (isset($show_as_php)) {
  41. $url_params['show_as_php'] = $show_as_php;
  42. }
  43. Core::sendHeaderLocation(
  44. './index.php'
  45. . Url::getCommonRaw($url_params)
  46. );
  47. }
  48. exit;
  49. }
  50. }
  51. } // end if (ensures db exists)
  52. if (empty($is_table)
  53. && !defined('PMA_SUBMIT_MULT')
  54. && !defined('TABLE_MAY_BE_ABSENT')
  55. ) {
  56. // Not a valid table name -> back to the db_sql.php
  57. if (strlen($table) > 0) {
  58. $is_table = $GLOBALS['dbi']->getCachedTableContent(array($db, $table), false);
  59. if (! $is_table) {
  60. $_result = $GLOBALS['dbi']->tryQuery(
  61. 'SHOW TABLES LIKE \''
  62. . $GLOBALS['dbi']->escapeString($table) . '\';',
  63. PhpMyAdmin\DatabaseInterface::CONNECT_USER,
  64. PhpMyAdmin\DatabaseInterface::QUERY_STORE
  65. );
  66. $is_table = @$GLOBALS['dbi']->numRows($_result);
  67. $GLOBALS['dbi']->freeResult($_result);
  68. }
  69. } else {
  70. $is_table = false;
  71. }
  72. if (! $is_table) {
  73. if (!defined('IS_TRANSFORMATION_WRAPPER')) {
  74. if (strlen($table) > 0) {
  75. // SHOW TABLES doesn't show temporary tables, so try select
  76. // (as it can happen just in case temporary table, it should be
  77. // fast):
  78. /**
  79. * @todo should this check really
  80. * only happen if IS_TRANSFORMATION_WRAPPER?
  81. */
  82. $_result = $GLOBALS['dbi']->tryQuery(
  83. 'SELECT COUNT(*) FROM ' . PhpMyAdmin\Util::backquote($table)
  84. . ';',
  85. PhpMyAdmin\DatabaseInterface::CONNECT_USER,
  86. PhpMyAdmin\DatabaseInterface::QUERY_STORE
  87. );
  88. $is_table = ($_result && @$GLOBALS['dbi']->numRows($_result));
  89. $GLOBALS['dbi']->freeResult($_result);
  90. }
  91. if (! $is_table) {
  92. include './db_sql.php';
  93. exit;
  94. }
  95. }
  96. if (! $is_table) {
  97. exit;
  98. }
  99. }
  100. } // end if (ensures table exists)