@@ -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