Files
Sdf1_login/api/gh_cfg.php
T
ypsdf1 a8ac78a87f 15m44w 续5c 根治横条消失: 删全仓 curl_close + JSON 端点抑制 deprecation 输出
真根因(推翻 token 过期说): PHP8.5 下 curl_close() 已废弃, 每次调用都输出
<br><b>Deprecated</b> 污染 JSON 响应体 → 前端 res.json() 解析失败进 catch
(端点 catch 注释'不显示') → GitHub 授权横条消失。实测 gh_cfg 后端验活 status=200
ok=true 但响应体被警告污染, 非纯 JSON。

修: ① 全仓 8 处独立 curl_close 语句删除(PHP8+ 是 no-op,  出作用域自动关闭,
   删了不改行为还消除 deprecation 源头); ② gh_cfg/gh_diag/gh_git_probe 顶部加
   error_reporting(0)+display_errors=0 兜底, 彻底防任何警告污染响应体。
实测部署后 gh_cfg.php 返回纯 JSON, gh_authorized=true。
2026-11-25 20:40:38 +08:00

88 lines
5.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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;
}
}
}
success([
'gh_enabled' => !empty($cfg['gh_enabled']),
'gh_repo' => isset($cfg['gh_repo']) ? $cfg['gh_repo'] : '',
'gh_authorized' => $ghAuthorized,
], 'ok');