Shikiryu_Backup/app/code/Transport/FTP.php

102 lines
2.8 KiB
PHP
Raw Normal View History

2015-07-03 00:00:30 +02:00
<?php
2016-07-03 21:54:35 +02:00
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;
2016-07-03 21:54:35 +02:00
public function __construct($backup, $config)
{
2015-07-12 20:34:54 +02:00
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)) {
2016-07-03 21:54:35 +02:00
$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) {
2016-07-03 21:54:35 +02:00
$msg = sprintf('Connexion FTP %s refusée avec %s et %s', $this->host, $this->login, $this->password);
throw new \Exception($msg);
2015-07-03 00:00:30 +02:00
}
2015-07-09 00:46:17 +02:00
2016-07-03 21:54:35 +02:00
$this->setFiles($this->backup->getFilesTobackup());
$this->setStreams($this->backup->getStreamsTobackup());
2015-07-03 00:00:30 +02:00
}
2016-07-03 21:54:35 +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
2016-07-03 21:54:35 +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;
}
2016-07-03 21:54:35 +02:00
2016-07-01 21:59:39 +02:00
/**
* @return bool
*/
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);
2016-07-03 21:54:35 +02:00
if (!empty($this->files)) {
2015-07-09 00:46:17 +02:00
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
}
}
}
2016-07-03 21:54:35 +02:00
if (!empty($this->streams)) {
2015-07-09 00:46:17 +02:00
foreach ($this->streams as $name => $stream) {
2016-07-03 21:54:35 +02:00
if (count(explode('.', $name)) < 2) {
2015-07-03 00:00:30 +02:00
$name = 'backup' . $name . '.txt';
2016-07-03 21:54:35 +02:00
}
2015-07-03 00:00:30 +02:00
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.');
}
2016-07-03 21:54:35 +02:00
2016-07-01 21:59:39 +02:00
return $sent;
2015-07-03 00:00:30 +02:00
}
2016-07-03 21:54:35 +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
}
}