From e98d6ad7e8a2d1519cba9cce0e6cc05a117d5937 Mon Sep 17 00:00:00 2001 From: Thomas Skerbis Date: Mon, 2 Sep 2024 15:45:42 +0200 Subject: [PATCH] Update terminal.php --- lib/terminal.php | 126 +++++++++++++++++------------------------------ 1 file changed, 46 insertions(+), 80 deletions(-) diff --git a/lib/terminal.php b/lib/terminal.php index 8e9f48f..e81196d 100644 --- a/lib/terminal.php +++ b/lib/terminal.php @@ -6,37 +6,33 @@ * * @package Terminal.php * @author SmartWF - * @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 @@ -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 @@ -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", ' '], - ['<', '>', '
', '     ', ' '], - $input - ); + return str_replace(['<', '>', "\n", "\t", ' '], ['<', '>', '
', '     ', ' '], $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) ?? ''); } }