|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* sysPass |
|
4
|
|
|
* |
|
5
|
|
|
* @author nuxsmin |
|
6
|
|
|
* @link https://syspass.org |
|
7
|
|
|
* @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org |
|
8
|
|
|
* |
|
9
|
|
|
* This file is part of sysPass. |
|
10
|
|
|
* |
|
11
|
|
|
* sysPass is free software: you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU General Public License as published by |
|
13
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
14
|
|
|
* (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* sysPass is distributed in the hope that it will be useful, |
|
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19
|
|
|
* GNU General Public License for more details. |
|
20
|
|
|
* |
|
21
|
|
|
* You should have received a copy of the GNU General Public License |
|
22
|
|
|
* along with sysPass. If not, see <http://www.gnu.org/licenses/>. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace SP\Storage\File; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
use SP\Core\Exceptions\CheckException; |
|
29
|
|
|
use SP\Core\PhpExtensionChecker; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Class ArchiveHandler |
|
33
|
|
|
* |
|
34
|
|
|
* @package SP\Storage\File |
|
35
|
|
|
*/ |
|
36
|
|
|
final class ArchiveHandler |
|
37
|
|
|
{ |
|
38
|
|
|
const COMPRESS_EXTENSION = '.tar.gz'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var PhpExtensionChecker |
|
42
|
|
|
*/ |
|
43
|
|
|
private $extensionChecker; |
|
44
|
|
|
/** |
|
45
|
|
|
* @var FileHandler |
|
46
|
|
|
*/ |
|
47
|
|
|
private $archive; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* ArchiveHandler constructor. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $archive |
|
53
|
|
|
* @param PhpExtensionChecker $extensionChecker |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct(string $archive, PhpExtensionChecker $extensionChecker) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->extensionChecker = $extensionChecker; |
|
58
|
|
|
$this->archive = new FileHandler(self::makeArchiveName($archive)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $archive |
|
63
|
|
|
* |
|
64
|
|
|
* @return bool|string |
|
65
|
|
|
*/ |
|
66
|
|
|
private static function makeArchiveName(string $archive) |
|
67
|
|
|
{ |
|
68
|
|
|
$archiveExtension = substr(self::COMPRESS_EXTENSION, 0, strrpos(self::COMPRESS_EXTENSION, '.')); |
|
69
|
|
|
|
|
70
|
|
|
if (is_file($archive)) { |
|
71
|
|
|
return substr($archive, 0, strrpos($archive, '.')) . $archiveExtension; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $archive . $archiveExtension; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Realizar un backup de la aplicación y comprimirlo. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $directory |
|
81
|
|
|
* @param string|null $regex |
|
82
|
|
|
* |
|
83
|
|
|
* @throws CheckException |
|
84
|
|
|
* @throws FileException |
|
85
|
|
|
*/ |
|
86
|
|
|
public function compressDirectory(string $directory, string $regex = null) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->extensionChecker->checkPharAvailable(true); |
|
89
|
|
|
|
|
90
|
|
|
$archive = new \PharData($this->archive->getFile()); |
|
91
|
|
|
$archive->buildFromDirectory($directory, $regex); |
|
92
|
|
|
$archive->compress(\Phar::GZ); |
|
93
|
|
|
|
|
94
|
|
|
// Delete the non-compressed archive |
|
95
|
|
|
$this->archive->delete(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Realizar un backup de la aplicación y comprimirlo. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $file |
|
102
|
|
|
* |
|
103
|
|
|
* @throws CheckException |
|
104
|
|
|
* @throws FileException |
|
105
|
|
|
*/ |
|
106
|
|
|
public function compressFile(string $file) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->extensionChecker->checkPharAvailable(true); |
|
109
|
|
|
|
|
110
|
|
|
$archive = new \PharData($this->archive->getFile()); |
|
111
|
|
|
$archive->addFile($file, basename($file)); |
|
112
|
|
|
$archive->compress(\Phar::GZ); |
|
113
|
|
|
|
|
114
|
|
|
// Delete the non-compressed archive |
|
115
|
|
|
$this->archive->delete(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return FileHandler |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getArchive(): FileHandler |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->archive; |
|
124
|
|
|
} |
|
125
|
|
|
} |