sql_query.php 15 KB

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