'true', 'COMPRAS_INVENTORY_ROOT' => $destination]; if ($changes !== $plan['proposed_settings_only']) { throw new LogicException('Unreviewed settings changes refused.'); } $after = [...$settings, ...$changes]; $encoded = json_encode($after, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR).PHP_EOL; $preflight = new Process([PHP_BINARY, 'scripts/check-inventory-conversion-pilot.php', '--read-only'], $root, timeout: 30); $preflight->mustRun(); $check = json_decode($preflight->getOutput(), true, 32, JSON_THROW_ON_ERROR); if ($check['state'] !== 'passed' || $check['active_settings_changed'] !== false) { throw new LogicException('Read-only installation preflight failed.'); } if ($argv[1] === '--preview') { echo json_encode(['state' => 'preview', 'verified_archive' => true, 'read_only_preflight' => true, 'changed_keys' => array_keys($changes), 'settings_or_database_written' => false], JSON_THROW_ON_ERROR).PHP_EOL; exit; } if (file_exists($destination)) { throw new LogicException('Existing inventory destination refused; preserve and inspect it.'); } $backup = 'C:/laragon/data/compras/backups/inventory-activation-'.gmdate('Ymd-His').'-'.bin2hex(random_bytes(6)); if (! mkdir($backup, 0700, true) || file_put_contents($backup.'/settings.before.private.json', $before, LOCK_EX) !== strlen($before) || ! hash_equals(hash('sha256', $before), hash_file('sha256', $backup.'/settings.before.private.json'))) { throw new LogicException('Verified private settings backup required.'); } $db = new PDO('pgsql:host=127.0.0.1;port=5432;dbname='.$settings['DB_DATABASE'], $settings['DB_USERNAME'], $settings['DB_PASSWORD'], [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]); $db->exec('SET default_transaction_read_only=on'); $frames = static function () use ($db): array { $db->beginTransaction(); try { $db->exec('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY'); $out = []; foreach ($db->query("SELECT tablename FROM pg_tables WHERE schemaname='public' ORDER BY tablename")->fetchAll(PDO::FETCH_COLUMN) as $table) { $quoted = '"'.str_replace('"', '""', $table).'"'; $rows = $db->query('SELECT row_to_json(t)::text FROM '.$quoted.' t ORDER BY row_to_json(t)::text')->fetchAll(PDO::FETCH_COLUMN); $out[$table] = hash('sha256', json_encode($rows, JSON_THROW_ON_ERROR)); } $db->commit(); return $out; } finally { if ($db->inTransaction()) { $db->rollBack(); } } }; $baseline = $frames(); file_put_contents($backup.'/table-hashes.private.json', json_encode($baseline, JSON_THROW_ON_ERROR)); $protected = ['env' => hash_file('sha256', $root.'/.env'), 'frontend' => hash_file('sha256', $root.'/public/build/manifest.json')]; $zip = new ZipArchive; if ($zip->open($archive) !== true || $zip->numFiles !== 4) { throw new LogicException('Reviewed archive required.'); } InventoryConversionRuntime::assertArchiveEntries($zip); if (! mkdir($destination, 0700) || ! $zip->extractTo($destination) || ! $zip->close()) { throw new LogicException('Inventory extraction failed; settings not changed.'); } InventoryConversionRuntime::verifyArtifact($destination); $temporary = $settingsPath.'.inventory-'.bin2hex(random_bytes(6)).'.tmp'; try { if (file_put_contents($temporary, $encoded, LOCK_EX) !== strlen($encoded) || ! hash_equals(hash('sha256', $before), hash_file('sha256', $settingsPath)) || ! rename($temporary, $settingsPath)) { throw new LogicException('Atomic settings selection failed.'); } $postcheck = new Process([PHP_BINARY, 'scripts/check-inventory-conversion-pilot.php', '--installed-read-only'], $root, timeout: 30); $postcheck->mustRun(); $verified = json_decode($postcheck->getOutput(), true, 32, JSON_THROW_ON_ERROR); if ($verified['state'] !== 'passed' || ! $verified['inventory_component_installed_or_activated'] || $frames() !== $baseline || hash_file('sha256', $root.'/.env') !== $protected['env'] || hash_file('sha256', $root.'/public/build/manifest.json') !== $protected['frontend'] || json_decode(file_get_contents($settingsPath), true, 32, JSON_THROW_ON_ERROR) !== $after) { throw new LogicException('Installed component conservation failed.'); } $result = ['date' => gmdate('c'), 'state' => 'passed', 'authorization' => 'inventory-conversion-pilot-authorization.json', 'target' => 'http://127.0.0.1:8005', 'backup_directory' => $backup, 'inventory_directory' => $destination, 'inventory_manifest_sha256' => InventoryConversionRuntime::MANIFEST_SHA256, 'settings_before_sha256' => hash('sha256', $before), 'settings_after_sha256' => hash('sha256', $encoded), 'changed_keys' => array_keys($changes), 'all_database_tables_preserved' => count($baseline), 'unrelated_settings_env_frontend_and_financial_receipt_preserved' => true, 'new_permissions_users_migrations_or_business_commands' => false, 'application_complete' => false]; file_put_contents($resultPath, json_encode($result, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR).PHP_EOL); echo json_encode($result, JSON_THROW_ON_ERROR).PHP_EOL; } catch (Throwable $error) { // No blind downgrade after possible converted effects or concurrent business edits. file_put_contents($backup.'/failure.private.json', json_encode(['state' => 'requires_inspection_before_configuration_recovery', 'error_class' => $error::class], JSON_THROW_ON_ERROR)); throw $error; } finally { if (is_file($temporary)) { unlink($temporary); } }