File: //usr/local/mailchannels/storage/file/FileStorage.php
<?php
namespace MailChannels;
require_once(dirname(__FILE__) . "/../Storage.php");
class FileStorage implements Storage {
const INBOUND_CONFIG_FILE = 'inboundConfig.json';
const OUTBOUND_CONFIG_FILE = 'outboundConfig.json';
protected $folder;
/**
* FileStorage constructor.
* @param $folder
* @throws FileNotWritableException
*/
public function __construct($folder) {
if (!is_writeable($folder)) {
throw new FileNotWritableException("folder '$folder' is not writable");
}
$this->folder = $folder;
}
/**
* @return InboundConfig
* @throws InvalidJsonStringException
*/
public function getInboundConfig() {
$configBuilder = (new InboundConfigBuilder());
if (!file_exists("$this->folder/" . self::INBOUND_CONFIG_FILE)) {
return $configBuilder->build();
}
$data = file_get_contents("$this->folder/" . self::INBOUND_CONFIG_FILE);
$configBuilder->fromJson($data);
return $configBuilder->build();
}
public function setInboundConfig(InboundConfig $config) {
$this->storeAsJson(self::INBOUND_CONFIG_FILE, $config);
}
public function getOutboundConfig() {
$config = App::getConfig();
$builder = (new OutboundConfigBuilder());
if (file_exists($config::EXIM_AUTH_USERNAME_FILE)) {
$builder->setUsername(file_get_contents($config::EXIM_AUTH_USERNAME_FILE));
}
if (file_exists($config::EXIM_AUTH_PASSWORD_FILE)) {
$builder->setPassword(file_get_contents($config::EXIM_AUTH_PASSWORD_FILE));
}
if (file_exists("$this->folder/" . self::OUTBOUND_CONFIG_FILE)) {
$data = file_get_contents("$this->folder/" . self::OUTBOUND_CONFIG_FILE);
$builder->fromJson($data);
}
return $builder->build();
}
public function setOutboundConfig(OutboundConfig $outboundConfig) {
$config = App::getConfig();
if (!is_writeable($config::EXIM_AUTH_USERNAME_FILE) && !is_writeable(dirname($config::EXIM_AUTH_USERNAME_FILE))) {
throw new FileNotWritableException("file at " . $config::EXIM_AUTH_USERNAME_FILE . " doesn't exist and couldn't be created");
} else if (!is_writeable($config::EXIM_AUTH_PASSWORD_FILE) && !is_writeable(dirname($config::EXIM_AUTH_PASSWORD_FILE))) {
throw new FileNotWritableException("file at " . $config::EXIM_AUTH_PASSWORD_FILE . " doesn't exist and couldn't be created");
}
file_put_contents($config::EXIM_AUTH_USERNAME_FILE, $outboundConfig->getUsername());
file_put_contents($config::EXIM_AUTH_PASSWORD_FILE, $outboundConfig->getPassword());
// setting these to 644 since 640 seemed to cause an issue reading the files from mailchannels_login authenticator
chmod($config::EXIM_AUTH_USERNAME_FILE, 0644);
chmod($config::EXIM_AUTH_PASSWORD_FILE, 0644);
$json = [
'autoUpdateSPFRecords' => $outboundConfig->autoUpdateSPFRecords(),
'enforceDNSLimit' => $outboundConfig->enforceDNSLimit(),
'enableDkim' => $outboundConfig->enableDkim(),
'enableMailManHeaders' => $outboundConfig->enableMailManHeaders(),
'enableDomainLockdown' => $outboundConfig->enableDomainLockdown()
];
$this->storeAsJson(self::OUTBOUND_CONFIG_FILE, $json);
}
private function storeAsJson($file, $obj) {
$json = null;
if ($obj instanceof \JsonSerializable) {
$json = json_encode($obj->jsonSerialize());
} else {
$json = json_encode($obj);
}
$lock = new FileLock($file);
$lock->runLocked(function() use ($file, $json) {
file_put_contents("$this->folder/" . $file, $json);
});
}
}