|
|
<?php |
|
|
require_once 'user-manager.php'; |
|
|
require_once 'storage.php'; |
|
|
|
|
|
|
|
|
$userManager = new UserManager(); |
|
|
if (!$userManager->isLoggedIn()) { |
|
|
header('Location: index.html'); |
|
|
exit; |
|
|
} |
|
|
|
|
|
$currentUser = $userManager->getCurrentUser(); |
|
|
|
|
|
|
|
|
$storageManager = new StorageManager(); |
|
|
|
|
|
|
|
|
$html = file_get_contents('editor.html'); |
|
|
|
|
|
|
|
|
$files = ''; |
|
|
$userFiles = []; |
|
|
|
|
|
try { |
|
|
$githubFiles = $storageManager->listFiles(); |
|
|
|
|
|
|
|
|
foreach ($githubFiles as $file) { |
|
|
if (in_array($file['name'], array('new-page-blank-template.html', 'editor.html'))) continue; |
|
|
|
|
|
$pathInfo = pathinfo($file['name']); |
|
|
$filename = $pathInfo['filename']; |
|
|
$folder = 'My Files (GitHub)'; |
|
|
$url = $file['path']; |
|
|
$name = $filename; |
|
|
$title = ucfirst($name); |
|
|
|
|
|
$files .= '<li class="file" data-url="' . htmlspecialchars($url) . '" data-folder="' . htmlspecialchars($folder) . '">'; |
|
|
$files .= '<label class="folder-name">' . htmlspecialchars($folder) . '</label>'; |
|
|
$files .= '<div class="file-name"><span>' . htmlspecialchars($title) . '</span> <div class="file-actions">'; |
|
|
$files .= '<button class="btn btn-sm btn-light load-file-btn" data-file="' . htmlspecialchars($url) . '">Load</button>'; |
|
|
$files .= '</div></div></li>'; |
|
|
|
|
|
$userFiles[] = [ |
|
|
'filename' => $url, |
|
|
'title' => $title, |
|
|
'folder' => $folder |
|
|
]; |
|
|
} |
|
|
} catch (Exception $e) { |
|
|
error_log("Editor: Error loading GitHub files: " . $e->getMessage()); |
|
|
} |
|
|
|
|
|
|
|
|
$localUserDir = "user-files/$currentUser"; |
|
|
if (is_dir($localUserDir)) { |
|
|
$localFiles = glob("$localUserDir/*.html"); |
|
|
|
|
|
foreach ($localFiles as $file) { |
|
|
if (in_array(basename($file), array('new-page-blank-template.html', 'editor.html'))) continue; |
|
|
$pathInfo = pathinfo($file); |
|
|
$filename = $pathInfo['filename']; |
|
|
$folder = 'Local Files'; |
|
|
$url = str_replace(__DIR__ . DIRECTORY_SEPARATOR, '', $file); |
|
|
$url = str_replace('\\', '/', $url); |
|
|
$name = $filename; |
|
|
$title = ucfirst($name); |
|
|
|
|
|
$files .= '<li class="file" data-url="' . htmlspecialchars($url) . '" data-folder="' . htmlspecialchars($folder) . '">'; |
|
|
$files .= '<label class="folder-name">' . htmlspecialchars($folder) . '</label>'; |
|
|
$files .= '<div class="file-name"><span>' . htmlspecialchars($title) . '</span> <div class="file-actions">'; |
|
|
$files .= '<button class="btn btn-sm btn-light load-file-btn" data-file="' . htmlspecialchars($url) . '">Load</button>'; |
|
|
$files .= '</div></div></li>'; |
|
|
|
|
|
$userFiles[] = [ |
|
|
'filename' => $url, |
|
|
'title' => $title, |
|
|
'folder' => $folder |
|
|
]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
$demoFiles = array_merge(glob('demo/*/*.html'), glob('demo/*.html')); |
|
|
|
|
|
foreach ($demoFiles as $file) { |
|
|
if (in_array(basename($file), array('new-page-blank-template.html', 'editor.html'))) continue; |
|
|
$pathInfo = pathinfo($file); |
|
|
$filename = $pathInfo['filename']; |
|
|
$folder = ucfirst(basename(dirname($file))); |
|
|
$url = $file; |
|
|
$name = $filename; |
|
|
$title = ucfirst($name); |
|
|
|
|
|
$files .= '<li class="file" data-url="' . htmlspecialchars($url) . '" data-folder="' . htmlspecialchars($folder) . '">'; |
|
|
$files .= '<label class="folder-name">' . htmlspecialchars($folder) . ' (Demo)</label>'; |
|
|
$files .= '<div class="file-name"><span>' . htmlspecialchars($title) . '</span> <div class="file-actions">'; |
|
|
$files .= '<button class="btn btn-sm btn-light load-file-btn" data-file="' . htmlspecialchars($url) . '">Load</button>'; |
|
|
$files .= '</div></div></li>'; |
|
|
} |
|
|
|
|
|
|
|
|
$userInfo = '<div class="user-info alert alert-info">'; |
|
|
$userInfo .= '<strong>当前用户:</strong> ' . htmlspecialchars($currentUser); |
|
|
$userInfo .= '<br><strong>存储:</strong> ' . StorageConfig::getStorageType(); |
|
|
$userInfo .= '<br><strong>GitHub文件:</strong> ' . count($githubFiles ?? []) . ' 个'; |
|
|
if (isset($localFiles)) { |
|
|
$userInfo .= '<br><strong>本地缓存:</strong> ' . count($localFiles) . ' 个'; |
|
|
} |
|
|
$userInfo .= '</div>'; |
|
|
|
|
|
|
|
|
$fileManagementScript = ' |
|
|
<script> |
|
|
window.vvvebUserFiles = ' . json_encode($userFiles) . '; |
|
|
window.vvvebCurrentUser = "' . htmlspecialchars($currentUser) . '"; |
|
|
|
|
|
// Enhanced file loading with GitHub support |
|
|
function loadFileFromGitHub(filename) { |
|
|
console.log("Loading file from GitHub:", filename); |
|
|
|
|
|
fetch("save.php?action=loadFile&file=" + encodeURIComponent(filename)) |
|
|
.then(response => response.json()) |
|
|
.then(data => { |
|
|
if (data.success) { |
|
|
Vvveb.Builder.loadHtml(data.content); |
|
|
console.log("File loaded successfully from GitHub"); |
|
|
|
|
|
// Update current file reference |
|
|
window.currentFile = filename; |
|
|
|
|
|
// Update page title if available |
|
|
const titleMatch = data.content.match(/<title>(.*?)<\/title>/i); |
|
|
if (titleMatch) { |
|
|
const titleInput = document.querySelector("input[name=title]"); |
|
|
if (titleInput) titleInput.value = titleMatch[1]; |
|
|
} |
|
|
} else { |
|
|
alert("Error loading file: " + (data.message || "Unknown error")); |
|
|
} |
|
|
}) |
|
|
.catch(error => { |
|
|
console.error("Error loading file:", error); |
|
|
alert("Error loading file from GitHub"); |
|
|
}); |
|
|
} |
|
|
|
|
|
// Override the existing file loading |
|
|
document.addEventListener("DOMContentLoaded", function() { |
|
|
// Add enhanced file loading for GitHub files |
|
|
document.querySelectorAll(".load-file-btn").forEach(btn => { |
|
|
btn.addEventListener("click", function() { |
|
|
const filename = this.getAttribute("data-file"); |
|
|
loadFileFromGitHub(filename); |
|
|
}); |
|
|
}); |
|
|
|
|
|
// Add refresh button for file list |
|
|
const refreshBtn = document.createElement("button"); |
|
|
refreshBtn.className = "btn btn-sm btn-secondary"; |
|
|
refreshBtn.innerHTML = "🔄 刷新文件列表"; |
|
|
refreshBtn.onclick = function() { |
|
|
location.reload(); |
|
|
}; |
|
|
|
|
|
const fileManager = document.querySelector("#filemanager .file-manager-header"); |
|
|
if (fileManager) { |
|
|
fileManager.appendChild(refreshBtn); |
|
|
} |
|
|
}); |
|
|
</script>'; |
|
|
|
|
|
|
|
|
$html = str_replace('<!--files-li-->', $files, $html); |
|
|
$html = str_replace('</head>', $fileManagementScript . '</head>', $html); |
|
|
$html = str_replace('<div id="filemanager">', '<div id="filemanager">' . $userInfo, $html); |
|
|
|
|
|
echo $html; |
|
|
?> |
|
|
|