1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Collector; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Collector; |
5
|
|
|
use phpbu\App\Backup\Path; |
6
|
|
|
use phpbu\App\Backup\Target; |
7
|
|
|
use phpbu\App\Util; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Sftp class. |
11
|
|
|
* |
12
|
|
|
* @package phpbu |
13
|
|
|
* @subpackage Backup |
14
|
|
|
* @author Sebastian Feldmann <[email protected]> |
15
|
|
|
* @author Vitaly Baev <[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
|
|
|
* @since Class available since Release 5.1.0 |
20
|
|
|
*/ |
21
|
|
|
class Sftp extends Collector |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var \phpseclib\Net\SFTP |
25
|
|
|
*/ |
26
|
|
|
protected $sftp; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* SFTP remote path |
30
|
|
|
* |
31
|
|
|
* @var Path |
32
|
|
|
*/ |
33
|
|
|
protected $path; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* OpenStack constructor. |
37
|
|
|
* |
38
|
|
|
* @param \phpbu\App\Backup\Target $target |
39
|
|
|
* @param \phpseclib\Net\SFTP $sftp |
40
|
|
|
* @param Path $path |
41
|
|
|
*/ |
42
|
1 |
|
public function __construct(Target $target, \phpseclib\Net\SFTP $sftp, Path $path) |
43
|
|
|
{ |
44
|
1 |
|
$this->sftp = $sftp; |
45
|
1 |
|
$this->path = $path; |
46
|
1 |
|
$this->setUp($target); |
47
|
1 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get all created backups. |
51
|
|
|
* |
52
|
|
|
* @return \phpbu\App\Backup\File[] |
53
|
|
|
*/ |
54
|
1 |
|
public function getBackupFiles(): array |
55
|
|
|
{ |
56
|
1 |
|
$this->collect($this->path->getPathThatIsNotChanging()); |
57
|
1 |
|
return $this->files; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
private function collect(string $path) |
61
|
|
|
{ |
62
|
|
|
// collect all matching sub directories and get all the backup files |
63
|
1 |
|
$depth = Util\Path::getPathDepth($path); |
64
|
1 |
|
$list = $this->sftp->_list($path); |
65
|
1 |
|
if (!$list) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
1 |
|
if ($depth < $this->path->getPathDepth()) { |
69
|
|
|
foreach ($list as $file) { |
70
|
|
|
if (in_array($file['filename'], ['.', '..'])) { |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
if ($this->isValidDirectory($file, $depth)) { |
74
|
|
|
$this->collect($path . '/' . $file['filename']); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} else { |
78
|
1 |
|
$this->collectFiles($list, $path); |
79
|
|
|
} |
80
|
1 |
|
} |
81
|
|
|
|
82
|
1 |
|
private function collectFiles(array $fileList, string $path) |
83
|
|
|
{ |
84
|
1 |
|
foreach ($fileList as $file) { |
85
|
1 |
|
if (in_array($file['filename'], ['.', '..'])) { |
86
|
1 |
|
continue; |
87
|
|
|
} |
88
|
|
|
// skip currently created backup |
89
|
1 |
|
if ($path . '/' . $file['filename'] == $this->path->getPath() . '/' . $this->target->getFilename()) { |
90
|
1 |
|
continue; |
91
|
|
|
} |
92
|
1 |
|
if ($this->isFileMatch($path . '/' . $file['filename'])) { |
93
|
1 |
|
$file = new \phpbu\App\Backup\File\Sftp($this->sftp, $file, $path); |
94
|
1 |
|
$this->files[$file->getMTime()] = $file; |
95
|
|
|
} |
96
|
|
|
} |
97
|
1 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Check if the iterated file is part of a valid target path. |
101
|
|
|
* |
102
|
|
|
* @param array $file |
103
|
|
|
* @param int $depth |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
|
|
protected function isValidDirectory(array $file, int $depth) |
107
|
|
|
{ |
108
|
|
|
return $file['type'] == 2 && $this->isMatchingDirectory($file['filename'], $depth); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Does a directory match the respective target path. |
113
|
|
|
* |
114
|
|
|
* @param string $dir |
115
|
|
|
* @param int $depth |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
protected function isMatchingDirectory(string $dir, int $depth) |
119
|
|
|
{ |
120
|
|
|
$dirTarget = $this->path->getPathElementAtIndex($depth); |
121
|
|
|
$dirRegex = Util\Path::datePlaceholdersToRegex($dirTarget); |
122
|
|
|
return preg_match('#' . $dirRegex . '#i', $dir); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|