1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Sync; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Result; |
5
|
|
|
use phpbu\App\Backup\Sync; |
6
|
|
|
use phpbu\App\Backup\Target; |
7
|
|
|
use phpbu\App\Util\Arr; |
8
|
|
|
use phpbu\App\Util\Str; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Ftp sync |
12
|
|
|
* |
13
|
|
|
* @package phpbu |
14
|
|
|
* @subpackage Backup |
15
|
|
|
* @author Chris Hawes <[email protected]> |
16
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
17
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
18
|
|
|
* @link http://phpbu.de/ |
19
|
|
|
*/ |
20
|
|
|
class Ftp extends Xtp implements Simulator |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Setup the Ftp sync. |
24
|
|
|
* |
25
|
|
|
* @param array $config |
26
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
27
|
|
|
*/ |
28
|
6 |
|
public function setup(array $config) |
29
|
|
|
{ |
30
|
6 |
|
$path = Arr::getValue($config, 'path', ''); |
31
|
6 |
|
if ('/' === substr($path, 0, 1)) { |
32
|
1 |
|
throw new Exception('absolute path is not allowed'); |
33
|
|
|
} |
34
|
5 |
|
parent::setup($config); |
35
|
2 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Check for required loaded libraries or extensions. |
39
|
|
|
* |
40
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
41
|
|
|
*/ |
42
|
5 |
|
protected function checkRequirements() |
43
|
|
|
{ |
44
|
5 |
|
if (!function_exists('ftp_connect')) { |
45
|
|
|
throw new Exception('ftp functions not enabled'); |
46
|
|
|
} |
47
|
5 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return implemented (*)TP protocol name. |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
1 |
|
protected function getProtocolName() |
55
|
|
|
{ |
56
|
1 |
|
return 'FTP'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* (non-PHPDoc) |
61
|
|
|
* |
62
|
|
|
* @see \phpbu\App\Backup\Sync::sync() |
63
|
|
|
* @param \phpbu\App\Backup\Target $target |
64
|
|
|
* @param \phpbu\App\Result $result |
65
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
66
|
|
|
*/ |
67
|
|
|
public function sync(Target $target, Result $result) |
68
|
|
|
{ |
69
|
|
|
// silence ftp errors |
70
|
|
|
$old = error_reporting(0); |
71
|
|
|
if (!$ftpConnection = ftp_connect($this->host)) { |
72
|
|
|
throw new Exception( |
73
|
|
|
sprintf( |
74
|
|
|
'Unable to connect to ftp server %s', |
75
|
|
|
$this->host |
76
|
|
|
) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
if (!ftp_login($ftpConnection, $this->user, $this->password)) { |
81
|
|
|
error_reporting($old); |
82
|
|
|
throw new Exception( |
83
|
|
|
sprintf( |
84
|
|
|
'authentication failed for %s@%s%s', |
85
|
|
|
$this->user, |
86
|
|
|
$this->host, |
87
|
|
|
empty($this->password) ? '' : ' with password ****' |
88
|
|
|
) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$remoteFilename = $target->getFilename(); |
93
|
|
|
$localFile = $target->getPathname(); |
94
|
|
|
|
95
|
|
|
if ('' !== $this->remotePath) { |
96
|
|
|
$remoteDirs = explode('/', $this->remotePath); |
97
|
|
|
foreach ($remoteDirs as $dir) { |
98
|
|
|
if (!ftp_chdir($ftpConnection, $dir)) { |
99
|
|
|
$result->debug(sprintf('creating remote dir \'%s\'', $dir)); |
100
|
|
|
ftp_mkdir($ftpConnection, $dir); |
101
|
|
|
ftp_chdir($ftpConnection, $dir); |
102
|
|
|
} else { |
103
|
|
|
$result->debug(sprintf('change to remote dir \'%s\'', $dir)); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
$result->debug(sprintf('store file \'%s\' as \'%s\'', $localFile, $remoteFilename)); |
108
|
|
|
|
109
|
|
|
if (!ftp_put($ftpConnection, $remoteFilename, $localFile, FTP_BINARY)) { |
110
|
|
|
$error = error_get_last(); |
111
|
|
|
$message = $error['message']; |
112
|
|
|
throw new Exception(sprintf('error uploading file: %s - %s', $localFile, $message)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
error_reporting($old); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|