133 lines
7.0 KiB
PHP
133 lines
7.0 KiB
PHP
<?php
|
|
require_once __DIR__ . '/includes/auth.php';
|
|
require_login();
|
|
|
|
$user = get_logged_in_user();
|
|
if (!$user) {
|
|
header('Location: logout.php');
|
|
exit;
|
|
}
|
|
|
|
$success = '';
|
|
$error = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$name = trim($_POST['name'] ?? '');
|
|
$email = trim($_POST['email'] ?? '');
|
|
$phone = trim($_POST['phone'] ?? '');
|
|
$password = $_POST['password'] ?? '';
|
|
$password_confirm = $_POST['password_confirm'] ?? '';
|
|
|
|
$profile_picture = $user['profile_picture'] ?? null;
|
|
|
|
// Handle image upload
|
|
if (isset($_FILES['profile_picture']) && $_FILES['profile_picture']['error'] === UPLOAD_ERR_OK) {
|
|
$allowed_types = ['image/jpeg', 'image/png', 'image/webp', 'image/gif'];
|
|
if (in_array($_FILES['profile_picture']['type'], $allowed_types)) {
|
|
$ext = pathinfo($_FILES['profile_picture']['name'], PATHINFO_EXTENSION);
|
|
$filename = 'user_' . $user['id'] . '_' . time() . '.' . $ext;
|
|
$upload_dir = __DIR__ . '/assets/images/uploads/';
|
|
if (!is_dir($upload_dir)) {
|
|
mkdir($upload_dir, 0775, true);
|
|
}
|
|
$dest = $upload_dir . $filename;
|
|
if (move_uploaded_file($_FILES['profile_picture']['tmp_name'], $dest)) {
|
|
$profile_picture = 'assets/images/uploads/' . $filename;
|
|
} else {
|
|
$error = t('Failed to move uploaded file.', 'فشل نقل الملف المرفوع.');
|
|
}
|
|
} else {
|
|
$error = t('Invalid image format. Only JPG, PNG, WEBP, and GIF are allowed.', 'تنسيق الصورة غير صالح. يُسمح فقط بـ JPG و PNG و WEBP و GIF.');
|
|
}
|
|
}
|
|
|
|
if (empty($name) || empty($email)) {
|
|
if (!$error) $error = t('Name and email are required.', 'الاسم والبريد الإلكتروني مطلوبان.');
|
|
} elseif ($password !== $password_confirm) {
|
|
if (!$error) $error = t('Passwords do not match.', 'كلمتا المرور غير متطابقتين.');
|
|
} elseif (!$error) {
|
|
$stmt = db()->prepare("SELECT id FROM users WHERE email = ? AND id != ?");
|
|
$stmt->execute([$email, $user['id']]);
|
|
if ($stmt->fetchColumn()) {
|
|
$error = t('Email already taken.', 'البريد الإلكتروني مستخدم بالفعل.');
|
|
} else {
|
|
if ($password) {
|
|
$hash = password_hash($password, PASSWORD_DEFAULT);
|
|
$update = db()->prepare("UPDATE users SET name = ?, email = ?, phone = ?, profile_picture = ?, password = ? WHERE id = ?");
|
|
$update->execute([$name, $email, $phone, $profile_picture, $hash, $user['id']]);
|
|
} else {
|
|
$update = db()->prepare("UPDATE users SET name = ?, email = ?, phone = ?, profile_picture = ? WHERE id = ?");
|
|
$update->execute([$name, $email, $phone, $profile_picture, $user['id']]);
|
|
}
|
|
$success = t('Profile updated successfully.', 'تم تحديث الملف الشخصي بنجاح.');
|
|
$user = get_logged_in_user(); // Refresh
|
|
}
|
|
}
|
|
}
|
|
|
|
render_head(t('My Profile', 'الملف الشخصي'));
|
|
render_nav('profile.php');
|
|
?>
|
|
<main class="py-5 bg-light min-vh-100">
|
|
<div class="container">
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-8 col-lg-6">
|
|
<div class="card border-0 shadow-sm" style="border-radius: 1rem;">
|
|
<div class="card-body p-4 p-md-5">
|
|
|
|
<div class="d-flex align-items-center justify-content-between mb-4">
|
|
<h1 class="h4 mb-0 fw-bold"><?= h(t('My Profile', 'الملف الشخصي')) ?></h1>
|
|
<?php if (!empty($user['profile_picture'])): ?>
|
|
<img src="<?= h($user['profile_picture']) ?>?v=<?= time() ?>" alt="Profile Picture" class="rounded-circle shadow-sm" style="width: 60px; height: 60px; object-fit: cover;">
|
|
<?php else: ?>
|
|
<div class="rounded-circle shadow-sm bg-secondary text-white d-flex align-items-center justify-content-center" style="width: 60px; height: 60px;">
|
|
<?= h(strtoupper(mb_substr($user['name'] ?? '?', 0, 1, 'UTF-8'))) ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<?php if ($success): ?>
|
|
<div class="alert alert-success py-2 small"><?= h($success) ?></div>
|
|
<?php endif; ?>
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger py-2 small"><?= h($error) ?></div>
|
|
<?php endif; ?>
|
|
<form method="post" action="profile.php" enctype="multipart/form-data">
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label small fw-semibold"><?= h(t('Profile Picture', 'صورة الملف الشخصي')) ?></label>
|
|
<input type="file" name="profile_picture" class="form-control" accept="image/*">
|
|
<div class="form-text small"><?= h(t('Leave blank to keep current picture.', 'اتركه فارغاً للاحتفاظ بالصورة الحالية.')) ?></div>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label small fw-semibold"><?= h(t('Full Name', 'الاسم الكامل')) ?></label>
|
|
<input type="text" name="name" class="form-control" value="<?= h($user['name']) ?>" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label small fw-semibold"><?= h(t('Email address', 'البريد الإلكتروني')) ?></label>
|
|
<input type="email" name="email" class="form-control" value="<?= h($user['email']) ?>" required>
|
|
</div>
|
|
<div class="mb-4">
|
|
<label class="form-label small fw-semibold"><?= h(t('Telephone Number', 'رقم الهاتف')) ?></label>
|
|
<input type="tel" name="phone" class="form-control" value="<?= h($user['phone'] ?? '') ?>">
|
|
</div>
|
|
|
|
<h5 class="h6 mb-3 fw-bold border-top pt-4"><?= h(t('Change Password', 'تغيير كلمة المرور')) ?> <small class="text-secondary fw-normal"><?= h(t('(Optional)', '(اختياري)')) ?></small></h5>
|
|
<div class="mb-3">
|
|
<label class="form-label small fw-semibold"><?= h(t('New Password', 'كلمة المرور الجديدة')) ?></label>
|
|
<input type="password" name="password" class="form-control" placeholder="••••••••">
|
|
</div>
|
|
<div class="mb-4">
|
|
<label class="form-label small fw-semibold"><?= h(t('Confirm New Password', 'تأكيد كلمة المرور')) ?></label>
|
|
<input type="password" name="password_confirm" class="form-control" placeholder="••••••••">
|
|
</div>
|
|
<button type="submit" class="btn btn-dark w-100"><?= h(t('Save Changes', 'حفظ التغييرات')) ?></button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
<?php render_footer(); ?>
|