File: //usr/local/mailchannels/storage/file/SSHFileStorage.php
<?php
namespace MailChannels;
require_once(dirname(__FILE__) . "/FileStorage.php");
class SSHFileStorage extends FileStorage {
private $ip;
private $sshKey;
public function __construct($ip, $sshKey, $folder='tmp') {
$this->ip = $ip;
$this->sshKey = $sshKey;
parent::__construct($folder);
}
public function setInboundConfig(InboundConfig $config) {
parent::setInboundConfig($config);
$this->uploadFile(self::INBOUND_CONFIG_FILE);
}
public function getInboundConfig() {
$this->downloadFile(self::INBOUND_CONFIG_FILE);
return parent::getInboundConfig();
}
protected function uploadFile($file) {
$filePath = "$this->folder/$file";
$remoteFolderPath = Config::FILE_STORAGE_FOLDER;
exec("scp -i $this->sshKey $filePath ec2-user@$this->ip:/tmp");
exec("ssh -i $this->sshKey ec2-user@$this->ip sudo mv /tmp/$file $remoteFolderPath");
}
protected function downloadFile($file) {
$remoteFolderPath = Config::FILE_STORAGE_FOLDER;
$remoteFilePath = "$remoteFolderPath/$file";
exec("ssh -i $this->sshKey ec2-user@$this->ip sudo cp $remoteFilePath /tmp");
exec("scp -i $this->sshKey ec2-user@$this->ip:/tmp/$file $this->folder");
exec("ssh -i $this->sshKey ec2-user@$this->ip sudo rm /tmp/$file");
}
}