From f0e6b818afc6796af992ec068b3aaed2a44319f8 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Sun, 3 Jul 2016 21:42:47 +0900 Subject: [PATCH] Add method to recommend the best umask for the current server environment --- common/framework/storage.php | 52 ++++++++++++++++++++++++++++ tests/unit/framework/StorageTest.php | 23 ++++++++++++ 2 files changed, 75 insertions(+) diff --git a/common/framework/storage.php b/common/framework/storage.php index e44255c00..26ceacfbe 100644 --- a/common/framework/storage.php +++ b/common/framework/storage.php @@ -727,4 +727,56 @@ class Storage { self::$_umask = intval($umask); } + + /** + * Determine the best umask for this installation of Rhymix. + * + * @return int + */ + public static function recommendUmask() + { + // On Windows, set the umask to 0000. + if (strncasecmp(\PHP_OS, 'Win', 3) === 0) + { + return 0000; + } + + // Get the UID of the owner of the current file. + $file_uid = fileowner(__FILE__); + + // Get the UID of the current PHP process. + if (function_exists('posix_geteuid')) + { + $php_uid = posix_geteuid(); + } + else + { + $testfile = \RX_BASEDIR . 'files/cache/uidcheck'; + if (self::exists($testfile)) + { + self::delete($testfile); + } + if (self::write($testfile, 'TEST')) + { + $php_uid = fileowner($testfile); + self::delete($testfile); + } + else + { + $php_uid = -1; + } + } + + // If both UIDs are the same, set the umask to 0022. + if ($file_uid == $php_uid) + { + return 0022; + } + + // Otherwise, set the umask to 0000. + else + { + return 0000; + } + } } diff --git a/tests/unit/framework/StorageTest.php b/tests/unit/framework/StorageTest.php index 0118c8754..3dd2d3a0b 100644 --- a/tests/unit/framework/StorageTest.php +++ b/tests/unit/framework/StorageTest.php @@ -324,4 +324,27 @@ class StorageTest extends \Codeception\TestCase\Test $this->assertTrue(file_exists($sourcedir)); $this->assertFalse(Rhymix\Framework\Storage::deleteDirectory($nonexistent)); } + + public function testRecommendUmask() + { + $umask = Rhymix\Framework\Storage::recommendUmask(); + + if (strncasecmp(\PHP_OS, 'Win', 3) !== 0) + { + if (get_current_user() === exec('whoami')) + { + $this->assertEquals(0022, $umask); + } + else + { + $this->assertEquals(0, $umask); + } + } + else + { + $this->assertEquals(0, $umask); + } + + $this->assertFalse(file_exists(\RX_BASEDIR . 'files/cache/uidcheck')); + } }