From e5c5e7dc5e45ec402c5f6ab75f76619c6218b61a Mon Sep 17 00:00:00 2001 From: Matt Pass Date: Wed, 21 Nov 2012 17:50:30 +0000 Subject: [PATCH] Terminal plugin added Will die if in demoMode or not loggedIn Code needs revising to enable extra login, set sudo etc if necessary? --- lib/config.php | 3 +- plugins/terminal/icon.gif | Bin 0 -> 164 bytes plugins/terminal/index.php | 201 ++++++++++++++++++++++++++++++++++ plugins/terminal/terminal.css | 28 +++++ 4 files changed, 231 insertions(+), 1 deletion(-) create mode 100644 plugins/terminal/icon.gif create mode 100644 plugins/terminal/index.php create mode 100644 plugins/terminal/terminal.css diff --git a/lib/config.php b/lib/config.php index 9cc6da13..7c71b022 100644 --- a/lib/config.php +++ b/lib/config.php @@ -15,7 +15,8 @@ array("Adminer","plugins/adminer/icon.png","margin-top: 3px","plugins/adminer/adminer-3.4.0-mysql-en.php","_blank",""), array("Zip It!","plugins/zip-it/icon.png","margin-top: 3px; margin-left: 3px","plugins/zip-it/?zip=|&exclude=*.doc*.gif*.jpg*.jpeg*.pdf*.png*.swf*.xml*.zip","fileControl:Zipping Files","30"), array("ICErepo","plugins/ice-repo/icon.png","margin-top: 3px","plugins/ice-repo","_blank",""), - array("Dochub","plugins/dochub/icon.png","margin-top: 3px","http://dochub.io","_blank","") + array("Dochub","plugins/dochub/icon.png","margin-top: 3px","http://dochub.io","_blank",""), + array("Terminal","plugins/terminal/icon.gif","margin-top: 3px","plugins/terminal","_blank","") ), "theme" => "default", "tabWidth" => 4, diff --git a/plugins/terminal/icon.gif b/plugins/terminal/icon.gif new file mode 100644 index 0000000000000000000000000000000000000000..10416b4bcd63296d8de7a79a4b29ae648cb5afbc GIT binary patch literal 164 zcmZ?wbhEHblwpuz*v!DdP|m=R#=xM&z_6QvA(VkZkb%L4f#EI#gE0d`2Lr<@28MSG z3^N%R{sWbQfZ|UUFs%b3L1r+p1YS7lxq7d~>$AK6CusDgL`<1h$e{j8!uO1V-ohhi z9dO$3nz4g1z$_*qgh3{$yCtFJfGpEmK_7DwpY{hEEKCl}Ol}TfIFPq}`r{on IGK>t?06F+C%>V!Z literal 0 HcmV?d00001 diff --git a/plugins/terminal/index.php b/plugins/terminal/index.php new file mode 100644 index 00000000..f2727fb7 --- /dev/null +++ b/plugins/terminal/index.php @@ -0,0 +1,201 @@ + $_SESSION['pass']); +$aliases = array('la' => 'ls -la', + 'll' => 'ls -lvhF', + 'dir' => 'ls' ); + +class phpTerm { + function phpTerm() {} // constructor + + function formatPrompt() { + $user=shell_exec("whoami"); + $host=explode(".", shell_exec("uname -n")); + $_SESSION['prompt'] = "".rtrim($user).""."@"."".rtrim($host[0]).""; + } + + function checkPassword($passwd) { + if( !isset($_SERVER['PHP_AUTH_USER'])|| + !isset($_SERVER['PHP_AUTH_PW']) || + !isset($passwd[$_SERVER['PHP_AUTH_USER']]) || + $passwd[$_SERVER['PHP_AUTH_USER']] != $_SERVER['PHP_AUTH_PW']) { + return false; + } else { + return true; + } + } + + function logout() { + header('WWW-Authenticate: Basic realm="Terminal"'); + header('HTTP/1.0 401 Unauthorized'); + exit(); + } + + function initVars() { + if (empty($_SESSION['cwd']) || @!empty($_GET['reset'])) { + $_SESSION['cwd'] = getcwd(); + $_SESSION['history'] = array(); + $_SESSION['output'] = ''; + $_REQUEST['command'] =''; + } + } + + function buildCommandHistory() { + if(!empty($_REQUEST['command'])) { + if(get_magic_quotes_gpc()) { + $_REQUEST['command'] = stripslashes($_REQUEST['command']); + } + + // drop old commands from list if exists + if (($i = array_search($_REQUEST['command'], $_SESSION['history'])) !== false) { + unset($_SESSION['history'][$i]); + } + array_unshift($_SESSION['history'], $_REQUEST['command']); + + // append commmand */ + $_SESSION['output'] .= "{$_SESSION['prompt']}".":>"."{$_REQUEST['command']}"."\n"; + } + } + + function buildJavaHistory() { + // build command history for use in the JavaScript + if (empty($_SESSION['history'])) { + $_SESSION['js_command_hist'] = '""'; + } else { + $escaped = array_map('addslashes', $_SESSION['history']); + $_SESSION['js_command_hist'] = '"", "' . implode('", "', $escaped) . '"'; + } + } + + function outputHandle($aliases) { + if (preg_match('/^[[:blank:]]*cd[[:blank:]]*$/', @$_REQUEST['command'])) + { + $_SESSION['cwd'] = getcwd(); //dirname(__FILE__); + } + elseif(preg_match('/^[[:blank:]]*cd[[:blank:]]+([^;]+)$/', @$_REQUEST['command'], $regs)) { + // The current command is 'cd', which we have to handle as an internal shell command. + // absolute/relative path ?" + ($regs[1][0] == '/') ? $new_dir = $regs[1] : $new_dir = $_SESSION['cwd'] . '/' . $regs[1]; + + // cosmetics + while (strpos($new_dir, '/./') !== false) { + $new_dir = str_replace('/./', '/', $new_dir); + } + while (strpos($new_dir, '//') !== false) { + $new_dir = str_replace('//', '/', $new_dir); + } + while (preg_match('|/\.\.(?!\.)|', $new_dir)) { + $new_dir = preg_replace('|/?[^/]+/\.\.(?!\.)|', '', $new_dir); + } + + if(empty($new_dir)): $new_dir = "/"; endif; + + (@chdir($new_dir)) ? $_SESSION['cwd'] = $new_dir : $_SESSION['output'] .= "could not change to: $new_dir\n"; + } else { + /* The command is not a 'cd' command, so we execute it after + changing the directory and save the output. */ + chdir($_SESSION['cwd']); + + /* Alias expansion. */ + $length = strcspn(@$_REQUEST['command'], " \t"); + $token = substr(@$_REQUEST['command'], 0, $length); + if (isset($aliases[$token])) + $_REQUEST['command'] = $aliases[$token] . substr($_REQUEST['command'], $length); + + $p = proc_open(@$_REQUEST['command'], + array(1 => array('pipe', 'w'), + 2 => array('pipe', 'w')), $io); + + /* Read output sent to stdout. */ + while (!feof($io[1])) { + $_SESSION['output'] .= htmlspecialchars(fgets($io[1]),ENT_COMPAT, 'UTF-8'); + } + /* Read output sent to stderr. */ + while (!feof($io[2])) { + $_SESSION['output'] .= htmlspecialchars(fgets($io[2]),ENT_COMPAT, 'UTF-8'); + } + + fclose($io[1]); + fclose($io[2]); + proc_close($p); + } + } +} + +$terminal = new phpTerm; + +if ($_REQUEST['command']=="logout") { + $terminal->logout(); +} + +if(!$terminal->checkPassword($passwd)) { + header('WWW-Authenticate: Basic realm="Terminal"'); + header('HTTP/1.0 401 Unauthorized'); +} else { + $terminal->initVars(); + $terminal->buildCommandHistory(); + $terminal->buildJavaHistory(); + if(!isset($_SESSION['prompt'])):$terminal->formatPrompt(); endif; + $terminal->outputHandle($aliases); +?> + + + +PHP Terminal + + + + + + +
+ +
+ +

