Skip to content

Commit 3c12d8d

Browse files
authored
Merge pull request #600 from nextcloud/backport/598/stable29
[stable29] fix: handle disabled shell_exec
2 parents 22a8b35 + f647bbc commit 3c12d8d

2 files changed

Lines changed: 43 additions & 10 deletions

File tree

lib/OperatingSystems/FreeBSD.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,23 @@ public function getThermalZones(): array {
227227
return [];
228228
}
229229

230+
/**
231+
* Execute a command with shell_exec.
232+
*
233+
* The command will be escaped with escapeshellcmd.
234+
*
235+
* @throws RuntimeException if shell_exec is unavailable, the command failed or an empty response.
236+
*/
230237
protected function executeCommand(string $command): string {
231-
$output = @shell_exec(escapeshellcmd($command));
232-
if ($output === null || $output === '' || $output === false) {
238+
if (function_exists('shell_exec') === false) {
239+
throw new RuntimeException('shell_exec unavailable');
240+
}
241+
242+
$output = shell_exec(escapeshellcmd($command));
243+
if ($output === false || $output === null || $output === '') {
233244
throw new RuntimeException('No output for command: "' . $command . '"');
234245
}
246+
235247
return $output;
236248
}
237249

lib/OperatingSystems/Linux.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ public function getCpuName(): string {
121121
}
122122

123123
public function getTime(): string {
124-
return (string)shell_exec('date');
124+
try {
125+
return $this->executeCommand('date');
126+
} catch (RuntimeException $e) {
127+
return '';
128+
}
125129
}
126130

127131
public function getUptime(): int {
@@ -139,12 +143,17 @@ public function getUptime(): int {
139143
}
140144

141145
public function getNetworkInfo(): array {
142-
$result = [];
143-
$result['hostname'] = \gethostname();
144-
$dns = shell_exec('cat /etc/resolv.conf |grep -i \'^nameserver\'|head -n1|cut -d \' \' -f2');
145-
$result['dns'] = $dns;
146-
$gw = shell_exec('ip route | awk \'/default/ { print $3 }\'');
147-
$result['gateway'] = $gw;
146+
$result = [
147+
'hostname' => \gethostname(),
148+
'dns' => '',
149+
'gateway' => '',
150+
];
151+
152+
if (function_exists('shell_exec')) {
153+
$result['dns'] = shell_exec('cat /etc/resolv.conf |grep -i \'^nameserver\'|head -n1|cut -d \' \' -f2');
154+
$result['gateway'] = shell_exec('ip route | awk \'/default/ { print $3 }\'');
155+
}
156+
148157
return $result;
149158
}
150159

@@ -261,11 +270,23 @@ protected function readContent(string $filename): string {
261270
return trim($data);
262271
}
263272

273+
/**
274+
* Execute a command with shell_exec.
275+
*
276+
* The command will be escaped with escapeshellcmd.
277+
*
278+
* @throws RuntimeException if shell_exec is unavailable, the command failed or an empty response.
279+
*/
264280
protected function executeCommand(string $command): string {
265-
$output = @shell_exec(escapeshellcmd($command));
281+
if (function_exists('shell_exec') === false) {
282+
throw new RuntimeException('shell_exec unavailable');
283+
}
284+
285+
$output = shell_exec(escapeshellcmd($command));
266286
if ($output === false || $output === null || $output === '') {
267287
throw new RuntimeException('No output for command: "' . $command . '"');
268288
}
289+
269290
return $output;
270291
}
271292

0 commit comments

Comments
 (0)