Fix delete function with sudo and remove export feature

This commit is contained in:
2026-01-01 20:25:08 +11:00
parent 075faa5731
commit 809ce8b6f3
3 changed files with 9 additions and 67 deletions

View File

@@ -502,6 +502,7 @@ ln -sf $LOOT_DIR /var/www/html/loot 2>/dev/null
echo -e "$OKBLUE[*]$RESET Configuring permissions for Web UI... $RESET"
echo 'www-data ALL=(ALL) NOPASSWD: /usr/bin/bash /usr/share/sniper/sniper *' > /etc/sudoers.d/www-data-sniper
echo 'www-data ALL=(ALL) NOPASSWD: /usr/bin/pkill *' >> /etc/sudoers.d/www-data-sniper
echo 'www-data ALL=(ALL) NOPASSWD: /usr/bin/rm *' >> /etc/sudoers.d/www-data-sniper
chmod 440 /etc/sudoers.d/www-data-sniper
# Configure Apache Port 8888

View File

@@ -155,7 +155,6 @@ async function loadWorkspaces() {
</div>
<div class="workspace-actions">
<button class="btn btn-secondary" onclick="viewWorkspace('${escapeHtml(name)}')">View</button>
<button class="btn btn-secondary" onclick="exportWorkspace('${escapeHtml(name)}')">Export</button>
<button class="btn btn-danger" onclick="deleteWorkspace('${escapeHtml(name)}')">Delete</button>
</div>
</div>
@@ -193,28 +192,6 @@ async function viewWorkspace(name) {
}
}
async function exportWorkspace(name) {
showNotification("Creating export for: " + name + "...", "info");
try {
const response = await fetch("workspaces.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ action: "export", name: name }),
});
const result = await response.json();
if (result.success && result.downloadUrl) {
showNotification(`Export ready: ${result.filename} (${result.size})`, "success");
// Trigger download
window.open(result.downloadUrl, "_blank");
} else {
showNotification("Export failed: " + (result.error || "Unknown error"), "error");
}
} catch (error) {
showNotification("Export failed.", "error");
}
}
async function deleteWorkspace(name) {
if (!confirm(`Are you sure you want to delete workspace "${name}"?`)) return;

View File

@@ -97,41 +97,16 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($action === 'delete') {
if (is_dir($wsPath)) {
// Delete directory recursively
deleteDirectory($wsPath);
// Delete directory recursively using sudo
exec("sudo rm -rf " . escapeshellarg($wsPath));
if (!file_exists($wsPath)) {
echo json_encode(['success' => true, 'message' => 'Workspace deleted']);
} else {
echo json_encode(['success' => false, 'error' => 'Workspace not found']);
echo json_encode(['success' => false, 'error' => 'Failed to delete workspace. Check permissions.']);
}
exit;
}
if ($action === 'export') {
if (!is_dir($wsPath)) {
echo json_encode(['success' => false, 'error' => 'Workspace not found']);
exit;
}
$exportFile = EXPORT_DIR . '/' . $name . '.tar.gz';
// Create tar.gz archive
$cmd = "cd " . escapeshellarg(WORKSPACE_DIR) . " && tar -czf " . escapeshellarg($exportFile) . " " . escapeshellarg($name) . " 2>&1";
$output = shell_exec($cmd);
if (file_exists($exportFile)) {
$size = filesize($exportFile);
echo json_encode([
'success' => true,
'downloadUrl' => 'workspaces.php?action=download&name=' . urlencode($name),
'filename' => $name . '.tar.gz',
'size' => formatBytes($size),
'message' => 'Export created successfully'
]);
} else {
echo json_encode([
'success' => false,
'error' => 'Failed to create export: ' . $output
]);
echo json_encode(['success' => false, 'error' => 'Workspace not found']);
}
exit;
}
@@ -161,14 +136,3 @@ function formatBytes($bytes)
return $bytes . ' B';
}
}
function deleteDirectory($dir)
{
if (!is_dir($dir)) return false;
$files = array_diff(scandir($dir), ['.', '..']);
foreach ($files as $file) {
$path = $dir . '/' . $file;
is_dir($path) ? deleteDirectory($path) : unlink($path);
}
return rmdir($dir);
}