command.php 17 KB

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