sql_query.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2025
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. James Rose <[email protected]>
  21. */
  22. //includes files
  23. require_once dirname(__DIR__, 2) . "/resources/require.php";
  24. require_once "resources/check_auth.php";
  25. //permissions
  26. if (!permission_exists('sql_query')) {
  27. echo "access denied";
  28. exit;
  29. }
  30. //add multi-lingual support
  31. $language = new text;
  32. $text = $language->get();
  33. //connect to the database
  34. $database = database::new();
  35. //load editor preferences/defaults
  36. $setting_size = $settings->get('editor','font_size','12px');
  37. $setting_theme = $settings->get('editor','theme','cobalt');
  38. $setting_invisibles = $settings->get('editor','invisibles',false);
  39. $setting_indenting = $settings->get('editor','indent_guides',false);
  40. $setting_numbering = $settings->get('editor','line_numbers',true);
  41. //get the html values and set them as variables
  42. $code = trim($_POST["code"] ?? '');
  43. $command = trim($_POST["command"] ?? '');
  44. //set editor moder
  45. $mode = 'sql';
  46. //show the header
  47. require_once "resources/header.php";
  48. $document['title'] = $text['title-sql_query'];
  49. //pdo database connection
  50. require_once "sql_query_pdo.php";
  51. //scripts and styles
  52. ?>
  53. <script language="JavaScript" type="text/javascript">
  54. function submit_check() {
  55. document.getElementById('command').value = editor.getSession().getValue();
  56. if (document.getElementById('mode').value == 'sql') {
  57. $('#frm').prop('target', 'iframe').prop('action', 'sql_query_result.php?code='+ document.getElementById('code').value);
  58. }
  59. else {
  60. if (document.getElementById('command').value == '') {
  61. focus_editor();
  62. return false;
  63. }
  64. $('#frm').prop('target', '').prop('action', '');
  65. }
  66. return true;
  67. }
  68. function toggle_option(opt) {
  69. switch (opt) {
  70. case 'numbering': toggle_option_do('showLineNumbers'); toggle_option_do('fadeFoldWidgets'); break;
  71. case 'invisibles': toggle_option_do('showInvisibles'); break;
  72. case 'indenting': toggle_option_do('displayIndentGuides'); break;
  73. }
  74. focus_editor();
  75. }
  76. function toggle_option_do(opt_name) {
  77. var opt_val = editor.getOption(opt_name);
  78. editor.setOption(opt_name, ((opt_val) ? false : true));
  79. }
  80. function insert_clip(before, after) {
  81. var selected_text = editor.session.getTextRange(editor.getSelectionRange());
  82. editor.insert(before + selected_text + after);
  83. focus_editor();
  84. }
  85. function focus_editor() {
  86. editor.focus();
  87. }
  88. function set_handler(handler) {
  89. switch (handler) {
  90. case 'sql':
  91. document.getElementById('description').innerHTML = "<?php echo $text['description-sql'];?>";
  92. editor.getSession().setMode('ace/mode/sql');
  93. $('#mode option[value=sql]').prop('selected',true);
  94. $('#response').hide();
  95. break;
  96. default:
  97. break;
  98. }
  99. focus_editor();
  100. }
  101. function reset_editor() {
  102. editor.getSession().setValue('');
  103. $('#iframe').prop('src','sql_query_result.php');
  104. focus_editor();
  105. }
  106. </script>
  107. <style>
  108. div#editor {
  109. box-shadow: 0 3px 10px #333;
  110. text-align: left;
  111. width: 100%;
  112. height: calc(100% - 30px);
  113. font-size: 12px;
  114. }
  115. i.ace_control {
  116. cursor: pointer;
  117. margin-right: 5px;
  118. opacity: 0.5;
  119. }
  120. i.ace_control:hover {
  121. opacity: 1.0;
  122. }
  123. </style>
  124. <?php
  125. //create token
  126. $object = new token;
  127. $token = $object->create('/app/sql_query/sql_query.php');
  128. //show the header
  129. echo "<form method='post' name='frm' id='frm' action='sql_query_result.php' style='margin: 0;' target='iframe' onsubmit='return submit_check();'>\n";
  130. echo "<table cellpadding='0' cellspacing='0' border='0' width='100%'>";
  131. echo " <tr>";
  132. echo " <td valign='top' align='left' width='50%'>";
  133. echo " <b>".$text['title-sql_query']."</b>\n";
  134. echo " </td>";
  135. echo " <td valign='top' align='right' nowrap='nowrap'>";
  136. //sql controls
  137. echo " <span class='sql_controls'>";
  138. //echo " ".$text['label-table']."<br />";
  139. echo " <select name='table_name' id='table_name' class='formfld'>\n";
  140. echo " <option value=''></option>\n";
  141. switch ($db_type) {
  142. case 'sqlite': $sql = "select name from sqlite_master where type='table' order by name;"; break;
  143. 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;
  144. case 'mysql': $sql = "show tables"; break;
  145. }
  146. $result = $database->select($sql, null, 'all');
  147. if (is_array($result) && @sizeof($result) != 0) {
  148. foreach ($result as &$row) {
  149. $row = array_values($row);
  150. echo " <option value='".escape($row[0])."'>".escape($row[0])."</option>\n";
  151. }
  152. }
  153. unset($sql, $result, $row);
  154. echo " </select>\n";
  155. //echo " <br /><br />\n";
  156. //echo " ".$text['label-result_type']."<br />";
  157. echo " <select name='sql_type' id='sql_type' class='formfld'>\n";
  158. echo " <option value=''>".$text['option-result_type_view']."</option>\n";
  159. echo " <option value='csv'>".$text['option-result_type_csv']."</option>\n";
  160. echo " <option value='inserts'>".$text['option-result_type_insert']."</option>\n";
  161. echo " </select>\n";
  162. echo " </span>";
  163. echo " <input type='submit' class='btn' style='margin-top: 0px;' title=\"".$text['button-execute']." [Ctrl+Enter]\" value=\" ".$text['button-execute']." \" >"; //onclick=\"$('form#frm').submit();\"
  164. echo " <input type='button' class='btn' style='margin-top: 0px;' title=\"\" value=\" ".$text['button-reset']." \" onclick=\"reset_editor();\">";
  165. echo " </td>\n";
  166. echo " </tr>\n";
  167. echo " <tr>\n";
  168. echo " <td colspan='2'>\n";
  169. echo $text['description-sql_query']."\n";
  170. echo " </td>\n";
  171. echo " </tr>\n";
  172. echo "</table>";
  173. echo "<br>";
  174. //html form
  175. echo "<div class='card'>\n";
  176. echo " <input type='hidden' name='id' value='".escape($_REQUEST['id'] ?? '')."'>\n"; //sql db id
  177. echo " <input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  178. echo " <textarea name='command' id='command' style='display: none;'></textarea>";
  179. echo " <table cellpadding='0' cellspacing='0' border='0' style='width: 100%;'>\n";
  180. echo " <tr>";
  181. echo " <td style='width: 280px;' valign='top' nowrap>";
  182. echo " <table cellpadding='0' cellspacing='0' border='0' width='100%' height='100%'>";
  183. if (permission_exists('edit_view')) {
  184. echo " <tr>";
  185. echo " <td valign='top' height='100%'>";
  186. 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";
  187. echo " </td>";
  188. echo " </tr>";
  189. }
  190. echo " </table>";
  191. echo " </td>";
  192. echo " <td valign='top' style='height: 400px;'>"
  193. ?>
  194. <table cellpadding='0' cellspacing='0' border='0' style='width: 100%;'>
  195. <tr>
  196. <td valign='middle' style='padding: 0 6px;' width='100%'><span id='description'><?php echo !empty($text['description-'.!empty($handler)]); ?></span></td>
  197. <td valign='middle' style='padding: 0;'><img src='resources/images/blank.gif' style='width: 1px; height: 30px; border: none;'></td>
  198. <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>
  199. <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>
  200. <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>
  201. <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>
  202. <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>
  203. <td valign='middle' style='padding-left: 15px;'>
  204. <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();">
  205. <?php
  206. $modes['php'] = 'PHP';
  207. $modes['css'] = 'CSS';
  208. $modes['html'] = 'HTML';
  209. $modes['javascript'] = 'JS';
  210. $modes['json'] = 'JSON';
  211. $modes['ini'] = 'Conf';
  212. $modes['lua'] = 'Lua';
  213. $modes['text'] = 'Text';
  214. $modes['xml'] = 'XML';
  215. $modes['sql'] = 'SQL';
  216. foreach ($modes as $value => $label) {
  217. $selected = $value == $mode ? 'selected' : null;
  218. echo "<option value='".$value."' ".$selected.">".escape($label)."</option>\n";
  219. }
  220. ?>
  221. </select>
  222. </td>
  223. <td valign='middle' style='padding-left: 4px;'>
  224. <select id='size' style='height: 23px;' onchange="document.getElementById('editor').style.fontSize = this.options[this.selectedIndex].value; focus_editor();">
  225. <?php
  226. $sizes = explode(',','9px,10px,11px,12px,14px,16px,18px,20px');
  227. if (!in_array($setting_size, $sizes)) {
  228. echo "<option value='".$setting_size."'>".escape($setting_size)."</option>\n";
  229. echo "<option value='' disabled='disabled'></option>\n";
  230. }
  231. foreach ($sizes as $size) {
  232. $selected = ($size == $setting_size) ? 'selected' : null;
  233. echo "<option value='".$size."' ".$selected.">".escape($size)."</option>\n";
  234. }
  235. ?>
  236. </select>
  237. </td>
  238. <td valign='middle' style='padding-left: 4px; padding-right: 0px;'>
  239. <select id='theme' style='height: 23px;' onchange="editor.setTheme('ace/theme/' + this.options[this.selectedIndex].value); focus_editor();">
  240. <?php
  241. $themes['Light']['chrome']= 'Chrome';
  242. $themes['Light']['clouds']= 'Clouds';
  243. $themes['Light']['crimson_editor']= 'Crimson Editor';
  244. $themes['Light']['dawn']= 'Dawn';
  245. $themes['Light']['dreamweaver']= 'Dreamweaver';
  246. $themes['Light']['eclipse']= 'Eclipse';
  247. $themes['Light']['github']= 'GitHub';
  248. $themes['Light']['iplastic']= 'IPlastic';
  249. $themes['Light']['solarized_light']= 'Solarized Light';
  250. $themes['Light']['textmate']= 'TextMate';
  251. $themes['Light']['tomorrow']= 'Tomorrow';
  252. $themes['Light']['xcode']= 'XCode';
  253. $themes['Light']['kuroir']= 'Kuroir';
  254. $themes['Light']['katzenmilch']= 'KatzenMilch';
  255. $themes['Light']['sqlserver']= 'SQL Server';
  256. $themes['Dark']['ambiance']= 'Ambiance';
  257. $themes['Dark']['chaos']= 'Chaos';
  258. $themes['Dark']['clouds_midnight']= 'Clouds Midnight';
  259. $themes['Dark']['cobalt']= 'Cobalt';
  260. $themes['Dark']['idle_fingers']= 'idle Fingers';
  261. $themes['Dark']['kr_theme']= 'krTheme';
  262. $themes['Dark']['merbivore']= 'Merbivore';
  263. $themes['Dark']['merbivore_soft']= 'Merbivore Soft';
  264. $themes['Dark']['mono_industrial']= 'Mono Industrial';
  265. $themes['Dark']['monokai']= 'Monokai';
  266. $themes['Dark']['pastel_on_dark']= 'Pastel on dark';
  267. $themes['Dark']['solarized_dark']= 'Solarized Dark';
  268. $themes['Dark']['terminal']= 'Terminal';
  269. $themes['Dark']['tomorrow_night']= 'Tomorrow Night';
  270. $themes['Dark']['tomorrow_night_blue']= 'Tomorrow Night Blue';
  271. $themes['Dark']['tomorrow_night_bright']= 'Tomorrow Night Bright';
  272. $themes['Dark']['tomorrow_night_eighties']= 'Tomorrow Night 80s';
  273. $themes['Dark']['twilight']= 'Twilight';
  274. $themes['Dark']['vibrant_ink']= 'Vibrant Ink';
  275. foreach ($themes as $optgroup => $theme) {
  276. echo "<optgroup label='".$optgroup."'>\n";
  277. foreach ($theme as $value => $label) {
  278. $selected = strtolower($label) == strtolower($setting_theme) ? 'selected' : null;
  279. echo "<option value='".$value."' ".$selected.">".escape($label)."</option>\n";
  280. }
  281. echo "</optgroup>\n";
  282. }
  283. ?>
  284. </select>
  285. </td>
  286. </tr>
  287. </table>
  288. <div id='editor' style='resize:vertical; overflow:auto;'><?php echo $command; ?></div>
  289. <?php
  290. echo " </td>";
  291. echo " </tr>\n";
  292. echo " </table>";
  293. echo " </form>";
  294. echo " <br /><br />";
  295. ?>
  296. <script type="text/javascript" src="<?php echo PROJECT_PATH; ?>/resources/ace/ace.js" charset="utf-8"></script>
  297. <script type="text/javascript">
  298. //load ace editor
  299. var editor = ace.edit("editor");
  300. editor.setOptions({
  301. mode: 'ace/mode/<?php echo $mode;?>',
  302. theme: 'ace/theme/'+document.getElementById('theme').options[document.getElementById('theme').selectedIndex].value,
  303. selectionStyle: 'text',
  304. cursorStyle: 'smooth',
  305. showInvisibles: <?php echo $setting_invisibles ? 'true' : 'false';?>,
  306. displayIndentGuides: <?php echo $setting_indenting ? 'true' : 'false';?>,
  307. showLineNumbers: <?php echo $setting_numbering ? 'true' : 'false';?>,
  308. showGutter: true,
  309. scrollPastEnd: true,
  310. fadeFoldWidgets: <?php echo $setting_numbering ? 'true' : 'false';?>,
  311. showPrintMargin: false,
  312. highlightGutterLine: false,
  313. useSoftTabs: false
  314. });
  315. <?php if ($mode == 'php') { ?>
  316. editor.getSession().setMode({path:'ace/mode/php', inline:true});
  317. <?php } ?>
  318. document.getElementById('editor').style.fontSize='<?php echo escape($setting_size);?>';
  319. focus_editor();
  320. //keyboard shortcut to execute command
  321. <?php key_press('ctrl+enter', 'down', 'window', null, null, "$('form#frm').submit();", false); ?>
  322. //remove certain keyboard shortcuts
  323. editor.commands.bindKey("Ctrl-T", null); //disable transpose letters - prefer new browser tab
  324. editor.commands.bindKey("Ctrl-F", null); //disable find - control broken with bootstrap
  325. editor.commands.bindKey("Ctrl-H", null); //disable replace - control broken with bootstrap
  326. </script>
  327. <?php
  328. //sql result
  329. echo " <span id='sql_response'>";
  330. //echo " <b>".$text['label-results']."</b>\n";
  331. //echo " <br /><br />\n";
  332. 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";
  333. echo " </span>";
  334. echo "</div>\n";
  335. //show the footer
  336. require_once "resources/footer.php";