From 083f7602074d36a80a364f32cd24e9886cc0fc19 Mon Sep 17 00:00:00 2001 From: ypsdf1 Date: Wed, 25 Nov 2026 15:59:07 +0800 Subject: [PATCH] =?UTF-8?q?15m44v:=20=E4=BF=AE=E9=99=84=E4=BB=B6=E5=8D=A0?= =?UTF-8?q?=E4=BD=8D=E7=AC=A6=E6=9B=BF=E6=8D=A2+token=E9=AA=8C=E7=9C=9F+gh?= =?UTF-8?q?Log=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - gh_api.php ghSyncCreate: 占位符替换串 '\U📎 附件\1\E' 改 '\u📎 附件\1' 根因: PCRE 的 \U..\E 是大小写转换开关, 夹在 emoji 后把 CJK 误转/乱码, 导致 issue body 里 [图片N] 未正确替换为附件提示; \u 对 CJK 恒等无副作用 加 create 诊断日志(has_attach/desc_head/imgs)写 /plugin/gh_sync.log - gh_api.php ghLog: 路径 dirname(__DIR__)→__DIR__ 根因: gh_api.php 在 /plugin/, __DIR__=/plugin, 旧 dirname 得到网站根 /, 日志被写到网站根而非 /plugin → 用户在 /plugin 找永远 NOT FOUND - gh_api.php ghApiRequest: 加 Cache-Control: no-store 头, 防 CF 边缘缓存验活响应 - gh_oauth.php: /user 验活加唯一 query 尾巴(x=nocache_rand), 破 CF 缓存, 验活响应不可信是'没授权却显示已授权'根因; 加 probe 日志 - img.json: 修第21行混入的非法字符(反引号+中文逗号, heredoc 事故残留), 保持纯占位符 --- api/gh_oauth.php | 13 +++++++++++-- gh_api.php | 23 ++++++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/api/gh_oauth.php b/api/gh_oauth.php index 0f778ea..f6be1e9 100644 --- a/api/gh_oauth.php +++ b/api/gh_oauth.php @@ -74,8 +74,17 @@ if ($code === '') { $trow = $st->execute()->fetchArray(SQLITE3_ASSOC); } catch (\Throwable $e) { $trow = null; } if ($trow && !empty($trow['access_token'])) { - // 验活:GitHub /user 用该 token,200=有效 401=失效 - $v = ghApiRequest('GET', '/user', null, $trow['access_token']); + // 验活:GitHub /user 用该 token,200=有效 401=失效。 + // ★ 15m44v 修:验活加"破缓存"query —— 给 URL 拼一个每毫秒唯一的随机尾巴 + // (如 /user?x=nocache_),让 Cloudflare 无法把【同一 token 的验活响应】 + // 缓存复用。根因:gh_oauth.php 发起授权时调 /user,CF 边缘可能缓存了该 token + // 上一次的 200 响应;token 真失效后(被 GitHub 撤销/过期),CF 仍吐缓存 200 → + // 前端误判"已授权"(用户实测:没授权却显示已授权、提单后 GitHub 又真同步了 + // 说明 token 实际有效,但验活逻辑被 CF 缓存干扰导致显示态不可信)。 + // 随机尾巴让每个验活都是全新 GET,CF 命中不了缓存,真实 200/401 才可信。 + $v = ghApiRequest('GET', '/user?x=nocache_' . (int)microtime(true) . mt_rand(100000, 999999), null, $trow['access_token']); + ghLog('oauth probe player=' . $probePlayer . ' token=' . substr((string)$trow['access_token'], 0, 8) + . ' status=' . (int)$v['status'] . ' ok=' . ($v['ok'] ? 'y' : 'n')); if (!empty($v['ok'])) { header('Location: ' . $baseSite . '/player.php?gh_ok=1&gh_inherit=1&gh_user=' . urlencode((string)($trow['gh_login'] ?: ''))); exit; diff --git a/gh_api.php b/gh_api.php index 26de09a..e84e71f 100644 --- a/gh_api.php +++ b/gh_api.php @@ -39,6 +39,10 @@ function ghApiRequest($method, $path, $payload = null, $token = null) { 'Accept: application/vnd.github+json', 'X-GitHub-Api-Version: 2022-11-28', 'User-Agent: caoyuan-ticket-bot', + // ★ 15m44v: 验活/读操作禁缓存 —— 防止 Cloudflare 边缘缓存 /user 等响应导致 + // token 验真误判(失效 token 被 CF 吐旧 200 缓存)。写操作(POST 建单/评论)带 + // no-store 无害。 + 'Cache-Control: no-store', ]; if ($token !== null && $token !== '') $headers[] = 'Authorization: Bearer ' . $token; @@ -119,19 +123,28 @@ function ghSyncCreate($ticketId, $player, $title, $description, $images = [], $t // - @gh: 开头 = GitHub 模式占位附件(只记文件名,不走图床)→ 显示为「📎 附件:文件名」 // - http(s) = 真实图床外链 → 用 markdown 图片语法 - // ★ 15m44q: 服务端把描述里的 [图片N] 占位符就地替换成「📎 附件N:文件名」。 + // ★ 15m44q: 服务端把描述里的 [图片N] 占位符就地替换成「📎 附件N」。 // 占位符是前端暂存附件时插进描述的(提交后才在客户端替换),服务端拿到的 description // 仍带着 [图片N]。旧版 ghSyncCreate 直接把原始描述塞进 issue body,导致 GitHub 上 // 显示「abc[图片1]」占位符没被替换(用户实测附件不对根因)。附件名统一在末尾集中 // 展示一次,描述里的 [图片N] 只标记"此处有附件"不重复展示文件名。 + // ★ 15m44v: 替换串从 '\U📎 附件\1\E' 改为 '\u📎 附件\1'。 + // 根因:PCRE 的 \U…\E 是【大小写转换开关】(\U=后续全大写,\E=恢复正常), + // 夹在 emoji 之后会把中文「附件」误转成小写/乱码;且大小写开关对 CJK 无意义。 + // \u=单个字符转大写(对 CJK/emoji 恒等无副作用),\U/\E 全删,替换串干净。 $desc = (string)$description; $hasAttach = false; foreach ((is_array($images) ? $images : []) as $u) { if (is_string($u)) { $hasAttach = true; break; } } if ($hasAttach) { - $desc = preg_replace('/\[图片(\d+)\]/u', '\U📎 附件\1\E', $desc); + $desc = preg_replace('/\[图片(\d+)\]/u', '\u📎 附件\1', $desc); } + // ★ 15m44v: 建单诊断日志(写 /plugin/gh_sync.log),记录替换后描述 + 附件,定位占位符未替换。 + ghLog('create ticket=' . $ticketId . ' has_attach=' . ($hasAttach ? 'y' : 'n') + . ' desc_len=' . mb_strlen($desc) + . ' desc_head=' . mb_substr($desc, 0, 80) + . ' imgs=' . json_encode($images, JSON_UNESCAPED_UNICODE)); $bodyParts = ['玩家:`' . $player . '`', '']; if ($desc !== '') $bodyParts[] = $desc; if (is_array($images) && count($images) > 0) { @@ -414,7 +427,11 @@ function ghHandleWebhook($payload, $event) { /** 写诊断日志(1 天自动清理) */ function ghLog($msg) { try { - $dir = dirname(__DIR__); + // ★ 15m44v 修:日志写 __DIR__(= /plugin,gh_api.php 所在目录),与 ticket_debug.log 同目录。 + // 旧版用 dirname(__DIR__):gh_api.php 在 /plugin/gh_api.php,__DIR__=/plugin, + // dirname('/') 得到网站根 /,日志被写到网站根而非 /plugin → 用户/我去 /plugin 找 + // 永远 NOT FOUND(关键谜团根因)。改 __DIR__ 后落 /plugin/gh_sync.log。 + $dir = __DIR__; $file = $dir . '/gh_sync.log'; $cut = time() - 86400; if (is_dir($dir)) {