|
1
|
|
|
<?php |
|
2
|
|
|
namespace phpbu\App\Backup\Sync; |
|
3
|
|
|
|
|
4
|
|
|
use League\Flysystem\Config; |
|
5
|
|
|
use League\Flysystem\Filesystem; |
|
6
|
|
|
use League\Flysystem\Adapter\Ftp as FtpAdapter; |
|
7
|
|
|
use phpbu\App\Result; |
|
8
|
|
|
use phpbu\App\Backup\Target; |
|
9
|
|
|
use phpbu\App\Util\Arr; |
|
10
|
|
|
use phpbu\App\Util\Str; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Ftp sync |
|
14
|
|
|
* |
|
15
|
|
|
* @package phpbu |
|
16
|
|
|
* @subpackage Backup |
|
17
|
|
|
* @author Chris Hawes <[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 implements Simulator |
|
23
|
|
|
{ |
|
24
|
|
|
use Clearable; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* FlySystem instance |
|
28
|
6 |
|
* |
|
29
|
|
|
* @var Filesystem |
|
30
|
6 |
|
*/ |
|
31
|
6 |
|
protected $flySystem; |
|
32
|
1 |
|
|
|
33
|
|
|
/** |
|
34
|
5 |
|
* Determine should ftp connects via passive mode. |
|
35
|
2 |
|
* |
|
36
|
|
|
* @var bool |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $passiveMode; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Setup the Ftp sync. |
|
42
|
5 |
|
* |
|
43
|
|
|
* @param array $config |
|
44
|
5 |
|
* @throws \phpbu\App\Backup\Sync\Exception |
|
45
|
|
|
* @throws \phpbu\App\Exception |
|
46
|
|
|
*/ |
|
47
|
5 |
|
public function setup(array $config) |
|
48
|
|
|
{ |
|
49
|
|
|
parent::setup($config); |
|
50
|
|
|
|
|
51
|
|
|
$this->passiveMode = Str::toBoolean(Arr::getValue($config, 'passive_mode', ''), false); |
|
52
|
|
|
$this->setUpClearable($config); |
|
53
|
|
|
} |
|
54
|
1 |
|
|
|
55
|
|
|
/** |
|
56
|
1 |
|
* Check for required loaded libraries or extensions. |
|
57
|
|
|
* |
|
58
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function checkRequirements() |
|
61
|
|
|
{ |
|
62
|
|
|
if (!function_exists('ftp_connect')) { |
|
63
|
|
|
throw new Exception('ftp functions not enabled'); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Return implemented (*)TP protocol name. |
|
69
|
|
|
* |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function getProtocolName() |
|
73
|
|
|
{ |
|
74
|
|
|
return 'FTP'; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* (non-PHPDoc) |
|
79
|
|
|
* |
|
80
|
|
|
* @see \phpbu\App\Backup\Sync::sync() |
|
81
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
82
|
|
|
* @param \phpbu\App\Result $result |
|
83
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
|
84
|
|
|
*/ |
|
85
|
|
|
public function sync(Target $target, Result $result) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->flySystem = new Filesystem(new FtpAdapter([ |
|
88
|
|
|
'host' => $this->host, |
|
89
|
|
|
'username' => $this->user, |
|
90
|
|
|
'password' => $this->password, |
|
91
|
|
|
'root' => $this->remotePath, |
|
92
|
|
|
'passive' => $this->passiveMode, |
|
93
|
|
|
'timeout' => 30, |
|
94
|
|
|
]), new Config([ |
|
95
|
|
|
'disable_asserts' => true, |
|
96
|
|
|
])); |
|
97
|
|
|
|
|
98
|
|
|
// silence ftp errors |
|
99
|
|
|
$old = error_reporting(0); |
|
100
|
|
|
|
|
101
|
|
|
try { |
|
102
|
|
|
if ($this->flySystem->has($target->getFilename())) { |
|
103
|
|
|
$this->flySystem->update($target->getFilename(), file_get_contents($target->getPathname())); |
|
104
|
|
|
} else { |
|
105
|
|
|
$this->flySystem->write($target->getFilename(), file_get_contents($target->getPathname())); |
|
106
|
|
|
} |
|
107
|
|
|
} catch (\Exception $exception) { |
|
108
|
|
|
throw new Exception($exception->getMessage()); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// run remote cleanup |
|
112
|
|
|
$this->cleanup($target, $result); |
|
113
|
|
|
|
|
114
|
|
|
error_reporting($old); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Execute the remote clean up if needed |
|
119
|
|
|
* |
|
120
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
121
|
|
|
* @param \phpbu\App\Result $result |
|
122
|
|
|
*/ |
|
123
|
|
|
public function cleanup(Target $target, Result $result) |
|
124
|
|
|
{ |
|
125
|
|
|
if (!$this->cleaner) { |
|
126
|
|
|
return; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$collector = new \phpbu\App\Backup\Collector\Ftp($target, $this->flySystem); |
|
130
|
|
|
$this->cleaner->cleanup($target, $collector, $result); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|