1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Crypter; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Target; |
5
|
|
|
use phpbu\App\Cli\Executable; |
6
|
|
|
use phpbu\App\Result; |
7
|
|
|
use phpbu\App\Util; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Mcrypt 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 http://phpbu.de/ |
18
|
|
|
* @since Class available since Release 1.3.0 |
19
|
|
|
*/ |
20
|
|
|
class Mcrypt extends Abstraction implements Simulator |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Path to mcrypt command. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $pathToMcrypt; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Key to pass via cli |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $key; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Key file |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $keyFile; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Algorithm to use |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $algorithm; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Hash to use |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
private $hash; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Path to config file |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
private $config; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Keep the not encrypted file |
66
|
|
|
* |
67
|
|
|
* @var boolean |
68
|
|
|
*/ |
69
|
|
|
private $keepUncrypted; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Setup. |
73
|
|
|
* |
74
|
|
|
* @see \phpbu\App\Backup\Crypter |
75
|
|
|
* @param array $options |
76
|
|
|
* @throws Exception |
77
|
|
|
*/ |
78
|
|
|
public function setup(array $options = []) |
79
|
|
|
{ |
80
|
|
|
if (!Util\Arr::isSetAndNotEmptyString($options, 'algorithm')) { |
81
|
|
|
throw new Exception('mcrypt \'algorithm\' is mandatory'); |
82
|
|
|
} |
83
|
|
|
|
84
|
5 |
|
$this->pathToMcrypt = Util\Arr::getValue($options, 'pathToMcrypt', ''); |
85
|
|
|
$this->keepUncrypted = Util\Str::toBoolean(Util\Arr::getValue($options, 'keepUncrypted', ''), false); |
86
|
5 |
|
$this->key = Util\Arr::getValue($options, 'key', ''); |
87
|
1 |
|
$this->keyFile = $this->toAbsolutePath(Util\Arr::getValue($options, 'keyFile', '')); |
88
|
|
|
$this->algorithm = $options['algorithm']; |
89
|
|
|
$this->hash = Util\Arr::getValue($options, 'hash', ''); |
90
|
4 |
|
$this->config = $this->toAbsolutePath(Util\Arr::getValue($options, 'config', '')); |
91
|
4 |
|
|
92
|
4 |
|
if (empty($this->key) && empty($this->keyFile)) { |
93
|
4 |
|
throw new Exception('one of \'key\' or \'keyFile\' is mandatory'); |
94
|
4 |
|
} |
95
|
4 |
|
} |
96
|
4 |
|
|
97
|
4 |
|
/** |
98
|
|
|
* (non-PHPDoc) |
99
|
4 |
|
* |
100
|
1 |
|
* @see \phpbu\App\Backup\Crypter |
101
|
|
|
* @return string |
102
|
3 |
|
*/ |
103
|
|
|
public function getSuffix() : string |
104
|
|
|
{ |
105
|
|
|
return 'nc'; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Create the Exec to run the 'mcrypt' command. |
110
|
|
|
* |
111
|
|
|
* @param \phpbu\App\Backup\Target $target |
112
|
2 |
|
* @return \phpbu\App\Cli\Executable |
113
|
|
|
*/ |
114
|
2 |
|
protected function createExecutable(Target $target) : Executable |
115
|
|
|
{ |
116
|
2 |
|
$executable = new Executable\Mcrypt((string)$this->pathToMcrypt); |
117
|
|
|
$executable->useAlgorithm($this->algorithm) |
118
|
2 |
|
->useKey($this->key) |
119
|
1 |
|
->useKeyFile($this->keyFile) |
120
|
|
|
->useConfig($this->config) |
121
|
1 |
|
->useHash($this->hash) |
122
|
|
|
->saveAt($target->getPathname()) |
123
|
|
|
->deleteUncrypted(!$this->keepUncrypted); |
124
|
|
|
return $executable; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|