$v) { if (strpos($k, 'HTTP_') === 0) { $h[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($k, 5)))))] = $v; } } return $h; } function send_json($data, $status = 200) { http_response_code($status); header('Content-Type: application/json'); header('Access-Control-Allow-Origin: *'); echo json_encode($data, JSON_INVALID_UTF8_SUBSTITUTE | JSON_UNESCAPED_SLASHES); exit; } function read_log($file) { if (!is_file($file)) return []; $raw = file_get_contents($file); $data = $raw ? json_decode($raw, true) : []; return is_array($data) ? $data : []; } function change_log($file, $fn) { $fp = fopen($file, 'c+'); flock($fp, LOCK_EX); $raw = stream_get_contents($fp); $data = $raw ? json_decode($raw, true) : []; if (!is_array($data)) $data = []; $data = $fn($data); ftruncate($fp, 0); rewind($fp); fwrite($fp, json_encode($data, JSON_INVALID_UTF8_SUBSTITUTE | JSON_UNESCAPED_SLASHES)); fflush($fp); flock($fp, LOCK_UN); fclose($fp); } function require_login($password) { if ($password === '') return; $auth = ''; foreach (all_headers() as $k => $v) { if (strtolower($k) === 'authorization') $auth = $v; } if ($auth === '' && !empty($_SERVER['HTTP_AUTHORIZATION'])) $auth = $_SERVER['HTTP_AUTHORIZATION']; if ($auth === '' && !empty($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) $auth = $_SERVER['REDIRECT_HTTP_AUTHORIZATION']; if (stripos($auth, 'Basic ') === 0) { $decoded = base64_decode(substr($auth, 6)); $parts = explode(':', (string)$decoded, 2); if (isset($parts[1]) && hash_equals($password, $parts[1])) return; } header('WWW-Authenticate: Basic realm="Webhook logs"'); http_response_code(401); echo 'Login required'; exit; } // ---------- routing ---------- if ($path === '/' && $method === 'GET') { header('Location: ' . $BASE . $UI); exit; } if ($path === $UI || strpos($path, $UI . '/') === 0) { require_login($PASSWORD); $sub = substr($path, strlen($UI)); if (($sub === '' || $sub === '/') && $method === 'GET') { header('Content-Type: text/html; charset=utf-8'); echo INDEX_HTML(); exit; } if ($sub === '/api/requests' && $method === 'GET') { $after = isset($_GET['after']) ? (float)$_GET['after'] : 0; $all = read_log($DATA_FILE); $items = []; foreach ($all as $e) { if ($e['time'] > $after) $items[] = $e; } send_json(['items' => $items, 'count' => count($all)]); } if ($sub === '/api/requests' && $method === 'DELETE') { change_log($DATA_FILE, function ($d) { return []; }); send_json(['ok' => true]); } send_json(['error' => 'not found'], 404); } // ---------- capture any other request ---------- if ($method === 'OPTIONS') { header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Headers: *'); header('Access-Control-Allow-Methods: *'); http_response_code(204); exit; } $body = file_get_contents('php://input', false, null, 0, $MAX_BODY); if ($body === false) $body = ''; $binary = ($body !== '' && @preg_match('//u', $body) !== 1); $query = []; $qs = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY); if ($qs) { foreach (explode('&', $qs) as $pair) { if ($pair === '') continue; $kv = explode('=', $pair, 2); $k = urldecode($kv[0]); $query[$k][] = isset($kv[1]) ? urldecode($kv[1]) : ''; } } $ip = $_SERVER['REMOTE_ADDR'] ?? ''; foreach (all_headers() as $k => $v) { if (strtolower($k) === 'x-forwarded-for') $ip = trim(explode(',', $v)[0]); } $entry = [ 'id' => substr(bin2hex(random_bytes(8)), 0, 10), 'time' => microtime(true), 'method' => $method, 'path' => $path, 'query' => (object)$query, 'headers' => (object)all_headers(), 'ip' => $ip, 'size' => strlen($body), 'binary' => $binary, 'body' => $binary ? base64_encode($body) : $body, ]; change_log($DATA_FILE, function ($d) use ($entry, $MAX_KEEP) { $d[] = $entry; return array_slice($d, -$MAX_KEEP); }); send_json(['ok' => true, 'id' => $entry['id']]); // ---------- log viewer page ---------- function INDEX_HTML() { return <<<'HTML' Webhook logs

Webhook logs

connecting
HTML; }