1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the trait {@see \Mailcode\Traits\Commands\Validation\DecryptTrait}. |
4
|
|
|
* |
5
|
|
|
* @package Mailcode |
6
|
|
|
* @subpackage Validation |
7
|
|
|
* @see \Mailcode\Traits\Commands\Validation\DecryptTrait |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Mailcode\Traits\Commands\Validation; |
13
|
|
|
|
14
|
|
|
use Mailcode\Decrypt\DecryptSettings; |
15
|
|
|
use Mailcode\Interfaces\Commands\Validation\DecryptInterface; |
16
|
|
|
use Mailcode\Mailcode_Commands_Command_ShowVariable; |
17
|
|
|
use Mailcode\Mailcode_Commands_Keywords; |
18
|
|
|
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral; |
19
|
|
|
use function AppUtils\sb; |
20
|
|
|
use function Mailcode\t; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @package Mailcode |
24
|
|
|
* @subpackage Validation |
25
|
|
|
* @author Olaf Böcker <[email protected]> |
26
|
|
|
* |
27
|
|
|
* @see DecryptInterface |
28
|
|
|
*/ |
29
|
|
|
trait DecryptTrait |
30
|
|
|
{ |
31
|
|
|
private ?Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral $decryptionKeyToken = null; |
32
|
|
|
|
33
|
|
|
protected function validateSyntax_check_decrypt(): void |
34
|
|
|
{ |
35
|
|
|
$token = $this |
36
|
|
|
->requireParams() |
|
|
|
|
37
|
|
|
->getInfo() |
38
|
|
|
->getTokenByParamName(DecryptInterface::PARAMETER_NAME); |
39
|
|
|
|
40
|
|
|
if($token === null) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (!$token instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral) { |
45
|
|
|
$this->validationResult->makeError( |
46
|
|
|
t('Invalid decryption key token:') . ' ' . t('Expected a string.'), |
47
|
|
|
DecryptInterface::VALIDATION_DECRYPT_CODE_WRONG_TYPE |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->decryptionKeyToken = $token; |
52
|
|
|
|
53
|
|
|
$key = $this->getDecryptionKey(); |
54
|
|
|
|
55
|
|
|
if($key !== DecryptInterface::DEFAULT_DECRYPTION_KEY) { |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->validationResult->makeError( |
60
|
|
|
(string)sb() |
61
|
|
|
->t('Cannot use the default decryption key:') |
62
|
|
|
->t('No default key has been specified.') |
63
|
|
|
->t('A default key must be set, or a key must be specified in the command.'), |
64
|
|
|
DecryptInterface::VALIDATION_DECRYPT_NO_DEFAULT_KEY |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function isDecryptionEnabled() : bool |
69
|
|
|
{ |
70
|
|
|
return $this->getDecryptionKeyToken() !== null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getDecryptionKey() : string |
74
|
|
|
{ |
75
|
|
|
$key = $this->getDecryptionKeyToken(); |
76
|
|
|
if($key === null) { |
77
|
|
|
return ''; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$key = $key->getText(); |
81
|
|
|
|
82
|
|
|
if(empty($key) || $key === DecryptInterface::DEFAULT_DECRYPTION_KEY) { |
83
|
|
|
return DecryptSettings::getDefaultKey(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $key; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function enableDecryption(string $key=DecryptInterface::DEFAULT_DECRYPTION_KEY) : self |
90
|
|
|
{ |
91
|
|
|
$this->decryptionKeyToken = $this |
92
|
|
|
->requireParams() |
93
|
|
|
->getInfo() |
94
|
|
|
->addParamString(DecryptInterface::PARAMETER_NAME, $key); |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function disableDecryption() : self |
100
|
|
|
{ |
101
|
|
|
if(isset($this->decryptionKeyToken)) { |
102
|
|
|
$this |
103
|
|
|
->requireParams() |
104
|
|
|
->getInfo() |
105
|
|
|
->removeToken($this->decryptionKeyToken); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Gets the decryption key to use for the command. If none has |
113
|
|
|
* been specified in the original command, the default |
114
|
|
|
* decryption key is used as defined via {@see DecryptSettings::setDefaultKey()}. |
115
|
|
|
* |
116
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral|NULL |
117
|
|
|
*/ |
118
|
|
|
public function getDecryptionKeyToken(): ?Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral |
119
|
|
|
{ |
120
|
|
|
return $this->decryptionKeyToken; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|