diff --git a/admin/includes/header.php b/admin/includes/header.php index f11b1fc..b8c3b0f 100644 --- a/admin/includes/header.php +++ b/admin/includes/header.php @@ -579,7 +579,7 @@ function can_view($module) {
  • My Profile
  • Company Settings
  • -
  • "> Logout
  • +
  • Logout
  • diff --git a/admin/profile.php b/admin/profile.php index e01a1c8..c464fee 100644 --- a/admin/profile.php +++ b/admin/profile.php @@ -2,151 +2,197 @@ require_once __DIR__ . '/../db/config.php'; require_once __DIR__ . '/../includes/functions.php'; -init_session(); +require_login(); +$pdo = db(); +$currentUser = get_logged_user(); +$id = $currentUser['id']; -// Ensure login -if (!get_logged_user()) { - header('Location: ' . url('login.php')); +// Always fetch fresh data from DB +$stmt = $pdo->prepare("SELECT u.*, g.name as group_name, g.permissions + FROM users u + LEFT JOIN user_groups g ON u.group_id = g.id + WHERE u.id = ?"); +$stmt->execute([$id]); +$user = $stmt->fetch(PDO::FETCH_ASSOC); + +if (!$user) { + logout_user(); + header('Location: /login.php'); exit; } -$user = get_logged_user(); -$pdo = db(); -$error = ''; -$success = ''; +$message = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { - $full_name = $_POST['full_name'] ?? ''; - $email = $_POST['email'] ?? ''; - $new_password = $_POST['new_password'] ?? ''; - $confirm_password = $_POST['confirm_password'] ?? ''; - $profile_pic = $user['profile_pic'] ?? null; - - // Profile Pic Upload - if (isset($_FILES['profile_pic']) && $_FILES['profile_pic']['error'] === UPLOAD_ERR_OK) { - $upload_dir = __DIR__ . '/../assets/images/users/'; - if (!is_dir($upload_dir)) mkdir($upload_dir, 0777, true); - - $ext = pathinfo($_FILES['profile_pic']['name'], PATHINFO_EXTENSION); - $filename = 'user_' . $user['id'] . '_' . uniqid() . '.' . $ext; - $target = $upload_dir . $filename; - - if (move_uploaded_file($_FILES['profile_pic']['tmp_name'], $target)) { - $profile_pic = 'assets/images/users/' . $filename; - } - } - + $full_name = $_POST['full_name']; + $email = $_POST['email']; + + $pdo->beginTransaction(); try { - if (!empty($new_password)) { - if ($new_password !== $confirm_password) { - $error = 'Passwords do not match.'; - } else { - $hashed_pass = password_hash($new_password, PASSWORD_DEFAULT); - $stmt = $pdo->prepare("UPDATE users SET full_name = ?, email = ?, password = ?, profile_pic = ? WHERE id = ?"); - $stmt->execute([$full_name, $email, $hashed_pass, $profile_pic, $user['id']]); - $success = 'Profile and password updated successfully.'; - } - } else { - $stmt = $pdo->prepare("UPDATE users SET full_name = ?, email = ? , profile_pic = ? WHERE id = ?"); - $stmt->execute([$full_name, $email, $profile_pic, $user['id']]); - $success = 'Profile updated successfully.'; + $sql = "UPDATE users SET full_name = ?, email = ? WHERE id = ?"; + $params = [$full_name, $email, $id]; + + $stmt = $pdo->prepare($sql); + $stmt->execute($params); + + // Update password if provided + if (!empty($_POST['password'])) { + $password = password_hash($_POST['password'], PASSWORD_DEFAULT); + $pdo->prepare("UPDATE users SET password = ? WHERE id = ?")->execute([$password, $id]); } - if (empty($error)) { - // Update session data - $stmt = $pdo->prepare("SELECT u.*, g.name as group_name, g.permissions FROM users u LEFT JOIN user_groups g ON u.group_id = g.id WHERE u.id = ?"); - $stmt->execute([$user['id']]); - $_SESSION['user'] = $stmt->fetch(PDO::FETCH_ASSOC); - $user = $_SESSION['user']; + // Handle Profile Picture Upload + if (isset($_FILES['profile_pic']) && $_FILES['profile_pic']['error'] === UPLOAD_ERR_OK) { + $upload_dir = __DIR__ . '/../assets/images/users/'; + if (!is_dir($upload_dir)) { + mkdir($upload_dir, 0775, true); + } + + $file_tmp = $_FILES['profile_pic']['tmp_name']; + $file_name = $_FILES['profile_pic']['name']; + $file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION)); + $allowed_exts = ['jpg', 'jpeg', 'png', 'gif', 'webp']; + + if (in_array($file_ext, $allowed_exts)) { + $new_file_name = 'user_' . $id . '_' . uniqid() . '.' . $file_ext; + $upload_path = $upload_dir . $new_file_name; + + if (move_uploaded_file($file_tmp, $upload_path)) { + // Delete old profile pic if exists + if ($user['profile_pic'] && file_exists(__DIR__ . '/../' . $user['profile_pic'])) { + unlink(__DIR__ . '/../' . $user['profile_pic']); + } + + $profile_pic_path = 'assets/images/users/' . $new_file_name; + $pdo->prepare("UPDATE users SET profile_pic = ? WHERE id = ?")->execute([$profile_pic_path, $id]); + } + } } + + $pdo->commit(); + $message = '
    Profile updated successfully!
    '; + + // Refresh user data and update session + $stmt->execute([$id]); + $user = $stmt->fetch(PDO::FETCH_ASSOC); + $_SESSION['user'] = $user; + unset($_SESSION['user']['password']); + } catch (Exception $e) { - $error = 'Error: ' . $e->getMessage(); + $pdo->rollBack(); + $message = '
    Error updating profile: ' . $e->getMessage() . '
    '; } } -include __DIR__ . '/includes/header.php'; +include 'includes/header.php'; ?> -
    -
    -
    -
    -
    -
    My Profile
    -
    -
    - -