Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F11722882
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/OS/CurrentProcess.php b/src/OS/CurrentProcess.php
index 8e5740e..76d2c01 100644
--- a/src/OS/CurrentProcess.php
+++ b/src/OS/CurrentProcess.php
@@ -1,33 +1,59 @@
<?php
declare(strict_types=1);
namespace Keruald\OmniTools\OS;
class CurrentProcess {
/**
* Determines if the current process, ie the PHP interpreter,
* runs as root on UNIX systems or in elevated mode on Windows.
*
* Cygwin processes are considered as Windows processes.
*/
public static function isPrivileged () : bool {
if (CurrentOS::isWindows()) {
// `net session` is known to only work as privileged process.
// To wrap in cmd allows avoiding /dev/null for Cygwin,
// or $null when invoked from PowerShell. NUL: will always be used.
exec('cmd /C "net session >NUL 2>&1"', $_, $exitCode);
return $exitCode === 0;
}
if (!function_exists('posix_geteuid')) {
// POSIX PHP functions aren't always available, e.g. on FreeBSD
// In such cases, `id` will probably be available.
return trim((string)shell_exec('id -u')) === '0';
}
return posix_geteuid() === 0;
}
+ /**
+ * Determines the username of the current process, ie the PHP interpreter.
+ */
+ public static function getUsername () : string {
+ if (!extension_loaded("posix")) {
+ // POSIX PHP functions aren't always available.
+ // In such cases, `whoami` will probably be available.
+ $username = trim((string)shell_exec("whoami"));
+
+ if (CurrentOS::isWindows() && str_contains('\\', $username)) {
+ // Perhaps the domain information could be useful if the user
+ // can belong to an AD domain, to disambiguate between local
+ // accounts and AD accounts.
+ //
+ // If not attached to a domain, the PC hostname is used.
+ //
+ // We return only the username part to be coherent with UNIX.
+ return explode('\\', $username, 2)[1];
+ }
+
+ return $username;
+ }
+
+ return posix_getpwuid(posix_geteuid())["name"];
+ }
+
}
diff --git a/tests/OS/CurrentProcessTest.php b/tests/OS/CurrentProcessTest.php
new file mode 100644
index 0000000..16752c6
--- /dev/null
+++ b/tests/OS/CurrentProcessTest.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace Keruald\OmniTools\Tests\OS;
+
+use Keruald\OmniTools\OS\CurrentProcess;
+use PHPUnit\Framework\TestCase;
+
+class CurrentProcessTest extends TestCase {
+
+ // Probably more usernames are valid, but why tests
+ // would run in accounts using spaces or UTF-8 emojis?
+ const USERNAME_REGEXP = "/^([a-zA-Z][a-zA-Z0-9_]*)$/";
+
+ public function testGetUsername () {
+ $actual = CurrentProcess::getUsername();
+
+ $this->assertMatchesRegularExpression(self::USERNAME_REGEXP, $actual);
+ }
+
+}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Thu, Sep 18, 06:14 (20 h, 51 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2990103
Default Alt Text
(2 KB)
Attached To
Mode
rKOT Keruald OmniTools
Attached
Detach File
Event Timeline
Log In to Comment