From c62ef28fc3eeb10ce238b0ea56f817360298bce6 Mon Sep 17 00:00:00 2001 From: donbuehl Date: Mon, 19 Aug 2024 22:06:41 +0200 Subject: [PATCH] Refactor getUserShell() for improved clarity and maintainability - Introduce variable for better code readability - Simplify return logic using the default shell variable - Maintain comprehensive error handling with Throwable --- .../plugins/dynamix/include/OpenTerminal.php | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/emhttp/plugins/dynamix/include/OpenTerminal.php b/emhttp/plugins/dynamix/include/OpenTerminal.php index 3afd4d9f9..c0c1fdbac 100644 --- a/emhttp/plugins/dynamix/include/OpenTerminal.php +++ b/emhttp/plugins/dynamix/include/OpenTerminal.php @@ -30,26 +30,25 @@ $run = "$docroot/webGui/scripts/run_cmd"; if (!empty($display['tty'])) exec("sed -ri 's/fontSize=[0-9]+/fontSize={$display['tty']}/' /etc/default/ttyd"); function getUserShell() { - $shell = 'bash'; + $defaultShell = 'bash'; + try { $username = posix_getpwuid(posix_geteuid())['name']; $passwd = file_get_contents('/etc/passwd'); $lines = explode("\n", $passwd); foreach ($lines as $line) { - $parts = explode(':', $line); - if ($parts[0] === $username) { - $fullShellPath = end($parts); - $shell = basename(trim($fullShellPath)); - break; - } + $parts = explode(':', $line); + if ($parts[0] === $username) { + $fullShellPath = end($parts); + return basename(trim($fullShellPath)); + } } - } catch (Exception $e) { - syslog(LOG_ERR, "Error determining user shell: " . $e->getMessage()); + } catch (Throwable $t) { + syslog(LOG_ERR, 'Error determining user shell: ' . $t->getMessage()); + return defaultShell; } - syslog(LOG_INFO, sprintf("User shell determined: %s %s", $username, $shell)); - - return $shell; + return defaultShell; } function wait($name,$cmd) {