sql_query.php 15 KB

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