Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/newsfeed/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/newsfeed/profile.php

<?php
require_once 'config/config.php';
require_login();

$database = new Database();
$conn = $database->getConnection();
$user = new User($conn);

$current_user = $user->getById($_SESSION['user_id']);
$error = '';
$success = '';

// Handle profile update
if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'update_profile') {
    $name = sanitize_input($_POST['name'] ?? '');
    $email = sanitize_input($_POST['email'] ?? '');
    $username = sanitize_input($_POST['username'] ?? '');
    $telephone = sanitize_input($_POST['telephone'] ?? '');
    $address = sanitize_input($_POST['address'] ?? '');
    $description = sanitize_input($_POST['description'] ?? '');
    $location_type = sanitize_input($_POST['location_type'] ?? '');
    $location_name = sanitize_input($_POST['location_name'] ?? '');
    
    if (empty($name) || empty($email) || empty($username)) {
        $error = 'Please fill in all required fields';
    } elseif ($user->emailExists($email, $_SESSION['user_id'])) {
        $error = 'Email already exists';
    } elseif ($user->usernameExists($username, $_SESSION['user_id'])) {
        $error = 'Username already exists';
    } else {
        $update_data = [
            'name' => $name,
            'email' => $email,
            'username' => $username,
            'telephone' => $telephone,
            'address' => $address,
            'description' => $description,
            'location_type' => $location_type,
            'location_name' => $location_name
        ];
        
        if ($user->update($_SESSION['user_id'], $update_data)) {
            $_SESSION['user_name'] = $name;
            $_SESSION['location_type'] = $location_type;
            $_SESSION['location_name'] = $location_name;
            $success = 'Profile updated successfully!';
            $current_user = $user->getById($_SESSION['user_id']);
        } else {
            $error = 'Failed to update profile. Please try again.';
        }
    }
}

// Handle password change
if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'change_password') {
    $old_password = $_POST['old_password'] ?? '';
    $new_password = $_POST['new_password'] ?? '';
    $confirm_password = $_POST['confirm_password'] ?? '';
    
    if (empty($old_password) || empty($new_password) || empty($confirm_password)) {
        $error = 'Please fill in all password fields';
    } elseif ($new_password !== $confirm_password) {
        $error = 'New passwords do not match';
    } elseif (strlen($new_password) < 6) {
        $error = 'New password must be at least 6 characters long';
    } else {
        if ($user->changePassword($_SESSION['user_id'], $old_password, $new_password)) {
            $success = 'Password changed successfully!';
        } else {
            $error = 'Current password is incorrect';
        }
    }
}

