Files
BE_IBL/scripts/remask_patient_name.php
sas.fajri f667050200 FHM31052601IBL - update format masking nama: kata pertama penuh + inisial kata berikutnya
"FAJRI HARDHITA" → "FAJRI H*******" lebih readable untuk operasional.
Script remask_patient_name.php untuk re-apply ke data yang sudah dimasking.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-31 15:23:19 +07:00

77 lines
2.2 KiB
PHP

<?php
/**
* Re-mask M_PatientName dengan format baru: "NAMA DEPAN I******* N****"
* Dekripsi dari _enc lalu masking ulang kolom plaintext
* Jalankan setelah address & NIK migration selesai
*/
define('BASEPATH', true);
foreach (file(__DIR__ . '/../.env', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $l) {
if (strpos(trim($l), '#') === 0) continue;
[$k, $v] = array_map('trim', explode('=', $l, 2));
if ($k !== '') $_ENV[$k] = $v;
}
require __DIR__ . '/../application/libraries/Ibl_encryptor.php';
$enc = new Ibl_encryptor();
include __DIR__ . '/../application/config/database.php';
$cfg = $db['default'];
$pdo = new PDO(
"mysql:host={$cfg['hostname']};dbname={$cfg['database']};charset=utf8",
$cfg['username'], $cfg['password'],
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
function mask_name($v) {
if (!$v) return $v;
$v = trim($v);
$words = preg_split('/\s+/', $v);
if (count($words) === 1) {
$l = mb_strlen($v, 'UTF-8');
return $l <= 6 ? $v : mb_substr($v, 0, 6, 'UTF-8') . '***';
}
$first = $words[0];
$rest = array_slice($words, 1);
$masked = array_map(function($w) {
if (!$w) return '';
$init = mb_substr($w, 0, 1, 'UTF-8');
return $init . str_repeat('*', max(3, mb_strlen($w, 'UTF-8') - 1));
}, $rest);
return $first . ' ' . implode(' ', $masked);
}
echo "=== Re-mask M_PatientName (format baru) ===\n";
$total = 0;
$batch = 500;
$last_id = 0;
$stmt = $pdo->prepare(
"UPDATE m_patient SET M_PatientName = ? WHERE M_PatientID = ?"
);
while (true) {
$rows = $pdo->query(
"SELECT M_PatientID, M_PatientName_enc
FROM m_patient
WHERE M_PatientName_enc IS NOT NULL
AND M_PatientID > {$last_id}
ORDER BY M_PatientID ASC
LIMIT {$batch}"
)->fetchAll(PDO::FETCH_ASSOC);
if (empty($rows)) break;
foreach ($rows as $row) {
$real_name = $enc->decrypt($row['M_PatientName_enc']);
if ($real_name !== null) {
$stmt->execute([mask_name($real_name), $row['M_PatientID']]);
}
$last_id = $row['M_PatientID'];
$total++;
}
echo " {$total} rows...\n";
}
echo "Selesai: {$total} rows\n";