Files
Sdf1_login/api/gh_cfg.php
T
ypsdf1 1244bc1b48 15m44v 续: 修附件替换串 \u 残留+前端授权态误判
- gh_api.php ghSyncCreate 替换串终版:'\u📎 附件\1' 的 \u 在 PHP 单引号里是字面 反斜杠+u,
  传给 PCRE 替换串 \u=下个字符转大写, 对 emoji 无效→吐出字面 u (issue body 出现 'u 📎 附件1')。
  改 "\U0001F4CE 附件\1" (emoji 直接字面值, 只留 \1 反向引用, 无任何大小写/转义指令)。
- api/gh_cfg.php 加 gh_authorized 字段: 带 token 反解玩家名查 gh_auth_tokens, /user 验活(破CF缓存),
  有效→true。根治前端'重新进表单 URL 无 gh_ok 就误判未授权'。
- player.php loadGhCfg 改 fetch gh_cfg.php?token= 按 gh_authorized 显示已授权态(不再只靠 URL 参数)。
  commit 083f760/9b081a6
2026-11-25 16:13:37 +08:00

53 lines
2.6 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();
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 !== '') {
$pi = validateTokenSilent($probeTk);
$probePlayer = ($pi && !empty($pi['player'])) ? $pi['player'] : '';
if ($probePlayer !== '') {
try {
$db = getDB();
$db->exec("CREATE TABLE IF NOT EXISTS gh_auth_tokens (
player_name TEXT PRIMARY KEY, gh_login TEXT DEFAULT '',
access_token TEXT NOT NULL, updated_at INTEGER NOT NULL)");
$st = $db->prepare("SELECT access_token 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']);
}
} catch (\Throwable $e) { $ghAuthorized = false; }
}
}
success([
'gh_enabled' => !empty($cfg['gh_enabled']),
'gh_repo' => isset($cfg['gh_repo']) ? $cfg['gh_repo'] : '',
'gh_authorized' => $ghAuthorized,
], 'ok');