0) { @ob_end_clean(); } ob_start(); error_reporting(E_ALL); ini_set('display_errors', '0'); if (function_exists('opcache_invalidate')) { @opcache_invalidate(__FILE__); } require_once __DIR__ . '/../core.php'; require_once __DIR__ . '/../lsky.php'; require_once __DIR__ . '/../gh_api.php'; // Webhook 必须 POST + application/json if (strtoupper($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') { http_response_code(405); header('Content-Type: application/json; charset=utf-8'); echo json_encode(['success' => false, 'message' => 'method not allowed (POST only)']); exit; } $rawBody = file_get_contents('php://input'); if ($rawBody === false || $rawBody === '') { http_response_code(400); header('Content-Type: application/json; charset=utf-8'); echo json_encode(['success' => false, 'message' => 'empty body']); exit; } // 1) 签名校验 $sigHeader = isset($_SERVER['HTTP_X_HUB_SIGNATURE_256']) ? (string)$_SERVER['HTTP_X_HUB_SIGNATURE_256'] : ''; if (!ghWebhookVerify($rawBody, $sigHeader)) { ghLog('WEBHOOK REJECT: bad X-Hub-Signature-256'); http_response_code(403); header('Content-Type: application/json; charset=utf-8'); echo json_encode(['success' => false, 'message' => 'signature mismatch']); exit; } // 2) 解析 JSON $payload = json_decode($rawBody, true); if (!is_array($payload)) { http_response_code(400); header('Content-Type: application/json; charset=utf-8'); echo json_encode(['success' => false, 'message' => 'invalid json']); exit; } $event = strtolower(isset($_SERVER['HTTP_X_GITHUB_EVENT']) ? (string)$_SERVER['HTTP_X_GITHUB_EVENT'] : ''); if ($event === '') $event = isset($payload['event']) ? (string)$payload['event'] : 'unknown'; // 只关心 issue 相关事件 $handled = ghHandleWebhook($payload, $event); // GitHub 要求 webhook 返回 2xx 才认为投递成功;处理不了也回 200 + 标记,避免 GitHub 反复重发 header('Content-Type: application/json; charset=utf-8'); echo json_encode([ 'success' => true, 'message' => $handled ? 'handled' : 'ignored (not our ticket / no action)', 'event' => $event, ]); exit;