remove hard dependency on ext/posix
authorMichael Wallner <mike@php.net>
Wed, 19 Aug 2015 19:39:04 +0000 (21:39 +0200)
committerMichael Wallner <mike@php.net>
Wed, 19 Aug 2015 19:39:04 +0000 (21:39 +0200)
bin/pharext
src/pharext/Tempname.php

index f3ff98a63bad652eaf537cbd8877e2c814d42573..7640c6dff29c0b68c23dd6bfd4acd8e6fae23539 100755 (executable)
Binary files a/bin/pharext and b/bin/pharext differ
index d46250f7472b379e44ea17f98355af06d2f83eab..31160235ddc4ad7afb72cec19749aa171f02386e 100644 (file)
@@ -19,13 +19,23 @@ class Tempname
         * @param string $suffix e.g. file extension
         */
        public function __construct($prefix, $suffix = null) {
-               $temp = sys_get_temp_dir() . "/pharext-" . posix_getlogin();
+               $temp = sys_get_temp_dir() . "/pharext-" . $this->getUser();
                if (!is_dir($temp) && !mkdir($temp, 0700, true)) {
                        throw new Exception;
                }
                $this->name = $temp ."/". uniqid($prefix) . $suffix;
        }
 
+       private function getUser() {
+               if (extension_loaded("posix") && function_exists("posix_getpwuid")) {
+                       return posix_getpwuid(posix_getuid())["name"];
+               }
+               return trim(`whoami 2>/dev/null`)
+                       ?: trim(`id -nu 2>/dev/null`)
+                       ?: getenv("USER")
+                       ?: get_current_user();
+       }
+
        /**
         * @return string
         */