$flash = get_flash_message();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Profile - COP News Portal</title>
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body>
    <header class="header">
        <nav class="navbar">
            <a href="dashboard.php" class="logo">
                <i class="fas fa-church"></i>
                COP News Portal
            </a>
            <ul class="nav-links">
                <li><a href="dashboard.php"><i class="fas fa-tachometer-alt"></i> Dashboard</a></li>
                <li><a href="news/index.php"><i class="fas fa-newspaper"></i> News</a></li>
                <li><a href="news/create.php"><i class="fas fa-plus"></i> Add News</a></li>
                <?php if (($_SESSION['account_type'] ?? '') === 'admin' || ($_SESSION['account_type'] ?? '') === 'superuser'): ?>
                    <li><a href="admin/"><i class="fas fa-cog"></i> Admin</a></li>
                <?php endif; ?>
                <li><a href="profile.php" class="active"><i class="fas fa-user"></i> Profile</a></li>
                <li><a href="logout.php"><i class="fas fa-sign-out-alt"></i> Logout</a></li>
            </ul>
        </nav>
    </header>

    <main class="container" style="margin-top: 2rem;">
        <?php if ($flash): ?>
            <div class="alert alert-<?php echo $flash['type']; ?>">
                <i class="fas fa-info-circle"></i> <?php echo $flash['message']; ?>
            </div>
        <?php endif; ?>

        <?php if ($error): ?>
            <div class="alert alert-error">
                <i class="fas fa-exclamation-circle"></i> <?php echo $error; ?>
            </div>
        <?php endif; ?>

        <?php if ($success): ?>
            <div class="alert alert-success">
                <i class="fas fa-check-circle"></i> <?php echo $success; ?>
            </div>
        <?php endif; ?>

        <div class="grid grid-2">
            <!-- Profile Information -->
            <div class="card">
                <div class="card-header">
                    <h2><i class="fas fa-user"></i> Profile Information</h2>
                </div>
                <div class="card-body">
                    <form method="POST" action="">
                        <input type="hidden" name="action" value="update_profile">
                        
                        <div class="form-group">
                            <label for="name" class="form-label">Full Name *</label>
                            <input type="text" id="name" name="name" class="form-control" 
                                   value="<?php echo htmlspecialchars($_POST['name'] ?? $current_user['name']); ?>" required>
                        </div>
                        
                        <div class="form-group">
                            <label for="email" class="form-label">Email *</label>
                            <input type="email" id="email" name="email" class="form-control" 
                                   value="<?php echo htmlspecialchars($_POST['email'] ?? $current_user['email']); ?>" required>
                        </div>
                        
                        <div class="form-group">
                            <label for="username" class="form-label">Username *</label>
                            <input type="text" id="username" name="username" class="form-control" 
                                   value="<?php echo htmlspecialchars($_POST['username'] ?? $current_user['username']); ?>" required>
                        </div>
                        
                        <div class="form-group">
                            <label for="telephone" class="form-label">Telephone</label>
                            <input type="tel" id="telephone" name="telephone" class="form-control" 
                                   value="<?php echo htmlspecialchars($_POST['telephone'] ?? $current_user['telephone']); ?>">
                        </div>
                        
                        <div class="form-group">
                            <label for="location_type" class="form-label">Location Type</label>
                            <select id="location_type" name="location_type" class="form-control form-select">
                                <option value="assembly" <?php echo ($_POST['location_type'] ?? $current_user['location_type']) === 'assembly' ? 'selected' : ''; ?>>Assembly</option>
                                <option value="district" <?php echo ($_POST['location_type'] ?? $current_user['location_type']) === 'district' ? 'selected' : ''; ?>>District</option>
                                <option value="area" <?php echo ($_POST['location_type'] ?? $current_user['location_type']) === 'area' ? 'selected' : ''; ?>>Area</option>
                            </select>
                        </div>
                        
                        <div class="form-group">
                            <label for="location_name" class="form-label">Location Name</label>
                            <input type="text" id="location_name" name="location_name" class="form-control" 
                                   value="<?php echo htmlspecialchars($_POST['location_name'] ?? $current_user['location_name'] ?? ''); ?>" 
                                   placeholder="e.g., Tema Assembly, Accra District">
                        </div>
                        
                        <div class="form-group">
                            <label for="address" class="form-label">Address</label>
                            <textarea id="address" name="address" class="form-control" rows="3"><?php echo htmlspecialchars($_POST['address'] ?? $current_user['address'] ?? ''); ?></textarea>
                        </div>
                        
                        <div class="form-group">
                            <label for="description" class="form-label">Description</label>
                            <textarea id="description" name="description" class="form-control" rows="3" 
                                      placeholder="Tell us about yourself..."><?php echo htmlspecialchars($_POST['description'] ?? $current_user['description'] ?? ''); ?></textarea>
                        </div>
                        
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-save"></i> Update Profile
                        </button>
                    </form>
                </div>
            </div>

            <!-- Change Password -->
            <div class="card">
                <div class="card-header">
                    <h2><i class="fas fa-lock"></i> Change Password</h2>
                </div>
                <div class="card-body">
                    <form method="POST" action="">
                        <input type="hidden" name="action" value="change_password">
                        
                        <div class="form-group">
                            <label for="old_password" class="form-label">Current Password *</label>
                            <input type="password" id="old_password" name="old_password" class="form-control" required>
                        </div>
                        
                        <div class="form-group">
                            <label for="new_password" class="form-label">New Password *</label>
                            <input type="password" id="new_password" name="new_password" class="form-control" required>
                            <small style="color: var(--primary-grey);">Minimum 6 characters</small>
                        </div>
                        
                        <div class="form-group">
                            <label for="confirm_password" class="form-label">Confirm New Password *</label>
                            <input type="password" id="confirm_password" name="confirm_password" class="form-control" required>
                        </div>
                        
                        <button type="submit" class="btn btn-warning">
                            <i class="fas fa-key"></i> Change Password
                        </button>
                    </form>
                </div>
            </div>
        </div>

        <!-- Account Information -->
        <div class="card mt-4">
            <div class="card-header">
                <h2><i class="fas fa-info-circle"></i> Account Information</h2>
            </div>
            <div class="card-body">
                <div class="grid grid-3">
                    <div>
                        <strong>Account Type:</strong><br>
                        <span class="badge badge-<?php echo $current_user['account_type']; ?>">
                            <?php echo ucfirst($current_user['account_type']); ?>
                        </span>
                    </div>
                    <div>
                        <strong>Member Since:</strong><br>
                        <?php echo date('F j, Y', strtotime($current_user['created_at'])); ?>
                    </div>
                    <div>
                        <strong>Last Updated:</strong><br>
                        <?php echo date('F j, Y', strtotime($current_user['updated_at'])); ?>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <style>
        .active {
            color: var(--primary-blue) !important;
            font-weight: 600;
        }
        .badge-user {
            background: var(--primary-blue);
            color: white;
        }
        .badge-admin {
            background: var(--warning);
            color: white;
        }
        .badge-superuser {
            background: var(--error);
            color: white;
        }
    </style>
</body>
</html>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists