[]]; $handle = @fopen($cacheFile, 'c+'); if ($handle === false) { return true; } try { if (!flock($handle, LOCK_EX)) { return true; } $contents = stream_get_contents($handle); if ($contents !== false && $contents !== '') { $decoded = json_decode($contents, true); if (is_array($decoded) && isset($decoded['requests'])) { $data = $decoded; } } $data['requests'] = array_values(array_filter( $data['requests'], static fn($timestamp) => ($currentTime - (int) $timestamp) < 3600 )); if (count($data['requests']) >= $maxPerHour) { return false; } $data['requests'][] = $currentTime; ftruncate($handle, 0); rewind($handle); fwrite($handle, json_encode($data)); } finally { flock($handle, LOCK_UN); fclose($handle); } return true; } function getValidatedDomainParam(string $param = 'domain'): ?string { if (empty($_GET[$param])) { return null; } $domain = trim((string) $_GET[$param]); $domain = preg_replace('/^(https?:\/\/)?/', '', $domain); $domain = explode('/', $domain)[0]; $domain = explode(':', $domain)[0]; if (!preg_match('/^[a-zA-Z0-9][a-zA-Z0-9\-\.]*\.[a-zA-Z]{2,}$/', $domain)) { return null; } return $domain; } function rejectApiRateLimit(): void { http_response_code(429); echo json_encode(['error' => 'Zu viele Anfragen. Bitte versuchen Sie es später erneut.']); exit; }