1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Crypter; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Crypter; |
5
|
|
|
use phpbu\App\Backup\Target; |
6
|
|
|
use phpbu\App\Cli\Executable; |
7
|
|
|
use phpbu\App\Result; |
8
|
|
|
use phpbu\App\Util; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* OpenSSL crypter class. |
12
|
|
|
* |
13
|
|
|
* @package phpbu |
14
|
|
|
* @subpackage Backup |
15
|
|
|
* @author Sebastian Feldmann <[email protected]> |
16
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
17
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
18
|
|
|
* @link http://phpbu.de/ |
19
|
|
|
* @since Class available since Release 2.1.6 |
20
|
|
|
*/ |
21
|
|
|
class OpenSSL extends Key implements Crypter |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Path to mcrypt command. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $pathToOpenSSL; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Key file |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $certFile; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Algorithm to use |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $algorithm; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Password to use |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $password; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Keep the not encrypted file |
53
|
|
|
* |
54
|
|
|
* @var boolean |
55
|
|
|
*/ |
56
|
|
|
private $keepUncrypted; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Setup. |
60
|
|
|
* |
61
|
|
|
* @see \phpbu\App\Backup\Crypter |
62
|
|
|
* @param array $options |
63
|
|
|
* @throws Exception |
64
|
|
|
*/ |
65
|
|
|
public function setup(array $options = array()) |
66
|
|
|
{ |
67
|
|
|
if (!Util\Arr::isSetAndNotEmptyString($options, 'algorithm')) { |
68
|
|
|
throw new Exception('openssl expects \'algorithm\''); |
69
|
|
|
} |
70
|
5 |
|
if (!Util\Arr::isSetAndNotEmptyString($options, 'password') |
71
|
|
|
&& !Util\Arr::isSetAndNotEmptyString($options, 'certFile')) { |
72
|
5 |
|
throw new Exception('openssl expects \'key\' or \'password\''); |
73
|
1 |
|
} |
74
|
|
|
|
75
|
4 |
|
$this->pathToOpenSSL = Util\Arr::getValue($options, 'pathToOpenSSL'); |
76
|
4 |
|
$this->keepUncrypted = Util\Str::toBoolean(Util\Arr::getValue($options, 'keepUncrypted', ''), false); |
77
|
1 |
|
$this->certFile = $this->toAbsolutePath(Util\Arr::getValue($options, 'certFile')); |
78
|
|
|
$this->algorithm = Util\Arr::getValue($options, 'algorithm'); |
79
|
|
|
$this->password = Util\Arr::getValue($options, 'password'); |
80
|
3 |
|
} |
81
|
3 |
|
|
82
|
3 |
|
/** |
83
|
3 |
|
* (non-PHPDoc) |
84
|
3 |
|
* |
85
|
3 |
|
* @see \phpbu\App\Backup\Crypter |
86
|
3 |
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function getSuffix() |
89
|
|
|
{ |
90
|
|
|
return 'enc'; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Create the Exec to run the 'mcrypt' command. |
95
|
|
|
* |
96
|
2 |
|
* @param \phpbu\App\Backup\Target $target |
97
|
|
|
* @return \phpbu\App\Cli\Executable |
98
|
2 |
|
*/ |
99
|
|
|
public function getExecutable(Target $target) |
100
|
2 |
|
{ |
101
|
|
|
if (null == $this->executable) { |
102
|
2 |
|
$this->executable = new Executable\OpenSSL($this->pathToOpenSSL); |
103
|
1 |
|
$this->executable->encryptFile($target->getPathname()); |
104
|
|
|
|
105
|
1 |
|
// use key or password to encrypt |
106
|
|
|
if (!empty($this->certFile)) { |
107
|
|
|
$this->executable->useSSLCert($this->certFile); |
108
|
|
|
} else { |
109
|
|
|
$this->executable->usePassword($this->password) |
110
|
|
|
->encodeBase64(true); |
111
|
|
|
} |
112
|
|
|
$this->executable->useAlgorithm($this->algorithm) |
113
|
1 |
|
->deleteUncrypted(!$this->keepUncrypted); |
114
|
|
|
} |
115
|
1 |
|
|
116
|
|
|
return $this->executable; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|