$>

+
+ + + + \ No newline at end of file diff --git a/plugins/terminal/terminal.css b/plugins/terminal/terminal.css new file mode 100644 index 00000000..16770050 --- /dev/null +++ b/plugins/terminal/terminal.css @@ -0,0 +1,28 @@ +/* First, reset everything to a standard */ +html, body, div, span, applet, object, iframe, +h1, h2, h3, h4, h5, h6, p, blockquote, pre, +a, abbr, acronym, address, big, cite, code, +del, dfn, em, font, img, ins, kbd, q, s, samp, +small, strike, strong, sub, sup, tt, var, +b, u, i, center, +dl, dt, dd, ol, ul, li, +fieldset, form, input, label, legend, +table, caption, tbody, tfoot, thead, tr, th, td { + font-family: verdana, arial, monospace, sans-serif; + border: 0; + margin: 0; + padding: 0; + outline: 0; + font-size: 12px; + vertical-align: top; +} + +html, body {width: 100%; height: 100%; background: #000} + +.head {position: fixed; top: 0; padding: 2px; background: rgba(124,124,124,0.8); color: #fff; font-weight: bold; z-index: 1} +textarea {position: absolute; display: block; top: 0; padding: 0; width: 100%; height: 100%; min-height: 100%; border: 0; background: #000; color: #0c0} +textarea:focus {outline: none} +p {color: #0c0} +.commandLine {position: fixed; width: 100%; bottom: 0; padding: 2px; background: rgba(32,32,32,0.9); z-index: 1} +.command {width: 95%; font-family: verdana, arial, monospace, sans-serif; border: none; background: transparent; color: #0c0} +.command:focus {outline: none} \ No newline at end of file