snappymail/plugins/demo-account/storage.php

78 lines
3.1 KiB
PHP
Raw Normal View History

<?php
use RainLoop\Providers\Storage\Enumerations\StorageType;
class DemoStorage extends \RainLoop\Providers\Storage\FileStorage
{
private static $gc_done;
/**
2021-11-09 00:40:21 +08:00
* @param \RainLoop\Model\Account|string|null $mAccount
*/
2022-03-21 20:00:10 +08:00
public function GenerateFilePath($mAccount, int $iStorageType, bool $bMkDir = false) : string
{
$sEmail = '';
if ($mAccount instanceof \RainLoop\Model\MainAccount) {
$sEmail = $mAccount->Email();
} else if (\is_string($mAccount)) {
$sEmail = $mAccount;
}
if ($sEmail != $this->sDemoEmail) {
2022-03-21 20:00:10 +08:00
return parent::GenerateFilePath($mAccount, $iStorageType, $bMkDir);
}
2021-11-09 00:52:40 +08:00
$sDataPath = "{$this->sDataPath}/demo";
// Garbage collection
if (!static::$gc_done) {
static::$gc_done = true;
if (!\random_int(0, \max(50, \ini_get('session.gc_divisor')))) {
\MailSo\Base\Utils::RecTimeDirRemove($sDataPath, 3600 * 3); // 3 hours
}
2021-11-09 00:52:40 +08:00
}
2023-02-06 18:51:41 +08:00
// $_COOKIE['smtoken']
if (empty($_COOKIE['smctoken'])) {
\SnappyMail\Cookies::set('smctoken', \base64_encode(\random_bytes(16)), 0, false);
}
$sDataPath .= '/' . \MailSo\Base\Utils::SecureFileName($_COOKIE['smctoken']);
2022-10-12 16:24:30 +08:00
if (!\is_dir($sDataPath) && \mkdir($sDataPath, 0700, true)) {
\file_put_contents("{$sDataPath}/settings",'{"RemoveColors":true,"ListInlineAttachments":true}');
if (\mkdir($sDataPath.'/.gnupg/private-keys-v1.d', 0700, true)) {
2022-11-15 20:37:25 +08:00
// AES
\file_put_contents("{$sDataPath}/.gnupg/private-keys-v1.d/3106F4281F98D820114228FEF16B5BA0D78AA005.key",file_get_contents("{$this->sDataPath}/demo.pgp/.gnupg/private-keys-v1.d/3106F4281F98D820114228FEF16B5BA0D78AA005.key"));
\file_put_contents("{$sDataPath}/.gnupg/private-keys-v1.d/82CA239C482423D364BFD6DFC3E400B3B98AD66F.key",file_get_contents("{$this->sDataPath}/demo.pgp/.gnupg/private-keys-v1.d/82CA239C482423D364BFD6DFC3E400B3B98AD66F.key"));
// ECC
2022-10-12 16:24:30 +08:00
\file_put_contents("{$sDataPath}/.gnupg/private-keys-v1.d/5A1A6C7310D0508C68E8E74F15068301E83FD1AE.key",file_get_contents("{$this->sDataPath}/demo.pgp/.gnupg/private-keys-v1.d/5A1A6C7310D0508C68E8E74F15068301E83FD1AE.key"));
\file_put_contents("{$sDataPath}/.gnupg/private-keys-v1.d/886921A7E06BE56F8E8C51797BB476BB26DF21BF.key",file_get_contents("{$this->sDataPath}/demo.pgp/.gnupg/private-keys-v1.d/886921A7E06BE56F8E8C51797BB476BB26DF21BF.key"));
2022-11-15 20:37:25 +08:00
2022-10-12 16:24:30 +08:00
\file_put_contents("{$sDataPath}/.gnupg/pubring.kbx",file_get_contents("{$this->sDataPath}/demo.pgp/.gnupg/pubring.kbx"));
\file_put_contents("{$sDataPath}/.gnupg/trustdb.gpg",file_get_contents("{$this->sDataPath}/demo.pgp/.gnupg/trustdb.gpg"));
}
}
if (StorageType::SIGN_ME === $iStorageType) {
$sDataPath .= '/.sign_me';
} else if (StorageType::SESSION === $iStorageType) {
$sDataPath .= '/.sessions';
2022-01-28 22:43:40 +08:00
} else if (StorageType::PGP === $iStorageType) {
2022-01-28 22:59:41 +08:00
$sDataPath .= '/.pgp';
$sDataPath = "{$this->sDataPath}/demo.pgp/.pgp";
$bMkDir = true;
}
2021-11-09 00:52:40 +08:00
2022-01-28 22:43:40 +08:00
if ($bMkDir && !\is_dir($sDataPath) && !\mkdir($sDataPath, 0700, true))
{
throw new \RainLoop\Exceptions\Exception('Can\'t make storage directory "'.$sDataPath.'"');
}
return $sDataPath . '/';
}
private $sDemoEmail;
public function setDemoEmail(string $sEmail)
{
$this->sDemoEmail = $sEmail;
}
}