|
1
|
|
|
<?php |
|
2
|
|
|
namespace phpbu\App\Backup\Sync; |
|
3
|
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Collector; |
|
5
|
|
|
use phpbu\App\Backup\Target; |
|
6
|
|
|
use phpbu\App\Configuration; |
|
7
|
|
|
use phpbu\App\Result; |
|
8
|
|
|
use phpbu\App\Util; |
|
9
|
|
|
use phpseclib; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Sftp sync |
|
13
|
|
|
* |
|
14
|
|
|
* @package phpbu |
|
15
|
|
|
* @subpackage Backup |
|
16
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
17
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
|
18
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
19
|
|
|
* @link http://phpbu.de/ |
|
20
|
|
|
* @since Class available since Release 1.0.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
class Sftp extends Xtp |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var phpseclib\Net\SFTP |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $sftp; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $privateKey; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $privateKeyPassword; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* (non-PHPDoc) |
|
41
|
|
|
* |
|
42
|
|
|
* @see \phpbu\App\Backup\Sync::setup() |
|
43
|
|
|
* @param array $config |
|
44
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
|
45
|
|
|
* @throws \phpbu\App\Exception |
|
46
|
|
|
*/ |
|
47
|
9 |
|
public function setup(array $config) |
|
48
|
|
|
{ |
|
49
|
9 |
|
if (!Util\Arr::isSetAndNotEmptyString($config, 'password') && !Util\Arr::isSetAndNotEmptyString($config, 'private_key')) { |
|
50
|
4 |
|
throw new Exception('\'password\' or \'private_key\' must be presented'); |
|
51
|
|
|
} |
|
52
|
5 |
|
parent::setup($config); |
|
53
|
|
|
|
|
54
|
5 |
|
$this->time = time(); |
|
|
|
|
|
|
55
|
5 |
|
$privateKey = Util\Arr::getValue($config, 'private_key', ''); |
|
56
|
5 |
|
if (!empty($privateKey)) { |
|
57
|
|
|
// get absolute private key path |
|
58
|
2 |
|
$privateKey = realpath(Util\Path::toAbsolutePath($privateKey, Configuration::getWorkingDirectory())); |
|
59
|
2 |
|
if ($privateKey === false) { |
|
60
|
1 |
|
throw new \phpbu\App\Backup\Sync\Exception("Private key not found at specified path"); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
4 |
|
$this->privateKey = $privateKey; |
|
64
|
4 |
|
$this->privateKeyPassword = Util\Arr::getValue($config, 'private_key_password', ''); |
|
65
|
|
|
|
|
66
|
4 |
|
$this->setUpClearable($config); |
|
67
|
4 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Check for required loaded libraries or extensions. |
|
71
|
|
|
* |
|
72
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
|
73
|
|
|
*/ |
|
74
|
5 |
|
protected function checkRequirements() |
|
75
|
|
|
{ |
|
76
|
5 |
|
if (!class_exists('\\phpseclib\\Net\\SFTP')) { |
|
77
|
|
|
throw new Exception('phpseclib not installed - use composer to install "phpseclib/phpseclib" version 2.x'); |
|
78
|
|
|
} |
|
79
|
5 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Return implemented (*)TP protocol name. |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
1 |
|
protected function getProtocolName() |
|
87
|
|
|
{ |
|
88
|
1 |
|
return 'SFTP'; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* (non-PHPDoc) |
|
93
|
|
|
* |
|
94
|
|
|
* @see \phpbu\App\Backup\Sync::sync() |
|
95
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
96
|
|
|
* @param \phpbu\App\Result $result |
|
97
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function sync(Target $target, Result $result) |
|
100
|
|
|
{ |
|
101
|
1 |
|
$this->sftp = $this->login(); |
|
102
|
1 |
|
$remoteFilename = $target->getFilename(); |
|
103
|
1 |
|
$localFile = $target->getPathname(); |
|
104
|
|
|
|
|
105
|
1 |
|
foreach ($this->getRemoteDirectoryList() as $dir) { |
|
106
|
1 |
|
if (!$this->sftp->is_dir($dir)) { |
|
107
|
1 |
|
$result->debug(sprintf('creating remote dir \'%s\'', $dir)); |
|
108
|
1 |
|
$this->sftp->mkdir($dir); |
|
109
|
|
|
} |
|
110
|
1 |
|
$result->debug(sprintf('change to remote dir \'%s\'', $dir)); |
|
111
|
1 |
|
$this->sftp->chdir($dir); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
$result->debug(sprintf('store file \'%s\' as \'%s\'', $localFile, $remoteFilename)); |
|
115
|
1 |
|
$result->debug(sprintf('last error \'%s\'', $this->sftp->getLastSFTPError())); |
|
116
|
|
|
|
|
117
|
1 |
|
if (!$this->sftp->put($remoteFilename, $localFile, phpseclib\Net\SFTP::SOURCE_LOCAL_FILE)) { |
|
118
|
|
|
throw new Exception(sprintf('error uploading file: %s - %s', $localFile, $this->sftp->getLastSFTPError())); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
// run remote cleanup |
|
122
|
1 |
|
$this->cleanup($target, $result); |
|
123
|
1 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Create a sftp handle. |
|
127
|
|
|
* |
|
128
|
|
|
* @return \phpseclib\Net\SFTP |
|
129
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function login() : phpseclib\Net\SFTP |
|
132
|
|
|
{ |
|
133
|
|
|
// silence phpseclib |
|
134
|
|
|
$old = error_reporting(0); |
|
135
|
|
|
$sftp = new phpseclib\Net\SFTP($this->host); |
|
136
|
|
|
if ($this->privateKey) { |
|
137
|
|
|
$auth = new phpseclib\Crypt\RSA(); |
|
138
|
|
|
$auth->loadKey(file_get_contents($this->privateKey)); |
|
139
|
|
|
if ($this->privateKeyPassword) { |
|
140
|
|
|
$auth->setPassword($this->privateKeyPassword); |
|
141
|
|
|
} |
|
142
|
|
|
} else { |
|
143
|
|
|
$auth = $this->password; |
|
144
|
|
|
} |
|
145
|
|
|
if (!$sftp->login($this->user, $auth)) { |
|
146
|
|
|
error_reporting($old); |
|
147
|
|
|
throw new Exception( |
|
148
|
|
|
sprintf( |
|
149
|
|
|
'authentication failed for %s@%s%s', |
|
150
|
|
|
$this->user, |
|
151
|
|
|
$this->host, |
|
152
|
|
|
empty($this->password) ? '' : ' with password ****' |
|
153
|
|
|
) |
|
154
|
|
|
); |
|
155
|
|
|
} |
|
156
|
|
|
// restore old error reporting |
|
157
|
|
|
error_reporting($old); |
|
158
|
|
|
|
|
159
|
|
|
return $sftp; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Return list of remote directories to travers. |
|
164
|
|
|
* |
|
165
|
|
|
* @return array |
|
166
|
|
|
*/ |
|
167
|
1 |
|
private function getRemoteDirectoryList() : array |
|
168
|
|
|
{ |
|
169
|
1 |
|
return Util\Path::getDirectoryListFromAbsolutePath($this->remotePath); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Creates collector for SFTP |
|
174
|
|
|
* |
|
175
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
176
|
|
|
* @return \phpbu\App\Backup\Collector |
|
177
|
|
|
*/ |
|
178
|
|
|
protected function createCollector(Target $target): Collector |
|
179
|
|
|
{ |
|
180
|
|
|
return new Collector\Sftp($target, $this->sftp, $this->remotePath); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: