|
Server : LiteSpeed System : Linux terra.hostitbro.com 5.14.0-611.54.3.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 7 16:31:24 EDT 2026 x86_64 User : outerorb ( 1091) PHP Version : 8.1.34 Disable Function : mail Directory : /home2/outerorb/.trash/survey_system/ | |
|
Path: /home2/outerorb/.trash/survey_system/user_toggle_status.php
Size: 3.77 KB
Permissions: 0666
<?php
require_once 'config.php';
// Check if user is logged in and is admin
if (!is_logged_in()) {
header("Location: index.php");
exit;
}
$user = current_user();
if ($user['role'] !== 'admin') {
header("Location: dashboard.php");
exit;
}
$errors = [];
$user_id = $_GET['id'] ?? null;
if ($user_id === null || !ctype_digit($user_id)) {
$errors[] = "Invalid user ID.";
} else {
$user_id = (int)$user_id;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['confirm'])) {
if (empty($errors)) {
// Prevent admin from disabling themselves
if ($user_id === $user['id']) {
$errors[] = "You cannot disable your own account.";
require_once 'inc/eventlog.php';
log_event('user_disable_failed', ['reason' => 'self_disable', 'user_id' => $user_id], 'failure');
} else {
// Check current status
$stmt = $pdo->prepare("SELECT active FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$current = $stmt->fetchColumn();
if ($current == 1) {
// Disable user
$stmt = $pdo->prepare("UPDATE users SET active = 0 WHERE id = ?");
$stmt->execute([$user_id]);
require_once 'inc/eventlog.php';
log_event('user_disabled', ['user_id' => $user_id]);
header("Location: users.php?msg=User+disabled+successfully");
exit;
} else {
// Enable user
$stmt = $pdo->prepare("UPDATE users SET active = 1 WHERE id = ?");
$stmt->execute([$user_id]);
require_once 'inc/eventlog.php';
log_event('user_enabled', ['user_id' => $user_id]);
header("Location: users.php?msg=User+enabled+successfully");
exit;
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<?php include __DIR__ . '/inc/header.php'; ?>
<div class="container mt-5">
<h2>Enable/Disable User</h2>
<?php if (!empty($errors)): ?>
<div class="alert alert-danger">
<ul class="mb-0">
<?php foreach ($errors as $error): ?>
<li><?= htmlspecialchars($error) ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<?php if (empty($errors) && $_SERVER['REQUEST_METHOD'] !== 'POST'): ?>
<?php
// Get current status
$stmt = $pdo->prepare("SELECT active, username FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user_data = $stmt->fetch();
$current = $user_data['active'];
?>
<?php if ($current == 1): ?>
<p>Are you sure you want to <b>disable</b> user <b><?= htmlspecialchars($user_data['username']) ?></b>? (User will be hidden from dashboard agent card but visible elsewhere)</p>
<form method="post" action="user_toggle_status.php?id=<?= htmlspecialchars($user_id) ?>">
<button type="submit" name="confirm" class="btn btn-warning">Disable</button>
<a href="users.php" class="btn btn-secondary ms-2">Cancel</a>
</form>
<?php else: ?>
<p>User <b><?= htmlspecialchars($user_data['username']) ?></b> is currently <b>disabled</b>. Do you want to <b>enable</b> this user?</p>
<form method="post" action="user_toggle_status.php?id=<?= htmlspecialchars($user_id) ?>">
<button type="submit" name="confirm" class="btn btn-success">Enable</button>
<a href="users.php" class="btn btn-secondary ms-2">Cancel</a>
</form>
<?php endif; ?>
<?php endif; ?>
</div>
<?php include __DIR__ . '/inc/footer.php'; ?>
</html>