sql.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. /**
  11. * Gets some core libraries
  12. */
  13. require_once 'libraries/common.inc.php';
  14. require_once 'libraries/Table.class.php';
  15. require_once 'libraries/Header.class.php';
  16. require_once 'libraries/check_user_privileges.lib.php';
  17. require_once 'libraries/bookmark.lib.php';
  18. require_once 'libraries/sql.lib.php';
  19. require_once 'libraries/sqlparser.lib.php';
  20. $response = PMA_Response::getInstance();
  21. $header = $response->getHeader();
  22. $scripts = $header->getScripts();
  23. $scripts->addFile('jquery/jquery-ui-timepicker-addon.js');
  24. $scripts->addFile('jquery/jquery.uitablefilter.js');
  25. $scripts->addFile('tbl_change.js');
  26. $scripts->addFile('indexes.js');
  27. $scripts->addFile('gis_data_editor.js');
  28. $scripts->addFile('multi_column_sort.js');
  29. /**
  30. * Set ajax_reload in the response if it was already set
  31. */
  32. if (isset($ajax_reload) && $ajax_reload['reload'] === true) {
  33. $response->addJSON('ajax_reload', $ajax_reload);
  34. }
  35. /**
  36. * Defines the url to return to in case of error in a sql statement
  37. */
  38. // Security checkings
  39. if (! empty($goto)) {
  40. $is_gotofile = preg_replace('@^([^?]+).*$@s', '\\1', $goto);
  41. if (! @file_exists('' . $is_gotofile)) {
  42. unset($goto);
  43. } else {
  44. $is_gotofile = ($is_gotofile == $goto);
  45. }
  46. } else {
  47. if (empty($table)) {
  48. $goto = $cfg['DefaultTabDatabase'];
  49. } else {
  50. $goto = $cfg['DefaultTabTable'];
  51. }
  52. $is_gotofile = true;
  53. } // end if
  54. if (! isset($err_url)) {
  55. $err_url = (! empty($back) ? $back : $goto)
  56. . '?' . PMA_URL_getCommon($db)
  57. . ((strpos(' ' . $goto, 'db_') != 1 && strlen($table))
  58. ? '&amp;table=' . urlencode($table)
  59. : ''
  60. );
  61. } // end if
  62. // Coming from a bookmark dialog
  63. if (isset($_POST['bkm_fields']['bkm_sql_query'])) {
  64. $sql_query = $_POST['bkm_fields']['bkm_sql_query'];
  65. } elseif (isset($_GET['sql_query'])) {
  66. $sql_query = $_GET['sql_query'];
  67. }
  68. // This one is just to fill $db
  69. if (isset($_POST['bkm_fields']['bkm_database'])) {
  70. $db = $_POST['bkm_fields']['bkm_database'];
  71. }
  72. // During grid edit, if we have a relational field, show the dropdown for it.
  73. if (isset($_REQUEST['get_relational_values'])
  74. && $_REQUEST['get_relational_values'] == true
  75. ) {
  76. PMA_getRelationalValues($db, $table);
  77. // script has exited at this point
  78. }
  79. // Just like above, find possible values for enum fields during grid edit.
  80. if (isset($_REQUEST['get_enum_values']) && $_REQUEST['get_enum_values'] == true) {
  81. PMA_getEnumOrSetValues($db, $table, "enum");
  82. // script has exited at this point
  83. }
  84. // Find possible values for set fields during grid edit.
  85. if (isset($_REQUEST['get_set_values']) && $_REQUEST['get_set_values'] == true) {
  86. PMA_getEnumOrSetValues($db, $table, "set");
  87. // script has exited at this point
  88. }
  89. /**
  90. * Check ajax request to set the column order and visibility
  91. */
  92. if (isset($_REQUEST['set_col_prefs']) && $_REQUEST['set_col_prefs'] == true) {
  93. PMA_setColumnOrderOrVisibility($table, $db);
  94. // script has exited at this point
  95. }
  96. // Default to browse if no query set and we have table
  97. // (needed for browsing from DefaultTabTable)
  98. if (empty($sql_query) && strlen($table) && strlen($db)) {
  99. $sql_query = PMA_getDefaultSqlQueryForBrowse($db, $table);
  100. // set $goto to what will be displayed if query returns 0 rows
  101. $goto = '';
  102. } else {
  103. // Now we can check the parameters
  104. PMA_Util::checkParameters(array('sql_query'));
  105. }
  106. /**
  107. * Parse and analyze the query
  108. */
  109. require_once 'libraries/parse_analyze.inc.php';
  110. /**
  111. * Check rights in case of DROP DATABASE
  112. *
  113. * This test may be bypassed if $is_js_confirmed = 1 (already checked with js)
  114. * but since a malicious user may pass this variable by url/form, we don't take
  115. * into account this case.
  116. */
  117. if (PMA_hasNoRightsToDropDatabase(
  118. $analyzed_sql_results, $cfg['AllowUserDropDatabase'], $is_superuser
  119. )) {
  120. PMA_Util::mysqlDie(
  121. __('"DROP DATABASE" statements are disabled.'),
  122. '',
  123. '',
  124. $err_url
  125. );
  126. } // end if
  127. /**
  128. * Need to find the real end of rows?
  129. */
  130. if (isset($find_real_end) && $find_real_end) {
  131. $unlim_num_rows = PMA_findRealEndOfRows($db, $table);
  132. }
  133. /**
  134. * Bookmark add
  135. */
  136. if (isset($_POST['store_bkm'])) {
  137. PMA_addBookmark($cfg['PmaAbsoluteUri'], $goto);
  138. // script has exited at this point
  139. } // end if
  140. /**
  141. * Sets or modifies the $goto variable if required
  142. */
  143. if ($goto == 'sql.php') {
  144. $is_gotofile = false;
  145. $goto = 'sql.php' . PMA_URL_getCommon(
  146. array(
  147. 'db' => $db,
  148. 'table' => $table,
  149. 'sql_query' => $sql_query
  150. )
  151. );
  152. } // end if
  153. PMA_executeQueryAndSendQueryResponse(
  154. $analyzed_sql_results,
  155. $is_gotofile,
  156. $db,
  157. $table,
  158. isset($find_real_end) ? $find_real_end : null,
  159. isset($import_text) ? $import_text : null,
  160. isset($extra_data) ? $extra_data : null,
  161. $is_affected,
  162. isset($message_to_show) ? $message_to_show : null,
  163. isset($disp_mode) ? $disp_mode : null,
  164. isset($message) ? $message : null,
  165. isset($sql_data) ? $sql_data : null,
  166. $goto,
  167. $pmaThemeImage,
  168. isset($disp_query) ? $display_query : null,
  169. isset($disp_message) ? $disp_message : null,
  170. isset($query_type) ? $query_type : null,
  171. $sql_query,
  172. isset($selected) ? $selected : null,
  173. isset($complete_query) ? $complete_query : null
  174. );
  175. ?>