1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Cli\Executable; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Cli\Executable; |
5
|
|
|
use phpbu\App\Exception; |
6
|
|
|
use SebastianFeldmann\Cli\CommandLine; |
7
|
|
|
use SebastianFeldmann\Cli\Command\Executable as Cmd; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Mcrypt executable 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 http://phpbu.de/ |
18
|
|
|
* @since Class available since Release 2.1.0 |
19
|
|
|
*/ |
20
|
|
|
class Mcrypt extends Abstraction implements Executable |
21
|
|
|
{ |
22
|
|
|
use OptionMasker; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Key to pass via cli |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $key; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Key file |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $keyFile; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Algorithm to use |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $algorithm; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Hash to use |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $hash; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Path to config file |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $config; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Keep the not encrypted file |
61
|
|
|
* |
62
|
|
|
* @var boolean |
63
|
|
|
*/ |
64
|
|
|
private $deleteUncrypted = true; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Path to the encrypted file |
68
|
|
|
* |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
private $targetFile; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Constructor. |
75
|
|
|
* |
76
|
8 |
|
* @param string $path |
77
|
|
|
*/ |
78
|
8 |
|
public function __construct(string $path = '') |
79
|
8 |
|
{ |
80
|
8 |
|
$this->setup('mcrypt', $path); |
81
|
|
|
$this->setMaskCandidates(['key']); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Set the target file. |
86
|
|
|
* |
87
|
|
|
* @param string $path |
88
|
7 |
|
* @return \phpbu\App\Cli\Executable\Mcrypt |
89
|
|
|
*/ |
90
|
7 |
|
public function saveAt(string $path) : Mcrypt |
91
|
7 |
|
{ |
92
|
|
|
$this->targetFile = $path; |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Delete the uncrypted data. |
98
|
|
|
* |
99
|
|
|
* @param bool $bool |
100
|
2 |
|
* @return \phpbu\App\Cli\Executable\Mcrypt |
101
|
|
|
*/ |
102
|
2 |
|
public function deleteUncrypted(bool $bool) : Mcrypt |
103
|
2 |
|
{ |
104
|
|
|
$this->deleteUncrypted = $bool; |
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Key to use for encryption. |
110
|
|
|
* |
111
|
|
|
* @param string $key |
112
|
5 |
|
* @return \phpbu\App\Cli\Executable\Mcrypt |
113
|
|
|
*/ |
114
|
5 |
|
public function useKey(string $key) : Mcrypt |
115
|
5 |
|
{ |
116
|
|
|
$this->key = $key; |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Key file to use for encryption. |
122
|
|
|
* |
123
|
|
|
* @param string $keyFile |
124
|
3 |
|
* @return \phpbu\App\Cli\Executable\Mcrypt |
125
|
|
|
*/ |
126
|
3 |
|
public function useKeyFile(string $keyFile) : Mcrypt |
127
|
3 |
|
{ |
128
|
|
|
$this->keyFile = $keyFile; |
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Set algorithm to use. |
134
|
|
|
* |
135
|
|
|
* @param string $algorithm |
136
|
8 |
|
* @return \phpbu\App\Cli\Executable\Mcrypt |
137
|
|
|
*/ |
138
|
8 |
|
public function useAlgorithm(string $algorithm) : Mcrypt |
139
|
8 |
|
{ |
140
|
|
|
$this->algorithm = $algorithm; |
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Hash to use for encryption. |
146
|
|
|
* |
147
|
|
|
* @param string $hash |
148
|
5 |
|
* @return \phpbu\App\Cli\Executable\Mcrypt |
149
|
|
|
*/ |
150
|
5 |
|
public function useHash(string $hash) : Mcrypt |
151
|
5 |
|
{ |
152
|
|
|
$this->hash = $hash; |
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Set path to sync to. |
158
|
|
|
* |
159
|
|
|
* @param string $path |
160
|
3 |
|
* @return \phpbu\App\Cli\Executable\Mcrypt |
161
|
|
|
*/ |
162
|
3 |
|
public function useConfig(string $path) : Mcrypt |
163
|
3 |
|
{ |
164
|
|
|
$this->config = $path; |
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Mcrypt CommandLine generator. |
170
|
|
|
* |
171
|
|
|
* @return \SebastianFeldmann\Cli\CommandLine |
172
|
8 |
|
* @throws \phpbu\App\Exception |
173
|
|
|
*/ |
174
|
8 |
|
protected function createCommandLine() : CommandLine |
175
|
1 |
|
{ |
176
|
|
|
if (empty($this->targetFile)) { |
177
|
7 |
|
throw new Exception('target file is missing'); |
178
|
1 |
|
} |
179
|
|
|
if (empty($this->key) && empty($this->keyFile)) { |
180
|
6 |
|
throw new Exception('one of \'key\' or \'keyFile\' is mandatory'); |
181
|
6 |
|
} |
182
|
6 |
|
$process = new CommandLine(); |
183
|
|
|
$cmd = new Cmd($this->binary); |
184
|
|
|
$process->addCommand($cmd); |
185
|
6 |
|
|
186
|
6 |
|
$cmd->addOptionIfNotEmpty('-u', $this->deleteUncrypted, false); |
187
|
|
|
$cmd->addOptionIfNotEmpty('-k', $this->key, true, ' '); |
188
|
6 |
|
$cmd->addOptionIfNotEmpty('-f', $this->keyFile, true, ' '); |
189
|
|
|
$cmd->addOptionIfNotEmpty('-h', $this->hash, true, ' '); |
190
|
6 |
|
$cmd->addOptionIfNotEmpty('-a', $this->algorithm, true, ' '); |
191
|
6 |
|
$cmd->addOptionIfNotEmpty('-c', $this->config, true, ' '); |
192
|
6 |
|
|
193
|
6 |
|
$cmd->addArgument($this->targetFile); |
194
|
6 |
|
|
195
|
6 |
|
return $process; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|