verify($plan['runtime_directory'], $plan['runtime_manifest']); $assets = $root.'/'.$plan['frontend']['candidate_root']; $manifest = file_get_contents($assets.'/manifest.json'); if (! hash_equals($plan['frontend']['manifest_sha256'], hash('sha256', $manifest))) { throw new LogicException('STANDALONE_ACTIVATION_FRONTEND_CHANGED'); } foreach ($plan['frontend']['files'] as $file => $hash) { if (! str_starts_with($file, 'assets/') || str_contains($file, '..') || ! hash_equals($hash, hash_file('sha256', $assets.'/'.$file)) || (is_file($root.'/public/build/'.$file) && ! hash_equals($hash, hash_file('sha256', $root.'/public/build/'.$file)))) { throw new LogicException('STANDALONE_ACTIVATION_ASSET_CONFLICT'); } } $after = [...$settings, ...$plan['proposed_runtime_settings']]; $protectedBefore = $settings; $protectedAfter = $after; foreach (array_keys($plan['proposed_runtime_settings']) as $key) { unset($protectedBefore[$key], $protectedAfter[$key]); } if ($protectedBefore !== $protectedAfter || $settings['PAYABLE_SOURCE_INSTANCE'] !== $after['PAYABLE_SOURCE_INSTANCE']) { throw new LogicException('STANDALONE_ACTIVATION_UNRELATED_SETTINGS'); } $encoded = json_encode($after, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR).PHP_EOL; $directory = 'C:/laragon/data/compras/backups/activation-'.gmdate('Ymd-His').'-'.bin2hex(random_bytes(6)); if (! mkdir($directory, 0700, true)) { throw new LogicException('STANDALONE_ACTIVATION_BACKUP_UNAVAILABLE'); } file_put_contents($directory.'/settings.before.private.json', $before, LOCK_EX); file_put_contents($directory.'/manifest.before.json', $manifestBefore, LOCK_EX); if (! hash_equals(hash('sha256', $before), hash_file('sha256', $directory.'/settings.before.private.json')) || ! hash_equals(hash('sha256', $manifestBefore), hash_file('sha256', $directory.'/manifest.before.json'))) { throw new RuntimeException('STANDALONE_ACTIVATION_BACKUP_INCOMPLETE'); } $envHash = hash_file('sha256', $root.'/.env'); $db = new PDO('pgsql:host=127.0.0.1;port='.(int) $settings['DB_PORT'].';dbname='.$settings['DB_DATABASE'], $settings['DB_USERNAME'], $settings['DB_PASSWORD'], [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]); $frames = static function () use ($db): array { $db->beginTransaction(); try { $db->exec('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY'); $result = []; 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); $result[$table] = hash('sha256', json_encode($rows, JSON_THROW_ON_ERROR)); } $db->commit(); return $result; } finally { if ($db->inTransaction()) { $db->rollBack(); } } }; $baseline = $frames(); file_put_contents($directory.'/table-hashes.private.json', json_encode($baseline, JSON_THROW_ON_ERROR)); $replace = static function (string $path, string $bytes, string $expected): void { if (! hash_equals($expected, hash_file('sha256', $path))) { throw new LogicException('STANDALONE_ACTIVATION_CONCURRENT_EDIT'); } $temporary = $path.'.standalone-'.bin2hex(random_bytes(6)).'.tmp'; $stream = fopen($temporary, 'x'); if ($stream === false) { throw new RuntimeException('STANDALONE_ACTIVATION_STAGE_FAILED'); } try { if (fwrite($stream, $bytes) !== strlen($bytes) || ! fflush($stream)) { throw new RuntimeException('STANDALONE_ACTIVATION_STAGE_INCOMPLETE'); } } finally { fclose($stream); } if (! hash_equals($expected, hash_file('sha256', $path)) || ! rename($temporary, $path)) { throw new RuntimeException('STANDALONE_ACTIVATION_ATOMIC_REPLACE_FAILED'); } }; $manifestChanged = $settingsChanged = false; $report = ['date' => gmdate('c'), 'state' => 'in_progress', 'authorization' => 'compras-standalone-pilot-activation-authorization.json', 'target' => $plan['target'], 'backup_directory' => $directory, 'runtime_manifest' => $plan['runtime_manifest'], 'frontend_manifest' => $plan['frontend']['manifest_sha256'], 'new_grants_or_users' => false, 'business_commands' => false]; file_put_contents($directory.'/progress.private.json', json_encode($report, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR)); try { foreach ($plan['frontend']['files'] as $file => $hash) { $destination = $root.'/public/build/'.$file; if (! is_file($destination)) { $out = fopen($destination, 'x'); if ($out === false) { throw new RuntimeException('STANDALONE_ACTIVATION_ASSET_WRITE'); } $bytes = file_get_contents($assets.'/'.$file); try { if (fwrite($out, $bytes) !== strlen($bytes)) { throw new RuntimeException('STANDALONE_ACTIVATION_ASSET_INCOMPLETE'); } } finally { fclose($out); } } if (! hash_equals($hash, hash_file('sha256', $destination))) { throw new LogicException('STANDALONE_ACTIVATION_ASSET_COPY_CHANGED'); } } $replace($manifestPath, $manifest, hash('sha256', $manifestBefore)); $manifestChanged = true; $replace($settingsPath, $encoded, hash('sha256', $before)); $settingsChanged = true; $http = []; foreach (['/avaliar', '/login'] as $path) { $body = file_get_contents($plan['target'].$path, false, stream_context_create(['http' => ['timeout' => 5, 'follow_location' => 0, 'ignore_errors' => true]])); if (! is_string($body) || ! preg_match('/^HTTP\/\S+ (200|302)\b/', $http_response_header[0] ?? '', $status)) { throw new RuntimeException('STANDALONE_ACTIVATION_HTTP_FAILED'); } $http[$path] = (int) $status[1]; } if ($frames() !== $baseline || hash_file('sha256', $root.'/.env') !== $envHash || json_decode(file_get_contents($settingsPath), true, 32, JSON_THROW_ON_ERROR) !== $after) { throw new LogicException('STANDALONE_ACTIVATION_CONSERVATION_FAILED'); } $report += ['capabilities_enabled' => count($plan['feature_names']), 'assets_verified' => count($plan['frontend']['files']), 'all_database_tables_preserved' => count($baseline), 'unrelated_settings_preserved' => true, 'source_instance_preserved' => true, 'public_env_preserved' => true, 'http' => $http, 'public_domain_runtime_activated' => false]; $report['state'] = 'passed'; } catch (Throwable $error) { if ($settingsChanged) { $replace($settingsPath, $before, hash('sha256', $encoded)); } if ($manifestChanged) { $replace($manifestPath, $manifestBefore, hash('sha256', $manifest)); } $report['state'] = 'failed_configuration_and_manifest_restored'; $report['failure_class'] = get_class($error); throw $error; } finally { file_put_contents($directory.'/result.private.json', json_encode($report, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR)); file_put_contents($resultPath, json_encode($report, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR).PHP_EOL); } echo json_encode(['state' => $report['state'], 'capabilities_enabled' => $report['capabilities_enabled'], 'assets_verified' => $report['assets_verified'], 'database_tables_preserved' => $report['all_database_tables_preserved'], 'business_commands' => false, 'new_grants' => false], JSON_THROW_ON_ERROR).PHP_EOL;