-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathSqlController.php
More file actions
231 lines (198 loc) · 7.8 KB
/
SqlController.php
File metadata and controls
231 lines (198 loc) · 7.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Controllers\Sql;
use PhpMyAdmin\Bookmarks\BookmarkRepository;
use PhpMyAdmin\Config;
use PhpMyAdmin\Config\PageSettings;
use PhpMyAdmin\Controllers\InvocableController;
use PhpMyAdmin\Core;
use PhpMyAdmin\Current;
use PhpMyAdmin\Html\Generator;
use PhpMyAdmin\Http\Response;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\Import\Import;
use PhpMyAdmin\Message;
use PhpMyAdmin\ParseAnalyze;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Routing\Route;
use PhpMyAdmin\Sql;
use PhpMyAdmin\Url;
use PhpMyAdmin\UrlParams;
use function __;
use function is_string;
use function mb_strpos;
use function str_contains;
use function urlencode;
#[Route('/sql', ['GET', 'POST'])]
readonly class SqlController implements InvocableController
{
public function __construct(
private ResponseRenderer $response,
private Sql $sql,
private PageSettings $pageSettings,
private BookmarkRepository $bookmarkRepository,
private Config $config,
) {
}
public function __invoke(ServerRequest $request): Response
{
$this->pageSettings->init('Browse');
$this->response->addHTML($this->pageSettings->getErrorHTML());
$this->response->addHTML($this->pageSettings->getHTML());
$this->response->addScriptFiles([
'vendor/jquery/jquery.uitablefilter.js',
'table/change.js',
'gis_data_editor.js',
'multi_column_sort.js',
]);
/**
* Defines the url to return to in case of error in a sql statement
*/
$isGotofile = true;
if (UrlParams::$goto === '') {
if (Current::$table === '') {
UrlParams::$goto = Url::getFromRoute($this->config->config->DefaultTabDatabase);
} else {
UrlParams::$goto = Url::getFromRoute($this->config->config->DefaultTabTable);
}
}
$errorUrl = UrlParams::$back !== '' ? UrlParams::$back : UrlParams::$goto;
$errorUrl .= Url::getCommon(
['db' => Current::$database],
! str_contains($errorUrl, '?') ? '?' : '&',
);
if (
(mb_strpos(' ' . $errorUrl, 'db_') !== 1
|| ! str_contains($errorUrl, '?route=/database/'))
&& Current::$table !== ''
) {
$errorUrl .= '&table=' . urlencode(Current::$table);
}
/** @var array<string>|null $bkmFields */
$bkmFields = $request->getParsedBodyParam('bkm_fields');
$sqlQuery = $request->getParsedBodyParamAsStringOrNull('sql_query');
// Coming from a bookmark dialog
if ($bkmFields !== null && $bkmFields['bkm_sql_query'] != null) {
Current::$sqlQuery = $bkmFields['bkm_sql_query'];
} elseif ($sqlQuery !== null) {
Current::$sqlQuery = $sqlQuery;
} elseif ($request->hasQueryParam('sql_query') && $request->hasQueryParam('sql_signature')) {
$sqlQuery = $request->getQueryParam('sql_query');
if (
is_string($sqlQuery)
&& Core::checkSqlQuerySignature($sqlQuery, $request->getQueryParam('sql_signature'))
) {
Current::$sqlQuery = $sqlQuery;
}
}
// This one is just to fill $db
if ($bkmFields !== null && $bkmFields['bkm_database'] != null) {
Current::$database = $bkmFields['bkm_database'];
}
// Default to browse if no query set and we have table
// (needed for browsing from DefaultTabTable)
if (Current::$sqlQuery === '' && Current::$table !== '' && Current::$database !== '') {
Current::$sqlQuery = $this->sql->getDefaultSqlQueryForBrowse(Current::$database, Current::$table);
// set $goto to what will be displayed if query returns 0 rows
UrlParams::$goto = '';
} elseif (Current::$sqlQuery === '') {
return $this->response->missingParameterError('sql_query');
}
/**
* Parse and analyze the query
*/
[$statementInfo, Current::$database, $tableFromSql] = ParseAnalyze::sqlQuery(
Current::$sqlQuery,
Current::$database,
$request->isAjax(),
);
if (Current::$table != $tableFromSql && $tableFromSql !== '') {
Current::$table = $tableFromSql;
}
/**
* Check rights in case of DROP DATABASE
*
* This test may be bypassed if $is_js_confirmed = 1 (already checked with js)
* but since a malicious user may pass this variable by url/form, we don't take
* into account this case.
*/
if ($this->sql->hasNoRightsToDropDatabase($statementInfo)) {
$errorMessage = Generator::mysqlDie(
__('"DROP DATABASE" statements are disabled.'),
'',
false,
);
if ($this->response->isAjax()) {
$this->response->setRequestStatus(false);
$this->response->addJSON('message', $errorMessage);
return $this->response->response();
}
$this->response->addHTML($errorMessage . Generator::getBackUrlHtml($errorUrl));
return $this->response->response();
}
/**
* Bookmark add
*/
$storeBkm = $request->hasBodyParam('store_bkm');
$bkmAllUsers = $request->getParsedBodyParam('bkm_all_users'); // Should this be hasBodyParam?
if ($storeBkm && $bkmFields !== null) {
return $this->addBookmark(UrlParams::$goto, $bkmFields, (bool) $bkmAllUsers);
}
/**
* Sets or modifies the $goto variable if required
*/
if (UrlParams::$goto === Url::getFromRoute('/sql')) {
$isGotofile = false;
UrlParams::$goto = Url::getFromRoute('/sql', [
'db' => Current::$database,
'table' => Current::$table,
'sql_query' => Current::$sqlQuery,
]);
}
Current::$messageToShow = $request->getParsedBodyParamAsString('message_to_show', '');
$this->response->addHTML($this->sql->executeQueryAndSendQueryResponse(
$request,
$statementInfo,
$isGotofile,
Current::$database,
Current::$table,
Import::$importText,
Current::$messageToShow,
UrlParams::$goto,
Current::$dispQuery !== null ? Current::$displayQuery : null,
Current::$displayMessage ?? '',
Current::$sqlQuery,
Current::$completeQuery ?? Current::$sqlQuery,
));
return $this->response->response();
}
/** @param array<string> $bkmFields */
private function addBookmark(string $goto, array $bkmFields, bool $bkmAllUsers): Response
{
$bookmark = $this->bookmarkRepository->createBookmark(
$bkmFields['bkm_sql_query'],
$bkmFields['bkm_label'],
$bkmFields['bkm_user'],
$bkmFields['bkm_database'],
$bkmAllUsers,
);
$result = null;
if ($bookmark !== false) {
$result = $bookmark->save();
}
if (! $this->response->isAjax()) {
$this->response->redirect('./' . $goto . '&label=' . $bkmFields['bkm_label']);
return $this->response->response();
}
if ($result) {
$msg = Message::success(__('Bookmark %s has been created.'));
$msg->addParam($bkmFields['bkm_label']);
$this->response->addJSON('message', $msg);
return $this->response->response();
}
$msg = Message::error(__('Bookmark not created!'));
$this->response->setRequestStatus(false);
$this->response->addJSON('message', $msg);
return $this->response->response();
}
}