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 Remote implements Collector |
||
22 | { |
||
23 | /** |
||
24 | * @var \phpseclib\Net\SFTP |
||
25 | */ |
||
26 | protected $sftp; |
||
27 | |||
28 | /** |
||
29 | * OpenStack constructor. |
||
30 | * |
||
31 | * @param \phpbu\App\Backup\Target $target |
||
32 | * @param \phpbu\App\Backup\Path $path |
||
33 | * @param \phpseclib\Net\SFTP $sftp |
||
34 | */ |
||
35 | 4 | public function __construct(Target $target, Path $path, \phpseclib\Net\SFTP $sftp) |
|
36 | { |
||
37 | 4 | $this->setUp($target, $path); |
|
38 | 4 | $this->sftp = $sftp; |
|
39 | 4 | } |
|
40 | |||
41 | /** |
||
42 | * Get all created backups. |
||
43 | * |
||
44 | * @return \phpbu\App\Backup\File[] |
||
45 | */ |
||
46 | 4 | protected function collectBackups(): array |
|
47 | { |
||
48 | 4 | $this->collect($this->path->getPathThatIsNotChanging()); |
|
49 | 4 | return $this->files; |
|
0 ignored issues
–
show
|
|||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Collect all remote files in all matching directories. |
||
54 | * |
||
55 | * @param string $path |
||
56 | */ |
||
57 | 4 | private function collect(string $path) |
|
58 | { |
||
59 | // collect all matching sub directories and get all the backup files |
||
60 | 4 | $depth = Util\Path::getPathDepth($path); |
|
61 | 4 | $list = $this->sftp->_list($path); |
|
62 | 4 | if (!$list) { |
|
63 | 2 | return; |
|
64 | } |
||
65 | 2 | if ($depth < $this->path->getPathDepth()) { |
|
66 | 1 | foreach ($list as $file) { |
|
67 | 1 | if (in_array($file['filename'], ['.', '..'])) { |
|
68 | 1 | continue; |
|
69 | } |
||
70 | 1 | if ($this->isValidDirectory($file, $depth)) { |
|
71 | 1 | $this->collect($path . '/' . $file['filename']); |
|
72 | } |
||
73 | } |
||
74 | } else { |
||
75 | 2 | $this->collectFiles($list, $path); |
|
76 | } |
||
77 | 2 | } |
|
78 | |||
79 | /** |
||
80 | * Collect matching files in a directory. |
||
81 | * |
||
82 | * @param array $fileList |
||
83 | * @param string $path |
||
84 | */ |
||
85 | 2 | private function collectFiles(array $fileList, string $path) |
|
86 | { |
||
87 | 2 | foreach ($fileList as $file) { |
|
88 | 2 | if (in_array($file['filename'], ['.', '..'])) { |
|
89 | 2 | continue; |
|
90 | } |
||
91 | 2 | if ($this->isFileMatch($path . '/' . $file['filename'])) { |
|
92 | 2 | $file = new \phpbu\App\Backup\File\Sftp($this->sftp, $file, $path); |
|
93 | 2 | $index = $this->getFileIndex($file); |
|
94 | 2 | $this->files[$index] = $file; |
|
95 | } |
||
96 | } |
||
97 | 2 | } |
|
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 | 1 | protected function isValidDirectory(array $file, int $depth) |
|
107 | { |
||
108 | 1 | 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 | 1 | protected function isMatchingDirectory(string $dir, int $depth) |
|
119 | { |
||
120 | 1 | $dirTarget = $this->path->getPathElementAtIndex($depth); |
|
121 | 1 | $dirRegex = Util\Path::datePlaceholdersToRegex($dirTarget); |
|
122 | 1 | return preg_match('#' . $dirRegex . '#i', $dir); |
|
0 ignored issues
–
show
|
|||
123 | } |
||
124 | } |
||
125 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: