prepare('SELECT * FROM users WHERE id = ?'); $stmt->execute([$target_id]); $profile = $stmt->fetch(); if (!$profile) { header('Location: ' . BASE_URL . '/admin/'); exit; } // ── Handle form submissions ─────────────────────────────────────────────────── if ($_SERVER['REQUEST_METHOD'] === 'POST') { $action = $_POST['action'] ?? ''; // ── Update profile ─────────────────────────────────────────────────────── if ($action === 'update_profile') { $new_display = trim($_POST['display_name'] ?? ''); $pdo->prepare('UPDATE users SET display_name=? WHERE id=?')->execute([$new_display, $target_id]); // Refresh session if editing own profile if ($target_id === $uid) { $_SESSION['display_name'] = $new_display; } $messages[] = 'Profile updated.'; $profile['display_name'] = $new_display; } // ── Update email ───────────────────────────────────────────────────────── if ($action === 'update_email') { $new_email = trim($_POST['new_email'] ?? ''); $cur_pass = $_POST['current_password'] ?? ''; if (!filter_var($new_email, FILTER_VALIDATE_EMAIL)) { $errors[] = 'Invalid email address.'; } elseif (!password_verify($cur_pass, $profile['password_hash'])) { $errors[] = 'Current password is incorrect.'; } else { $chk = $pdo->prepare('SELECT id FROM users WHERE email=? AND id!=?'); $chk->execute([$new_email, $target_id]); if ($chk->fetch()) { $errors[] = 'That email is already in use.'; } else { $pdo->prepare('UPDATE users SET email=? WHERE id=?')->execute([$new_email, $target_id]); if ($target_id === $uid) $_SESSION['email'] = $new_email; $messages[] = 'Email updated.'; $profile['email'] = $new_email; } } } // ── Change password ─────────────────────────────────────────────────────── if ($action === 'change_password') { $cur_pass = $_POST['current_password'] ?? ''; $new_pass = $_POST['new_password'] ?? ''; $conf_pass = $_POST['confirm_password'] ?? ''; if (!password_verify($cur_pass, $profile['password_hash'])) { $errors[] = 'Current password is incorrect.'; } elseif (strlen($new_pass) < 8) { $errors[] = 'New password must be at least 8 characters.'; } elseif ($new_pass !== $conf_pass) { $errors[] = 'New passwords do not match.'; } else { $hash = password_hash($new_pass, PASSWORD_BCRYPT); $pdo->prepare('UPDATE users SET password_hash=? WHERE id=?')->execute([$hash, $target_id]); $messages[] = 'Password changed successfully.'; $profile['password_hash'] = $hash; } } // ── Superadmin: change rosary limit ────────────────────────────────────── if ($action === 'update_limit' && has_role('superadmin')) { $new_limit = (int)($_POST['rosary_limit'] ?? 1); $pdo->prepare('UPDATE users SET rosary_limit=? WHERE id=?')->execute([$new_limit, $target_id]); if ($target_id === $uid) $_SESSION['rosary_limit'] = $new_limit; $messages[] = 'Rosary limit updated.'; $profile['rosary_limit'] = $new_limit; } } $is_own = ($target_id === $uid); $is_super = has_role('superadmin'); $role_labels = ['superadmin'=>'Superadmin','admin'=>'Admin','superuser'=>'Superuser','user'=>'User']; ?>