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
|
|
|
public function setValue($value, $record = null, $markChanged = true) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
// $markChanged is not used |
66
|
|
|
// The value might come encrypted from the database |
67
|
|
|
if ($value && EncryptHelper::isEncrypted($value)) { |
68
|
|
|
try { |
69
|
|
|
$this->value = $this->getEncryptedField()->decryptValue($value); |
|
|
|
|
70
|
|
|
} catch (InvalidCiphertextException $ex) { |
71
|
|
|
$this->encryptionException = $ex; |
72
|
|
|
// rotate backend ? |
73
|
|
|
if (EncryptHelper::getAutomaticRotation()) { |
74
|
|
|
$encryption = EncryptHelper::getEncryption($value); |
75
|
|
|
$engine = EncryptHelper::getEngineForEncryption($encryption); |
76
|
|
|
$oldEncryptedField = $this->getEncryptedField($engine); |
77
|
|
|
$this->value = $oldEncryptedField->decryptValue($value); |
78
|
|
|
} else { |
79
|
|
|
$this->value = $value; |
80
|
|
|
} |
81
|
|
|
} catch (Exception $ex) { |
82
|
|
|
$this->encryptionException = $ex; |
83
|
|
|
// We cannot decrypt |
84
|
|
|
$this->value = $this->nullValue(); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
} else { |
87
|
|
|
$this->value = $value; |
88
|
|
|
} |
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
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