Cody Cook 635b3ddcbc
Some checks failed
SonarQube Scan / SonarQube Trigger (push) Failing after 5m27s
Address changes.
2025-02-22 17:20:19 -08:00

368 lines
18 KiB
PHP

<?php
require_once 'includes/globals.php';
require_once 'vendor/autoload.php';
use DJMixHosting\Database;
use DJMixHosting\User;
use DJMixHosting\SessionManager;
// Ensure the session is started and the user is authenticated.
SessionManager::start();
if (!SessionManager::getUser()) {
header("Location: login.php");
exit;
}
$db = new Database($config);
$userId = SessionManager::getUser()['id'];
// Instantiate the User object; this loads user details internally.
$user = new User($db, $userId);
// Handle POST requests for profile updates.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
if (!isset($_POST['action'])) {
throw new Exception("No action specified.");
}
$action = $_POST['action'];
switch ($action) {
case 'updateUsername':
$newUsername = trim($_POST['new_username'] ?? '');
$message = $user->updateUsername($newUsername);
$_SESSION['success'] = $message;
break;
case 'updateEmail':
$newEmail = trim($_POST['new_email'] ?? '');
$message = $user->updateEmail($newEmail, $config);
$_SESSION['success'] = $message;
break;
case 'updateName':
$firstName = trim($_POST['first_name'] ?? '');
$lastName = trim($_POST['last_name'] ?? '');
$message = $user->updateName($firstName, $lastName);
$_SESSION['success'] = $message;
break;
case 'updatePassword':
$currentPassword = $_POST['current_password'] ?? '';
$newPassword = $_POST['new_password'] ?? '';
$confirmPassword = $_POST['confirm_password'] ?? '';
$message = $user->updatePassword($currentPassword, $newPassword, $confirmPassword);
$_SESSION['success'] = $message;
break;
// Optionally, handle other actions such as profile picture update.
default:
throw new Exception("Invalid action.");
}
} catch (Exception $e) {
$_SESSION['error'] = $e->getMessage();
}
header("Location: profile.php");
exit;
}
// Retrieve user data using getter methods.
$userData = [
'id' => $user->getId(),
'username' => $user->getUsername(),
'firstName' => $user->getFirstName(),
'lastName' => $user->getLastName(),
'email' => $user->getEmail(),
'img' => $user->getImg(),
'emailVerified' => $user->getVerified(),
];
// Determine if editing should be disabled (e.g., if email not verified)
$editingDisabled = ((int)$userData['emailVerified'] !== 1);
$alertMessage = $editingDisabled ? "Please verify your email to enable profile editing." : "";
require_once 'includes/header.php';
?>
<section>
<div class="container py-5">
<div class="row">
<div class="col">
<nav aria-label="breadcrumb" class="bg-body-tertiary rounded-3 p-3 mb-4">
<ol class="breadcrumb mb-0">
<li class="breadcrumb-item"><a href="/"><?php echo $locale['home']; ?></a></li>
<li class="breadcrumb-item active" aria-current="page">Profile</li>
</ol>
</nav>
</div>
</div>
<!-- Display Flash Alerts -->
<?php if (isset($_SESSION['error'])): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<?php echo htmlspecialchars($_SESSION['error']); ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php unset($_SESSION['error']); ?>
<?php endif; ?>
<?php if (isset($_SESSION['success'])): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<?php echo htmlspecialchars($_SESSION['success']); ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php unset($_SESSION['success']); ?>
<?php endif; ?>
<?php if (!empty($alertMessage)): ?>
<div class="alert alert-warning" role="alert">
<?php echo htmlspecialchars($alertMessage); ?>
</div>
<?php endif; ?>
<div class="row">
<!-- Left Sidebar: Profile Picture and Controls -->
<div class="col-lg-4">
<div class="card mb-4">
<div class="card-body bg-body-secondary text-center">
<img src="<?php echo htmlspecialchars($userData['img'] ?: 'default_profile.png'); ?>"
alt="avatar"
class="rounded-circle img-fluid" style="width: 150px;">
<br/>
<button type="button" class="btn btn-sm btn-secondary mb-2"
<?php echo ($editingDisabled) ? 'disabled' : ''; ?>
data-bs-toggle="modal" data-bs-target="#profilePictureModal">
<?php echo $locale['changePicture']; ?>
</button>
</div>
</div>
<!-- List group for username, email, name, and password -->
<div class="list-group mb-4">
<div class="list-group-item d-flex justify-content-between align-items-center">
<span><?php echo $locale['username']; ?>: <?php echo htmlspecialchars($userData['username']); ?></span>
<button type="button" class="btn btn-sm btn-secondary me-1"
<?php echo ($editingDisabled) ? 'disabled' : ''; ?>
data-bs-toggle="modal" data-bs-target="#usernameModal">
<?php echo $locale['change']; ?>
</button>
</div>
<div class="list-group-item d-flex justify-content-between align-items-center">
<span><?php echo $locale['email']; ?>: <?php echo htmlspecialchars($userData['email']); ?></span>
<div>
<button type="button" class="btn btn-sm btn-secondary me-1"
<?php echo ($editingDisabled) ? 'disabled' : ''; ?>
data-bs-toggle="modal" data-bs-target="#emailModal">
<?php echo $locale['change']; ?>
</button><?php if (!$userData['emailVerified']) { ?>
<button type="button" class="btn btn-sm btn-primary"
data-bs-toggle="modal" data-bs-target="#verifyEmailModal">
<?php echo $locale['verify']; ?>
</button>
<?php } ?></div>
</div>
<div class="list-group-item d-flex justify-content-between align-items-center">
<span><?php echo $locale['name']; ?>: <?php echo htmlspecialchars($userData['firstName'] . ' ' . $userData['lastName']); ?></span>
<button type="button" class="btn btn-sm btn-secondary me-1"
<?php echo ($editingDisabled) ? 'disabled' : ''; ?>
data-bs-toggle="modal" data-bs-target="#nameModal">
<?php echo $locale['change']; ?>
</button>
</div>
<div class="list-group-item d-flex justify-content-between align-items-center">
<span>Password</span>
<button type="button" class="btn btn-sm btn-secondary me-1"
<?php echo ($editingDisabled) ? 'disabled' : ''; ?>
data-bs-toggle="modal" data-bs-target="#passwordModal">
<?php echo $locale['change']; ?>
</button>
</div>
</div>
</div>
<!-- Right Content: Additional Options -->
<div class="col-lg-8">
<div class="card mb-4">
<div class="card-body bg-body-secondary">
<h5>Additional Features</h5>
<p>Followed DJs and recent ratings will appear here once implemented.</p>
</div>
</div>
<?php
// If the user is an admin, display a new box with session output.
$currentUser = SessionManager::getUser();
if (isset($currentUser['role']) && $currentUser['role'] === 'admin'):
?>
<div class="card mb-4">
<div class="card-body bg-body-secondary">
<h5>Admin Session Output</h5>
<pre id="adminSessionOutput"><?php echo htmlspecialchars(print_r($_SESSION, true)); ?></pre>
</div>
</div>
<?php endif; ?>
</div>
</div>
</section>
<!-- Modals -->
<!-- 1. Profile Picture Modal -->
<div class="modal fade" id="profilePictureModal" tabindex="-1" aria-labelledby="profilePictureModalLabel" aria-hidden="true">
<div class="modal-dialog">
<form action="profile.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="updateProfilePicture">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="profilePictureModalLabel">Change Profile Picture</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<!-- CSRF token can be added here as needed -->
<div class="mb-3">
<label for="profilePicture" class="form-label">Select new profile picture</label>
<input type="file" class="form-control" id="profilePicture" name="profile_picture" accept="image/*" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Update Picture</button>
</div>
</div>
</form>
</div>
</div>
<!-- 2. Username Modal -->
<div class="modal fade" id="usernameModal" tabindex="-1" aria-labelledby="usernameModalLabel" aria-hidden="true">
<div class="modal-dialog">
<form action="profile.php" method="post">
<input type="hidden" name="action" value="updateUsername">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="usernameModalLabel">Change Username</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label for="newUsername" class="form-label">New Username</label>
<input type="text" class="form-control" id="newUsername" name="new_username" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Update Username</button>
</div>
</div>
</form>
</div>
</div>
<!-- 3. Email Modal -->
<div class="modal fade" id="emailModal" tabindex="-1" aria-labelledby="emailModalLabel" aria-hidden="true">
<div class="modal-dialog">
<form action="profile.php" method="post">
<input type="hidden" name="action" value="updateEmail">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="emailModalLabel">Change Email</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label for="newEmail" class="form-label">New Email Address</label>
<input type="email" class="form-control" id="newEmail" name="new_email" required>
</div>
<p class="text-muted">Note: Changing your email will require you to verify the new address.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Update Email</button>
</div>
</div>
</form>
</div>
</div>
<!-- 4. Verify Email Modal -->
<div class="modal fade" id="verifyEmailModal" tabindex="-1" aria-labelledby="verifyEmailModalLabel" aria-hidden="true">
<div class="modal-dialog">
<form action="verify_email.php" method="post">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="verifyEmailModalLabel">Verify Your Email</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>An email with a verification code has been sent to your address. Please enter the code below. (Or click the link in the email to auto-verify.)</p>
<div class="mb-3">
<label for="verificationCode" class="form-label">Verification Code</label>
<input type="text" class="form-control" id="verificationCode" name="verification_code" required>
</div>
<p class="small text-muted">You can only request a new code once every 15 minutes.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Verify Email</button>
</div>
</div>
</form>
</div>
</div>
<!-- 5. Name Modal -->
<div class="modal fade" id="nameModal" tabindex="-1" aria-labelledby="nameModalLabel" aria-hidden="true">
<div class="modal-dialog">
<form action="profile.php" method="post">
<input type="hidden" name="action" value="updateName">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="nameModalLabel">Change Your Name</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label for="firstName" class="form-label">First Name</label>
<input type="text" class="form-control" id="firstName" name="first_name" value="<?php echo htmlspecialchars($userData['firstName']); ?>" required>
</div>
<div class="mb-3">
<label for="lastName" class="form-label">Last Name</label>
<input type="text" class="form-control" id="lastName" name="last_name" value="<?php echo htmlspecialchars($userData['lastName']); ?>" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Update Name</button>
</div>
</div>
</form>
</div>
</div>
<!-- 6. Password Modal -->
<div class="modal fade" id="passwordModal" tabindex="-1" aria-labelledby="passwordModalLabel" aria-hidden="true">
<div class="modal-dialog">
<form action="profile.php" method="post">
<input type="hidden" name="action" value="updatePassword">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="passwordModalLabel">Change Password</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label for="currentPassword" class="form-label">Current Password</label>
<input type="password" class="form-control" id="currentPassword" name="current_password" required>
</div>
<div class="mb-3">
<label for="newPassword" class="form-label">New Password</label>
<input type="password" class="form-control" id="newPassword" name="new_password" required>
</div>
<div class="mb-3">
<label for="confirmPassword" class="form-label">Confirm New Password</label>
<input type="password" class="form-control" id="confirmPassword" name="confirm_password" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Change Password</button>
</div>
</div>
</form>
</div>
</div>
<?php require_once 'includes/footer.php'; ?>