VvvebJs / github-directory-setup.php
CatPtain's picture
Upload 5 files
92a44b9 verified
<?php
require_once __DIR__ . '/storage.php';
// 简单认证
$testAuth = false;
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
$users = StorageConfig::getUsers();
$testAuth = isset($users[$_SERVER['PHP_AUTH_USER']]) &&
$users[$_SERVER['PHP_AUTH_USER']] === $_SERVER['PHP_AUTH_PW'];
}
if (!$testAuth) {
header('WWW-Authenticate: Basic realm="VvvebJs GitHub Directory Setup"');
header('HTTP/1.0 401 Unauthorized');
die('Authentication required');
}
$github = StorageConfig::getGitHubConfig();
$currentUser = StorageConfig::getCurrentUser();
$userPath = StorageConfig::getUserPath();
// 目录设置结果
$setupResults = [];
if ($_POST['setup_directories'] ?? false) {
try {
error_log("GitHub Directory Setup: Starting directory creation process");
$githubStorage = new GitHubStorage($github);
// 1. 创建基础 pages 目录
$pagesReadme = "# VvvebJs Pages Directory
This directory contains all HTML pages created with VvvebJs editor.
## Structure
- `users/` - Individual user directories
- Each user has their own isolated directory
Generated: " . date('Y-m-d H:i:s') . " UTC
";
$result1 = $githubStorage->saveFile('README.md', $pagesReadme);
$setupResults['pages_dir'] = [
'success' => $result1,
'path' => 'pages/README.md',
'message' => $result1 ? 'Pages directory created successfully' : 'Failed to create pages directory'
];
// 2. 创建 users 目录
if ($result1) {
$usersReadme = "# VvvebJs Users Directory
This directory contains individual user folders for VvvebJs.
## User Isolation
Each user gets their own directory to store HTML pages:
- User directories are named after the username
- Files are automatically organized by user
- No user can access another user's files
Generated: " . date('Y-m-d H:i:s') . " UTC
";
$result2 = $githubStorage->saveFile('users/README.md', $usersReadme);
$setupResults['users_dir'] = [
'success' => $result2,
'path' => 'pages/users/README.md',
'message' => $result2 ? 'Users directory created successfully' : 'Failed to create users directory'
];
// 3. 创建当前用户目录
if ($result2) {
$userReadme = "# VvvebJs User Directory: $currentUser
This is your personal directory for HTML pages created with VvvebJs.
## Your Information
- Username: $currentUser
- Directory: pages/$userPath
- Created: " . date('Y-m-d H:i:s') . " UTC
## Usage
All your HTML files will be saved in this directory automatically.
You can only access and modify files in your own directory.
Happy building! 🚀
";
$result3 = $githubStorage->saveFile($userPath . 'README.md', $userReadme);
$setupResults['user_dir'] = [
'success' => $result3,
'path' => 'pages/' . $userPath . 'README.md',
'message' => $result3 ? "Personal directory created for user: $currentUser" : 'Failed to create personal directory'
];
// 4. 创建一个示例 HTML 文件
if ($result3) {
$sampleHtml = '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to VvvebJs - ' . htmlspecialchars($currentUser) . '</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 50px auto;
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
line-height: 1.6;
}
.container {
background: rgba(255, 255, 255, 0.1);
padding: 30px;
border-radius: 15px;
backdrop-filter: blur(10px);
}
h1 {
text-align: center;
margin-bottom: 30px;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}
.welcome-box {
background: rgba(255, 255, 255, 0.2);
padding: 20px;
border-radius: 10px;
margin: 20px 0;
}
.success {
color: #4CAF50;
font-weight: bold;
font-size: 1.2em;
}
.info {
background: rgba(255, 255, 255, 0.1);
padding: 15px;
border-radius: 8px;
margin: 15px 0;
}
.btn {
display: inline-block;
background: #4CAF50;
color: white;
padding: 12px 24px;
text-decoration: none;
border-radius: 6px;
margin: 10px 0;
transition: background 0.3s;
}
.btn:hover {
background: #45a049;
}
</style>
</head>
<body>
<div class="container">
<h1>🎉 Welcome to VvvebJs!</h1>
<div class="welcome-box">
<h2>Hello, ' . htmlspecialchars($currentUser) . '!</h2>
<p class="success">✅ Your VvvebJs environment is now ready!</p>
</div>
<div class="info">
<h3>📁 Your Directory Setup</h3>
<p><strong>Personal Directory:</strong> <code>pages/' . htmlspecialchars($userPath) . '</code></p>
<p><strong>Repository:</strong> <code>' . htmlspecialchars($github['owner']) . '/' . htmlspecialchars($github['repo']) . '</code></p>
<p><strong>Branch:</strong> <code>' . htmlspecialchars($github['branch']) . '</code></p>
</div>
<div class="info">
<h3>🚀 What\'s Next?</h3>
<ul>
<li>Use the VvvebJs visual editor to create amazing web pages</li>
<li>All your files will be automatically saved to GitHub</li>
<li>Your work is isolated in your personal directory</li>
<li>Collaborate safely with other users</li>
</ul>
</div>
<div class="info">
<h3>📊 Setup Information</h3>
<p><strong>Created:</strong> ' . date('Y-m-d H:i:s') . ' UTC</p>
<p><strong>File:</strong> welcome-sample.html</p>
<p><strong>Status:</strong> <span class="success">Active</span></p>
</div>
<div style="text-align: center; margin-top: 30px;">
<a href="../editor.html" class="btn">🎨 Start Creating</a>
<a href="https://github.com/' . htmlspecialchars($github['owner']) . '/' . htmlspecialchars($github['repo']) . '" class="btn" target="_blank">📁 View Repository</a>
</div>
</div>
</body>
</html>';
$result4 = $githubStorage->saveFile($userPath . 'welcome-sample.html', $sampleHtml);
$setupResults['sample_file'] = [
'success' => $result4,
'path' => 'pages/' . $userPath . 'welcome-sample.html',
'message' => $result4 ? 'Sample HTML file created successfully' : 'Failed to create sample file'
];
}
}
}
// 汇总结果
$allSuccess = true;
foreach ($setupResults as $result) {
if (!$result['success']) {
$allSuccess = false;
break;
}
}
$setupResults['overall'] = [
'success' => $allSuccess,
'message' => $allSuccess ? 'All directories and files created successfully!' : 'Some operations failed - check details below'
];
} catch (Exception $e) {
error_log("GitHub Directory Setup Error: " . $e->getMessage());
$setupResults['error'] = [
'success' => false,
'message' => 'Setup failed: ' . $e->getMessage()
];
}
}
// 检查当前目录状态
function checkDirectoryStatus($githubStorage, $path) {
$url = "https://api.github.com/repos/{$githubStorage->owner}/{$githubStorage->repo}/contents/{$path}";
$response = $githubStorage->makeRequest($url, 'GET');
return $response !== false;
}
$currentStatus = [];
if (!empty($github['token'])) {
try {
$githubStorage = new GitHubStorage($github);
$currentStatus = [
'pages' => checkDirectoryStatus($githubStorage, 'pages'),
'users' => checkDirectoryStatus($githubStorage, 'pages/users'),
'user_dir' => checkDirectoryStatus($githubStorage, 'pages/' . trim($userPath, '/'))
];
} catch (Exception $e) {
$currentStatus['error'] = $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>VvvebJs GitHub 目录设置</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
.config-value { background-color: #f8f9fa; padding: 2px 6px; border-radius: 3px; font-family: monospace; }
.status-ok { color: #28a745; }
.status-error { color: #dc3545; }
.status-warning { color: #ffc107; }
.directory-tree { background: #f8f9fa; padding: 15px; border-radius: 5px; font-family: monospace; }
</style>
</head>
<body>
<div class="container mt-5">
<div class="row">
<div class="col-md-10 mx-auto">
<h1 class="mb-4">🏗️ VvvebJs GitHub 目录设置工具</h1>
<div class="alert alert-info">
<h4>🔍 问题诊断</h4>
<p>根据日志信息,GitHub API 返回 404 错误,表示 <code>pages/</code> 目录在仓库中不存在。</p>
<p>此工具将自动创建所需的目录结构来解决这个问题。</p>
</div>
<!-- 当前配置 -->
<div class="card mb-4">
<div class="card-header">
<h3>📋 当前配置</h3>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<p><strong>GitHub 仓库:</strong> <span class="config-value"><?= htmlspecialchars($github['owner']) ?>/<?= htmlspecialchars($github['repo']) ?></span></p>
<p><strong>分支:</strong> <span class="config-value"><?= htmlspecialchars($github['branch']) ?></span></p>
<p><strong>基础路径:</strong> <span class="config-value"><?= htmlspecialchars($github['path']) ?></span></p>
</div>
<div class="col-md-6">
<p><strong>当前用户:</strong> <span class="config-value"><?= htmlspecialchars($currentUser) ?></span></p>
<p><strong>用户路径:</strong> <span class="config-value"><?= htmlspecialchars($userPath) ?></span></p>
<p><strong>完整路径:</strong> <span class="config-value"><?= htmlspecialchars($github['path'] . $userPath) ?></span></p>
</div>
</div>
</div>
</div>
<!-- 目录状态检查 -->
<div class="card mb-4">
<div class="card-header">
<h3>📁 当前目录状态</h3>
</div>
<div class="card-body">
<?php if (isset($currentStatus['error'])): ?>
<div class="alert alert-warning">
<strong>检查失败:</strong> <?= htmlspecialchars($currentStatus['error']) ?>
</div>
<?php else: ?>
<div class="row">
<div class="col-md-4">
<div class="text-center p-3 border rounded">
<h5>基础目录</h5>
<span class="<?= ($currentStatus['pages'] ?? false) ? 'status-ok' : 'status-error' ?>">
<?= ($currentStatus['pages'] ?? false) ? '✅ 存在' : '❌ 不存在' ?>
</span>
<br><small><code>pages/</code></small>
</div>
</div>
<div class="col-md-4">
<div class="text-center p-3 border rounded">
<h5>用户目录</h5>
<span class="<?= ($currentStatus['users'] ?? false) ? 'status-ok' : 'status-error' ?>">
<?= ($currentStatus['users'] ?? false) ? '✅ 存在' : '❌ 不存在' ?>
</span>
<br><small><code>pages/users/</code></small>
</div>
</div>
<div class="col-md-4">
<div class="text-center p-3 border rounded">
<h5>个人目录</h5>
<span class="<?= ($currentStatus['user_dir'] ?? false) ? 'status-ok' : 'status-error' ?>">
<?= ($currentStatus['user_dir'] ?? false) ? '✅ 存在' : '❌ 不存在' ?>
</span>
<br><small><code>pages/<?= htmlspecialchars(trim($userPath, '/')) ?>/</code></small>
</div>
</div>
</div>
<?php endif; ?>
</div>
</div>
<!-- 目录设置工具 -->
<div class="card mb-4">
<div class="card-header">
<h3>🛠️ 目录设置工具</h3>
</div>
<div class="card-body">
<?php if (empty($setupResults)): ?>
<div class="alert alert-primary">
<h5>🚀 即将创建的目录结构</h5>
<div class="directory-tree">
<?= htmlspecialchars($github['repo']) ?>/
├── pages/
│ ├── README.md # 目录说明文件
│ └── users/
│ ├── README.md # 用户目录说明
│ └── <?= str_replace('/', '', $userPath) ?>/
│ ├── README.md # 个人目录说明
│ └── welcome-sample.html # 示例HTML文件
</div>
</div>
<form method="POST">
<button type="submit" name="setup_directories" value="1" class="btn btn-primary btn-lg">
🏗️ 创建目录结构
</button>
</form>
<?php else: ?>
<h5>设置结果</h5>
<?php if (isset($setupResults['overall'])): ?>
<div class="alert alert-<?= $setupResults['overall']['success'] ? 'success' : 'danger' ?>">
<h6>
<span class="<?= $setupResults['overall']['success'] ? 'status-ok' : 'status-error' ?>">
<?= $setupResults['overall']['success'] ? '✅ 设置成功' : '❌ 设置失败' ?>
</span>
</h6>
<p><?= htmlspecialchars($setupResults['overall']['message']) ?></p>
</div>
<?php endif; ?>
<div class="row">
<?php foreach ($setupResults as $key => $result): ?>
<?php if ($key === 'overall' || $key === 'error') continue; ?>
<div class="col-md-6 mb-3">
<div class="card">
<div class="card-body">
<h6 class="card-title">
<?= ucfirst(str_replace('_', ' ', $key)) ?>
<span class="<?= $result['success'] ? 'status-ok' : 'status-error' ?>">
<?= $result['success'] ? '✅' : '❌' ?>
</span>
</h6>
<p class="card-text">
<small class="text-muted"><?= htmlspecialchars($result['path'] ?? '') ?></small><br>
<?= htmlspecialchars($result['message']) ?>
</p>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php if (isset($setupResults['error'])): ?>
<div class="alert alert-danger">
<h6>错误详情</h6>
<p><?= htmlspecialchars($setupResults['error']['message']) ?></p>
</div>
<?php endif; ?>
<?php if ($setupResults['overall']['success'] ?? false): ?>
<div class="alert alert-success">
<h5>🎉 设置完成!</h5>
<p>GitHub 目录结构已成功创建,现在可以正常使用 VvvebJs 编辑器了。</p>
<div class="mt-3">
<a href="https://github.com/<?= htmlspecialchars($github['owner']) ?>/<?= htmlspecialchars($github['repo']) ?>/tree/<?= htmlspecialchars($github['branch']) ?>/pages" target="_blank" class="btn btn-outline-primary">
📁 查看GitHub目录
</a>
<a href="editor.html" class="btn btn-success">
🎨 开始使用编辑器
</a>
</div>
</div>
<?php endif; ?>
<div class="mt-3">
<form method="POST">
<button type="submit" name="setup_directories" value="1" class="btn btn-secondary">
🔄 重新设置
</button>
</form>
</div>
<?php endif; ?>
</div>
</div>
<!-- 使用说明 -->
<div class="card mb-4">
<div class="card-header">
<h3>📖 使用说明</h3>
</div>
<div class="card-body">
<div class="accordion" id="instructionAccordion">
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#inst1">
为什么需要创建目录结构?
</button>
</h2>
<div id="inst1" class="accordion-collapse collapse show" data-bs-parent="#instructionAccordion">
<div class="accordion-body">
<p>GitHub API 在访问不存在的路径时会返回 404 错误。VvvebJs 需要特定的目录结构:</p>
<ul>
<li><code>pages/</code> - 所有HTML页面的基础目录</li>
<li><code>pages/users/</code> - 用户隔离目录</li>
<li><code>pages/users/用户名/</code> - 每个用户的个人目录</li>
</ul>
<p>创建这些目录后,VvvebJs 就能正常保存和读取文件了。</p>
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#inst2">
设置完成后如何使用?
</button>
</h2>
<div id="inst2" class="accordion-collapse collapse" data-bs-parent="#instructionAccordion">
<div class="accordion-body">
<ol>
<li>目录创建成功后,返回 VvvebJs 编辑器</li>
<li>创建或编辑HTML页面</li>
<li>点击保存按钮</li>
<li>文件将自动保存到你的GitHub仓库个人目录中</li>
<li>可以在GitHub上查看和管理你的文件</li>
</ol>
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#inst3">
如果设置失败怎么办?
</button>
</h2>
<div id="inst3" class="accordion-collapse collapse" data-bs-parent="#instructionAccordion">
<div class="accordion-body">
<p>常见原因和解决方案:</p>
<ul>
<li><strong>GitHub Token权限不足:</strong> 确保Token有<code>repo</code>权限</li>
<li><strong>仓库不存在:</strong> 检查仓库名称和所有者名称是否正确</li>
<li><strong>分支不存在:</strong> 确认分支名称正确(通常是mainmaster)</li>
<li><strong>网络问题:</strong> 重试设置过程</li>
</ul>
<p>可以使用其他测试工具进一步诊断问题。</p>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- 操作按钮 -->
<div class="card">
<div class="card-body text-center">
<div class="row">
<div class="col-md-3">
<a href="github-advanced-test.php" class="btn btn-primary w-100">完整测试</a>
</div>
<div class="col-md-3">
<a href="hf-space-check.php" class="btn btn-info w-100">HF Space检查</a>
</div>
<div class="col-md-3">
<a href="directory-creation-test.php" class="btn btn-warning w-100">目录测试</a>
</div>
<div class="col-md-3">
<a href="editor.html" class="btn btn-success w-100">返回编辑器</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>