[ 'method' => 'HEAD', 'timeout' => $timeoutSeconds, 'ignore_errors' => true, 'follow_location' => 0, 'max_redirects' => 0, 'header' => "User-Agent: ServerStatus/1.0\r\n", ], 'ssl' => [ 'verify_peer' => true, 'verify_peer_name' => true, ], ]); $code = null; $error = null; set_error_handler(static function (int $severity, string $message) use (&$error): bool { $error = $message; return true; }); $fp = @fopen($url, 'rb', false, $ctx); restore_error_handler(); if ($fp !== false) { fclose($fp); } $ms = (int)round((microtime(true) - $start) * 1000); // Parse status code from $http_response_header if (isset($http_response_header) && is_array($http_response_header)) { foreach ($http_response_header as $h) { if (preg_match('#^HTTP/\S+\s+(\d{3})#i', (string)$h, $m)) { $code = (int)$m[1]; break; } } } $ok = ($code !== null) && ($code >= 200 && $code < 400); if ($code === null && $error === null) { $error = 'Unbekannter Fehler'; } return ['ok' => $ok, 'code' => $code, 'ms' => $ms, 'error' => $error]; } function build_server_status(array $targets): array { $allowedHosts = []; foreach ($targets as $t) { if (!empty($t['url'])) { $p = parse_url((string)$t['url']); if (is_array($p) && !empty($p['host'])) { $allowedHosts[] = strtolower((string)$p['host']); } } } $allowedHosts = array_values(array_unique($allowedHosts)); $results = []; foreach ($targets as $t) { $url = (string)($t['url'] ?? ''); if ($url === '' || !is_allowed_http_url($url, $allowedHosts)) { $results[] = ['url' => $url, 'state' => 'unknown', 'ms' => null, 'detail' => 'Nicht erlaubt']; continue; } $r = check_http_head($url); $results[] = [ 'url' => $url, 'state' => $r['ok'] ? 'up' : 'down', 'ms' => $r['ms'], 'detail' => $r['ok'] ? null : ($r['error'] ?? null), ]; } return $results; }