59 public function mask($text)
65 $text = preg_replace_callback(
66 '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/',
80 $text = preg_replace_callback(
81 '/\b(?!\d{1,5}[A-Za-z]\b)(?=[A-Z0-9]*[0-9])(?=[A-Z0-9]*[A-Z])[A-Z0-9-_]{4,}\b/i',
94 $text = preg_replace_callback(
95 '/\b(?:\d[ -]*?){13,19}\b/',
96 [$this,
'maskCreditCardCallback'],
101 $text = preg_replace_callback(
102 '/\b[A-Z]{2}[0-9]{2}[a-zA-Z0-9]{4,30}\b/',
107 function (array $m) {
118 $text = preg_replace_callback(
119 '/(?i)(?:account\s+num(?:ber)?|bank\s+acct|acct\s*#)[:\s#]*\b(\d{8,17})\b/',
124 function (array $m) {
133 'name' =>
'EU VAT Numbers',
134 'regex' =>
'/\b(AT|BE|BG|CY|CZ|DE|DK|EE|EL|ES|FI|FR|GB|GR|HR|HU|IE|IT|LT|LU|LV|MT|NL|PL|PT|RO|SE|SI|SK)(?![a-z])[0-9A-Z]{2,12}\b/i',
138 'name' =>
'Canadian GST/HST Numbers',
139 'regex' =>
'/\b\d{9}\s*RT\s*\d{4}\b/i',
143 'name' =>
'Australian ABN (Australian Business Number)',
144 'regex' =>
'/\b\d{2}\s*\d{3}\s*\d{3}\s*\d{3}\b/',
148 'name' =>
'Norwegian MVA (VAT) Numbers',
149 'regex' =>
'/\b\d{9}\s*MVA\b/i',
153 'name' =>
'Swiss VAT Numbers (MWST/TVA/IVA)',
154 'regex' =>
'/\bCHE-?\d{3}\.?\d{3}\.?\d{3}\s*(MWST|TVA|IVA)\b/i',
159 foreach ($taxPatterns as $pattern) {
160 $text = preg_replace_callback(
166 function (array $m) use ($pattern) {
167 return $this->
createToken($m[0], $pattern[
'token']);
176 'name' =>
'Greek International Numbers',
181 'regex' =>
'/(?<![\w+])(?:\+30|0030)[\s.\-]?(?:2\d|69)(?:[\s.\-()]?\d){8}(?!\d)/',
185 'name' =>
'Generic International Numbers',
190 'regex' =>
'/(?<![\w+])(?:\+|00)\d{1,3}(?:[\s.\-()]?\d){8,13}(?!\d)/',
194 'name' =>
'Greek National Numbers',
205 'regex' =>
'/(?<!\d)(?:2\d|69)(?:[\s.\-()]?\d){8}(?!\d)/',
209 'name' =>
'French National Numbers',
213 'regex' =>
'/\b0[1-9](?:[\s.-]?\d){8}\b/',
218 foreach ($phonePatterns as $pattern) {
219 $text = preg_replace_callback(
225 function (array $m) use ($pattern) {
226 return $this->
createToken($m[0], $pattern[
'token']);
240 $address_keywords =
'Street|Road|Avenue|Lane|Boulevard|Drive|Court|Way|Plaza|Square|Rue|Allée|Impasse|Chemin|Place|Quai|Via|Viale|Piazza|Corso|Avenida|Paseo|Camino|Strasse|Straße|Platz|Gasse|Weg|Οδός|Οδ\.|Λεωφόρος|Λεωφ\.|Πλατεία';
241 $address_abbreviations =
'St|Rd|Ave|Ln|Blvd|Dr|Ct|Sq|Str';
244 $address_keywords_lower =
'rue|avenue|boulevard|chemin|place|allée|impasse|quai|avenida|paseo|camino|via|viale|piazza|corso';
245 $address_suffixes =
'strasse|straße|weg|platz|gasse|allee|ring|damm|ufer|laan|straat|gracht|vej|gatan|gade';
246 $word =
'\p{Lu}[\p{L}\p{N}\.\'\-]*';
247 $particle =
'\p{Ll}{1,3}\b';
250 $number =
'\d{1,5}[A-Za-z]?(?!\d|[.,]\d)\b';
252 $town =
'(?=\s*,?\s*(?:\d{3}\s?\d{2}|\d{4,5})(?!\d)(?!\s*(?:[A-Z]{3}\b|[€$£¥₹%]))\s+\p{Lu})';
256 'name' =>
'Number First (e.g., 12 rue de Rivoli, 123 Main St, 221B Baker Street)',
257 'regex' =>
'/\b\d{1,5}[A-Za-z]?,?\s+(?:' . $word .
'\s+){0,3}(?:' . $address_keywords .
'|' . $address_abbreviations .
'|' . $address_keywords_lower .
')\b\.?(?:\s+(?:' . $word .
'|' . $particle .
')){0,4}/u',
261 'name' =>
'Name First, Keyword After (e.g., Main Street 42, Main St. 42)',
262 'regex' =>
'/\b' . $word .
'(?:\s+' . $word .
'){0,2}\s+(?:' . $address_keywords .
'|' . $address_abbreviations .
')\.?\s+' . $number .
'/u',
266 'name' =>
'Street Suffix (e.g., Musterweg 12, Hauptstraße 5)',
267 'regex' =>
'/\b\p{Lu}\p{L}*(?:' . $address_suffixes .
')\.?\s+' . $number .
'/u',
271 'name' =>
'Keyword First (e.g., Rue Victor Hugo 5, Οδός Ερμού 12)',
273 'regex' =>
'/\b(?:' . $address_keywords .
')(?:\s+(?:' . $word .
'|' . $particle .
')){1,4}\s+' . $number .
'/u',
277 'name' =>
'No Keyword, Postcode And Town After (e.g., ΦΟΡΜΙΩΝΟΣ 101, 115 28 Athens)',
278 'regex' =>
'/\b' . $word .
'(?:\s+' . $word .
'){0,2}\s+' . $number . $town .
'/u',
283 foreach ($addressPatterns as $pattern) {
285 $text = preg_replace_callback(
291 function (array $m) use ($pattern) {
292 return $this->
createToken($m[0], $pattern[
'token']);
301 'name' =>
'UK Postal Codes',
303 'regex' =>
'/\b[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}\b/i',
307 'name' =>
'Canadian Postal Codes',
309 'regex' =>
'/\b[A-CEGHJ-NPR-STV-Z]\d[A-CEGHJ-NPR-STV-Z][ -]?\d[A-CEGHJ-NPR-STV-Z]\d\b/i',
313 'name' =>
'French Postal Codes',
316 'regex' =>
'/\b(0[1-9]\d{3}|[1-8]\d{4}|9[0-5]\d{2}|2[AB]\d{3})\b/',
318 'amounts_look_alike' => true
321 'name' =>
'Greek Postal Codes',
324 'regex' =>
'/\b\d{3}\s?\d{2}\b/',
326 'amounts_look_alike' => true
329 'name' =>
'US ZIP Codes',
331 'regex' =>
'/\b\d{5}(?:-\d{4})?\b/',
333 'amounts_look_alike' => true
337 foreach ($zipCodePatterns as $pattern) {
338 if (!empty($pattern[
'amounts_look_alike'])) {
343 $text = preg_replace_callback(
349 function (array $m) use ($pattern) {
350 return $this->
createToken($m[0], $pattern[
'token']);
373 if (!preg_match_all($regex, $text, $m, PREG_OFFSET_CAPTURE)) {
376 $labels =
'amount|total|subtotal|sum|price|qty|quantity|balance|due|paid|net|gross|montant|prix|somme|betrag|summe|preis|importe|precio|ποσό|σύνολο|τιμή|αξία';
378 foreach ($m[0] as $found) {
379 $value = (string) $found[0];
380 $start = (int) $found[1];
381 $end = $start + strlen($value);
382 $before = substr($text, max(0, $start - 30), $start - max(0, $start - 30));
383 $after = substr($text, $end, 12);
384 if (preg_match(
'/\d[.,]$/', $before) || preg_match(
'/^[.,]\d/', $after)) {
387 if (preg_match(
'/(?:[€$£¥₹]|\b[A-Z]{3})\s*$/u', $before) || preg_match(
'/^\s*(?:[€$£¥₹%]|[A-Z]{3}\b)/u', $after)) {
390 if (preg_match(
'/(?:'.$labels.
')\s*[:=]?\s*$/iu', $before)) {
393 $replace[] = array($start, strlen($value), $this->
createToken($value, $token));
396 foreach (array_reverse($replace) as $r) {
397 $text = substr($text, 0, $r[0]).$r[2].substr($text, $r[0] + $r[1]);
412 if (empty($this->map)) {
417 $search = array_keys($this->map);
418 $replace = array_values($this->map);
420 return str_replace($search, $replace, $jsonString);
439 if (!preg_match_all(
'/\b[A-Z]{6}([A-Z0-9]{2})([A-Z0-9]{3})?\b/', $text, $m, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) {
443 foreach (array_reverse($m) as $hit) {
444 list($code, $pos) = $hit[0];
445 $tail = $hit[1][0].(isset($hit[2]) ? $hit[2][0] :
'');
446 $start = max(0, $pos - 30);
447 $before = substr($text, $start, $pos - $start);
448 if (!preg_match(
'/(bic|swift)/i', $before) && !preg_match(
'/[0-9]/', $tail)) {
451 $text = substr($text, 0, $pos).$this->createToken($code,
'SWIFT').substr($text, $pos + strlen($code));
466 $potentialCc = $matches[0];
484 $digits = preg_replace(
'/\D/',
'', $number);
487 if (strlen($digits) < 13 || strlen($digits) > 19) {
493 $isEvenDigit =
false;
496 for ($i = strlen($digits) - 1; $i >= 0; $i--) {
497 $digit = (int) $digits[$i];
508 $isEvenDigit = !$isEvenDigit;
512 return ($sum % 10) === 0;
524 if (empty($this->map)) {
529 $text = $this->
unmask($text);
533 foreach ($this->map as $fullToken => $originalValue) {
536 $strippedToken = substr($fullToken, 2, -2);
538 if (strpos($text, $strippedToken) !==
false) {
540 $text = str_replace($strippedToken, $originalValue, $text);
557 if ($this->salt !==
'') {
576 $existing = array_search($value, $this->map,
true);
577 if ($existing !==
false) {
578 return (
string) $existing;
585 $token =
"[[{$type}_{$this->index}{$this->salt}]]";
586 $this->map[$token] = $value;
607 foreach ($names as $name) {
608 $name = trim((
string) $name);
617 $clean = array_unique($clean);
618 usort($clean,
function (
string $a,
string $b) {
621 foreach ($clean as $name) {
625 $pattern =
'/(?<![\p{L}\p{N}])'.preg_quote($name,
'/').
'(?![\p{L}\p{N}])/iu';
626 $parts = preg_split(
'/(\[\[[A-Z]+_[^\]]*\]\])/', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
627 if (!is_array($parts)) {
631 foreach ($parts as $pi => $part) {
632 if ($pi % 2 === 1 || !preg_match($pattern, $part)) {
636 $parts[$pi] = (string) preg_replace($pattern, $this->
createToken($name,
'NAME'), $part);
639 $text = implode(
'', $parts);
Class to manage privacy data masking and unmasking.
maskNames($text, $names)
Mask the names of the objects carried in this payload.
startSession()
Start the masking session for this guard instance, once.
mask($text)
Mask sensitive GDPR data in the query.
maskCreditCardCallback(array $matches)
Callback function for preg_replace_callback to mask credit cards.
unmaskAiResponse($text)
Unmasks a string from an AI response, handling cases where the AI might have stripped the [[ and ]] d...
passesLuhnCheck($number)
Validates a number string using the Luhn algorithm.
unmask($jsonString)
Restore real data from a masked string.
createToken($value, $type)
Create a unique token and store the original value in the map.
maskOutsideAmounts($text, $regex, $token)
Mask the matches of a pattern, except those that are an amount: part of a larger number (12,...
maskSwift($text)
Mask SWIFT / BIC codes.
dol_strlen($string, $stringencoding='UTF-8', $binarycount=0)
Make a strlen call.
dol_substr($string, $start, $length=null, $stringencoding='', $trunconbytes=0)
Make a substring.
dol_hash($chain, $type='0', $nosalt=0, $mode=0)
Returns a hash (non reversible encryption) of a string.