|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* neuralyzer : Data Anonymization Library and CLI Tool |
|
4
|
|
|
* |
|
5
|
|
|
* PHP Version 7.1 |
|
6
|
|
|
* |
|
7
|
|
|
* @author Emmanuel Dyan |
|
8
|
|
|
* @author Rémi Sauvat |
|
9
|
|
|
* @copyright 2018 Emmanuel Dyan |
|
10
|
|
|
* |
|
11
|
|
|
* @package edyan/neuralyzer |
|
12
|
|
|
* |
|
13
|
|
|
* @license GNU General Public License v2.0 |
|
14
|
|
|
* |
|
15
|
|
|
* @link https://github.com/edyan/neuralyzer |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace Edyan\Neuralyzer\Anonymizer; |
|
19
|
|
|
|
|
20
|
|
|
use Doctrine\DBAL\Configuration as DbalConfiguration; |
|
21
|
|
|
use Doctrine\DBAL\DriverManager as DbalDriverManager; |
|
22
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
|
23
|
|
|
use Edyan\Neuralyzer\Exception\NeuralizerException; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Implement AbstractAnonymizer for DB, to read and write data via Doctrine DBAL |
|
27
|
|
|
*/ |
|
28
|
|
|
class DB extends AbstractAnonymizer |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* Doctrine DB Adapter |
|
32
|
|
|
* |
|
33
|
|
|
* @var \Doctrine\DBAL\Connection |
|
34
|
|
|
*/ |
|
35
|
|
|
private $conn; |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Init connection |
|
40
|
|
|
* |
|
41
|
|
|
* @param $params Parameters to send to Doctrine DB |
|
42
|
|
|
*/ |
|
43
|
29 |
|
public function __construct(array $params) |
|
44
|
|
|
{ |
|
45
|
29 |
|
$this->conn = DbalDriverManager::getConnection($params, new DbalConfiguration()); |
|
46
|
28 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get Doctrine Connection |
|
51
|
|
|
* @return Doctrine\DBAL\Connection |
|
52
|
|
|
*/ |
|
53
|
17 |
|
public function getConn() |
|
54
|
|
|
{ |
|
55
|
17 |
|
return $this->conn; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Process an entity by reading / writing to the DB |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $entity |
|
63
|
|
|
* @param callable|null $callback |
|
64
|
|
|
* @param bool $pretend |
|
65
|
|
|
* @param bool $returnRes |
|
66
|
|
|
* |
|
67
|
|
|
* @return void|array |
|
68
|
|
|
*/ |
|
69
|
13 |
|
public function processEntity( |
|
70
|
|
|
string $entity, |
|
71
|
|
|
callable $callback = null, |
|
72
|
|
|
bool $pretend = true, |
|
73
|
|
|
bool $returnRes = false |
|
74
|
|
|
): array { |
|
75
|
13 |
|
$schema = $this->conn->getSchemaManager(); |
|
76
|
13 |
|
if ($schema->tablesExist($entity) === false) { |
|
|
|
|
|
|
77
|
1 |
|
throw new NeuralizerException("Table $entity does not exist"); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
12 |
|
$this->entity = $entity; |
|
81
|
12 |
|
$queries = []; |
|
82
|
|
|
|
|
83
|
12 |
|
$actionsOnThatEntity = $this->whatToDoWithEntity(); |
|
84
|
|
|
|
|
85
|
10 |
|
if ($actionsOnThatEntity & self::TRUNCATE_TABLE) { |
|
86
|
4 |
|
$where = $this->getWhereConditionInConfig(); |
|
87
|
4 |
|
$query = $this->runDelete($where, $pretend); |
|
88
|
3 |
|
($returnRes === true ? array_push($queries, $query) : ''); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
9 |
|
if ($actionsOnThatEntity & self::UPDATE_TABLE) { |
|
92
|
|
|
// I need to read line by line if I have to update the table |
|
93
|
|
|
// to make sure I do update by update (slower but no other choice for now) |
|
94
|
9 |
|
$rowNum = 0; |
|
95
|
|
|
|
|
96
|
9 |
|
$key = $this->getPrimaryKey(); |
|
97
|
8 |
|
$this->entityCols = $this->getTableCols(); |
|
98
|
|
|
|
|
99
|
8 |
|
$queryBuilder = $this->conn->createQueryBuilder(); |
|
100
|
8 |
|
$rows = $queryBuilder->select($key)->from($this->entity)->execute(); |
|
101
|
|
|
|
|
102
|
8 |
|
foreach ($rows as $row) { |
|
|
|
|
|
|
103
|
7 |
|
$data = $this->generateFakeData(); |
|
104
|
7 |
|
$queryBuilder = $this->prepareUpdate($data, $key, $row[$key]); |
|
105
|
|
|
|
|
106
|
7 |
|
($returnRes === true ? array_push($queries, $this->getRawSQL($queryBuilder)) : ''); |
|
107
|
|
|
|
|
108
|
7 |
|
if ($pretend === false) { |
|
109
|
4 |
|
$this->runUpdate($queryBuilder); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
7 |
|
if (!is_null($callback)) { |
|
113
|
7 |
|
$callback(++$rowNum); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
8 |
|
return $queries; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Identify the primary key for a table |
|
124
|
|
|
* |
|
125
|
|
|
* @return string Field's name |
|
126
|
|
|
*/ |
|
127
|
9 |
|
private function getPrimaryKey() |
|
128
|
|
|
{ |
|
129
|
9 |
|
$schema = $this->conn->getSchemaManager(); |
|
130
|
9 |
|
$tableDetails = $schema->listTableDetails($this->entity); |
|
131
|
9 |
|
if ($tableDetails->hasPrimaryKey() === false) { |
|
132
|
1 |
|
throw new NeuralizerException("Can't find a primary key for '{$this->entity}'"); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
8 |
|
return $tableDetails->getPrimaryKey()->getColumns()[0]; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Retrieve columns list for a table with type and length |
|
141
|
|
|
* |
|
142
|
|
|
* @return array $cols |
|
143
|
|
|
*/ |
|
144
|
8 |
|
private function getTableCols() |
|
145
|
|
|
{ |
|
146
|
8 |
|
$schema = $this->conn->getSchemaManager(); |
|
147
|
8 |
|
$tableCols = $schema->listTableColumns($this->entity); |
|
148
|
8 |
|
$cols = []; |
|
149
|
8 |
|
foreach ($tableCols as $col) { |
|
150
|
8 |
|
$cols[$col->getName()] = [ |
|
151
|
8 |
|
'length' => $col->getLength(), |
|
152
|
8 |
|
'type' => $col->getType(), |
|
153
|
|
|
]; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
8 |
|
return $cols; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Execute the Update with PDO |
|
162
|
|
|
* |
|
163
|
|
|
* @param array $data Array of fields => value to update the table |
|
164
|
|
|
* @param string $primaryKey |
|
165
|
|
|
* @param string $primaryKeyVal Primary Key's Value |
|
166
|
|
|
* @return string Doctrine DBAL QueryBuilder |
|
167
|
|
|
*/ |
|
168
|
7 |
|
private function prepareUpdate(array $data, string $primaryKey, $primaryKeyVal) |
|
169
|
|
|
{ |
|
170
|
7 |
|
$queryBuilder = $this->conn->createQueryBuilder(); |
|
171
|
7 |
|
$queryBuilder = $queryBuilder->update($this->entity); |
|
172
|
7 |
|
foreach ($data as $field => $value) { |
|
173
|
7 |
|
$type = $this->entityCols[$field]['type']; |
|
174
|
7 |
|
$queryBuilder = $queryBuilder->set($field, $this->getCondition($field, $type)); |
|
175
|
7 |
|
$queryBuilder = $queryBuilder->setParameter(":$field", $value, $type); |
|
176
|
7 |
|
} |
|
177
|
|
|
$queryBuilder = $queryBuilder->where("$primaryKey = :$primaryKey"); |
|
178
|
7 |
|
$queryBuilder = $queryBuilder->setParameter(":$primaryKey", $primaryKeyVal); |
|
179
|
7 |
|
|
|
180
|
|
|
return $queryBuilder; |
|
181
|
7 |
|
} |
|
182
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Execute the Update with PDO |
|
186
|
|
|
* |
|
187
|
|
|
* @param QueryBuilder $queryBuilder |
|
188
|
|
|
*/ |
|
189
|
|
|
private function runUpdate(QueryBuilder $queryBuilder) |
|
190
|
4 |
|
{ |
|
191
|
|
|
try { |
|
192
|
|
|
$queryBuilder->execute(); |
|
193
|
4 |
|
} catch (\Doctrine\DBAL\Exception\DriverException $e) { |
|
194
|
|
|
throw new NeuralizerException( |
|
195
|
|
|
"Problem anonymizing {$this->entity} (" . $e->getMessage() . ')' |
|
196
|
|
|
); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
4 |
|
|
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* To debug, build the final SQL (can be approximative) |
|
203
|
|
|
* @param QueryBuilder $queryBuilder |
|
204
|
|
|
* @return string |
|
205
|
|
|
*/ |
|
206
|
|
|
private function getRawSQL(QueryBuilder $queryBuilder) |
|
207
|
6 |
|
{ |
|
208
|
|
|
$sql = $queryBuilder->getSQL(); |
|
209
|
6 |
|
foreach ($queryBuilder->getParameters() as $parameter => $value) { |
|
210
|
6 |
|
if (is_object($value)) { |
|
211
|
6 |
|
$value = '{object}'; |
|
212
|
6 |
|
} |
|
213
|
|
|
$sql = str_replace($parameter, "'$value'", $sql); |
|
214
|
6 |
|
} |
|
215
|
|
|
|
|
216
|
|
|
return $sql; |
|
217
|
6 |
|
} |
|
218
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Execute the Delete with PDO |
|
222
|
|
|
* |
|
223
|
|
|
* @param string $where |
|
224
|
|
|
* @param bool $pretend |
|
225
|
|
|
* |
|
226
|
|
|
* @return string |
|
227
|
|
|
*/ |
|
228
|
|
|
private function runDelete(string $where, bool $pretend): string |
|
229
|
4 |
|
{ |
|
230
|
|
|
$queryBuilder = $this->conn->createQueryBuilder(); |
|
231
|
4 |
|
$queryBuilder = $queryBuilder->delete($this->entity); |
|
232
|
4 |
|
if (!empty($where)) { |
|
233
|
4 |
|
$queryBuilder = $queryBuilder->where($where); |
|
234
|
3 |
|
} |
|
235
|
|
|
$sql = $queryBuilder->getSQL(); |
|
236
|
4 |
|
|
|
237
|
|
|
if ($pretend === true) { |
|
238
|
4 |
|
return $sql; |
|
239
|
1 |
|
} |
|
240
|
|
|
|
|
241
|
|
|
try { |
|
242
|
|
|
$queryBuilder->execute(); |
|
243
|
3 |
|
} catch (\Exception $e) { |
|
244
|
1 |
|
throw new NeuralizerException('Query DELETE Error (' . $e->getMessage() . ')'); |
|
245
|
1 |
|
} |
|
246
|
|
|
|
|
247
|
|
|
return $sql; |
|
248
|
2 |
|
} |
|
249
|
|
|
|
|
250
|
|
|
private function getCondition(string $field, string $type) |
|
251
|
|
|
{ |
|
252
|
|
|
$type = strtolower($type); |
|
253
|
|
|
$condition = "(CASE $field WHEN NULL THEN NULL ELSE :$field END)"; |
|
254
|
|
|
switch ($type) { |
|
255
|
|
|
case 'date': |
|
256
|
|
|
return "CAST($condition as DATE)"; |
|
257
|
|
|
case 'datetime': |
|
258
|
|
|
return "CAST($condition as DATETIME)"; |
|
259
|
|
|
case 'time': |
|
260
|
|
|
return "CAST($condition as TIME)"; |
|
261
|
|
|
case 'decimal': |
|
262
|
|
|
case 'float': |
|
263
|
|
|
return "CAST($condition as DECIMAL)"; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
return $condition; |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: