Translated using Weblate (German)
[phpmyadmin.git] / sql.php
blob15f4152bc3b0409011a8b2fd8db5dd479794ff49
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * SQL executor
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 declare(strict_types=1);
12 use PhpMyAdmin\CheckUserPrivileges;
13 use PhpMyAdmin\Config\PageSettings;
14 use PhpMyAdmin\DatabaseInterface;
15 use PhpMyAdmin\ParseAnalyze;
16 use PhpMyAdmin\Response;
17 use PhpMyAdmin\Sql;
18 use PhpMyAdmin\Url;
19 use PhpMyAdmin\Util;
21 if (! defined('ROOT_PATH')) {
22 define('ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR);
25 global $cfg, $containerBuilder, $pmaThemeImage;
27 require_once ROOT_PATH . 'libraries/common.inc.php';
29 /** @var Response $response */
30 $response = $containerBuilder->get(Response::class);
32 /** @var DatabaseInterface $dbi */
33 $dbi = $containerBuilder->get(DatabaseInterface::class);
35 /** @var CheckUserPrivileges $checkUserPrivileges */
36 $checkUserPrivileges = $containerBuilder->get('check_user_privileges');
37 $checkUserPrivileges->getPrivileges();
39 PageSettings::showGroup('Browse');
41 $header = $response->getHeader();
42 $scripts = $header->getScripts();
43 $scripts->addFile('vendor/jquery/jquery.uitablefilter.js');
44 $scripts->addFile('table/change.js');
45 $scripts->addFile('indexes.js');
46 $scripts->addFile('gis_data_editor.js');
47 $scripts->addFile('multi_column_sort.js');
49 /** @var Sql $sql */
50 $sql = $containerBuilder->get('sql');
52 /**
53 * Set ajax_reload in the response if it was already set
55 if (isset($ajax_reload) && $ajax_reload['reload'] === true) {
56 $response->addJSON('ajax_reload', $ajax_reload);
59 /**
60 * Defines the url to return to in case of error in a sql statement
62 $is_gotofile = true;
63 if (empty($goto)) {
64 if (empty($table)) {
65 $goto = Util::getScriptNameForOption(
66 $cfg['DefaultTabDatabase'],
67 'database'
69 } else {
70 $goto = Util::getScriptNameForOption(
71 $cfg['DefaultTabTable'],
72 'table'
75 } // end if
77 if (! isset($err_url)) {
78 $err_url = (! empty($back) ? $back : $goto)
79 . '?' . Url::getCommon(['db' => $GLOBALS['db']])
80 . ((mb_strpos(' ' . $goto, 'db_') != 1
81 && strlen($table) > 0)
82 ? '&amp;table=' . urlencode($table)
83 : ''
85 } // end if
87 // Coming from a bookmark dialog
88 if (isset($_POST['bkm_fields']['bkm_sql_query'])) {
89 $sql_query = $_POST['bkm_fields']['bkm_sql_query'];
90 } elseif (isset($_POST['sql_query'])) {
91 $sql_query = $_POST['sql_query'];
94 // This one is just to fill $db
95 if (isset($_POST['bkm_fields']['bkm_database'])) {
96 $db = $_POST['bkm_fields']['bkm_database'];
99 // During grid edit, if we have a relational field, show the dropdown for it.
100 if (isset($_POST['get_relational_values'])
101 && $_POST['get_relational_values'] == true
103 $sql->getRelationalValues($db, $table);
104 // script has exited at this point
107 // Just like above, find possible values for enum fields during grid edit.
108 if (isset($_POST['get_enum_values']) && $_POST['get_enum_values'] == true) {
109 $sql->getEnumOrSetValues($db, $table, "enum");
110 // script has exited at this point
114 // Find possible values for set fields during grid edit.
115 if (isset($_POST['get_set_values']) && $_POST['get_set_values'] == true) {
116 $sql->getEnumOrSetValues($db, $table, "set");
117 // script has exited at this point
120 if (isset($_GET['get_default_fk_check_value'])
121 && $_GET['get_default_fk_check_value'] == true
123 $response = Response::getInstance();
124 $response->addJSON(
125 'default_fk_check_value',
126 Util::isForeignKeyCheck()
128 exit;
132 * Check ajax request to set the column order and visibility
134 if (isset($_POST['set_col_prefs']) && $_POST['set_col_prefs'] == true) {
135 $sql->setColumnOrderOrVisibility($table, $db);
136 // script has exited at this point
139 // Default to browse if no query set and we have table
140 // (needed for browsing from DefaultTabTable)
141 if (empty($sql_query) && strlen($table) > 0 && strlen($db) > 0) {
142 $sql_query = $sql->getDefaultSqlQueryForBrowse($db, $table);
144 // set $goto to what will be displayed if query returns 0 rows
145 $goto = '';
146 } else {
147 // Now we can check the parameters
148 Util::checkParameters(['sql_query']);
152 * Parse and analyze the query
154 list(
155 $analyzed_sql_results,
156 $db,
157 $table_from_sql
158 ) = ParseAnalyze::sqlQuery($sql_query, $db);
159 // @todo: possibly refactor
160 extract($analyzed_sql_results);
162 if ($table != $table_from_sql && ! empty($table_from_sql)) {
163 $table = $table_from_sql;
168 * Check rights in case of DROP DATABASE
170 * This test may be bypassed if $is_js_confirmed = 1 (already checked with js)
171 * but since a malicious user may pass this variable by url/form, we don't take
172 * into account this case.
174 if ($sql->hasNoRightsToDropDatabase(
175 $analyzed_sql_results,
176 $cfg['AllowUserDropDatabase'],
177 $dbi->isSuperuser()
178 )) {
179 Util::mysqlDie(
180 __('"DROP DATABASE" statements are disabled.'),
182 false,
183 $err_url
185 } // end if
188 * Need to find the real end of rows?
190 if (isset($find_real_end) && $find_real_end) {
191 $unlim_num_rows = $sql->findRealEndOfRows($db, $table);
196 * Bookmark add
198 if (isset($_POST['store_bkm'])) {
199 $sql->addBookmark($goto);
200 // script has exited at this point
201 } // end if
205 * Sets or modifies the $goto variable if required
207 if ($goto == 'sql.php') {
208 $is_gotofile = false;
209 $goto = 'sql.php' . Url::getCommon(
211 'db' => $db,
212 'table' => $table,
213 'sql_query' => $sql_query,
216 } // end if
218 $sql->executeQueryAndSendQueryResponse(
219 $analyzed_sql_results, // analyzed_sql_results
220 $is_gotofile, // is_gotofile
221 $db, // db
222 $table, // table
223 isset($find_real_end) ? $find_real_end : null, // find_real_end
224 isset($import_text) ? $import_text : null, // sql_query_for_bookmark
225 isset($extra_data) ? $extra_data : null, // extra_data
226 isset($message_to_show) ? $message_to_show : null, // message_to_show
227 isset($message) ? $message : null, // message
228 isset($sql_data) ? $sql_data : null, // sql_data
229 $goto, // goto
230 $pmaThemeImage, // pmaThemeImage
231 isset($disp_query) ? $display_query : null, // disp_query
232 isset($disp_message) ? $disp_message : null, // disp_message
233 isset($query_type) ? $query_type : null, // query_type
234 $sql_query, // sql_query
235 isset($selected) ? $selected : null, // selectedTables
236 isset($complete_query) ? $complete_query : null // complete_query