ning lock mode + loopback kickoff detection'); // Basic auth sync must run before the loopback probe so the probe // already carries the detected Authorization header. self::syncDetectedBasicAuth($reset); $lockResult = LockUtil::redetectLockMode(); $loopbackPass = ClientSideKick::canServerSelfRequest(); } catch (\Throwable $e) { DupLog::traceError('SERVER DETECTION: failed — ' . $e->getMessage()); self::resetServerDetected(); return [ 'lockResult' => [ 'sqlReliable' => false, 'fileReliable' => false, 'sqlError' => $e->getMessage(), 'fileError' => $e->getMessage(), ], 'loopbackPass' => false, ]; } return [ 'lockResult' => $lockResult, 'loopbackPass' => $loopbackPass, ]; } /** * Sync the stored basic auth credentials with the ones detected from the * current request, when the basic auth mode is 'auto'. * * When nothing is detected the stored credentials are cleared only on * authoritative runs (interactive settings save / Tools redetect, where the * request went through the real basic auth). Background runs (cron, system * cron) may simply lack the auth server variables: there the stored * credentials are kept — a stale Authorization header on a server without * basic auth is ignored, while clearing valid credentials would break builds. * * @param bool $authoritative True when the current request context is authoritative * * @return void */ public static function syncDetectedBasicAuth(bool $authoritative): void { $dGlobal = DynamicGlobalEntity::getInstance(); if ($dGlobal->getValString(DynamicGlobalEntity::BASIC_AUTH_MODE_KEY) !== 'auto') { return; } $detected = SnapServer::detectBasicAuthCredentials(); if ($detected !== null) { $dGlobal->setValString(DynamicGlobalEntity::BASIC_AUTH_USER_KEY, $detected['user']); $dGlobal->setValString(DynamicGlobalEntity::BASIC_AUTH_PASSWORD_KEY, $detected['password'], true); } elseif ($authoritative) { $dGlobal->setValString(DynamicGlobalEntity::BASIC_AUTH_USER_KEY, ''); $dGlobal->setValString(DynamicGlobalEntity::BASIC_AUTH_PASSWORD_KEY, '', true); } } /** * Reset detection state and schedule a fresh async detection. * * Called after migration (updateAftreInstall) when the host environment * has changed. Backups will hold until the new detection completes. * * @return void */ public static function resetAndReschedule(): void { ClientSideKick::resetLoopbackCache(); self::resetServerDetected(); self::scheduleDetection(); } /** * @return bool True once server detection has run at least once */ public static function isServerDetected(): bool { return DynamicGlobalEntity::getInstance()->getValBool(self::SERVER_DETECTED_KEY); } /** * @return void */ public static function markServerDetected(): void { DynamicGlobalEntity::getInstance()->setValBool(self::SERVER_DETECTED_KEY, true, true); } /** * @return void */ private static function resetServerDetected(): void { DynamicGlobalEntity::getInstance()->removeVal(self::SERVER_DETECTED_KEY, true); } /** * Exclude all host-specific detection keys from settings export. * * @param string[] $skipExportData keys already excluded * * @return string[] */ public static function addSkipExportKeys(array $skipExportData): array { $skipExportData[] = self::SERVER_DETECTED_KEY; $skipExportData[] = ClientSideKick::KICKOFF_DGLOBAL_KEY; $skipExportData[] = ClientSideKick::LOOPBACK_SCHEME_DGLOBAL_KEY; $skipExportData[] = ClientSideKick::KICKOFF_OVERRIDE_KEY; $skipExportData[] = ClientSideKick::AJAX_PROTOCOL_OVERRIDE_KEY; $skipExportData[] = ClientSideKick::AJAX_URL_OVERRIDE_KEY; $skipExportData[] = DynamicGlobalEntity::BASIC_AUTH_MODE_KEY; $skipExportData[] = DynamicGlobalEntity::BASIC_AUTH_USER_KEY; $skipExportData[] = DynamicGlobalEntity::BASIC_AUTH_PASSWORD_KEY; return $skipExportData; } }