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
|
|
|
* Check for required loaded libraries or extensions. |
24
|
|
|
* |
25
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
26
|
|
|
*/ |
27
|
|
|
protected function checkRequirements() |
28
|
|
|
{ |
29
|
|
|
if (!function_exists('ftp_connect')) { |
30
|
|
|
throw new Exception('ftp functions not enabled'); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Return implemented (*)TP protocol name. |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
protected function getProtocolName() |
40
|
|
|
{ |
41
|
|
|
return 'FTP'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* (non-PHPDoc) |
46
|
|
|
* |
47
|
|
|
* @see \phpbu\App\Backup\Sync::sync() |
48
|
|
|
* @param \phpbu\App\Backup\Target $target |
49
|
|
|
* @param \phpbu\App\Result $result |
50
|
5 |
|
* @throws \phpbu\App\Backup\Sync\Exception |
51
|
|
|
*/ |
52
|
5 |
|
public function sync(Target $target, Result $result) |
53
|
|
|
{ |
54
|
|
|
// silence ftp errors |
55
|
5 |
|
$old = error_reporting(0); |
56
|
1 |
|
if (!$ftpConnection = ftp_connect($this->host)) { |
57
|
|
|
throw new Exception( |
58
|
4 |
|
sprintf( |
59
|
1 |
|
'Unable to connect to ftp server %s', |
60
|
|
|
$this->host |
61
|
3 |
|
) |
62
|
1 |
|
); |
63
|
|
|
} |
64
|
2 |
|
|
65
|
2 |
View Code Duplication |
if (!ftp_login($ftpConnection, $this->user, $this->password)) { |
66
|
1 |
|
error_reporting($old); |
67
|
|
|
throw new Exception( |
68
|
1 |
|
sprintf( |
69
|
1 |
|
'authentication failed for %s@%s%s', |
70
|
1 |
|
$this->user, |
71
|
1 |
|
$this->host, |
72
|
1 |
|
empty($this->password) ? '' : ' with password ****' |
73
|
|
|
) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$remoteFilename = $target->getFilename(); |
78
|
|
|
$localFile = $target->getPathname(); |
79
|
|
|
|
80
|
|
|
if ('' !== $this->remotePath) { |
81
|
|
|
$remoteDirs = explode('/', $this->remotePath); |
82
|
|
|
foreach ($remoteDirs as $dir) { |
83
|
|
|
if (!ftp_chdir($ftpConnection, $dir)) { |
84
|
|
|
$result->debug(sprintf('creating remote dir \'%s\'', $dir)); |
85
|
|
|
ftp_mkdir($ftpConnection, $dir); |
86
|
|
|
ftp_chdir($ftpConnection, $dir); |
87
|
|
|
} else { |
88
|
|
|
$result->debug(sprintf('change to remote dir \'%s\'', $dir)); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
$result->debug(sprintf('store file \'%s\' as \'%s\'', $localFile, $remoteFilename)); |
93
|
|
|
|
94
|
|
|
if (!ftp_put($ftpConnection, $remoteFilename, $localFile, FTP_BINARY)) { |
95
|
|
|
$error = error_get_last(); |
96
|
|
|
$message = $error['message']; |
97
|
|
|
throw new Exception(sprintf('error uploading file: %s - %s', $localFile, $message)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
error_reporting($old); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|