mirror of
https://github.com/Chouchen/Shikiryu_Backup.git
synced 2021-06-30 16:02:14 +02:00
Added validators and new transport
This commit is contained in:
@@ -234,6 +234,8 @@ class Email extends TransportAbstract
|
||||
*/
|
||||
public function send()
|
||||
{
|
||||
|
||||
// TODO check if file is empty
|
||||
|
||||
// Checking files are selected
|
||||
$zip = new \ZipArchive(); // Load zip library
|
||||
|
@@ -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()
|
||||
|
43
app/code/Transport/Folder.php
Normal file
43
app/code/Transport/Folder.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Backup\Transport;
|
||||
|
||||
|
||||
use Shikiryu\Backup\Backup\BackupAbstract;
|
||||
|
||||
class Folder extends TransportAbstract
|
||||
{
|
||||
|
||||
protected $folder;
|
||||
|
||||
public function __construct(BackupAbstract $backup, array $config = array())
|
||||
{
|
||||
parent::__construct($backup, $config);
|
||||
|
||||
if (!empty($this->folder)) {
|
||||
$this->folder = sprintf('%s/', rtrim($this->folder, '/'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function send()
|
||||
{
|
||||
foreach ($this->backup->getFilesToBackup() as $file => $name) {
|
||||
if (copy($file, $this->folder . $name) === false) {
|
||||
throw new \Exception(sprintf('Copy of %s in %s failed', $name, $this->folder));
|
||||
};
|
||||
}
|
||||
foreach ($this->backup->getStreamsToBackup() as $name => $file) {
|
||||
if (count(explode('.', $name)) < 2) {
|
||||
$name = 'backup' . $name . '.txt';
|
||||
}
|
||||
if (file_put_contents($this->folder . $name, $file) === false) {
|
||||
throw new \Exception(sprintf('Saving of %s in %s failed', $name, $this->folder));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
@@ -12,6 +12,10 @@ abstract class TransportAbstract
|
||||
|
||||
public function __construct(BackupAbstract $backup, array $config)
|
||||
{
|
||||
|
||||
foreach ($config as $name => $value) {
|
||||
$this->$name = $value;
|
||||
}
|
||||
$this->config = $config;
|
||||
$this->backup = $backup;
|
||||
}
|
||||
|
Reference in New Issue
Block a user