# Copyright: (c) 2008 # License: GPL v.2 # Version: 0.1 # # $Id: wwwshell.php 18 2008-01-24 00:56:30Z urzenia $ # vim: ft=php # configuration # your home directory define ('HOME', '/home/mysz'); # your aliases $aliases = array ( 'll' => 'ls -l', 'la' => 'ls -la', '..' => 'cd ..', '...' => 'cd ../..', 'pax' => 'ps awux', 'paxg' => 'ps awux | grep', 'g' => 'grep', 'gi' => 'grep -i', 'gr' => 'grep -ir', ); # do not touch below this line - except you exactly now what are you doing... putenv ('HOME='.HOME); error_reporting (E_ALL|E_STRICT); if (!session_id ()) { session_start (); } function str_entit ($str) { $pat = array ( '<' => '<', '>' => '>', '"' => '"', "\r" => '', ); return strtr ($str, $pat); } function debug () { $args = func_get_args (); echo "
DEBUG:\n";
$i = 0;
foreach ($args as $arg) {
printf ("%d. %s\n", ++$i, print_r ($arg, 1));
}
echo '';
}
function expand_alias ($command) {
$length = strcspn ($command, " \t");
$cmd = substr ($command, 0, $length);
if (isset ($GLOBALS['aliases'][$cmd])) {
return $GLOBALS['aliases'][$cmd] . substr ($command, $length);
}
return $command;
}
function execute ($command) {
$output = array ();
$res = proc_open ($command, array (
1 => array('pipe', 'w'),
2 => array('pipe', 'w')
), $output);
$return = array ('stderr' => '', 'stdout' => '');
while (!feof($output[1])) {
$return['stdout'] .= fgets($output[1]);
}
while (!feof($output[2])) {
$return['stderr'] .= fgets($output[2]);
}
fclose($output[1]);
fclose($output[2]);
proc_close($res);
return $return;
}
$results = '';
# cleat output console
if (isset ($_GET['clear'])) {
$_SESSION['results'] = $results;
}
# prepend console history
if (isset ($_SESSION['results'])) {
$results .= $_SESSION['results'];
}
# set cwd
if (!isset ($_SESSION['cwd'])) {
$_SESSION['cwd'] = getenv ('HOME') ? getenv ('HOME') : getcwd ();
}
# chdir to stored value
else {
@chdir ($_SESSION['cwd']);
}
if (isset ($_GET['command']) && $_GET['command']) {
# magic_quotes_gpc :/
if (get_magic_quotes_gpc ()) {
$_GET['command'] = stripslashes ($_GET['command']);
}
# clean command
$command = $_GET['command'];
$command = str_replace (array ("\r", "\n"), array ('', ' '), $command);
# default values
$return = array ('stdout' => '', 'stderr' => '');
# aliases
$command = expand_alias ($command);
# cd - we change directory
if (preg_match ('#^\s*cd(?:\s+(.*))?#', $command, $match)) {
if (!isset ($match[1]) || trim ($match[1]) == '~') {
$match[1] = getenv ('HOME');
}
$cwd = realpath (trim ($match[1]));
if ($cwd && is_dir ($cwd) && is_executable ($cwd)) {
if (@chdir ($cwd)) {
$_SESSION['cwd'] = $cwd;
}
else {
$return['stderr'] = sprintf ('Cannot change directory to "%s".', $cwd);
}
}
else {
$return['stderr'] = sprintf ('Cannot change directory to "%s".', $cwd);
}
}
# other command
else {
$return = execute ($command);
foreach (array ('stdout', 'stderr') as $io) {
if (!isset ($return[$io])) {
continue;
}
$return[$io] = str_entit ($return[$io]);
$return[$io] = str_replace (array (' ', "\t", "\n"), array (' ', ' ', "'. $_SESSION['cwd'] .' $ '.str_entit ($_GET['command'])."
\n"; if ($return['stdout']) { $results .= ''. $return['stdout'] ."
\n"; } if ($return['stderr']) { $results .= ''. $return['stderr'] ."
\n"; } # store results in session $_SESSION['results'] = $results; } ?>