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 64d5b37 commit e98d6ad
Showing 1 changed file with 46 additions and 80 deletions.
126 changes: 46 additions & 80 deletions lib/terminal.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,33 @@
*
* @package Terminal.php
* @author SmartWF <[email protected]>
* @modified by Claude
*/

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;
private array $environment_variables = [];

public function __construct(string $path = '')
{
$this->current_directory = $path ?: getcwd();
$this->environment_variables['HOME'] = $this->current_directory;
$this->_cd($path);
}

private function shell(string $cmd): string
Expand All @@ -46,7 +42,7 @@ private function shell(string $cmd): string

private function commandExists(string $command): bool
{
return !empty($this->shell('command -v ' . escapeshellarg($command)));
return !empty($this->shell('command -v ' . $command));
}

public function __call(string $cmd, array $arg): string
Expand All @@ -56,92 +52,62 @@ public function __call(string $cmd, array $arg): string

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);
$cmd = explode(' ', $command)[0];
$arg = count(explode(' ', $command)) > 1 ? implode(' ', array_slice(explode(' ', $command), 1)) : '';

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

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

public function normalizeHtml(string $input): string
{
return str_replace(
['<', '>', "\n", "\t", ' '],
['&lt;', '&gt;', '<br>', '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', '&nbsp;'],
$input
);
return str_replace(['<', '>', "\n", "\t", ' '], ['&lt;', '&gt;', '<br>', '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', '&nbsp;'], $input);
}

private function _cd(string $path): string
/**
* Array of Local Commands
* @return array
*/
private function getLocalCommands(): array
{
$new_path = realpath($this->current_directory . '/' . $path);
if ($new_path && is_dir($new_path)) {
$this->current_directory = $new_path;
return '';
$commands = array_filter(get_class_methods($this), function ($i) {return ($i[0] == '_' && $i[1] != '_') ? true : false;});
foreach ($commands as $i => $command) {
$commands[$i] = substr($command, 1);
}
return "cd: $path: No such directory";
}

private function _pwd(): string
{
return $this->current_directory;
}

private function _echo(string $arg): string
{
return $arg;
}

private function _ls(string $arg = ''): string
{
$path = $arg ?: $this->current_directory;
$items = scandir($path);
return implode("\n", array_diff($items, ['.', '..']));
return $commands;
}

private function _export(string $arg): string
private function _cd(string $path): string
{
$parts = explode('=', $arg, 2);
if (count($parts) === 2) {
$this->environment_variables[$parts[0]] = $parts[1];
return '';
if ($path) {
chdir($path);
}
return "export: Invalid syntax";
return '';
}

private function _env(): string
private function _moin(): string
{
$output = '';
foreach ($this->environment_variables as $key => $value) {
$output .= "$key=$value\n";
}
return rtrim($output);
return 'Selber';
}

private function _moin(): string
private function _pwd(): string
{
return 'Selber';
return getcwd() ?: '';
}

private function _ping(string $a): string
{
if (strpos($a, '-c ') !== false) {
return $this->shell('ping ' . escapeshellarg($a));
return trim(shell_exec('ping ' . $a) ?? '');
}
return $this->shell('ping -c 4 ' . escapeshellarg($a));
return trim(shell_exec('ping -c 4 ' . $a) ?? '');
}
}

0 comments on commit e98d6ad

Please sign in to comment.