Shikiryu_Backup/app/code/Transport/FTP.php

103 lines
2.7 KiB
PHP
Raw Normal View History

2015-07-03 00:00:30 +02:00
<?php
2015-07-10 19:07:14 +02:00
namespace Shikiryu\Backup\Transport;
2015-07-03 00:00:30 +02:00
2015-07-10 19:07:14 +02:00
class Ftp extends TransportAbstract
2015-07-09 00:46:17 +02:00
{
2015-07-03 00:00:30 +02:00
2015-07-09 00:46:17 +02:00
private $connection;
2015-07-12 20:34:54 +02:00
protected $host;
protected $login;
protected $password;
protected $folder;
2015-07-09 00:46:17 +02:00
private $files;
private $streams;
2015-07-12 20:34:54 +02:00
public function __construct($backup, $config) {
parent::__construct($backup, $config);
2015-07-09 00:46:17 +02:00
2015-07-12 20:34:54 +02:00
// $this->path = $this->config['path'];
if (!empty($this->folder)) {
$this->folder = sprintf('/%s/', ltrim(rtrim($this->folder, '/'),'/'));
2015-07-03 00:00:30 +02:00
}
2015-07-12 20:34:54 +02:00
$this->connection = ftp_connect($this->host);
if ($this->connection == false) {
throw new \Exception(sprintf('I can\'t connect to the FTP %s', $this->host));
}
2015-07-09 00:46:17 +02:00
2015-07-12 20:34:54 +02:00
$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));
2015-07-03 00:00:30 +02:00
}
2015-07-09 00:46:17 +02:00
$this->setFiles($this->backup->getFilesToBackup());
$this->setStreams($this->backup->getStreamsToBackup());
2015-07-03 00:00:30 +02:00
}
2015-07-09 00:46:17 +02:00
private function setFiles($files = array())
2015-07-03 00:00:30 +02:00
{
2015-07-12 20:34:54 +02:00
if (is_array($files) && !empty($files)) {
2015-07-09 00:46:17 +02:00
$this->files = $files;
2015-07-12 20:34:54 +02:00
}
2015-07-03 00:00:30 +02:00
return $this;
}
2015-07-09 00:46:17 +02:00
private function setStreams($streams = array()) {
2015-07-12 20:34:54 +02:00
if (is_array($streams) && !empty($streams)) {
2015-07-09 00:46:17 +02:00
$this->streams = $streams;
2015-07-12 20:34:54 +02:00
}
2015-07-03 00:00:30 +02:00
return $this;
}
2015-07-09 00:46:17 +02:00
public function send()
2015-07-03 00:00:30 +02:00
{
2015-07-09 00:46:17 +02:00
$sent = true;
2015-07-12 20:34:54 +02:00
ftp_pasv($this->connection, true);
2015-07-09 00:46:17 +02:00
if (!empty($this->files)){
foreach ($this->files as $file => $name) {
2015-07-12 20:34:54 +02:00
$upload = ftp_put($this->connection, $this->folder.$name, $file, FTP_BINARY);
2015-07-03 00:00:30 +02:00
if (!$upload) {
2015-07-09 00:46:17 +02:00
$sent = false;
2015-07-12 20:34:54 +02:00
echo 'FTP upload manquée de '.$file.' vers '.$this->folder.$name;
2015-07-03 00:00:30 +02:00
}
}
}
2015-07-09 00:46:17 +02:00
if (!empty($this->streams)){
foreach ($this->streams as $name => $stream) {
2015-07-03 00:00:30 +02:00
if (count(explode('.', $name)) < 2)
$name = 'backup' . $name . '.txt';
file_put_contents($name, $stream);
2015-07-12 20:34:54 +02:00
$upload = ftp_put($this->connection, $this->folder.$name, $name, FTP_ASCII);
2015-07-03 00:00:30 +02:00
if (!$upload) {
2015-07-12 20:34:54 +02:00
echo 'FTP upload manquée de '.$name.' vers '.$this->folder.$name;
2015-07-09 00:46:17 +02:00
$sent = false;
2015-07-03 00:00:30 +02:00
}
unlink($name);
}
}
2015-07-12 20:34:54 +02:00
if (!$sent) {
throw new \Exception('At least an upload didnt work.');
}
2015-07-03 00:00:30 +02:00
}
2015-07-09 00:46:17 +02:00
public function __destruct()
2015-07-03 00:00:30 +02:00
{
2015-07-09 00:46:17 +02:00
ftp_close($this->connection);
2015-07-03 00:00:30 +02:00
}
}
?>