Example #1
0
function exec_command($command, $col=80)
{	
	chdir($_SESSION['DIR']);
	//echo $_SESSION['DIR']."\n";
	
	$_RESULT['startdir'] = getcwd_short();
	
	$out = array();
	$ret = false;
	
	$command = ltrim($command);
	if(substr($command,0,3) == 'cd ' || substr($command,0,6) == 'chdir ')
	{
		if(substr($command,0,3) == 'cd ') $folder = trim(substr($command,3));
		else $folder = trim(substr($command,6));
		
		if($folder[0] == '"')
		{
			if($folder[strlen($folder)-1] == '"') $folder = stripcslashes(substr($folder, 1, strlen($folder)-2));
			else $folder = stripcslashes(substr($folder,1));
		}else
		{
			$folder = str_replace('\\ ', ' ', $folder);
		}
		
		if($folder[0]=='~') $folder = HOMEDIR.'/'.substr($folder,1);
		
		if(@chdir($folder))
		{
			$_SESSION['DIR'] = abs_path($folder);
			//echo $_SESSION['DIR'];
			$_RESULT['output']='';
		}else
		{
			$_RESULT['output']='cd: cannot change directory';
		}

	}else if(trim($command) == 'exit' || trim($command) == 'quit')
	{
		$_RESULT['exit'] = true;
	}else if(trim($command) == 'pwd')
	{
		$_RESULT['output'] = getcwd();
	}else
	{
		$tmp = explode(' ', $command);
		$cmd = $tmp[0];
		
		$ex = false; /* exists ? */
		list($dirs, $exts) = get_path_dirs();
		
		foreach($dirs as $dir)
		{
			foreach($exts as $ext)
			{
				if(file_exists($dir.'/'.$cmd.$ext))
				{
					$ex = true;
					break(2);
				}
			}
		}
		
		
		/*if(!$ex)
		{
			$out[] = 'command not found';
		}else
		{*/
		
		#if(is_callable('proc_open'))
		#{
			$ret = -1;
			
			#if(is_callable('putenv'))
			#{
				// for ls 
				putenv('ROWS=24');
				putenv('COLUMNS='.$col);
			#}

            $separator = uniqid();

//            $command .= '; echo '.$separator.' $?';

//            $command .= '; echo '.$separator.'; set; echo '.$separator.'; /bin/pwd';
			
			// code is taken from PHP Shell
			if($p = proc_open($command, array(1 => array('pipe', 'w'), 2 => array('pipe', 'w')), $io))
			{
				$out = array('');
				$out[0] .= stream_get_contents($io[1]).stream_get_contents($io[2]);
				
				fclose($io[1]);
	            fclose($io[2]);
	            $ret = proc_close($p);
			}
		
		#}else
		#{
		#	exec('('.$command.') 2>&1 <"'.(file_exists('/dev/null') ? '/dev/null' : 'nul').'"', $out, $ret);
		#}
		
		if($ret!=0 && !$ex) $out = array(GREET.': '.$cmd.': command not found');
		/*}*/
		
		//$fp = popen($command.' 2>&1')
		$_RESULT['output'] = implode("\n", $out);
	}
	
	$add = GREET.'$ ';
	
	$_RESULT['output'] = exec_split($_RESULT['output'], $col);
	$_RESULT['cmd'] = exec_split($add.$command, $col);
	$_RESULT['cmd'] = substr($_RESULT['cmd'],strlen($add)+strlen('&nbsp;')-1);
	
	$_RESULT['dir'] = getcwd_short();
	
	return $_RESULT;
}
Example #2
0
	break;
case 'update':
	$_REQUEST['act'] = 'download-new'; /* for correct work of update_dolphin() */
	$_RESULT = update_dolphin(create_function('$cmd','return true;'))===true;
	break;
case 'ping':
	$_RESULT = 'pong';
	break;
case 'handletab':
	$cmd = ltrim($_REQUEST['cmd']);
	
	$parts = explode(' ', $cmd);
	
	if(sizeof($parts) <= 1) /* autocomplete command */
	{
		list($dirs, $exts) = get_path_dirs();
		foreach($exts as $k=>$v) $exts[$k] = trim(strtolower($v));
		$tmp = strtolower($cmd);
		$l = strlen($cmd);
		$found = array();
		
		foreach($dirs as $d)
		{
			if( !@$dh = opendir($d) ) continue;
			
			while( ($f = readdir($dh)) !== false)
			{
				if($f=='.'&&$f=='..') continue;
				
				if(strtolower(substr($f, 0, $l)) == $tmp && is_file($d.'/'.$f))
				{