1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Sync; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Collector; |
5
|
|
|
use phpbu\App\Backup\Path; |
6
|
|
|
use phpbu\App\Backup\Target; |
7
|
|
|
use phpbu\App\Result; |
8
|
|
|
use phpbu\App\Util; |
9
|
|
|
use SebastianFeldmann\Ftp\Client; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Ftp sync |
13
|
|
|
* |
14
|
|
|
* @package phpbu |
15
|
|
|
* @subpackage Backup |
16
|
|
|
* @author Chris Hawes <[email protected]> |
17
|
|
|
* @author Sebastian Feldmann <[email protected]> |
18
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
19
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
20
|
|
|
* @link http://phpbu.de/ |
21
|
|
|
*/ |
22
|
|
|
class Ftp extends Xtp |
23
|
|
|
{ |
24
|
|
|
use Cleanable; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* FTP connection stream |
28
|
|
|
* |
29
|
|
|
* @var \SebastianFeldmann\Ftp\Client |
30
|
|
|
*/ |
31
|
|
|
protected $ftpClient; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Determine should ftp connects via passive mode. |
35
|
|
|
* |
36
|
|
|
* @var bool |
37
|
|
|
*/ |
38
|
|
|
protected $passive; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Setup the Ftp sync. |
42
|
|
|
* |
43
|
|
|
* @param array $config |
44
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
45
|
|
|
* @throws \phpbu\App\Exception |
46
|
|
|
*/ |
47
|
6 |
|
public function setup(array $config) |
48
|
|
|
{ |
49
|
6 |
|
$path = Util\Arr::getValue($config, 'path', ''); |
50
|
6 |
|
if ('/' === substr($path, 0, 1)) { |
51
|
1 |
|
throw new Exception('absolute path is not allowed'); |
52
|
|
|
} |
53
|
5 |
|
if (!Util\Arr::isSetAndNotEmptyString($config, 'password')) { |
54
|
1 |
|
throw new Exception('option \'password\' is missing'); |
55
|
|
|
} |
56
|
4 |
|
parent::setup($config); |
57
|
|
|
|
58
|
2 |
|
$this->passive = Util\Str::toBoolean(Util\Arr::getValue($config, 'passive', ''), false); |
59
|
2 |
|
$this->setUpCleanable($config); |
60
|
2 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Check for required loaded libraries or extensions. |
64
|
|
|
* |
65
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
66
|
|
|
*/ |
67
|
4 |
|
protected function checkRequirements() |
68
|
|
|
{ |
69
|
4 |
|
if (!function_exists('ftp_connect')) { |
70
|
|
|
throw new Exception('ftp functions not enabled'); |
71
|
|
|
} |
72
|
4 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Return implemented (*)TP protocol name. |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
1 |
|
protected function getProtocolName() |
80
|
|
|
{ |
81
|
1 |
|
return 'FTP'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* (non-PHPDoc) |
86
|
|
|
* |
87
|
|
|
* @see \phpbu\App\Backup\Sync::sync() |
88
|
|
|
* @param \phpbu\App\Backup\Target $target |
89
|
|
|
* @param \phpbu\App\Result $result |
90
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
91
|
|
|
*/ |
92
|
|
|
public function sync(Target $target, Result $result) |
93
|
|
|
{ |
94
|
|
|
try { |
95
|
|
|
$client = $this->connect(); |
96
|
|
|
$remoteFilename = $target->getFilename(); |
97
|
|
|
$localFile = $target->getPathname(); |
98
|
|
|
|
99
|
|
|
$client->uploadFile($localFile, Util\Path::withTrailingSlash($this->remotePath), $remoteFilename); |
100
|
|
|
$result->debug(sprintf('store file \'%s\' as \'%s\'', $localFile, $remoteFilename)); |
101
|
|
|
|
102
|
|
|
} catch (\Exception $e) { |
103
|
|
|
throw new Exception($e->getMessage()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$this->cleanup($target, $result); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return FTP client wrapping the ftp connection. |
111
|
|
|
* |
112
|
|
|
* @return \SebastianFeldmann\Ftp\Client |
113
|
|
|
*/ |
114
|
|
|
protected function connect() |
115
|
|
|
{ |
116
|
|
|
if ($this->ftpClient === null) { |
117
|
|
|
$login = $this->user . ($this->password ? ':' . $this->password : ''); |
118
|
|
|
$this->ftpClient = new Client('ftp://' . $login . '@' . $this->host, $this->passive); |
119
|
|
|
} |
120
|
|
|
return $this->ftpClient; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Creates collector for FTP |
125
|
|
|
* |
126
|
|
|
* @param \phpbu\App\Backup\Target $target |
127
|
|
|
* @return \phpbu\App\Backup\Collector |
128
|
|
|
*/ |
129
|
|
|
protected function createCollector(Target $target): Collector |
130
|
|
|
{ |
131
|
|
|
return new Collector\Ftp($target, new Path($this->remotePath), $this->ftpClient); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|