Shikiryu_Backup/Transport/FTP.php

91 lines
2.4 KiB
PHP
Raw Normal View History

2015-07-03 00:00:30 +02:00
<?php
2015-07-09 00:46:17 +02:00
class Shikiryu_Backup_Transport_Ftp extends Shikiryu_Backup_Transport_Abstract
{
2015-07-03 00:00:30 +02:00
2015-07-09 00:46:17 +02:00
private $path;
2015-07-03 00:00:30 +02:00
2015-07-09 00:46:17 +02:00
private $connection;
2015-07-03 00:00:30 +02:00
2015-07-09 00:46:17 +02:00
private $files;
private $streams;
public function __construct($backup, $server, $login, $pwd, $path='/') {
parent::__construct($backup);
$this->path = $this->config['path'];
if (!empty($this->config['path'])) {
$this->path = sprintf('/%s/', ltrim(rtrim($this->config['path'], '/'),'/'));
2015-07-03 00:00:30 +02:00
}
2015-07-09 00:46:17 +02:00
$this->connection = ftp_connect($this->config['host']);
$login = ftp_login($this->connection, $this->config['login'], $this->config['password']);
if (!$this->connection || !$login) {
2015-07-03 00:00:30 +02:00
throw new Exception('Connexion FTP refusée.');
}
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
{
if (is_array($files) && !empty($files))
2015-07-09 00:46:17 +02:00
$this->files = $files;
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-03 00:00:30 +02:00
if (is_array($streams) && !empty($streams))
2015-07-09 00:46:17 +02:00
$this->streams = $streams;
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;
if (!empty($this->files)){
foreach ($this->files as $file => $name) {
// TODO PASSIVE MODE
$upload = ftp_put($this->connection, $this->path.$name, $file, FTP_ASCII);
2015-07-03 00:00:30 +02:00
if (!$upload) {
2015-07-09 00:46:17 +02:00
$sent = false;
echo 'FTP upload manquée de '.$file.' vers '.$this->path.$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-09 00:46:17 +02:00
// TODO PASSIVE MODE
$upload = ftp_put($this->connection, $this->path.$name, $name, FTP_ASCII);
2015-07-03 00:00:30 +02:00
if (!$upload) {
echo 'FTP upload manquée de '.$name.' vers '.$this->_path.$name;
2015-07-09 00:46:17 +02:00
$sent = false;
2015-07-03 00:00:30 +02:00
}
unlink($name);
}
}
}
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
}
}
?>