0) { @ob_end_clean(); } ob_start(); // ★ 15m44w 续5c 根治横条消失:PHP8.5 下 deprecation/notice 输出
污染 JSON 响应体, // 前端 res.json() 解析失败进 catch → 授权横条不显示。抑制一切错误输出保响应体纯净。 error_reporting(0); ini_set('display_errors', '0'); header('Content-Type: application/json; charset=utf-8'); header('X-Content-Type-Options: nosniff'); header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0'); require_once __DIR__ . '/../core.php'; require_once __DIR__ . '/../lsky.php'; require_once __DIR__ . '/../gh_api.php'; // 15m44v: ghApiRequest(验活 token) $cfg = loadImgJson(); $ghAuthorized = false; // ★ 15m44z 续(19:59 修):gh_token_fresh 区分「刚授权(免验活必有效)」vs「老 token(验活判)」。 // 刚走完 gh_oauth 回调的玩家 updated_at 很近(<5min),token 必有效 —— 不再发 /user 验活 // (CF 边缘偶发卡住/401 会把有效 token 误判成失效 → 前端显示「token 可能已失效」误导)。 $ghTokenFresh = false; $ghTokenUpdatedAt = 0; // ★ 15m44v: 带 token 时查该玩家是否已有【有效】GitHub token(验活,非只看存没存)。 // 前端 loadGhCfg 改成 fetch('gh_cfg.php?token=' + TOKEN) 后,据此显示「已授权」态, // 根治"重新进表单 URL 无 gh_ok 就误判为未授权"的老问题。 $probeTk = isset($_GET['token']) ? trim((string)$_GET['token']) : ''; if ($cfg['gh_enabled'] && $probeTk !== '') { // ★ 15m44w 续5 根治横条消失:身份反解不依赖 weblogin token 是否未过期。 // 旧逻辑走 validateTokenSilent → weblogin token 已过期(600s 短命)则返回 false → // probePlayer 空 → gh_authorized 恒 false → 授权横条消失。 // 但 gh_cfg 验活只需确认"这个 web_token 属于哪个玩家",玩家身份是确定的, // 不需要登录 token 仍有效。故直接查 weblogin_tokens(Java 侧)拿 player_name, // 不做过期判定;同时保留 validateTokenSilent(普通 java token)作为主路径。 $probePlayer = ''; $pi = validateTokenSilent($probeTk); // 普通 java tokens(未过期才命中) if ($pi && !empty($pi['player'])) { $probePlayer = $pi['player']; } else { // 普通 token 没命中(或已过期)→ 查 weblogin_tokens 拿玩家身份(不过期也认) try { $dbProbe = getDB(); $stProbe = $dbProbe->prepare("SELECT player_name FROM weblogin_tokens WHERE web_token = :wtk"); $stProbe->bindValue(':wtk', $probeTk, SQLITE3_TEXT); $pr = $stProbe->execute()->fetchArray(SQLITE3_ASSOC); if ($pr && !empty($pr['player_name'])) $probePlayer = $pr['player_name']; } catch (\Throwable $e) { ghLog('cfg weblogin identity lookup fail: ' . $e->getMessage()); } } if ($probePlayer !== '') { try { $db = getDB(); // ★ 15m44w 续4:建表用 VARCHAR(500) 对齐线上 caoyuanphp.gh_auth_tokens 实际 schema // (旧版 TEXT PRIMARY KEY 在 MySQL 下 TEXT 不能做 PK 报 1101; 表已存在时 IF NOT EXISTS // 跳过不抛, 但为双模式兼容(老 SQLite 库也建 VARCHAR 无害)改 VARCHAR(500))。 $db->exec("CREATE TABLE IF NOT EXISTS gh_auth_tokens ( player_name VARCHAR(500) PRIMARY KEY, gh_login VARCHAR(500) DEFAULT '', access_token TEXT NOT NULL, updated_at INTEGER NOT NULL)"); $st = $db->prepare("SELECT access_token, gh_login, updated_at FROM gh_auth_tokens WHERE player_name = :p AND access_token != '' ORDER BY updated_at DESC LIMIT 1"); $st->bindValue(':p', $probePlayer, SQLITE3_TEXT); $trow = $st->execute()->fetchArray(SQLITE3_ASSOC); if ($trow && !empty($trow['access_token'])) { // ★ 15m44z 续(19:59 根治):OAuth 回调 fresh flag(10min,按玩家名)→ 免验活必有效。 // 彻底避开 CF 边缘 /user 验活偶发 401 把刚授权的有效 token 误判成「可能已失效」。 $freshDir = dirname(__DIR__) . '/../gh_oauth_fresh'; $freshFlag = $freshDir . '/' . md5($probePlayer) . '.json'; if (is_readable($freshFlag)) { if (is_array($ff) && (int)$ff['exp'] > time() && (string)($ff['player'] ?? '') === $probePlayer) { $ghAuthorized = true; $ghTokenFresh = true; ghLog('cfg verify player=' . $probePlayer . ' fresh-flag=y (OAuth 回调刚授权, 免验活)'); } else { @unlink($freshFlag); // 过期/玩家不匹配 → 清掉 } } if (!$ghTokenFresh) { // ★ 15m44z 续(19:59 修):刚授权(updated_at 距今 < 5min)→ token 必有效,免验活。 // 根治「刚走完 gh_oauth 回调回来,gh_cfg 却显示 token 可能已失效」的误导。 // 老 token(>5min)才走 /user 验活判真失效。 $ghTokenUpdatedAt = (int)($trow['updated_at'] ?? 0); $ghTokenFresh = ($ghTokenUpdatedAt > 0 && (time() - $ghTokenUpdatedAt) < 300); if ($ghTokenFresh) { $ghAuthorized = true; // 刚授权 = 有效,不再发 /user(防 CF 卡住误判 401) ghLog('cfg verify player=' . $probePlayer . ' token=' . substr((string)$trow['access_token'], 0, 8) . ' fresh=y updated_at=' . $ghTokenUpdatedAt . ' (免验活, 刚授权必有效)'); } else { // 验活:/user 加唯一 query 破 CF 缓存(同 gh_oauth.php 15m44v 修法) $v = ghApiRequest('GET', '/user?x=nocache_' . (int)microtime(true) . mt_rand(100000, 999999), null, $trow['access_token']); $ghAuthorized = !empty($v['ok']); // ★ 诊断日志:验活结果写 gh_sync.log,定位横条消失断点(status/ok/token 前缀) ghLog('cfg verify player=' . $probePlayer . ' token=' . substr((string)$trow['access_token'], 0, 8) . ' status=' . (int)($v['status'] ?? -1) . ' ok=' . ($v['ok'] ? 'y' : 'n')); } } // end if (!ghTokenFresh) } else { ghLog('cfg verify player=' . $probePlayer . ' no token row in gh_auth_tokens'); } } catch (\Throwable $e) { ghLog('cfg verify exception: ' . $e->getMessage()); $ghAuthorized = false; } } } // ★ 15m44z 第三部分+开关:匿名工单开关可用性。 // gh_anon_available = appbot 启用 + 匿名模式开(gh_anon_mode, img.json「匿名模式」字段) // + 真能取到 installation token。三者齐备前端才显示「匿名工单」开关。 // 匿名模式关 → 即使 appbot 配了也 gh_anon_available=false,gh 模式一律实名授权。 $ghAppbot = !empty($cfg['gh_app_bot']); $ghAnonMode = !isset($cfg['gh_anon_mode']) || $cfg['gh_anon_mode'] === true; // 缺省开 $ghAnonAvailable = false; if ($ghAppbot && $ghAnonMode) { try { if (function_exists('ghAppBotReady')) $ghAnonAvailable = ghAppBotReady(); } catch (\Throwable $e) { $ghAnonAvailable = false; } } // ★ 15m44z 第三部分+token保留:切匿名模式 ≠ 放弃实名授权。 // 玩家切匿名后,bot 通道可用(ghs_ token 不依赖玩家 OAuth),但玩家之前授权的 // GitHub OAuth token 仍然保留在 gh_auth_tokens 表里。即便 /user 验活 401 失效, // 只要 token 行还在,gh_authorized 也应为 true(「token 已保留,可重新授权」), // 避免前端误判"未授权"让玩家以为切匿名后授权被清掉了。 // 判法:$ghAnonAvailable=true 且 $ghAuthorized 验活 401 失败 + gh_auth_tokens 有 token 行 // → 改 gh_authorized=true(但 gh_token_fresh 保持 false,前端显示「已保留」而非「已授权」)。 // ★ 15m44z 续(19:59):兜底只对「老 token 验活 401」生效;刚授权(fresh)已在上面直接判有效, // 不会进这个分支,所以刚授权不会误显示「token 可能已失效」。 if ($ghAnonAvailable && $probePlayer !== '' && !$ghAuthorized && !$ghTokenFresh) { try { $dbTok = getDB(); $stTok = $dbTok->prepare("SELECT access_token, updated_at FROM gh_auth_tokens WHERE player_name = :p AND access_token != '' ORDER BY updated_at DESC LIMIT 1"); $stTok->bindValue(':p', $probePlayer, SQLITE3_TEXT); $tokRow = $stTok->execute()->fetchArray(SQLITE3_ASSOC); if ($tokRow && !empty($tokRow['access_token'])) { // token 行还在(哪怕 401 失效)→ 保留授权记录,前端可提示重新授权 $ghAuthorized = true; ghLog('cfg: anon available, token retained (401 but row exists) player=' . $probePlayer . ' token=' . substr((string)$tokRow['access_token'], 0, 8)); } } catch (\Throwable $e) { ghLog('cfg token retention check fail: ' . $e->getMessage()); } } success([ 'gh_enabled' => !empty($cfg['gh_enabled']), 'gh_repo' => isset($cfg['gh_repo']) ? $cfg['gh_repo'] : '', 'gh_authorized' => $ghAuthorized, 'gh_token_fresh' => $ghTokenFresh, // ★ 15m44z 续(19:59):刚授权免验活=必有效 'gh_appbot' => $ghAppbot, 'gh_anon_available' => $ghAnonAvailable, ], 'ok');