VvvebJs / github-test.php
CatPtain's picture
Upload 4 files
7a57f48 verified
<?php
// GitHub connectivity and permissions test
header('Content-Type: text/html; charset=utf-8');
echo '<!DOCTYPE html><html><head><meta charset="utf-8"><title>GitHub 连接测试</title>';
echo '<style>body{font-family:monospace;max-width:800px;margin:20px auto;padding:20px;}';
echo '.status{padding:10px;margin:5px 0;border-radius:5px;}';
echo '.ok{background:#d4edda;color:#155724;} .error{background:#f8d7da;color:#721c24;} .warning{background:#fff3cd;color:#856404;}</style>';
echo '</head><body>';
require_once __DIR__ . '/storage.php';
$github = StorageConfig::getGitHubConfig();
if (empty($github['token']) || empty($github['owner']) || empty($github['repo'])) {
echo '<div class="status error">❌ GitHub 配置不完整</div>';
exit;
}
echo '<h2>GitHub 连接诊断</h2>';
// 通用 HTTP 头部设置
$headers = [
'Authorization: token ' . $github['token'],
'User-Agent: VvvebJs-Editor/1.0 (GitHub-API-Client)',
'Accept: application/vnd.github.v3+json',
'Content-Type: application/json'
];
$context = stream_context_create([
'http' => [
'method' => 'GET',
'header' => implode("\r\n", $headers),
'timeout' => 30
]
]);
echo '<h3>1. GitHub Token 验证</h3>';
$response = @file_get_contents('https://api.github.com/user', false, $context);
if ($response === false) {
echo '<div class="status error">❌ 无法连接到 GitHub API</div>';
$error = error_get_last();
echo '<div class="status error">错误: ' . htmlspecialchars($error['message']) . '</div>';
} else {
$user = json_decode($response, true);
echo '<div class="status ok">✅ 有效 (HTTP 200)</div>';
echo '<div class="status ok">用户信息: ' . htmlspecialchars($user['name'] ?? $user['login']) . '</div>';
echo '<div class="status ok">用户名: ' . htmlspecialchars($user['login']) . '</div>';
}
echo '<h3>2. 仓库访问检查</h3>';
$repo_url = "https://api.github.com/repos/{$github['owner']}/{$github['repo']}";
$response = @file_get_contents($repo_url, false, $context);
if ($response === false) {
echo '<div class="status error">❌ 无法访问仓库</div>';
$error = error_get_last();
echo '<div class="status error">错误: ' . htmlspecialchars($error['message']) . '</div>';
} else {
$repo = json_decode($response, true);
echo '<div class="status ok">✅ 可访问 (HTTP 200)</div>';
echo '<div class="status ok">仓库全名: ' . htmlspecialchars($repo['full_name']) . '</div>';
echo '<div class="status ok">私有仓库: ' . ($repo['private'] ? '是' : '否') . '</div>';
echo '<div class="status ok">默认分支: ' . htmlspecialchars($repo['default_branch']) . '</div>';
// 自动使用仓库的默认分支
$actual_branch = $repo['default_branch'];
echo '<div class="status warning">⚠️ 将使用默认分支: ' . htmlspecialchars($actual_branch) . '</div>';
}
echo '<h3>3. 分支检查</h3>';
// 使用实际的默认分支进行检查
$branch_to_check = isset($actual_branch) ? $actual_branch : $github['branch'];
$branch_url = "https://api.github.com/repos/{$github['owner']}/{$github['repo']}/branches/{$branch_to_check}";
$response = @file_get_contents($branch_url, false, $context);
if ($response === false) {
echo '<div class="status error">❌ 分支不存在 (HTTP 404)</div>';
echo '<div class="status warning">⚠️ 尝试创建分支...</div>';
// 尝试获取主分支的最新 commit
$commits_url = "https://api.github.com/repos/{$github['owner']}/{$github['repo']}/commits?per_page=1";
$commits_response = @file_get_contents($commits_url, false, $context);
if ($commits_response) {
$commits = json_decode($commits_response, true);
if (!empty($commits)) {
$latest_sha = $commits[0]['sha'];
// 创建分支
$create_context = stream_context_create([
'http' => [
'method' => 'POST',
'header' => implode("\r\n", $headers),
'content' => json_encode([
'ref' => "refs/heads/{$branch_to_check}",
'sha' => $latest_sha
]),
'timeout' => 30
]
]);
$create_response = @file_get_contents("https://api.github.com/repos/{$github['owner']}/{$github['repo']}/git/refs", false, $create_context);
if ($create_response) {
echo '<div class="status ok">✅ 分支创建成功</div>';
} else {
echo '<div class="status error">❌ 分支创建失败</div>';
}
}
}
} else {
echo '<div class="status ok">✅ 分支存在</div>';
}
echo '<h3>4. 文件创建权限测试</h3>';
$test_path = trim($github['path'], '/') . '/test-' . time() . '.txt';
$file_content = base64_encode('VvvebJs test file - ' . date('Y-m-d H:i:s'));
// 改进的文件创建请求
$create_file_context = stream_context_create([
'http' => [
'method' => 'PUT',
'header' => implode("\r\n", $headers),
'content' => json_encode([
'message' => 'VvvebJs: Test file creation',
'content' => $file_content,
'branch' => $branch_to_check
]),
'timeout' => 30
]
]);
$file_url = "https://api.github.com/repos/{$github['owner']}/{$github['repo']}/contents/{$test_path}";
$response = @file_get_contents($file_url, false, $create_file_context);
if ($response === false) {
echo '<div class="status error">❌ 无法创建文件</div>';
$error = error_get_last();
echo '<div class="status error">错误详情: ' . htmlspecialchars($error['message']) . '</div>';
// 检查是否是路径问题
if (strpos($github['path'], '/') !== false) {
echo '<div class="status warning">💡 尝试在根目录创建测试文件...</div>';
$root_test_path = 'vvvebjs-test-' . time() . '.txt';
$root_file_url = "https://api.github.com/repos/{$github['owner']}/{$github['repo']}/contents/{$root_test_path}";
$root_response = @file_get_contents($root_file_url, false, $create_file_context);
if ($root_response) {
echo '<div class="status ok">✅ 根目录文件创建成功</div>';
echo '<div class="status warning">⚠️ 建议将 GITHUB_PATH 设置为空或根目录</div>';
// 清理测试文件
$cleanup_context = stream_context_create([
'http' => [
'method' => 'DELETE',
'header' => implode("\r\n", $headers),
'content' => json_encode([
'message' => 'VvvebJs: Cleanup test file',
'sha' => json_decode($root_response, true)['content']['sha'],
'branch' => $branch_to_check
]),
'timeout' => 30
]
]);
@file_get_contents($root_file_url, false, $cleanup_context);
}
}
} else {
echo '<div class="status ok">✅ 文件创建成功</div>';
$file_info = json_decode($response, true);
echo '<div class="status ok">文件路径: ' . htmlspecialchars($file_info['content']['path']) . '</div>';
// 清理测试文件
$cleanup_context = stream_context_create([
'http' => [
'method' => 'DELETE',
'header' => implode("\r\n", $headers),
'content' => json_encode([
'message' => 'VvvebJs: Cleanup test file',
'sha' => $file_info['content']['sha'],
'branch' => $branch_to_check
]),
'timeout' => 30
]
]);
$cleanup_response = @file_get_contents($file_url, false, $cleanup_context);
if ($cleanup_response) {
echo '<div class="status ok">✅ 测试文件已清理</div>';
}
}
echo '<h3>5. 推荐配置</h3>';
if (isset($actual_branch) && $actual_branch !== $github['branch']) {
echo '<div class="status warning">⚠️ 建议更新分支配置:</div>';
echo '<div class="status warning">GITHUB_BRANCH=' . htmlspecialchars($actual_branch) . '</div>';
}
echo '<div class="status ok">✅ 推荐的环境变量配置:</div>';
echo '<pre style="background:#f8f9fa;padding:15px;border-radius:5px;margin:10px 0;">';
echo 'GITHUB_TOKEN=' . substr($github['token'], 0, 8) . '...<br>';
echo 'GITHUB_OWNER=' . htmlspecialchars($github['owner']) . '<br>';
echo 'GITHUB_REPO=' . htmlspecialchars($github['repo']) . '<br>';
echo 'GITHUB_BRANCH=' . htmlspecialchars(isset($actual_branch) ? $actual_branch : $github['branch']) . '<br>';
echo 'GITHUB_PATH= # 建议留空或设置为根目录<br>';
echo '</pre>';
echo '<div style="margin-top:30px;">';
echo '<a href="config.php" style="background:#007bff;color:white;padding:10px 20px;text-decoration:none;border-radius:5px;">返回配置页面</a>';
echo '</div>';
echo '</body></html>';
?>