File size: 13,219 Bytes
676a3be |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
<?php
// 测试当前 GitHub Token 配置
// 根据截图中的信息进行测试
// 从截图推断的配置
$config = [
'owner' => 'CaPaCaptain', // 从截图中的仓库访问看到
'repo' => 'VvvebJs_huggingface_db_01', // 从截图中看到
'branch' => 'main',
'path' => 'pages/',
'token' => $_ENV['GITHUB_TOKEN'] ?? getenv('GITHUB_TOKEN') ?: ''
];
function testGitHubAPI($url, $token, $method = 'GET', $data = null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: token ' . $token,
'User-Agent: VvvebJs-TokenTest/1.0',
'Accept: application/vnd.github.v3+json',
'Content-Type: application/json'
]);
if ($data) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
}
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
return [
'success' => $httpCode >= 200 && $httpCode < 300,
'code' => $httpCode,
'response' => $response,
'error' => $error
];
}
$results = [];
if ($_POST['test'] ?? false) {
// 测试 1: Token 有效性
$results['token'] = testGitHubAPI('https://api.github.com/user', $config['token']);
// 测试 2: 仓库访问
$repoUrl = "https://api.github.com/repos/{$config['owner']}/{$config['repo']}";
$results['repo'] = testGitHubAPI($repoUrl, $config['token']);
// 测试 3: 分支检查
$branchUrl = "https://api.github.com/repos/{$config['owner']}/{$config['repo']}/branches/{$config['branch']}";
$results['branch'] = testGitHubAPI($branchUrl, $config['token']);
// 测试 4: 写入权限测试
$testFile = 'token-test-' . date('Y-m-d-H-i-s') . '.html';
$testContent = '<!DOCTYPE html><html><head><title>Token Test</title></head><body><h1>GitHub Token 测试</h1><p>创建时间: ' . date('Y-m-d H:i:s') . '</p></body></html>';
$createUrl = "https://api.github.com/repos/{$config['owner']}/{$config['repo']}/contents/{$config['path']}{$testFile}";
$results['write'] = testGitHubAPI($createUrl, $config['token'], 'PUT', [
'message' => 'VvvebJs Token 测试文件',
'content' => base64_encode($testContent),
'branch' => $config['branch']
]);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>当前 GitHub Token 测试</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
<div class="row">
<div class="col-md-8 mx-auto">
<h1 class="mb-4">🔑 当前 GitHub Token 配置测试</h1>
<!-- 配置信息 -->
<div class="card mb-4">
<div class="card-header bg-info text-white">
<h3 class="mb-0">📋 从截图推断的配置</h3>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<p><strong>GitHub Owner:</strong> <code><?= htmlspecialchars($config['owner']) ?></code></p>
<p><strong>Repository:</strong> <code><?= htmlspecialchars($config['repo']) ?></code></p>
<p><strong>Token 状态:</strong> <?= !empty($config['token']) ? '✅ 已配置' : '❌ 未找到' ?></p>
</div>
<div class="col-md-6">
<p><strong>Branch:</strong> <code><?= htmlspecialchars($config['branch']) ?></code></p>
<p><strong>Path:</strong> <code><?= htmlspecialchars($config['path']) ?></code></p>
<p><strong>完整 URL:</strong><br><small><code>https://github.com/<?= htmlspecialchars($config['owner']) ?>/<?= htmlspecialchars($config['repo']) ?></code></small></p>
</div>
</div>
</div>
</div>
<?php if (!empty($config['token'])): ?>
<!-- 测试按钮 -->
<div class="card mb-4">
<div class="card-body text-center">
<form method="POST">
<button type="submit" name="test" value="1" class="btn btn-primary btn-lg">
🧪 测试 Token 权限
</button>
</form>
</div>
</div>
<?php if (!empty($results)): ?>
<!-- 测试结果 -->
<div class="card mb-4">
<div class="card-header">
<h3>🔬 测试结果</h3>
</div>
<div class="card-body">
<!-- Token 测试 -->
<div class="mb-4">
<h5>1. Token 验证</h5>
<?php $test = $results['token']; ?>
<div class="alert alert-<?= $test['success'] ? 'success' : 'danger' ?>">
<strong>结果:</strong> <?= $test['success'] ? '✅ Token 有效' : '❌ Token 无效' ?> (HTTP <?= $test['code'] ?>)
<?php if ($test['success'] && $test['response']): ?>
<?php $user = json_decode($test['response'], true); ?>
<br><strong>当前用户:</strong> <?= htmlspecialchars($user['login'] ?? 'Unknown') ?>
<br><strong>用户名:</strong> <?= htmlspecialchars($user['name'] ?? 'N/A') ?>
<br><strong>用户类型:</strong> <?= htmlspecialchars($user['type'] ?? 'N/A') ?>
<?php endif; ?>
</div>
</div>
<!-- 仓库测试 -->
<div class="mb-4">
<h5>2. 仓库访问测试</h5>
<?php $test = $results['repo']; ?>
<div class="alert alert-<?= $test['success'] ? 'success' : 'danger' ?>">
<strong>结果:</strong> <?= $test['success'] ? '✅ 仓库可访问' : '❌ 仓库无法访问' ?> (HTTP <?= $test['code'] ?>)
<?php if ($test['success'] && $test['response']): ?>
<?php $repo = json_decode($test['response'], true); ?>
<br><strong>仓库全名:</strong> <?= htmlspecialchars($repo['full_name'] ?? 'Unknown') ?>
<br><strong>私有仓库:</strong> <?= ($repo['private'] ?? false) ? '是' : '否' ?>
<br><strong>默认分支:</strong> <?= htmlspecialchars($repo['default_branch'] ?? 'Unknown') ?>
<?php if (isset($repo['permissions'])): ?>
<br><strong>权限:</strong>
- Admin: <?= $repo['permissions']['admin'] ? '✅' : '❌' ?>
- Push: <?= $repo['permissions']['push'] ? '✅' : '❌' ?>
- Pull: <?= $repo['permissions']['pull'] ? '✅' : '❌' ?>
<?php endif; ?>
<?php endif; ?>
</div>
</div>
<!-- 分支测试 -->
<div class="mb-4">
<h5>3. 分支检查</h5>
<?php $test = $results['branch']; ?>
<div class="alert alert-<?= $test['success'] ? 'success' : 'warning' ?>">
<strong>结果:</strong> <?= $test['success'] ? '✅ 分支存在' : '⚠️ 分支问题' ?> (HTTP <?= $test['code'] ?>)
<?php if ($test['success'] && $test['response']): ?>
<?php $branch = json_decode($test['response'], true); ?>
<br><strong>分支名:</strong> <?= htmlspecialchars($branch['name'] ?? 'Unknown') ?>
<br><strong>最新提交:</strong> <?= htmlspecialchars(substr($branch['commit']['sha'] ?? '', 0, 8)) ?>
<?php elseif ($test['code'] == 404): ?>
<br><strong>提示:</strong> 分支 "<?= $config['branch'] ?>" 不存在。可能需要使用 "master" 分支。
<?php endif; ?>
</div>
</div>
<!-- 写入测试 -->
<div class="mb-4">
<h5>4. 文件写入权限测试</h5>
<?php $test = $results['write']; ?>
<div class="alert alert-<?= $test['success'] ? 'success' : 'danger' ?>">
<strong>结果:</strong> <?= $test['success'] ? '✅ 写入成功' : '❌ 写入失败' ?> (HTTP <?= $test['code'] ?>)
<?php if ($test['success']): ?>
<br><strong>🎉 恭喜!Token 配置完全正确,可以正常保存文件到 GitHub。</strong>
<br>您可以开始使用 VvvebJs 编辑器了!
<?php else: ?>
<br><strong>错误详情:</strong>
<pre style="font-size: 12px; max-height: 200px; overflow-y: auto;"><?= htmlspecialchars($test['response']) ?></pre>
<?php endif; ?>
</div>
</div>
</div>
</div>
<!-- 建议配置 -->
<div class="card">
<div class="card-header">
<h3>⚙️ 推荐的环境变量配置</h3>
</div>
<div class="card-body">
<p>在您的 Hugging Face Space Settings 中设置以下环境变量:</p>
<div class="bg-light p-3 rounded">
<code>GITHUB_TOKEN=your_token_here</code><br>
<code>GITHUB_OWNER=<?= htmlspecialchars($config['owner']) ?></code><br>
<code>GITHUB_REPO=<?= htmlspecialchars($config['repo']) ?></code><br>
<code>GITHUB_BRANCH=<?= $results['branch']['success'] ? $config['branch'] : 'master' ?></code><br>
<code>GITHUB_PATH=<?= htmlspecialchars($config['path']) ?></code><br>
<code>STORAGE_TYPE=github</code><br>
</div>
<?php if (!$results['branch']['success'] && $results['branch']['code'] == 404): ?>
<div class="alert alert-warning mt-3">
<strong>⚠️ 注意:</strong> 检测到分支 "main" 不存在,建议使用 "master" 分支。
</div>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<?php else: ?>
<!-- Token 未配置 -->
<div class="alert alert-danger">
<h5>❌ GitHub Token 未配置</h5>
<p>请在环境变量中设置 <code>GITHUB_TOKEN</code>。</p>
<p>Token 应该是您在 GitHub 截图中创建的那个。</p>
</div>
<?php endif; ?>
<!-- 导航 -->
<div class="text-center mt-4">
<a href="config.php" class="btn btn-secondary me-2">🔙 返回配置</a>
<a href="github-token-helper.html" class="btn btn-info me-2">📖 Token 指南</a>
<a href="editor.html" class="btn btn-primary">🎨 打开编辑器</a>
</div>
</div>
</div>
</div>
</body>
</html> |