diff --git a/composer.json b/composer.json index 4307ec9..e55dc0b 100644 --- a/composer.json +++ b/composer.json @@ -1,16 +1,20 @@ { - "name": "machbarmacher/gdpr-dump", + "name": "uncinc/gdpr-dump", "description": "A drop-in replacement for mysqldump that optionally sanitizes DB fields for better GDPR conformity.", "require": { - "symfony/console": "^3.0", + "symfony/console": "^4", "ifsnop/mysqldump-php": "dev-master", "cweagans/composer-patches": "^1.6", - "bomoko/mysql-cnf-parser": "^0.0.2", + "bomoko/mysql-cnf-parser": "dev-master", "fzaninotto/faker": "^1.7", "symfony/event-dispatcher": "~3.4|~4.0" }, "license": "GPL-2.0-or-later", "authors": [ + { + "name": "Nico de Groot", + "email": "nico@uncinc.nl" + }, { "name": "Axel Rutz", "email": "axel.rutz@machbarmacher.net" diff --git a/src/MysqldumpGdpr.php b/src/MysqldumpGdpr.php index 1b384aa..c5d60d4 100644 --- a/src/MysqldumpGdpr.php +++ b/src/MysqldumpGdpr.php @@ -39,9 +39,51 @@ public function __construct( $this->debugSql = $dumpSettings['debug-sql']; unset($dumpSettings['debug-sql']); } + + $this->setTransformTableRowHook(function ($tableName, array $row) { + foreach ($row AS $colName => $colValue) { + $excludeRow = self::excludeRowCheck($row, $tableName, $colName); + if (!$excludeRow) { + $replacement = ColumnTransformer::replaceValue($tableName, $colName, $this->gdprReplacements[$tableName][$colName]); + if ($replacement !== FALSE) { + $row[$colName] = $replacement; + } + } + } + return $row; + }); + parent::__construct($dsn, $user, $pass, $dumpSettings, $pdoSettings); } + private function excludeRowCheck($row, $tableName, $colName) { + $table_exludes = $this->gdprReplacements[$tableName]['_exclude']; + + if (!empty($this->gdprReplacements[$tableName][$colName])) { + foreach ($row as $row_column_name => $row_column_value) { + if ($table_exludes[$row_column_name]) { + + // Check for direct matches. + if (in_array($row_column_value, $table_exludes[$row_column_name])) { + return TRUE; + } + + // Check for partial matches. + foreach ($table_exludes[$row_column_name] AS $value_to_ignore) { + if (strpos($value_to_ignore, '*') !== FALSE) { + $stripped_value_to_ignore = str_replace('*', '', $value_to_ignore); + if (strpos($row_column_value, $stripped_value_to_ignore) !== FALSE) { + return TRUE; + } + } + } + } + } + } + + return FALSE; + } + public function getColumnStmt($tableName) { $columnStmt = parent::getColumnStmt($tableName); @@ -61,15 +103,4 @@ public function getColumnStmt($tableName) return $columnStmt; } - protected function hookTransformColumnValue($tableName, $colName, $colValue) - { - if (!empty($this->gdprReplacements[$tableName][$colName])) { - $replacement = ColumnTransformer::replaceValue($tableName, $colName, $this->gdprReplacements[$tableName][$colName]); - if($replacement !== FALSE) { - return $replacement; - } - } - return $colValue; - } - }