<?php
//require_once __DIR__ . '/../auth/auth_check.php';
session_start();
require_once __DIR__ . '/../db_config.php';
$pdo = getDB();
$basePath = '/var/www/subdomains';
$message = '';
// --- Handle actions ---
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'], $_POST['username'])) {
$username = basename($_POST['username']);
$action = $_POST['action'];
try {
switch ($action) {
case 'delete_all':
// Remove Linux user + files
shell_exec("sudo userdel -r " . escapeshellarg($username));
shell_exec("sudo rm -rf " . escapeshellarg("$basePath/$username"));
$pdo->prepare("DELETE FROM sites WHERE system_user=?")->execute([$username]);
$pdo->prepare("DELETE FROM users WHERE username=?")->execute([$username]);
$message = "<div class='success'>User <b>$username</b> and files deleted.</div>";
break;
case 'remove_files':
shell_exec("sudo rm -rf " . escapeshellarg("$basePath/$username"));
$message = "<div class='success'>Files for <b>$username</b> deleted.</div>";
break;
case 'disable_user':
$pdo->prepare("UPDATE users SET status='disabled' WHERE username=?")->execute([$username]);
shell_exec("sudo usermod -L " . escapeshellarg($username));
$message = "<div class='success'>User <b>$username</b> disabled (locked).</div>";
break;
case 'enable_user':
$pdo->prepare("UPDATE users SET status='active' WHERE username=?")->execute([$username]);
shell_exec("sudo usermod -U " . escapeshellarg($username));
$message = "<div class='success'>User <b>$username</b> reactivated.</div>";
break;
}
} catch (Exception $e) {
$message = "<div class='error'>Error: " . htmlspecialchars($e->getMessage()) . "</div>";
}
}
// --- Fetch user/site list ---
$stmt = $pdo->query("
SELECT u.id, u.username, u.email, u.status, s.domain, s.plan, s.root_dir
FROM users u
LEFT JOIN sites s ON u.id = s.user_id
ORDER BY u.created_date DESC
");
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
// --- Fetch folder list ---
$folders = array_filter(scandir($basePath), fn($f) => $f[0] !== '.' && is_dir("$basePath/$f"));
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Manage Sites</title>
<style>
body { font-family: sans-serif; margin: 20px; background: #f4f4f4; }
table { width: 100%; border-collapse: collapse; background: white; }
th, td { padding: 10px; border-bottom: 1px solid #ddd; text-align: left; }
tr:hover { background: #f9f9f9; }
.success { background: #e6ffed; color: #256029; padding: 10px; border-radius: 6px; margin-bottom: 10px; }
.error { background: #ffe6e6; color: #9b1c1c; padding: 10px; border-radius: 6px; margin-bottom: 10px; }
form { display:inline; margin:0; }
button { padding: 6px 10px; border:none; border-radius:4px; cursor:pointer; }
.delete { background:#dc3545; color:white; }
.disable { background:#ffc107; }
.enable { background:#28a745; color:white; }
.remove { background:#6c757d; color:white; }
</style>
</head>
<body>
<h1>🗂️ Site Management</h1>
<?php echo $message; ?>
<table>
<tr><th>Username</th><th>Email</th><th>Domain</th><th>Plan</th><th>Status</th><th>Folder Exists</th><th>Actions</th></tr>
<?php foreach ($users as $u):
$folderExists = in_array($u['username'], $folders);
?>
<tr>
<td><?= htmlspecialchars($u['username']) ?></td>
<td><?= htmlspecialchars($u['email']) ?></td>
<td><?= htmlspecialchars($u['domain'] ?? '') ?></td>
<td><?= htmlspecialchars(strtoupper($u['plan'] ?? '')) ?></td>
<td><?= htmlspecialchars($u['status']) ?></td>
<td><?= $folderExists ? '✅' : '❌' ?></td>
<td>
<form method="POST" style="display:inline;">
<input type="hidden" name="username" value="<?= htmlspecialchars($u['username']) ?>">
<button name="action" value="disable_user" class="disable">Disable</button>
<button name="action" value="enable_user" class="enable">Enable</button>
<button name="action" value="remove_files" class="remove" onclick="return confirm('Remove files for <?= $u['username'] ?>?')">Remove Files</button>
<button name="action" value="delete_all" class="delete" onclick="return confirm('Delete <?= $u['username'] ?> completely?')">Delete All</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</table>
</body>
</html>