1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Crypter; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Restore\Plan; |
5
|
|
|
use phpbu\App\Backup\Target; |
6
|
|
|
use phpbu\App\Cli\Executable; |
7
|
|
|
use phpbu\App\Util; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Gpg crypter class |
11
|
|
|
* |
12
|
|
|
* @package phpbu |
13
|
|
|
* @subpackage Backup |
14
|
|
|
* @author Sebastian Feldmann <[email protected]> |
15
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
16
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
17
|
|
|
* @link https://phpbu.de/ |
18
|
|
|
* @since Class available since Release 6.0.1 |
19
|
|
|
*/ |
20
|
|
|
class Gpg extends Abstraction implements Simulator, Restorable |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Path to gpg command |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $pathToGpg; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Gpg user name |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $user; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Keep the not encrypted file |
38
|
|
|
* |
39
|
|
|
* @var bool |
40
|
|
|
*/ |
41
|
|
|
private $keepUncrypted; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Setup |
45
|
|
|
* |
46
|
|
|
* @see \phpbu\App\Backup\Crypter |
47
|
|
|
* @param array $options |
48
|
|
|
* @throws Exception |
49
|
|
|
*/ |
50
|
6 |
|
public function setup(array $options = []) |
51
|
|
|
{ |
52
|
6 |
|
if (!Util\Arr::isSetAndNotEmptyString($options, 'user')) { |
53
|
1 |
|
throw new Exception('gpg expects \'user\''); |
54
|
|
|
} |
55
|
|
|
|
56
|
5 |
|
$this->pathToGpg = Util\Arr::getValue($options, 'pathToOpenSSL', ''); |
57
|
5 |
|
$this->keepUncrypted = Util\Str::toBoolean(Util\Arr::getValue($options, 'keepUncrypted', ''), false); |
58
|
5 |
|
$this->user = $this->toAbsolutePath(Util\Arr::getValue($options, 'user', '')); |
59
|
5 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Return file suffix of encrypted target |
63
|
|
|
* |
64
|
|
|
* @see \phpbu\App\Backup\Crypter |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
1 |
|
public function getSuffix() : string |
68
|
|
|
{ |
69
|
1 |
|
return Executable\Gpg::SUFFIX; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Decrypt the backup |
74
|
|
|
* |
75
|
|
|
* @param \phpbu\App\Backup\Target $target |
76
|
|
|
* @param \phpbu\App\Backup\Restore\Plan $plan |
77
|
|
|
*/ |
78
|
1 |
|
public function restore(Target $target, Plan $plan) |
79
|
|
|
{ |
80
|
1 |
|
$executable = $this->createDecryptionGpg($target); |
81
|
1 |
|
$plan->addDecryptionCommand($executable->getCommand()); |
82
|
1 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Create the Executable to run the 'gpg' command |
86
|
|
|
* |
87
|
|
|
* @param \phpbu\App\Backup\Target $target |
88
|
|
|
* @return \phpbu\App\Cli\Executable |
89
|
|
|
*/ |
90
|
3 |
|
protected function createExecutable(Target $target): Executable |
91
|
|
|
{ |
92
|
3 |
|
return $this->createEncryptionGpg($target); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Create encryption Gpg |
97
|
|
|
* |
98
|
|
|
* @param \phpbu\App\Backup\Target $target |
99
|
|
|
* @return \phpbu\App\Cli\Executable\Gpg |
100
|
|
|
*/ |
101
|
3 |
|
private function createEncryptionGpg(Target $target): Executable\Gpg |
102
|
|
|
{ |
103
|
3 |
|
$executable = $this->createGpg($target); |
104
|
3 |
|
$executable->encryptFile($target->getPathname()) |
105
|
3 |
|
->deleteSource(!$this->keepUncrypted); |
106
|
|
|
|
107
|
3 |
|
return $executable; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Create decryption Gpg |
112
|
|
|
* |
113
|
|
|
* @param \phpbu\App\Backup\Target $target |
114
|
|
|
* @return \phpbu\App\Cli\Executable\Gpg |
115
|
|
|
*/ |
116
|
1 |
|
private function createDecryptionGpg(Target $target): Executable\Gpg |
117
|
|
|
{ |
118
|
1 |
|
$executable = $this->createGpg($target); |
119
|
1 |
|
$executable->decryptFile($target->getPathname()) |
120
|
1 |
|
->deleteSource(false); |
121
|
|
|
|
122
|
1 |
|
return $executable; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Setup an Gpg executable only thing missing is the decision of en or decryption |
127
|
|
|
* |
128
|
|
|
* @param \phpbu\App\Backup\Target $target |
129
|
|
|
* @return \phpbu\App\Cli\Executable\Gpg |
130
|
|
|
*/ |
131
|
4 |
|
private function createGpg(Target $target): Executable\Gpg |
132
|
|
|
{ |
133
|
4 |
|
$executable = new Executable\Gpg($this->pathToGpg); |
134
|
4 |
|
$executable->useUser($this->user); |
135
|
|
|
|
136
|
4 |
|
return $executable; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|