From f647bbc776db5f2df34df51df3eab09eeca10c7e Mon Sep 17 00:00:00 2001 From: Daniel Kesselberg Date: Mon, 6 May 2024 12:07:39 +0200 Subject: [PATCH] fix: handle disabled shell_exec Signed-off-by: Daniel Kesselberg --- lib/OperatingSystems/FreeBSD.php | 16 ++++++++++++-- lib/OperatingSystems/Linux.php | 37 +++++++++++++++++++++++++------- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/lib/OperatingSystems/FreeBSD.php b/lib/OperatingSystems/FreeBSD.php index 95b0ef11..8eae4531 100644 --- a/lib/OperatingSystems/FreeBSD.php +++ b/lib/OperatingSystems/FreeBSD.php @@ -227,11 +227,23 @@ public function getThermalZones(): array { return []; } + /** + * Execute a command with shell_exec. + * + * The command will be escaped with escapeshellcmd. + * + * @throws RuntimeException if shell_exec is unavailable, the command failed or an empty response. + */ protected function executeCommand(string $command): string { - $output = @shell_exec(escapeshellcmd($command)); - if ($output === null || $output === '' || $output === false) { + if (function_exists('shell_exec') === false) { + throw new RuntimeException('shell_exec unavailable'); + } + + $output = shell_exec(escapeshellcmd($command)); + if ($output === false || $output === null || $output === '') { throw new RuntimeException('No output for command: "' . $command . '"'); } + return $output; } diff --git a/lib/OperatingSystems/Linux.php b/lib/OperatingSystems/Linux.php index e2bf8a1d..96d59cc5 100644 --- a/lib/OperatingSystems/Linux.php +++ b/lib/OperatingSystems/Linux.php @@ -121,7 +121,11 @@ public function getCpuName(): string { } public function getTime(): string { - return (string)shell_exec('date'); + try { + return $this->executeCommand('date'); + } catch (RuntimeException $e) { + return ''; + } } public function getUptime(): int { @@ -139,12 +143,17 @@ public function getUptime(): int { } public function getNetworkInfo(): array { - $result = []; - $result['hostname'] = \gethostname(); - $dns = shell_exec('cat /etc/resolv.conf |grep -i \'^nameserver\'|head -n1|cut -d \' \' -f2'); - $result['dns'] = $dns; - $gw = shell_exec('ip route | awk \'/default/ { print $3 }\''); - $result['gateway'] = $gw; + $result = [ + 'hostname' => \gethostname(), + 'dns' => '', + 'gateway' => '', + ]; + + if (function_exists('shell_exec')) { + $result['dns'] = shell_exec('cat /etc/resolv.conf |grep -i \'^nameserver\'|head -n1|cut -d \' \' -f2'); + $result['gateway'] = shell_exec('ip route | awk \'/default/ { print $3 }\''); + } + return $result; } @@ -261,11 +270,23 @@ protected function readContent(string $filename): string { return trim($data); } + /** + * Execute a command with shell_exec. + * + * The command will be escaped with escapeshellcmd. + * + * @throws RuntimeException if shell_exec is unavailable, the command failed or an empty response. + */ protected function executeCommand(string $command): string { - $output = @shell_exec(escapeshellcmd($command)); + if (function_exists('shell_exec') === false) { + throw new RuntimeException('shell_exec unavailable'); + } + + $output = shell_exec(escapeshellcmd($command)); if ($output === false || $output === null || $output === '') { throw new RuntimeException('No output for command: "' . $command . '"'); } + return $output; }