Add method to recommend the best umask for the current server environment

This commit is contained in:
Kijin Sung 2016-07-03 21:42:47 +09:00
parent 480e27b55d
commit f0e6b818af
2 changed files with 75 additions and 0 deletions

View file

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

View file

@ -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'));
}
}