|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LeKoala\Encrypt; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DB; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use SilverStripe\ORM\DataQuery; |
|
8
|
|
|
use ParagonIE\CipherSweet\BlindIndex; |
|
9
|
|
|
use ParagonIE\CipherSweet\EncryptedField; |
|
10
|
|
|
use SilverStripe\ORM\DataObject; |
|
11
|
|
|
use SilverStripe\ORM\Filters\SearchFilter; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* A filter that helps searching against a full blind index |
|
15
|
|
|
*/ |
|
16
|
|
|
class EncryptedSearchFilter extends SearchFilter |
|
17
|
|
|
{ |
|
18
|
|
|
public function setModifiers(array $modifiers) |
|
19
|
|
|
{ |
|
20
|
|
|
if (!empty($modifiers)) { |
|
21
|
|
|
throw new InvalidArgumentException( |
|
22
|
|
|
get_class($this) . ' does not accept ' . implode(', ', $modifiers) . ' as modifiers' |
|
23
|
|
|
); |
|
24
|
|
|
} |
|
25
|
|
|
parent::setModifiers($modifiers); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
protected function getCaseSensitive() |
|
29
|
|
|
{ |
|
30
|
|
|
return null; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getDbName() |
|
34
|
|
|
{ |
|
35
|
|
|
$column = parent::getDbName(); |
|
36
|
|
|
$column = str_replace('"', '', $column); |
|
37
|
|
|
return '"' . $column . 'BlindIndex"'; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return EncryptedField |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getEncryptedField() |
|
44
|
|
|
{ |
|
45
|
|
|
$engine = EncryptHelper::getCipherSweet(); |
|
46
|
|
|
$table = DataObject::getSchema()->tableName($this->model); |
|
47
|
|
|
$encryptedField = (new EncryptedField($engine, $table, $this->name)) |
|
48
|
|
|
->addBlindIndex(new BlindIndex($this->name . "BlindIndex", [], 32)); |
|
49
|
|
|
return $encryptedField; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Accessor for the current value to be filtered on. |
|
54
|
|
|
* |
|
55
|
|
|
* @return string|array |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getEncryptedValue() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->getEncryptedField()->getBlindIndex($this->getValue(), $this->name . 'BlindIndex'); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Applies an exact match (equals) on a field value. |
|
64
|
|
|
* |
|
65
|
|
|
* @return DataQuery |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function applyOne(DataQuery $query) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->model = $query->applyRelation($this->relation); |
|
70
|
|
|
$where = DB::get_conn()->comparisonClause( |
|
71
|
|
|
$this->getDbName(), |
|
72
|
|
|
null, |
|
73
|
|
|
true, // exact? |
|
74
|
|
|
false, // negate? |
|
75
|
|
|
$this->getCaseSensitive(), |
|
|
|
|
|
|
76
|
|
|
true |
|
77
|
|
|
); |
|
78
|
|
|
$array = array($where => $this->getEncryptedValue()); |
|
79
|
|
|
return $query->where($array); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Applies an exact match (equals) on a field value against multiple |
|
84
|
|
|
* possible values. |
|
85
|
|
|
* |
|
86
|
|
|
* @return DataQuery |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function applyMany(DataQuery $query) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->model = $query->applyRelation($this->relation); |
|
91
|
|
|
$caseSensitive = $this->getCaseSensitive(); |
|
|
|
|
|
|
92
|
|
|
$values = $this->getEncryptedValue(); |
|
93
|
|
|
$column = $this->getDbName(); |
|
94
|
|
|
// For queries using the default collation (no explicit case) we can use the WHERE .. IN .. syntax, |
|
95
|
|
|
// providing simpler SQL than many WHERE .. OR .. fragments. |
|
96
|
|
|
// If values is an empty array, fall back to 3.1 behaviour and use empty string comparison |
|
97
|
|
|
if (empty($values)) { |
|
98
|
|
|
$values = array(''); |
|
99
|
|
|
} |
|
100
|
|
|
$placeholders = DB::placeholders($values); |
|
|
|
|
|
|
101
|
|
|
return $query->where(array( |
|
102
|
|
|
"$column IN ($placeholders)" => $values |
|
103
|
|
|
)); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Excludes an exact match (equals) on a field value. |
|
108
|
|
|
* |
|
109
|
|
|
* @return DataQuery |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function excludeOne(DataQuery $query) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->model = $query->applyRelation($this->relation); |
|
114
|
|
|
$column = $this->getDbName(); |
|
115
|
|
|
$where = DB::get_conn()->comparisonClause( |
|
116
|
|
|
$column, |
|
117
|
|
|
null, |
|
118
|
|
|
true, // exact? |
|
119
|
|
|
true, // negate? |
|
120
|
|
|
$this->getCaseSensitive(), |
|
|
|
|
|
|
121
|
|
|
true |
|
122
|
|
|
); |
|
123
|
|
|
$array = array($where => $this->getEncryptedValue()); |
|
124
|
|
|
return $query->where($array); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Excludes an exact match (equals) on a field value against multiple |
|
129
|
|
|
* possible values. |
|
130
|
|
|
* |
|
131
|
|
|
* @return DataQuery |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function excludeMany(DataQuery $query) |
|
134
|
|
|
{ |
|
135
|
|
|
$this->model = $query->applyRelation($this->relation); |
|
136
|
|
|
$caseSensitive = $this->getCaseSensitive(); |
|
|
|
|
|
|
137
|
|
|
$values = $this->getEncryptedValue(); |
|
138
|
|
|
$column = $this->getDbName(); |
|
139
|
|
|
if ($caseSensitive === null) { |
|
|
|
|
|
|
140
|
|
|
// For queries using the default collation (no explicit case) we can use the WHERE .. NOT IN .. syntax, |
|
141
|
|
|
// providing simpler SQL than many WHERE .. AND .. fragments. |
|
142
|
|
|
// If values is an empty array, fall back to 3.1 behaviour and use empty string comparison |
|
143
|
|
|
if (empty($values)) { |
|
144
|
|
|
$values = array(''); |
|
145
|
|
|
} |
|
146
|
|
|
$placeholders = DB::placeholders($values); |
|
|
|
|
|
|
147
|
|
|
return $query->where(array( |
|
148
|
|
|
"$column NOT IN ($placeholders)" => $values |
|
149
|
|
|
)); |
|
150
|
|
|
} else { |
|
151
|
|
|
// Generate reusable comparison clause |
|
152
|
|
|
$comparisonClause = DB::get_conn()->comparisonClause( |
|
153
|
|
|
$column, |
|
154
|
|
|
null, |
|
155
|
|
|
true, // exact? |
|
156
|
|
|
true, // negate? |
|
157
|
|
|
$this->getCaseSensitive(), |
|
158
|
|
|
true |
|
159
|
|
|
); |
|
160
|
|
|
// Since query connective is ambiguous, use AND explicitly here |
|
161
|
|
|
$count = count($values); |
|
162
|
|
|
$predicate = implode(' AND ', array_fill(0, $count, $comparisonClause)); |
|
163
|
|
|
return $query->where(array($predicate => $values)); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function isEmpty() |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->getValue() === array() || $this->getValue() === null || $this->getValue() === ''; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|