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
|
|
|
* OpenSSL 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 2.1.6 |
19
|
|
|
*/ |
20
|
|
|
class OpenSSL extends Abstraction implements Simulator |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Path to mcrypt command. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $pathToOpenSSL; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Key file |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $certFile; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Algorithm to use |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $algorithm; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Password to use |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $password; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Keep the not encrypted file |
52
|
|
|
* |
53
|
|
|
* @var boolean |
54
|
|
|
*/ |
55
|
|
|
private $keepUncrypted; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Setup. |
59
|
|
|
* |
60
|
|
|
* @see \phpbu\App\Backup\Crypter |
61
|
|
|
* @param array $options |
62
|
|
|
* @throws Exception |
63
|
|
|
*/ |
64
|
|
|
public function setup(array $options = []) |
65
|
|
|
{ |
66
|
|
|
if (!Util\Arr::isSetAndNotEmptyString($options, 'algorithm')) { |
67
|
|
|
throw new Exception('openssl expects \'algorithm\''); |
68
|
|
|
} |
69
|
|
|
if (!Util\Arr::isSetAndNotEmptyString($options, 'password') |
70
|
5 |
|
&& !Util\Arr::isSetAndNotEmptyString($options, 'certFile')) { |
71
|
|
|
throw new Exception('openssl expects \'key\' or \'password\''); |
72
|
5 |
|
} |
73
|
1 |
|
|
74
|
|
|
$this->pathToOpenSSL = Util\Arr::getValue($options, 'pathToOpenSSL', ''); |
75
|
4 |
|
$this->keepUncrypted = Util\Str::toBoolean(Util\Arr::getValue($options, 'keepUncrypted', ''), false); |
76
|
4 |
|
$this->certFile = $this->toAbsolutePath(Util\Arr::getValue($options, 'certFile', '')); |
77
|
1 |
|
$this->algorithm = Util\Arr::getValue($options, 'algorithm', ''); |
78
|
|
|
$this->password = Util\Arr::getValue($options, 'password', ''); |
79
|
|
|
} |
80
|
3 |
|
|
81
|
3 |
|
/** |
82
|
3 |
|
* Return file suffix of encrypted target. |
83
|
3 |
|
* |
84
|
3 |
|
* @see \phpbu\App\Backup\Crypter |
85
|
3 |
|
* @return string |
86
|
3 |
|
*/ |
87
|
|
|
public function getSuffix() : string |
88
|
|
|
{ |
89
|
|
|
return 'enc'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Create the Executable to run the 'mcrypt' command. |
94
|
|
|
* |
95
|
|
|
* @param \phpbu\App\Backup\Target $target |
96
|
2 |
|
* @return \phpbu\App\Cli\Executable |
97
|
|
|
*/ |
98
|
2 |
|
protected function createExecutable(Target $target) : Executable |
99
|
|
|
{ |
100
|
2 |
|
$executable = new Executable\OpenSSL($this->pathToOpenSSL); |
101
|
|
|
$executable->encryptFile($target->getPathname()); |
102
|
2 |
|
|
103
|
1 |
|
// use key or password to encrypt |
104
|
|
|
if (!empty($this->certFile)) { |
105
|
1 |
|
$executable->useSSLCert($this->certFile); |
106
|
|
|
} else { |
107
|
|
|
$executable->usePassword($this->password) |
108
|
|
|
->encodeBase64(true); |
109
|
|
|
} |
110
|
|
|
$executable->useAlgorithm($this->algorithm) |
111
|
|
|
->deleteUncrypted(!$this->keepUncrypted); |
112
|
|
|
return $executable; |
113
|
1 |
|
} |
114
|
|
|
} |
115
|
|
|
|