exec.php 15 KB

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