126 lines
5.4 KiB
PHP
126 lines
5.4 KiB
PHP
<?php
|
||
/**
|
||
* 15m44l 工单第二批:GitHub issue 双向同步 —— OAuth 授权端点(玩家逐次授权)
|
||
*
|
||
* 两个用途:
|
||
* 1) 发起授权(GET, 无 code):把浏览器 302 到 GitHub OAuth App 的 authorize 页,
|
||
* 带 state=玩家名(CSRF 防护)。玩家在前端点「授权 GitHub 工单同步」触发。
|
||
* 2) 授权回调(GET, 带 code+state):GitHub 回跳本端点,用 code+client_secret 换
|
||
* access_token,校验 state(玩家名),把 token 写进 web_tickets.gh_access_token
|
||
* (或按工单 gh_ticket_id 关联),供后续调 issue API。
|
||
*
|
||
* 凭据全在 img.json(repo.Client ID / Client secrets + webhook_secret),本文件不含敏感值。
|
||
* 配置未启用(gh_enabled=false)时,发起授权直接报错,避免误跳。
|
||
*
|
||
* 端点 URL(给用户配 GitHub OAuth App 的 Redirect URI):
|
||
* https://caoyuan.ypshidifu.cn/plugin/api/gh_oauth.php
|
||
*/
|
||
|
||
while (ob_get_level() > 0) { @ob_end_clean(); }
|
||
ob_start();
|
||
header('X-Content-Type-Options: nosniff');
|
||
|
||
error_reporting(E_ALL);
|
||
ini_set('display_errors', '0');
|
||
if (function_exists('opcache_invalidate')) { @opcache_invalidate(__FILE__); }
|
||
|
||
require_once __DIR__ . '/../core.php';
|
||
require_once __DIR__ . '/../lsky.php';
|
||
|
||
$cfg = loadImgJson();
|
||
if (!$cfg['gh_enabled']) {
|
||
// 发起授权:报"未启用";回调:GitHub 不该回跳未启用站点,同样报未启用
|
||
header('Content-Type: text/html; charset=utf-8');
|
||
echo '<meta charset="utf-8"><title>GitHub 工单同步未启用</title>'
|
||
. '<p>GitHub issue 工单同步未启用(img.json 的 模式.type 需填 GitHub 且 repo 段填全)。</p>'
|
||
. '<p><a href="javascript:history.back()">返回</a></p>';
|
||
exit;
|
||
}
|
||
|
||
$repo = $cfg['gh_repo']; // ypsdf1/caoyuanwiki
|
||
$clientId = $cfg['gh_client_id'];
|
||
$clientSec = $cfg['gh_client_secret'];
|
||
$baseSite = rtrim('https://caoyuan.ypshidifu.cn/plugin', '/');
|
||
$oauthUrl = $baseSite . '/api/gh_oauth.php';
|
||
|
||
// GitHub OAuth authorize / token 端点
|
||
$ghAuthorize = 'https://github.com/login/oauth/authorize';
|
||
$ghToken = 'https://github.com/login/oauth/access_token';
|
||
|
||
$code = isset($_GET['code']) ? trim((string)$_GET['code']) : '';
|
||
|
||
if ($code !== '') {
|
||
// ===== 回调:换 token =====
|
||
$state = isset($_GET['state']) ? trim((string)$_GET['state']) : '';
|
||
// state = 玩家名(发起授权时由前端传 player 拼进 state)
|
||
$player = $state;
|
||
$scope = 'repo'; // issue 读+写需要 repo 权限
|
||
|
||
// 用 code 换 access_token(client_secret 走 POST body)
|
||
$ch = curl_init();
|
||
curl_setopt($ch, CURLOPT_URL, $ghToken);
|
||
curl_setopt($ch, CURLOPT_POST, true);
|
||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
|
||
'client_id' => $clientId,
|
||
'client_secret' => $clientSec,
|
||
'code' => $code,
|
||
'scope' => $scope,
|
||
]));
|
||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
|
||
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
|
||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
|
||
$body = curl_exec($ch);
|
||
$errno = curl_errno($ch);
|
||
curl_close($ch);
|
||
if ($errno) {
|
||
header('Content-Type: text/html; charset=utf-8');
|
||
echo '<meta charset="utf-8"><p>获取 GitHub token 失败(cURL ' . htmlspecialchars($errno) . ')。</p>';
|
||
exit;
|
||
}
|
||
// GitHub 返回 application/x-www-form-urlencoded: "access_token=xxx&scope=repo&token_type=bearer"
|
||
parse_str((string)$body, $tok);
|
||
$accessToken = isset($tok['access_token']) ? (string)$tok['access_token'] : '';
|
||
$ghUser = isset($tok['user_login']) ? (string)$tok['user_login'] : '';
|
||
if ($accessToken === '') {
|
||
$err = isset($tok['error']) ? (string)$tok['error'] : 'unknown';
|
||
header('Content-Type: text/html; charset=utf-8');
|
||
echo '<meta charset="utf-8"><p>GitHub 授权失败: ' . htmlspecialchars($err) . '。</p>'
|
||
. '<p><a href="javascript:history.back()">返回</a></p>';
|
||
exit;
|
||
}
|
||
|
||
// 写库:把 token 存进 web_tickets 的 gh_access_token 列(按玩家名)
|
||
// 没有 gh_access_token 列则先 ALTER 兜底
|
||
$db = getDB();
|
||
try {
|
||
$db->exec("ALTER TABLE web_tickets ADD COLUMN gh_access_token TEXT DEFAULT ''");
|
||
} catch (\Throwable $e) { /* 列已存在 */ }
|
||
try {
|
||
$db->exec("ALTER TABLE web_tickets ADD COLUMN gh_issue_number TEXT DEFAULT ''");
|
||
} catch (\Throwable $e) { /* 列已存在 */ }
|
||
|
||
$upd = $db->prepare("UPDATE web_tickets SET gh_access_token = :t WHERE requester = :p AND (gh_access_token IS NULL OR gh_access_token = '')");
|
||
$upd->bindValue(':t', $accessToken, SQLITE3_TEXT);
|
||
$upd->bindValue(':p', $player, SQLITE3_TEXT);
|
||
$upd->execute();
|
||
|
||
// 回跳前端「工单」页,带成功标记,前端拿到后可开始同步
|
||
header('Location: ' . $baseSite . '/player.php?gh_ok=1&gh_user=' . urlencode($ghUser));
|
||
exit;
|
||
}
|
||
|
||
// ===== 发起授权 =====
|
||
// 前端传 ?player=玩家名(拼进 state 做 CSRF 校验);也可从 token 解析。
|
||
$playerParam = isset($_GET['player']) ? trim((string)$_GET['player']) : '';
|
||
$state = $playerParam !== '' ? $playerParam : 'anon';
|
||
$authUrl = $ghAuthorize . '?' . http_build_query([
|
||
'client_id' => $clientId,
|
||
'redirect_uri' => $oauthUrl,
|
||
'scope' => 'repo',
|
||
'state' => $state,
|
||
]);
|
||
header('Location: ' . $authUrl);
|
||
exit;
|