Added validators and new transport

This commit is contained in:
Shikiryu
2015-07-12 20:34:54 +02:00
parent 091dd4cf22
commit 8426972504
9 changed files with 165 additions and 138 deletions

View File

@@ -4,26 +4,32 @@ namespace Shikiryu\Backup\Transport;
class Ftp extends TransportAbstract
{
private $path;
private $connection;
protected $host;
protected $login;
protected $password;
protected $folder;
private $files;
private $streams;
public function __construct($backup, $server, $login, $pwd, $path='/') {
parent::__construct($backup);
public function __construct($backup, $config) {
parent::__construct($backup, $config);
$this->path = $this->config['path'];
if (!empty($this->config['path'])) {
$this->path = sprintf('/%s/', ltrim(rtrim($this->config['path'], '/'),'/'));
// $this->path = $this->config['path'];
if (!empty($this->folder)) {
$this->folder = sprintf('/%s/', ltrim(rtrim($this->folder, '/'),'/'));
}
$this->connection = ftp_connect($this->config['host']);
$this->connection = ftp_connect($this->host);
if ($this->connection == false) {
throw new \Exception(sprintf('I can\'t connect to the FTP %s', $this->host));
}
$login = ftp_login($this->connection, $this->config['login'], $this->config['password']);
if (!$this->connection || !$login) {
throw new Exception('Connexion FTP refusée.');
$login = @ftp_login($this->connection, $this->login, $this->password);
if ($login === false) {
throw new \Exception(sprintf('Connexion FTP %s refusée avec %s et %s', $this->host, $this->login, $this->password));
}
$this->setFiles($this->backup->getFilesToBackup());
@@ -32,27 +38,29 @@ class Ftp extends TransportAbstract
private function setFiles($files = array())
{
if (is_array($files) && !empty($files))
if (is_array($files) && !empty($files)) {
$this->files = $files;
}
return $this;
}
private function setStreams($streams = array()) {
if (is_array($streams) && !empty($streams))
if (is_array($streams) && !empty($streams)) {
$this->streams = $streams;
}
return $this;
}
public function send()
{
$sent = true;
ftp_pasv($this->connection, true);
if (!empty($this->files)){
foreach ($this->files as $file => $name) {
// TODO PASSIVE MODE
$upload = ftp_put($this->connection, $this->path.$name, $file, FTP_ASCII);
$upload = ftp_put($this->connection, $this->folder.$name, $file, FTP_BINARY);
if (!$upload) {
$sent = false;
echo 'FTP upload manquée de '.$file.' vers '.$this->path.$name;
echo 'FTP upload manquée de '.$file.' vers '.$this->folder.$name;
}
}
}
@@ -62,15 +70,18 @@ class Ftp extends TransportAbstract
if (count(explode('.', $name)) < 2)
$name = 'backup' . $name . '.txt';
file_put_contents($name, $stream);
// TODO PASSIVE MODE
$upload = ftp_put($this->connection, $this->path.$name, $name, FTP_ASCII);
$upload = ftp_put($this->connection, $this->folder.$name, $name, FTP_ASCII);
if (!$upload) {
echo 'FTP upload manquée de '.$name.' vers '.$this->_path.$name;
echo 'FTP upload manquée de '.$name.' vers '.$this->folder.$name;
$sent = false;
}
unlink($name);
}
}
if (!$sent) {
throw new \Exception('At least an upload didnt work.');
}
}
public function __destruct()