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
|
|
|
* Gpg 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 https://phpbu.de/ |
18
|
|
|
* @since Class available since Release 6.0.1 |
19
|
|
|
*/ |
20
|
|
|
class Gpg extends Abstraction implements Executable |
21
|
|
|
{ |
22
|
|
|
use OptionMasker; |
23
|
|
|
|
24
|
|
|
public const SUFFIX = 'gpg'; |
25
|
|
|
|
26
|
|
|
private const ACTION_ENCRYPT = 'e'; |
27
|
|
|
private const ACTION_DECRYPT = 'd'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Current action encrypt|decrypt |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $action; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Path to source file |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $sourceFile; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Path to encrypted file |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $targetFile; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Gpg user |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
private $user; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Keep the not encrypted file |
59
|
|
|
* |
60
|
|
|
* @var bool |
61
|
|
|
*/ |
62
|
|
|
private $deleteSource = true; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Constructor |
66
|
|
|
* |
67
|
|
|
* @param string $path |
68
|
|
|
*/ |
69
|
4 |
|
public function __construct(string $path = '') |
70
|
|
|
{ |
71
|
4 |
|
$this->setup('gpg', $path); |
72
|
4 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Encrypt a file |
76
|
|
|
* |
77
|
|
|
* @param string $file |
78
|
|
|
* @return \phpbu\App\Cli\Executable\Gpg |
79
|
|
|
*/ |
80
|
3 |
|
public function encryptFile(string $file): Gpg |
81
|
|
|
{ |
82
|
3 |
|
$this->action = self::ACTION_ENCRYPT; |
83
|
3 |
|
$this->sourceFile = $file; |
84
|
3 |
|
$this->targetFile = $file . '.gpg'; |
85
|
3 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Encrypt a file |
90
|
|
|
* |
91
|
|
|
* @param string $file |
92
|
|
|
* @return \phpbu\App\Cli\Executable\Gpg |
93
|
|
|
*/ |
94
|
1 |
|
public function decryptFile(string $file): Gpg |
95
|
|
|
{ |
96
|
1 |
|
$this->action = self::ACTION_DECRYPT; |
97
|
1 |
|
$this->sourceFile = $file . '.gpg'; |
98
|
1 |
|
$this->targetFile = $file; |
99
|
1 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Delete the uncrypted data |
104
|
|
|
* |
105
|
|
|
* @param boolean $bool |
106
|
|
|
* @return \phpbu\App\Cli\Executable\Gpg |
107
|
|
|
*/ |
108
|
4 |
|
public function deleteSource(bool $bool): Gpg |
109
|
|
|
{ |
110
|
4 |
|
$this->deleteSource = $bool; |
111
|
4 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Password to use for encryption |
116
|
|
|
* |
117
|
|
|
* @param string $user |
118
|
|
|
* @return \phpbu\App\Cli\Executable\Gpg |
119
|
|
|
*/ |
120
|
4 |
|
public function useUser(string $user): Gpg |
121
|
|
|
{ |
122
|
4 |
|
$this->user = $user; |
123
|
4 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Gpg CommandLine generator |
128
|
|
|
* |
129
|
|
|
* @return \SebastianFeldmann\Cli\CommandLine |
130
|
|
|
* @throws \phpbu\App\Exception |
131
|
|
|
*/ |
132
|
4 |
|
protected function createCommandLine(): CommandLine |
133
|
|
|
{ |
134
|
4 |
|
if (empty($this->sourceFile)) { |
135
|
|
|
throw new Exception('file is missing'); |
136
|
|
|
} |
137
|
|
|
|
138
|
4 |
|
$process = new CommandLine(); |
139
|
4 |
|
$cmd = new Cmd($this->binary); |
140
|
|
|
|
141
|
4 |
|
$process->addCommand($cmd); |
142
|
|
|
|
143
|
4 |
|
$this->addDeleteCommand($process); |
144
|
|
|
|
145
|
4 |
|
return $process; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Add the 'rm' command to remove the uncrypted file |
150
|
|
|
* |
151
|
|
|
* @param \SebastianFeldmann\Cli\CommandLine $process |
152
|
|
|
*/ |
153
|
4 |
|
protected function addDeleteCommand(CommandLine $process): void |
154
|
|
|
{ |
155
|
4 |
|
if ($this->deleteSource) { |
156
|
3 |
|
$cmd = new Cmd('rm'); |
157
|
3 |
|
$cmd->addArgument($this->sourceFile); |
158
|
3 |
|
$process->addCommand($cmd); |
159
|
|
|
} |
160
|
4 |
|
} |
161
|
|
|
} |
162
|
|
|
|