command.php 17 KB

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