Files
BE_IBL/scripts/migrate_encrypt_results.php
sas.fajri 17a788baac FHM31052601IBL - update FO registration controllers: decrypt PII sebelum return response
- Payment, History, Delivery: load ibl_encryptor, decrypt Name/Email/HP
- Order, Order copy: decrypt patient_name di get_header & get_order_header
- Order: pre-fetch decrypt email/HP sebelum UNION delivery query
- Order: enkripsi T_OrderDeliveryDestination saat INSERT, decrypt saat SELECT
- SQL: tambah kolom T_OrderDeliveryDestination_enc
- migrate_encrypt_results: tambah migrasi t_orderdelivery

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

150 lines
5.4 KiB
PHP

<?php
/**
* Batch migration: enkripsi data medis hasil lab dan log
* Jalankan via: php scripts/migrate_encrypt_results.php
* Aman dijalankan berulang: skip row yang sudah ada _enc-nya
*/
// Load .env
$env_file = __DIR__ . '/../.env';
if (!file_exists($env_file)) {
die("ERROR: .env tidak ditemukan di " . realpath(__DIR__ . '/..') . PHP_EOL);
}
foreach (file($env_file, 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;
}
define('BASEPATH', true);
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]
);
$batch = 500;
// Helper: migrate tabel dengan field-field sederhana (tanpa bidx)
function migrate_simple(PDO $pdo, Ibl_encryptor $enc, $table, $pk, $fields, $check_field)
{
echo "=== {$table} ===\n";
$total = 0;
$cols_sel = implode(', ', array_merge([$pk], $fields));
$set_parts = array();
foreach ($fields as $f) {
$set_parts[] = "{$f}_enc = ?";
}
$sets = implode(', ', $set_parts);
$stmt = $pdo->prepare("UPDATE {$table} SET {$sets} WHERE {$pk} = ?");
while (true) {
$rows = $pdo->query(
"SELECT {$cols_sel} FROM {$table} WHERE {$check_field}_enc IS NULL LIMIT 500"
)->fetchAll(PDO::FETCH_ASSOC);
if (empty($rows)) break;
foreach ($rows as $row) {
$params = array();
foreach ($fields as $f) {
$params[] = $enc->encrypt((string)(isset($row[$f]) ? $row[$f] : ''));
}
$params[] = $row[$pk];
$stmt->execute($params);
$total++;
}
echo " {$total} rows...\n";
}
echo "selesai: {$total} rows\n\n";
}
// ============================================================
// one_lab tables
// ============================================================
migrate_simple($pdo, $enc, 't_orderdelivery', 'T_OrderDeliveryID',
['T_OrderDeliveryDestination'],
'T_OrderDeliveryDestination');
migrate_simple($pdo, $enc, 't_orderdetail', 'T_OrderDetailID',
['T_OrderDetailResult', 'T_OrderDetailNote'],
'T_OrderDetailResult');
migrate_simple($pdo, $enc, 't_orderheader', 'T_OrderHeaderID',
['T_OrderHeaderDiagnose'],
'T_OrderHeaderDiagnose');
migrate_simple($pdo, $enc, 'so_resultentrydetail', 'So_ResultEntryDetailID',
['So_ResultEntryDetailResult'],
'So_ResultEntryDetailResult');
migrate_simple($pdo, $enc, 'so_resultentrydetail_other', 'So_ResultEntryDetailOtherID',
['So_ResultEntryDetailOtherResult', 'So_ResultEntryDetailOtherResultBefore'],
'So_ResultEntryDetailOtherResult');
migrate_simple($pdo, $enc, 'so_resultentry_fisik_umum', 'So_ResultEntryFisikUmumID',
['So_ResultEntryFisikUmumDetails'],
'So_ResultEntryFisikUmumDetails');
migrate_simple($pdo, $enc, 'so_resultentry_fisik_summary', 'So_ResultEntryFisikSummaryID',
['So_ResultEntryFisikSummaryValue', 'So_ResultEntryFisikSummaryValue2'],
'So_ResultEntryFisikSummaryValue');
migrate_simple($pdo, $enc, 'so_resultentry_other', 'So_ResultEntryOtherID',
['So_ResultEntryOtherNote'],
'So_ResultEntryOtherNote');
migrate_simple($pdo, $enc, 'so_resultentry_fisioterapi', 'So_ResultEntdyFisioterapiID',
['So_ResultEntdyFisioterapiDetails'],
'So_ResultEntdyFisioterapiDetails');
migrate_simple($pdo, $enc, 'so_resultentry_smwt', 'So_ResultentrySmwtID', [
'So_ResultentrySmwtWeight', 'So_ResultentrySmwtHeight', 'So_ResultentrySmwtBMI',
'So_ResultentrySmwtPreTensi', 'So_ResultentrySmwtPreSPO2', 'So_ResultentrySmwtPreNadi',
'So_ResultentrySmwtPostTensi', 'So_ResultentrySmwtPostSPO2', 'So_ResultentrySmwtPostNadi',
'So_ResultentrySmwtVOMax', 'So_ResultentrySmwtKategoriKebugaran',
], 'So_ResultentrySmwtWeight');
migrate_simple($pdo, $enc, 'so_resultentry_srq29_conclusion', 'So_ResultentrySrq29ConclusionID',
['So_ResultentrySrq29ConclusionResult'],
'So_ResultentrySrq29ConclusionResult');
migrate_simple($pdo, $enc, 'so_resultentrysdsinterpretation', 'So_ResultEntrySDSInterpretationID',
['So_ResultEntrySDSInterpretationDisplay'],
'So_ResultEntrySDSInterpretationDisplay');
migrate_simple($pdo, $enc, 'member_eligible', 'Member_EligibleID',
['Member_EligibleDescription'],
'Member_EligibleDescription');
// ============================================================
// one_lab_log tables (ganti koneksi ke DB log)
// ============================================================
$cfg_log = $db['one_lab_log'];
$pdo_log = new PDO(
"mysql:host={$cfg_log['hostname']};dbname={$cfg_log['database']};charset=utf8",
$cfg_log['username'],
$cfg_log['password'],
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
migrate_simple($pdo_log, $enc, 'log_patient', 'Log_PatientID',
['Log_PatientJsonBefore', 'Log_PatientJsonAfter'],
'Log_PatientJsonBefore');
migrate_simple($pdo_log, $enc, 'log_fo', 'Log_FoID',
['Log_FoJson'],
'Log_FoJson');
migrate_simple($pdo_log, $enc, 'log_resultentry', 'Log_ResultEntryID',
['Log_ResultEntryJSONBefore', 'Log_ResultEntryJSONAfter'],
'Log_ResultEntryJSONBefore');
echo "=== Semua migrasi hasil lab selesai ===\n";