87 lines
4.1 KiB
PHP
87 lines
4.1 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'] ?? '');
|
|
$password = $_POST['password'] ?? '';
|
|
$password_confirm = $_POST['password_confirm'] ?? '';
|
|
|
|
if (empty($name) || empty($email)) {
|
|
$error = t('Name and email are required.', 'الاسم والبريد الإلكتروني مطلوبان.');
|
|
} elseif ($password !== $password_confirm) {
|
|
$error = t('Passwords do not match.', 'كلمتا المرور غير متطابقتين.');
|
|
} else {
|
|
$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 = ?, password = ? WHERE id = ?");
|
|
$update->execute([$name, $email, $hash, $user['id']]);
|
|
} else {
|
|
$update = db()->prepare("UPDATE users SET name = ?, email = ? WHERE id = ?");
|
|
$update->execute([$name, $email, $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">
|
|
<h1 class="h4 mb-4 fw-bold"><?= h(t('My Profile', 'الملف الشخصي')) ?></h1>
|
|
<?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">
|
|
<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-4">
|
|
<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>
|
|
<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(); ?>
|