Skip to content

Commit

Permalink
Update terminal.php
Browse files Browse the repository at this point in the history
  • Loading branch information
skerbis authored Sep 2, 2024
1 parent 98f56ba commit 64d5b37
Showing 1 changed file with 37 additions and 56 deletions.
93 changes: 37 additions & 56 deletions lib/terminal.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace skerbis\terminal;

/**
* Terminal.php - Improved Terminal Emulator for PHP
* Terminal.php - Terminal Emulator for PHP
*
* @package Terminal.php
* @author SmartWF <[email protected]>
Expand All @@ -11,8 +11,23 @@
class TerminalPHP
{
private array $allowed_commands = [
'cd', 'chown', 'composer', 'date', 'df', 'echo', 'ffmpeg', 'find',
'free', 'git', 'grep', 'hostname', 'ls', 'php', 'pwd', 'tail', 'whoami'
'cd',
'chown',
'composer',
'date',
'df',
'echo',
'ffmpeg',
'find',
'free',
'git',
'grep',
'hostname',
'ls',
'php',
'pwd',
'tail',
'whoami',
];

private string $current_directory;
Expand All @@ -39,62 +54,28 @@ public function __call(string $cmd, array $arg): string
return $this->runCommand($cmd . (isset($arg[0]) ? ' ' . $arg[0] : ''));
}


public function runCommand(string $command): string
{
$parts = explode(' ', $command, 2);
$cmd = $parts[0];
$arg = $parts[1] ?? '';

// Handle environment variable expansion
$arg = preg_replace_callback('/\$(\w+)/', function($matches) {
return $this->environment_variables[$matches[1]] ?? '';
}, $arg);

if (method_exists($this, '_' . $cmd)) {
$method = '_' . $cmd;
return $this->$method($arg);
}

if (in_array($cmd, $this->allowed_commands)) {
return trim(shell_exec($command) ?? '');
} else {
return "terminal.php: Permission denied for command: $cmd";
}
}

public function runCommand(string $command): string
{
$parts = explode(' ', $command, 2);
$cmd = $parts[0];
$arg = $parts[1] ?? '';

// Handle environment variable expansion
$arg = preg_replace_callback('/\$(\w+)/', function($matches) {
return $this->environment_variables[$matches[1]] ?? '';
}, $arg);

if (method_exists($this, '_' . $cmd)) {
$method = '_' . $cmd;
return $this->$method($arg);
}

if (in_array($cmd, $this->allowed_commands)) {
$fullCommand = escapeshellcmd($cmd) . ' ' . escapeshellarg($arg);
$output = '';
$return_var = 0;

exec($fullCommand . ' 2>&1', $output, $return_var);

if ($return_var !== 0) {
return "Error executing command: " . implode("\n", $output);
public function runCommand(string $command): string
{
$parts = explode(' ', $command, 2);
$cmd = $parts[0];
$arg = $parts[1] ?? '';

// Handle environment variable expansion
$arg = preg_replace_callback('/\$(\w+)/', function($matches) {
return $this->environment_variables[$matches[1]] ?? '';
}, $arg);

if (method_exists($this, '_' . $cmd)) {
$method = '_' . $cmd;
return $this->$method($arg);
}

return implode("\n", $output);
} else {
return "terminal.php: Permission denied for command: $cmd";
if (in_array($cmd, $this->allowed_commands)) {
return trim(shell_exec($command) ?? '');
} else {
return "terminal.php: Permission denied for command: $cmd";
}
}
}

public function normalizeHtml(string $input): string
{
Expand Down

0 comments on commit 64d5b37

Please sign in to comment.