【匿名工单主体】 - App 鉴权: gh_api.php 新增 ghAppJwt(PEM签RS256 JWT)+ghAppInstallationToken(换1h installation token, 缓存到 /plugin/gh_app_token_cache.json 55min)+ghAppBotReady。 - 匿名代发: 用 installation token(g hs_前缀)建 issue 归属 bot(caoyuan-server[bot]), 不暴露提单人 GitHub 账号; 提单人信息写 issue 正文「玩家:xxx」+ PHP 后台保留。 - loadImgJson 解析 repo.imgrepo."github app"(appid/安装id/私钥路径/匿名模式)。 【匿名/实名开关(用户定调: 共存, 做成开关)】 - json「匿名模式」字段(github app 段): true=gh模式支持匿名(前端出开关); false=gh模式 一律实名授权(即使 appbot 配了也不给匿名选项)。 - 前端: gh_anon_available=true(匿名模式开+appbot可用)才显示「匿名工单」开关; 开关开→提交带 anon=1(bot代发), 关→走玩家OAuth建单; 模式留空(PHP内置工单)不给匿名。 - 建单: anon=1 且匿名模式开→appbot token; 否则玩家 OAuth token。 【归属派发(用户定调: 按工单所有人)】 - ghSyncCreate 建 issue 后回写 gh_issue_owner(bot/realname, 按 token 前缀 ghs_/gho_)。 - 撤单/完结/回复按 ghTicketTokenByTicket 归属: bot建的单→机器人代发撤/完结/评论; 实名单→玩家 OAuth token(保持现有逻辑)。 实测: App JWT→installation token→建issue#24(归属caoyuan-server[bot])全链路通; 线上 gh_cfg 返回 gh_anon_available=true。6件PHP+img.json模板+真值img.json+私钥全MATCH。
103 lines
5.8 KiB
PHP
103 lines
5.8 KiB
PHP
<?php
|
||
/**
|
||
* 15m44m: 工单前端判断「GitHub issue 同步是否启用」的懒重载端点。
|
||
* 返回 {gh_enabled:bool, gh_repo:string, gh_authorized:bool}。前端进工单表单时 fetch:
|
||
* - gh_enabled=true 才显示「授权 GitHub 工单同步」按钮
|
||
* - gh_repo 用于按钮文案(caoyuanwiki)
|
||
* - 15m44v: gh_authorized=true 时前端显示「已授权」态(不再误判为未授权)
|
||
* 带 ?token= 时反解玩家名查 gh_auth_tokens 是否已有有效 token。
|
||
*/
|
||
while (ob_get_level() > 0) { @ob_end_clean(); }
|
||
ob_start();
|
||
// ★ 15m44w 续5c 根治横条消失:PHP8.5 下 deprecation/notice 输出 <br> 污染 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;
|
||
|
||
// ★ 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 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'])) {
|
||
// 验活:/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'));
|
||
} 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; }
|
||
}
|
||
|
||
success([
|
||
'gh_enabled' => !empty($cfg['gh_enabled']),
|
||
'gh_repo' => isset($cfg['gh_repo']) ? $cfg['gh_repo'] : '',
|
||
'gh_authorized' => $ghAuthorized,
|
||
'gh_appbot' => $ghAppbot,
|
||
'gh_anon_available' => $ghAnonAvailable,
|
||
], 'ok');
|