-
Notifications
You must be signed in to change notification settings - Fork 5
/
sql_query.php
378 lines (339 loc) · 14.8 KB
/
sql_query.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?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-2024
the Initial Developer. All Rights Reserved.
Contributor(s):
Mark J Crane <[email protected]>
James Rose <[email protected]>
*/
//includes files
require_once dirname(__DIR__, 2) . "/resources/require.php";
require_once "resources/check_auth.php";
//permissions
if (permission_exists('sql_query')) {
//access granted
}
else {
echo "access denied";
exit;
}
//add multi-lingual support
$language = new text;
$text = $language->get();
//load editor preferences/defaults
$setting_size = (!empty($_SESSION["editor"]["font_size"]["text"])) ? $_SESSION["editor"]["font_size"]["text"] : '12px';
$setting_theme = (!empty($_SESSION["editor"]["theme"]["text"])) ? $_SESSION["editor"]["theme"]["text"] : 'cobalt';
$setting_invisibles = (!empty($_SESSION["editor"]["invisibles"]["boolean"])) ? $_SESSION["editor"]["invisibles"]["boolean"] : 'false';
$setting_indenting = (!empty($_SESSION["editor"]["indent_guides"]["boolean"])) ? $_SESSION["editor"]["indent_guides"]["boolean"] : 'false';
$setting_numbering = (!empty($_SESSION["editor"]["line_numbers"]["boolean"])) ? $_SESSION["editor"]["line_numbers"]["boolean"] : 'true';
//get the html values and set them as variables
$code = trim($_POST["code"] ?? '');
$command = trim($_POST["command"] ?? '');
//set editor moder
$mode = 'sql';
//show the header
require_once "resources/header.php";
$document['title'] = $text['title-sql_query'];
//pdo database connection
require_once "sql_query_pdo.php";
//scripts and styles
?>
<script language="JavaScript" type="text/javascript">
function submit_check() {
document.getElementById('command').value = editor.getSession().getValue();
if (document.getElementById('mode').value == 'sql') {
$('#frm').prop('target', 'iframe').prop('action', 'sql_query_result.php?code='+ document.getElementById('code').value);
}
else {
if (document.getElementById('command').value == '') {
focus_editor();
return false;
}
$('#frm').prop('target', '').prop('action', '');
}
return true;
}
function toggle_option(opt) {
switch (opt) {
case 'numbering': toggle_option_do('showLineNumbers'); toggle_option_do('fadeFoldWidgets'); break;
case 'invisibles': toggle_option_do('showInvisibles'); break;
case 'indenting': toggle_option_do('displayIndentGuides'); break;
}
focus_editor();
}
function toggle_option_do(opt_name) {
var opt_val = editor.getOption(opt_name);
editor.setOption(opt_name, ((opt_val) ? false : true));
}
function insert_clip(before, after) {
var selected_text = editor.session.getTextRange(editor.getSelectionRange());
editor.insert(before + selected_text + after);
focus_editor();
}
function focus_editor() {
editor.focus();
}
function set_handler(handler) {
switch (handler) {
case 'sql':
document.getElementById('description').innerHTML = "<?php echo $text['description-sql'];?>";
editor.getSession().setMode('ace/mode/sql');
$('#mode option[value=sql]').prop('selected',true);
$('#response').hide();
break;
default:
break;
}
focus_editor();
}
function reset_editor() {
editor.getSession().setValue('');
$('#iframe').prop('src','sql_query_result.php');
focus_editor();
}
</script>
<style>
div#editor {
box-shadow: 0 3px 10px #333;
text-align: left;
width: 100%;
height: calc(100% - 30px);
font-size: 12px;
}
i.ace_control {
cursor: pointer;
margin-right: 5px;
opacity: 0.5;
}
i.ace_control:hover {
opacity: 1.0;
}
</style>
<?php
//create token
$object = new token;
$token = $object->create('/app/sql_query/sql_query.php');
//show the header
echo "<form method='post' name='frm' id='frm' action='sql_query_result.php' style='margin: 0;' target='iframe' onsubmit='return submit_check();'>\n";
echo "<table cellpadding='0' cellspacing='0' border='0' width='100%'>";
echo " <tr>";
echo " <td valign='top' align='left' width='50%'>";
echo " <b>".$text['title-sql_query']."</b>\n";
echo " </td>";
echo " <td valign='top' align='right' nowrap='nowrap'>";
//sql controls
echo " <span class='sql_controls'>";
//echo " ".$text['label-table']."<br />";
echo " <select name='table_name' id='table_name' class='formfld'>\n";
echo " <option value=''></option>\n";
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;
$result = $database->select($sql, null, 'all');
if (is_array($result) && @sizeof($result) != 0) {
foreach ($result as &$row) {
$row = array_values($row);
echo " <option value='".escape($row[0])."'>".escape($row[0])."</option>\n";
}
}
unset($sql, $result, $row);
echo " </select>\n";
//echo " <br /><br />\n";
//echo " ".$text['label-result_type']."<br />";
echo " <select name='sql_type' id='sql_type' class='formfld'>\n";
echo " <option value=''>".$text['option-result_type_view']."</option>\n";
echo " <option value='csv'>".$text['option-result_type_csv']."</option>\n";
echo " <option value='inserts'>".$text['option-result_type_insert']."</option>\n";
echo " </select>\n";
echo " </span>";
echo " <input type='submit' class='btn' style='margin-top: 0px;' title=\"".$text['button-execute']." [Ctrl+Enter]\" value=\" ".$text['button-execute']." \" >"; //onclick=\"$('form#frm').submit();\"
echo " <input type='button' class='btn' style='margin-top: 0px;' title=\"\" value=\" ".$text['button-reset']." \" onclick=\"reset_editor();\">";
echo " </td>\n";
echo " </tr>\n";
echo " <tr>\n";
echo " <td colspan='2'>\n";
echo $text['description-sql_query']."\n";
echo " </td>\n";
echo " </tr>\n";
echo "</table>";
echo "<br>";
//html form
echo "<div class='card'>\n";
echo " <input type='hidden' name='id' value='".escape($_REQUEST['id'] ?? '')."'>\n"; //sql db id
echo " <input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
echo " <textarea name='command' id='command' style='display: none;'></textarea>";
echo " <table cellpadding='0' cellspacing='0' border='0' style='width: 100%;'>\n";
echo " <tr>";
echo " <td style='width: 280px;' valign='top' nowrap>";
echo " <table cellpadding='0' cellspacing='0' border='0' width='100%' height='100%'>";
if (permission_exists('edit_view')) {
echo " <tr>";
echo " <td valign='top' height='100%'>";
echo " <iframe id='clip_list' src='clip_list.php' style='border: none; border-top: 1px solid #ccc; border-bottom: 1px solid #ccc; height: calc(100% - 2px); width: calc(100% - 15px);'></iframe>\n";
echo " </td>";
echo " </tr>";
}
echo " </table>";
echo " </td>";
echo " <td valign='top' style='height: 400px;'>"
?>
<table cellpadding='0' cellspacing='0' border='0' style='width: 100%;'>
<tr>
<td valign='middle' style='padding: 0 6px;' width='100%'><span id='description'><?php echo !empty($text['description-'.!empty($handler)]); ?></span></td>
<td valign='middle' style='padding: 0;'><img src='resources/images/blank.gif' style='width: 1px; height: 30px; border: none;'></td>
<td valign='middle' style='padding-left: 6px;'><i class='fas fa-list-ul fa-lg ace_control' title="<?php echo $text['label-toggle_line_numbers']; ?>" onclick="toggle_option('numbering');"></i></td>
<td valign='middle' style='padding-left: 6px;'><i class='fas fa-eye-slash fa-lg ace_control' title="<?php echo $text['label-toggle_invisibles']; ?>" onclick="toggle_option('invisibles');"></i></td>
<td valign='middle' style='padding-left: 6px;'><i class='fas fa-indent fa-lg ace_control' title="<?php echo $text['label-toggle_indent_guides']; ?>" onclick="toggle_option('indenting');"></i></td>
<td valign='middle' style='padding-left: 6px;'><i class='fas fa-search fa-lg ace_control' title="<?php echo $text['label-find_replace']; ?>" onclick="editor.execCommand('replace');"></i></td>
<td valign='middle' style='padding-left: 6px;'><i class='fas fa-chevron-down fa-lg ace_control' title="<?php echo $text['label-go_to_line']; ?>" onclick="editor.execCommand('gotoline');"></i></td>
<td valign='middle' style='padding-left: 15px;'>
<select id='mode' style='height: 23px;' onchange="editor.getSession().setMode((this.options[this.selectedIndex].value == 'php') ? {path:'ace/mode/php', inline:true} : 'ace/mode/' + this.options[this.selectedIndex].value); focus_editor();">
<?php
$modes['php'] = 'PHP';
$modes['css'] = 'CSS';
$modes['html'] = 'HTML';
$modes['javascript'] = 'JS';
$modes['json'] = 'JSON';
$modes['ini'] = 'Conf';
$modes['lua'] = 'Lua';
$modes['text'] = 'Text';
$modes['xml'] = 'XML';
$modes['sql'] = 'SQL';
foreach ($modes as $value => $label) {
$selected = $value == $mode ? 'selected' : null;
echo "<option value='".$value."' ".$selected.">".escape($label)."</option>\n";
}
?>
</select>
</td>
<td valign='middle' style='padding-left: 4px;'>
<select id='size' style='height: 23px;' onchange="document.getElementById('editor').style.fontSize = this.options[this.selectedIndex].value; focus_editor();">
<?php
$sizes = explode(',','9px,10px,11px,12px,14px,16px,18px,20px');
if (!in_array($setting_size, $sizes)) {
echo "<option value='".$setting_size."'>".escape($setting_size)."</option>\n";
echo "<option value='' disabled='disabled'></option>\n";
}
foreach ($sizes as $size) {
$selected = ($size == $setting_size) ? 'selected' : null;
echo "<option value='".$size."' ".$selected.">".escape($size)."</option>\n";
}
?>
</select>
</td>
<td valign='middle' style='padding-left: 4px; padding-right: 0px;'>
<select id='theme' style='height: 23px;' onchange="editor.setTheme('ace/theme/' + this.options[this.selectedIndex].value); focus_editor();">
<?php
$themes['Light']['chrome']= 'Chrome';
$themes['Light']['clouds']= 'Clouds';
$themes['Light']['crimson_editor']= 'Crimson Editor';
$themes['Light']['dawn']= 'Dawn';
$themes['Light']['dreamweaver']= 'Dreamweaver';
$themes['Light']['eclipse']= 'Eclipse';
$themes['Light']['github']= 'GitHub';
$themes['Light']['iplastic']= 'IPlastic';
$themes['Light']['solarized_light']= 'Solarized Light';
$themes['Light']['textmate']= 'TextMate';
$themes['Light']['tomorrow']= 'Tomorrow';
$themes['Light']['xcode']= 'XCode';
$themes['Light']['kuroir']= 'Kuroir';
$themes['Light']['katzenmilch']= 'KatzenMilch';
$themes['Light']['sqlserver']= 'SQL Server';
$themes['Dark']['ambiance']= 'Ambiance';
$themes['Dark']['chaos']= 'Chaos';
$themes['Dark']['clouds_midnight']= 'Clouds Midnight';
$themes['Dark']['cobalt']= 'Cobalt';
$themes['Dark']['idle_fingers']= 'idle Fingers';
$themes['Dark']['kr_theme']= 'krTheme';
$themes['Dark']['merbivore']= 'Merbivore';
$themes['Dark']['merbivore_soft']= 'Merbivore Soft';
$themes['Dark']['mono_industrial']= 'Mono Industrial';
$themes['Dark']['monokai']= 'Monokai';
$themes['Dark']['pastel_on_dark']= 'Pastel on dark';
$themes['Dark']['solarized_dark']= 'Solarized Dark';
$themes['Dark']['terminal']= 'Terminal';
$themes['Dark']['tomorrow_night']= 'Tomorrow Night';
$themes['Dark']['tomorrow_night_blue']= 'Tomorrow Night Blue';
$themes['Dark']['tomorrow_night_bright']= 'Tomorrow Night Bright';
$themes['Dark']['tomorrow_night_eighties']= 'Tomorrow Night 80s';
$themes['Dark']['twilight']= 'Twilight';
$themes['Dark']['vibrant_ink']= 'Vibrant Ink';
foreach ($themes as $optgroup => $theme) {
echo "<optgroup label='".$optgroup."'>\n";
foreach ($theme as $value => $label) {
$selected = strtolower($label) == strtolower($setting_theme) ? 'selected' : null;
echo "<option value='".$value."' ".$selected.">".escape($label)."</option>\n";
}
echo "</optgroup>\n";
}
?>
</select>
</td>
</tr>
</table>
<div id='editor' style='resize:vertical; overflow:auto;'><?php echo $command; ?></div>
<?php
echo " </td>";
echo " </tr>\n";
echo " </table>";
echo " </form>";
echo " <br /><br />";
?>
<script type="text/javascript" src="<?php echo PROJECT_PATH; ?>/resources/ace/ace.js" charset="utf-8"></script>
<script type="text/javascript">
//load ace editor
var editor = ace.edit("editor");
editor.setOptions({
mode: 'ace/mode/<?php echo $mode;?>',
theme: 'ace/theme/'+document.getElementById('theme').options[document.getElementById('theme').selectedIndex].value,
selectionStyle: 'text',
cursorStyle: 'smooth',
showInvisibles: <?php echo $setting_invisibles;?>,
displayIndentGuides: <?php echo $setting_indenting;?>,
showLineNumbers: <?php echo $setting_numbering;?>,
showGutter: true,
scrollPastEnd: true,
fadeFoldWidgets: <?php echo $setting_numbering;?>,
showPrintMargin: false,
highlightGutterLine: false,
useSoftTabs: false
});
<?php if ($mode == 'php') { ?>
editor.getSession().setMode({path:'ace/mode/php', inline:true});
<?php } ?>
document.getElementById('editor').style.fontSize='<?php echo escape($setting_size);?>';
focus_editor();
//keyboard shortcut to execute command
<?php key_press('ctrl+enter', 'down', 'window', null, null, "$('form#frm').submit();", false); ?>
//remove certain keyboard shortcuts
editor.commands.bindKey("Ctrl-T", null); //disable transpose letters - prefer new browser tab
editor.commands.bindKey("Ctrl-F", null); //disable find - control broken with bootstrap
editor.commands.bindKey("Ctrl-H", null); //disable replace - control broken with bootstrap
</script>
<?php
//sql result
echo " <span id='sql_response'>";
//echo " <b>".$text['label-results']."</b>\n";
//echo " <br /><br />\n";
echo " <iframe name='iframe' id='iframe' style='width: calc(100% - 3px); height: 500px; background-color: #fff; border: 0px solid #c0c0c0;' src='sql_query_result.php'></iframe>\n";
echo " </span>";
echo "</div>\n";
//show the footer
require_once "resources/footer.php";
?>