sql.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * SQL executor
  5. *
  6. * @todo we must handle the case if sql.php is called directly with a query
  7. * that returns 0 rows - to prevent cyclic redirects or includes
  8. * @package PhpMyAdmin
  9. */
  10. use PhpMyAdmin\Config\PageSettings;
  11. use PhpMyAdmin\ParseAnalyze;
  12. use PhpMyAdmin\Response;
  13. use PhpMyAdmin\Sql;
  14. use PhpMyAdmin\Url;
  15. use PhpMyAdmin\Util;
  16. /**
  17. * Gets some core libraries
  18. */
  19. require_once 'libraries/common.inc.php';
  20. require_once 'libraries/check_user_privileges.inc.php';
  21. PageSettings::showGroup('Browse');
  22. $response = Response::getInstance();
  23. $header = $response->getHeader();
  24. $scripts = $header->getScripts();
  25. $scripts->addFile('vendor/jquery/jquery.uitablefilter.js');
  26. $scripts->addFile('tbl_change.js');
  27. $scripts->addFile('indexes.js');
  28. $scripts->addFile('gis_data_editor.js');
  29. $scripts->addFile('multi_column_sort.js');
  30. $sql = new Sql();
  31. /**
  32. * Set ajax_reload in the response if it was already set
  33. */
  34. if (isset($ajax_reload) && $ajax_reload['reload'] === true) {
  35. $response->addJSON('ajax_reload', $ajax_reload);
  36. }
  37. /**
  38. * Defines the url to return to in case of error in a sql statement
  39. */
  40. $is_gotofile = true;
  41. if (empty($goto)) {
  42. if (empty($table)) {
  43. $goto = Util::getScriptNameForOption(
  44. $GLOBALS['cfg']['DefaultTabDatabase'], 'database'
  45. );
  46. } else {
  47. $goto = Util::getScriptNameForOption(
  48. $GLOBALS['cfg']['DefaultTabTable'], 'table'
  49. );
  50. }
  51. } // end if
  52. if (! isset($err_url)) {
  53. $err_url = (! empty($back) ? $back : $goto)
  54. . '?' . Url::getCommon(array('db' => $GLOBALS['db']))
  55. . ((mb_strpos(' ' . $goto, 'db_') != 1
  56. && strlen($table) > 0)
  57. ? '&amp;table=' . urlencode($table)
  58. : ''
  59. );
  60. } // end if
  61. // Coming from a bookmark dialog
  62. if (isset($_POST['bkm_fields']['bkm_sql_query'])) {
  63. $sql_query = $_POST['bkm_fields']['bkm_sql_query'];
  64. } elseif (isset($_POST['sql_query'])) {
  65. $sql_query = $_POST['sql_query'];
  66. }
  67. // This one is just to fill $db
  68. if (isset($_POST['bkm_fields']['bkm_database'])) {
  69. $db = $_POST['bkm_fields']['bkm_database'];
  70. }
  71. // During grid edit, if we have a relational field, show the dropdown for it.
  72. if (isset($_POST['get_relational_values'])
  73. && $_POST['get_relational_values'] == true
  74. ) {
  75. $sql->getRelationalValues($db, $table);
  76. // script has exited at this point
  77. }
  78. // Just like above, find possible values for enum fields during grid edit.
  79. if (isset($_POST['get_enum_values']) && $_POST['get_enum_values'] == true) {
  80. $sql->getEnumOrSetValues($db, $table, "enum");
  81. // script has exited at this point
  82. }
  83. // Find possible values for set fields during grid edit.
  84. if (isset($_POST['get_set_values']) && $_POST['get_set_values'] == true) {
  85. $sql->getEnumOrSetValues($db, $table, "set");
  86. // script has exited at this point
  87. }
  88. if (isset($_GET['get_default_fk_check_value'])
  89. && $_GET['get_default_fk_check_value'] == true
  90. ) {
  91. $response = Response::getInstance();
  92. $response->addJSON(
  93. 'default_fk_check_value', Util::isForeignKeyCheck()
  94. );
  95. exit;
  96. }
  97. /**
  98. * Check ajax request to set the column order and visibility
  99. */
  100. if (isset($_POST['set_col_prefs']) && $_POST['set_col_prefs'] == true) {
  101. $sql->setColumnOrderOrVisibility($table, $db);
  102. // script has exited at this point
  103. }
  104. // Default to browse if no query set and we have table
  105. // (needed for browsing from DefaultTabTable)
  106. if (empty($sql_query) && strlen($table) > 0 && strlen($db) > 0) {
  107. $sql_query = $sql->getDefaultSqlQueryForBrowse($db, $table);
  108. // set $goto to what will be displayed if query returns 0 rows
  109. $goto = '';
  110. } else {
  111. // Now we can check the parameters
  112. Util::checkParameters(array('sql_query'));
  113. }
  114. /**
  115. * Parse and analyze the query
  116. */
  117. list(
  118. $analyzed_sql_results,
  119. $db,
  120. $table_from_sql
  121. ) = ParseAnalyze::sqlQuery($sql_query, $db);
  122. // @todo: possibly refactor
  123. extract($analyzed_sql_results);
  124. if ($table != $table_from_sql && !empty($table_from_sql)) {
  125. $table = $table_from_sql;
  126. }
  127. /**
  128. * Check rights in case of DROP DATABASE
  129. *
  130. * This test may be bypassed if $is_js_confirmed = 1 (already checked with js)
  131. * but since a malicious user may pass this variable by url/form, we don't take
  132. * into account this case.
  133. */
  134. if ($sql->hasNoRightsToDropDatabase(
  135. $analyzed_sql_results, $cfg['AllowUserDropDatabase'], $GLOBALS['dbi']->isSuperuser()
  136. )) {
  137. Util::mysqlDie(
  138. __('"DROP DATABASE" statements are disabled.'),
  139. '',
  140. false,
  141. $err_url
  142. );
  143. } // end if
  144. /**
  145. * Need to find the real end of rows?
  146. */
  147. if (isset($find_real_end) && $find_real_end) {
  148. $unlim_num_rows = $sql->findRealEndOfRows($db, $table);
  149. }
  150. /**
  151. * Bookmark add
  152. */
  153. if (isset($_POST['store_bkm'])) {
  154. $sql->addBookmark($goto);
  155. // script has exited at this point
  156. } // end if
  157. /**
  158. * Sets or modifies the $goto variable if required
  159. */
  160. if ($goto == 'sql.php') {
  161. $is_gotofile = false;
  162. $goto = 'sql.php' . Url::getCommon(
  163. array(
  164. 'db' => $db,
  165. 'table' => $table,
  166. 'sql_query' => $sql_query
  167. )
  168. );
  169. } // end if
  170. $sql->executeQueryAndSendQueryResponse(
  171. $analyzed_sql_results, // analyzed_sql_results
  172. $is_gotofile, // is_gotofile
  173. $db, // db
  174. $table, // table
  175. isset($find_real_end) ? $find_real_end : null, // find_real_end
  176. isset($import_text) ? $import_text : null, // sql_query_for_bookmark
  177. isset($extra_data) ? $extra_data : null, // extra_data
  178. isset($message_to_show) ? $message_to_show : null, // message_to_show
  179. isset($message) ? $message : null, // message
  180. isset($sql_data) ? $sql_data : null, // sql_data
  181. $goto, // goto
  182. $pmaThemeImage, // pmaThemeImage
  183. isset($disp_query) ? $display_query : null, // disp_query
  184. isset($disp_message) ? $disp_message : null, // disp_message
  185. isset($query_type) ? $query_type : null, // query_type
  186. $sql_query, // sql_query
  187. isset($selected) ? $selected : null, // selectedTables
  188. isset($complete_query) ? $complete_query : null // complete_query
  189. );