-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsql_query_result.php
353 lines (309 loc) · 10.1 KB
/
sql_query_result.php
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
/*
FusionPBX
Version: MPL 1.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is FusionPBX
The Initial Developer of the Original Code is
Mark J Crane <[email protected]>
Portions created by the Initial Developer are Copyright (C) 2008-2023
the Initial Developer. All Rights Reserved.
Contributor(s):
Mark J Crane <[email protected]>
*/
//includes files
require_once dirname(__DIR__, 2) . "/resources/require.php";
require_once "resources/check_auth.php";
//check permissions
if (permission_exists('sql_query')) {
//access granted
}
else {
echo "access denied";
exit;
}
//show blank iframe initially
if (empty($_REQUEST)) {
echo ' ';
exit;
}
//add multi-lingual support
$language = new text;
$text = $language->get();
//pdo database connection
if (strlen($_REQUEST['id']) > 0) {
require_once "sql_query_pdo.php";
}
//validate the token
$token = new token;
if (!$token->validate('/app/sql_query/sql_query.php')) {
message::add($text['message-invalid_token'],'negative');
echo "invalid token\n";
exit;
}
//get allowed table names
switch ($db_type) {
case 'sqlite': $sql = "select name from sqlite_master where type='table' order by name;"; break;
case 'pgsql': $sql = "select table_name as name from information_schema.tables where table_schema='public' and (table_type='BASE TABLE' or table_type='VIEW') order by table_type, table_name"; break;
case 'mysql': $sql = "show tables"; break;
}
$database = new database;
$rows = $database->select($sql, null, 'all');
if (is_array($rows) && @sizeof($rows) != 0) {
foreach ($rows as $row) {
$tables[] = $row['name'];
}
}
unset($sql, $rows, $row);
//show the content
if (!empty($_REQUEST)) {
//get the sql type, command and name
$sql_type = trim($_REQUEST["sql_type"]);
$sql_cmd = trim($_REQUEST["command"]);
$table_name = trim($_REQUEST["table_name"]);
$header = "<html>\n";
$header .= "<head>\n";
$header .= "<style type='text/css'>\n";
$header .= "\n";
$header .= "body {\n";
$header .= " font-family: arial;\n";
$header .= " font-size: 12px;\n";
$header .= " color: #444;\n";
$header .= "}\n";
$header .= "\n";
$header .= "th {\n";
$header .= " border-top: 1px solid #444;\n";
$header .= " border-bottom: 1px solid #444;\n";
$header .= " color: #fff;\n";
$header .= " font-size: 12px;\n";
$header .= " font-family: arial;\n";
$header .= " font-weight: bold;\n";
$header .= " background-color: #777;\n";
$header .= " padding: 4px 7px;\n";
$header .= " text-align: left;\n";
$header .= "}\n";
$header .= "\n";
$header .= ".row_style0 {\n";
$header .= " background-color: #eee;\n";
$header .= " border-bottom: 1px solid #999;\n";
$header .= " border-left: 1px solid #fff;\n";
$header .= " font-size: 12px;\n";
$header .= " color: #444;\n";
$header .= " text-align: left;\n";
$header .= " padding: 4px 7px;\n";
$header .= " text-align: left;\n";
$header .= " vertical-align: top;\n";
$header .= "}\n";
$header .= "\n";
$header .= ".row_style0 a:link{ color:#444; }\n";
$header .= ".row_style0 a:visited{ color:#444; }\n";
$header .= ".row_style0 a:hover{ color:#444; }\n";
$header .= ".row_style0 a:active{ color:#444; }\n";
$header .= "\n";
$header .= ".row_style1 {\n";
$header .= " border-bottom: 1px solid #999;\n";
$header .= " border-left: 1px solid #eee;\n";
$header .= " background-color: #fff;\n";
$header .= " font-size: 12px;\n";
$header .= " color: #444;\n";
$header .= " text-align: left;\n";
$header .= " padding: 4px 7px;\n";
$header .= " text-align: left;\n";
$header .= " vertical-align: top;\n";
$header .= "}\n";
$header .= "</style>";
$header .= "</head>\n";
$header .= "<body style='margin: 0; padding: 8;'>\n";
$footer = "<body>\n";
$footer .= "<html>\n";
if (empty($sql_type)) {
echo $header;
$c = 0;
$row_style["0"] = "row_style0";
$row_style["1"] = "row_style1";
//determine queries to run and show
if ($sql_cmd != '') { $sql_array = array_filter(explode(";", $sql_cmd)); }
if ($table_name != '' && in_array($table_name, $tables)) { $sql_array[] = "select * from ".$table_name; }
$show_query = (sizeof($sql_array) > 1) ? true : false;
if (is_array($sql_array)) foreach($sql_array as $sql_index => $sql) {
$sql = trim($sql);
if (sizeof($sql_array) > 1 || $show_query) {
if ($sql_index > 0) { echo "<br /><br /><br />"; }
echo "<span style='display: block; font-family: monospace; padding: 8px; color: green; background-color: #eefff0;'>".escape($sql).";</span><br />";
}
//connect to the database and run the sql query
$database = new database;
$result = $database->execute($sql, null, 'all');
$message = $database->message;
//show the number of records in the result
if (!empty($result) && is_array($result)) {
echo "<b>".$text['label-records'].": ".count($result)."</b>";
echo "<br /><br />\n";
}
else {
echo "<b>".$text['label-error']."</b>";
echo "<br /><br />\n";
echo $message['message']."<br />\n";
if (!empty($message['error'])) {
foreach ($message['error'] as $error) {
echo "<pre>".$error."</pre><br /><br />\n";
}
}
}
//show the results from the SQL query
echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
$x = 0;
if (is_array($result[0])) {
echo "<thead>\n";
echo " <tr>\n";
foreach ($result[0] as $key => $value) {
echo "<th>".escape($key)."</th>\n";
$column_array[$x++] = $key;
}
echo " </tr>\n";
echo "</thead>\n";
}
$x = 1;
if (is_array($result)) {
echo "<tbody>\n";
foreach ($result as &$row) {
if ($x++ > 1000) { break; }
echo "<tr>\n";
if (is_array($column_array)) {
foreach ($column_array as $column_index => $column) {
echo "<td class='".$row_style[$c]."' ".(($column_index == 0) ? "style='border-left: none;'" : null).">".escape($row[$column])." </td>\n";
}
}
echo "</tr>\n";
$c = ($c == 0) ? 1 : 0;
}
echo "</tbody>\n";
}
echo "</table>\n";
echo "<br>\n";
unset($result, $column_array);
}
echo $footer;
}
if ($sql_type == "inserts") {
echo $header;
$sql = trim($sql ?? '');
//get the table data
$sql = (strlen($sql_cmd) == 0 && in_array($table_name, $tables)) ? "select * from ".$table_name : $sql_cmd;
if (strlen($sql) > 0) {
$database = new database;
$result = $database->execute($sql);
$message = $database->message;
if ($message['message'] != 'OK' || $message['code'] != 200) {
echo "<b>".$text['label-error']."</b>";
echo "<br /><br />\n";
echo $message['message'].' ['.$message['code']."]<br />\n";
if (is_array($message['error']) && @sizeof($message['error']) != 0) {
foreach ($message['error'] as $error) {
echo "<pre>".$error."</pre><br /><br />\n";
}
}
exit;
}
$x = 0;
if (is_array($result[0])) {
foreach ($result[0] as $key => $value) {
$column_array[$x++] = $key;
}
}
$column_array_count = count($column_array);
if (is_array($result)) {
foreach ($result as $index => &$row) {
echo "<div style='font-family: monospace; border-bottom: 1px solid #ccc; padding-bottom: 8px; ".($index != 0 ? 'padding-top: 8px;' : null)."'>\n";
echo "insert into ".$table_name." (";
if (is_array($column_array)) {
foreach ($column_array as $column) {
if ($column != "menuid" && $column != "menuparentid") {
$columns[] = $column;
}
}
}
if (is_array($columns) && sizeof($columns) > 0) {
echo implode(', ', $columns);
}
echo ") values (";
if (is_array($column_array)) {
foreach ($column_array as $column) {
if ($column != "menuid" && $column != "menuparentid") {
$values[] = $row[$column] != '' ? "'".escape($row[$column])."'" : 'null';
}
}
}
if (is_array($values) && sizeof($values) > 0) {
echo implode(', ', $values);
}
echo ");\n";
echo "</div>\n";
unset($columns, $values);
}
}
}
echo $footer;
}
if ($sql_type == "csv") {
//set the headers
header('Content-type: application/octet-binary');
if (strlen($sql_cmd) > 0) {
header('Content-Disposition: attachment; filename=data.csv');
}
else if (in_array($table_name, $tables)) {
header('Content-Disposition: attachment; filename='.$table_name.'.csv');
}
//get the table data
if (strlen($sql_cmd) > 0) {
$sql = $sql_cmd;
}
else if (in_array($table_name, $tables)) {
$sql = "select * from ".$table_name;
}
if (strlen($sql) > 0) {
$database = new database;
$result = $database->execute($sql);
$message = $database->message;
if ($message['message'] != 'OK' || $message['code'] != 200) {
echo "<b>".$text['label-error']."</b>";
echo "<br /><br />\n";
echo $message['message'].' ['.$message['code']."]<br />\n";
if (is_array($message['error']) && @sizeof($message['error']) != 0) {
foreach ($message['error'] as $error) {
echo "<pre>".$error."</pre><br /><br />\n";
}
}
exit;
}
//build the column array
$x = 0;
if (is_array($result[0])) {
foreach ($result[0] as $key => $value) {
$column_array[$x] = $key;
$x++;
}
}
//column names
echo '"'.implode('","', $column_array).'"'."\r\n";
//column values
if (is_array($result)) {
foreach ($result as &$row) {
$x = 1;
foreach ($column_array as $column) {
echo '"'.$row[$column].'"'.($x++ < count($column_array) ? ',' : null);
}
echo "\n";
}
}
}
}
}
?>