Improved private data directory handling

This commit is contained in:
djmaze 2021-12-15 14:51:52 +01:00
parent 41a98d68c5
commit 2aceb89a8b
5 changed files with 58 additions and 50 deletions

View file

@ -797,8 +797,8 @@ class Actions
$sPassword = $oConfig->Get('security', 'admin_password', ''); $sPassword = $oConfig->Get('security', 'admin_password', '');
if (!$sPassword) { if (!$sPassword) {
$sPassword = \substr(\base64_encode(\random_bytes(16)), 0, 12); $sPassword = \substr(\base64_encode(\random_bytes(16)), 0, 12);
\file_put_contents($passfile, $sPassword . "\n"); Utils::saveFile($passfile, $sPassword . "\n");
\chmod($passfile, 0600); // \chmod($passfile, 0600);
$oConfig->SetPassword($sPassword); $oConfig->SetPassword($sPassword);
$oConfig->Save(); $oConfig->Save();
} }

View file

@ -294,8 +294,11 @@ abstract class AbstractConfig
} }
$this->clearCache(); $this->clearCache();
return false !== \file_put_contents($this->sFile,
\RainLoop\Utils::saveFile($this->sFile,
(\strlen($this->sFileHeader) ? $this->sFileHeader : ''). (\strlen($this->sFileHeader) ? $this->sFileHeader : '').
$sNewLine.\implode($sNewLine, $aResultLines)); $sNewLine.\implode($sNewLine, $aResultLines));
return true;
} }
} }

View file

@ -159,8 +159,9 @@ class DefaultDomain implements \RainLoop\Providers\Domain\DomainAdminInterface
$this->oCacher->Delete($this->wildcardDomainsCacheKey()); $this->oCacher->Delete($this->wildcardDomainsCacheKey());
} }
$mResult = \file_put_contents($this->sDomainPath.'/'.$sRealFileName.'.ini', $oDomain->ToIniString()); \RainLoop\Utils::saveFile($this->sDomainPath.'/'.$sRealFileName.'.ini', $oDomain->ToIniString());
return \is_int($mResult) && 0 < $mResult;
return true;
} }
public function SaveAlias(string $sName, string $sAlias) : bool public function SaveAlias(string $sName, string $sAlias) : bool
@ -172,8 +173,8 @@ class DefaultDomain implements \RainLoop\Providers\Domain\DomainAdminInterface
$this->oCacher->Delete($this->wildcardDomainsCacheKey()); $this->oCacher->Delete($this->wildcardDomainsCacheKey());
} }
$mResult = \file_put_contents($this->sDomainPath.'/'.$sRealFileName.'.alias', $sAlias); \RainLoop\Utils::saveFile($this->sDomainPath.'/'.$sRealFileName.'.alias', $sAlias);
return \is_int($mResult) && 0 < $mResult; return true;
} }
public function Disable(string $sName, bool $bDisable) : bool public function Disable(string $sName, bool $bDisable) : bool
@ -204,8 +205,8 @@ class DefaultDomain implements \RainLoop\Providers\Domain\DomainAdminInterface
} }
} }
$aResult = \array_unique($aResult); \RainLoop\Utils::saveFile($this->sDomainPath.'/disabled', \trim(\implode(',', \array_unique($aResult)), ', '));
return false !== \file_put_contents($this->sDomainPath.'/disabled', \trim(\implode(',', $aResult), ', ')); return true;
} }
public function Delete(string $sName) : bool public function Delete(string $sName) : bool

View file

@ -220,4 +220,16 @@ class Utils
{ {
return \preg_replace('#[|\\\\?*<":>+\\[\\]/&\\s\\pC]#su', '-', $filename); return \preg_replace('#[|\\\\?*<":>+\\[\\]/&\\s\\pC]#su', '-', $filename);
} }
public static function saveFile(string $filename, string $data) : void
{
$dir = \dirname($filename);
if (!\is_dir($dir) && !\mkdir($dir, 0700, true)) {
throw new \RainLoop\Exceptions\Exception('Failed to create directory "'.$dir.'"');
}
if (false === \file_put_contents($filename, $data)) {
throw new \RainLoop\Exceptions\Exception('Failed to save file "'.$filename.'"');
}
\chmod($filename, 0600);
}
} }

View file

@ -182,22 +182,15 @@ if (defined('APP_VERSION'))
clearstatcache(); clearstatcache();
} }
foreach (array('logs', 'cache', 'configs', 'plugins', 'storage') as $sName) foreach (array('logs', 'cache', 'configs', 'domains', 'plugins', 'storage') as $sName)
{ {
if (!is_dir(APP_PRIVATE_DATA.$sName)) if (!is_dir(APP_PRIVATE_DATA.$sName))
{ {
mkdir(APP_PRIVATE_DATA.$sName, 0755, true); mkdir(APP_PRIVATE_DATA.$sName, 0700, true);
} }
} }
if (!file_exists(APP_PRIVATE_DATA.'domains/disabled')) if (!file_exists(APP_PRIVATE_DATA.'domains/disabled') && is_dir(APP_PRIVATE_DATA.'domains'))
{
if (!is_dir(APP_PRIVATE_DATA.'domains'))
{
mkdir(APP_PRIVATE_DATA.'domains', 0755);
}
if (is_dir(APP_PRIVATE_DATA.'domains'))
{ {
$sFile = $sNewFile = $sNewFileName = ''; $sFile = $sNewFile = $sNewFileName = '';
$aFiles = glob(APP_VERSION_ROOT_PATH.'app/domains/*'); $aFiles = glob(APP_VERSION_ROOT_PATH.'app/domains/*');
@ -242,7 +235,6 @@ if (defined('APP_VERSION'))
unset($aFiles, $sFile, $sNewFileName, $sNewFile); unset($aFiles, $sFile, $sNewFileName, $sNewFile);
} }
} }
}
unset($sSalt, $sData, $sInstalled, $sPrivateDataFolderInternalName); unset($sSalt, $sData, $sInstalled, $sPrivateDataFolderInternalName);
} }