|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LeKoala\Encrypt; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use ParagonIE\CipherSweet\EncryptedField; |
|
7
|
|
|
use ParagonIE\CipherSweet\Exception\InvalidCiphertextException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* This trait allow encryption for fields that don't |
|
11
|
|
|
* require a blind index |
|
12
|
|
|
*/ |
|
13
|
|
|
trait HasEncryption |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Exception |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $encryptionException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param CipherSweet $engine |
|
|
|
|
|
|
22
|
|
|
* @return EncryptedField |
|
23
|
|
|
*/ |
|
24
|
|
|
public function getEncryptedField($engine = null) |
|
25
|
|
|
{ |
|
26
|
|
|
if ($engine === null) { |
|
27
|
|
|
$engine = EncryptHelper::getCipherSweet(); |
|
28
|
|
|
} |
|
29
|
|
|
$encryptedField = new EncryptedField($engine, $this->tableName, $this->name); |
|
30
|
|
|
return $encryptedField; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return Exception |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getEncryptionException() |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->encryptionException; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* prepValueForDB gets passed $this->value |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $value |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function prepValueForDB($value) |
|
48
|
|
|
{ |
|
49
|
|
|
if (!$value) { |
|
50
|
|
|
if ($this->getNullifyEmpty() || $value === null) { |
|
|
|
|
|
|
51
|
|
|
return null; |
|
52
|
|
|
} |
|
53
|
|
|
return ''; |
|
54
|
|
|
} |
|
55
|
|
|
// Don't encrypt twice |
|
56
|
|
|
if (EncryptHelper::isEncrypted($value)) { |
|
57
|
|
|
return $value; |
|
58
|
|
|
} |
|
59
|
|
|
$encryptedValue = $this->getEncryptedField()->encryptValue($value); |
|
60
|
|
|
return $encryptedValue; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getDecryptedValue() |
|
67
|
|
|
{ |
|
68
|
|
|
if (EncryptHelper::isEncrypted($this->value)) { |
|
69
|
|
|
return $this->getEncryptedField()->decryptValue($this->value); |
|
70
|
|
|
} |
|
71
|
|
|
return $this->value; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function setValue($value, $record = null, $markChanged = true) |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
|
|
// Return early if we keep encrypted value in memory |
|
77
|
|
|
if (!EncryptHelper::getAutomaticDecryption()) { |
|
78
|
|
|
$this->value = $value; |
|
|
|
|
|
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// $markChanged is not used |
|
83
|
|
|
// The value might come encrypted from the database |
|
84
|
|
|
if ($value && EncryptHelper::isEncrypted($value)) { |
|
85
|
|
|
try { |
|
86
|
|
|
$this->value = $this->getEncryptedField()->decryptValue($value); |
|
87
|
|
|
} catch (InvalidCiphertextException $ex) { |
|
88
|
|
|
$this->encryptionException = $ex; |
|
89
|
|
|
// rotate backend ? |
|
90
|
|
|
if (EncryptHelper::getAutomaticRotation()) { |
|
91
|
|
|
$encryption = EncryptHelper::getEncryption($value); |
|
92
|
|
|
$engine = EncryptHelper::getEngineForEncryption($encryption); |
|
93
|
|
|
$oldEncryptedField = $this->getEncryptedField($engine); |
|
94
|
|
|
$this->value = $oldEncryptedField->decryptValue($value); |
|
95
|
|
|
} else { |
|
96
|
|
|
$this->value = $value; |
|
97
|
|
|
} |
|
98
|
|
|
} catch (Exception $ex) { |
|
99
|
|
|
$this->encryptionException = $ex; |
|
100
|
|
|
// We cannot decrypt |
|
101
|
|
|
$this->value = $this->nullValue(); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
} else { |
|
104
|
|
|
$this->value = $value; |
|
105
|
|
|
} |
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths