1 | <?php |
||
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() |
|
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) |
|
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 |
||
161 | |||
162 | /** |
||
163 | * Return list of remote directories to travers. |
||
164 | * |
||
165 | * @return array |
||
166 | */ |
||
167 | 1 | private function getRemoteDirectoryList() : array |
|
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 |
||
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: