Files
web_work/public/profile.php
kirill.khorkov f4f57bd153 fix
2025-12-17 01:24:01 +03:00

262 lines
9.6 KiB
PHP

<?php
session_start();
require_once __DIR__ . '/config/database.php';
if (!isset($_SESSION['isLoggedIn']) || $_SESSION['isLoggedIn'] !== true) {
header('Location: login.php?error=auth_required');
exit();
}
$user_id = $_SESSION['user_id'] ?? 0;
$db = Database::getInstance()->getConnection();
$user = null;
try {
$stmt = $db->prepare("SELECT user_id, email, full_name, phone, city FROM users WHERE user_id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$error = "Ошибка загрузки данных: " . $e->getMessage();
}
if (!$user) {
header('Location: login.php?error=user_not_found');
exit();
}
$update_errors = $_SESSION['update_errors'] ?? [];
$update_success = $_SESSION['update_success'] ?? '';
unset($_SESSION['update_errors']);
unset($_SESSION['update_success']);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$full_name = trim($_POST['fio'] ?? '');
$city = trim($_POST['city'] ?? '');
$phone = trim($_POST['phone'] ?? '');
$password = $_POST['password'] ?? '';
$confirm_password = $_POST['confirm-password'] ?? '';
$errors = [];
if (empty($full_name) || strlen($full_name) < 3) {
$errors[] = 'ФИО должно содержать минимум 3 символа';
}
if (empty($city) || strlen($city) < 2) {
$errors[] = 'Введите корректное название города';
}
if (empty($phone) || !preg_match('/^(\+7|8)[\s-]?\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{2}[\s-]?\d{2}$/', $phone)) {
$errors[] = 'Введите корректный номер телефона';
}
if (!empty($password)) {
if (strlen($password) < 6) {
$errors[] = 'Пароль должен содержать минимум 6 символов';
}
if ($password !== $confirm_password) {
$errors[] = 'Пароли не совпадают';
}
}
if (empty($errors)) {
try {
if (!empty($password)) {
$password_hash = password_hash($password, PASSWORD_DEFAULT);
$stmt = $db->prepare("
UPDATE users
SET full_name = ?, phone = ?, city = ?, password_hash = ?, updated_at = CURRENT_TIMESTAMP
WHERE user_id = ?
");
$stmt->execute([$full_name, $phone, $city, $password_hash, $user_id]);
} else {
$stmt = $db->prepare("
UPDATE users
SET full_name = ?, phone = ?, city = ?, updated_at = CURRENT_TIMESTAMP
WHERE user_id = ?
");
$stmt->execute([$full_name, $phone, $city, $user_id]);
}
$_SESSION['full_name'] = $full_name;
$_SESSION['user_phone'] = $phone;
$_SESSION['user_city'] = $city;
$_SESSION['update_success'] = 'Профиль успешно обновлен!';
header('Location: profile.php');
exit();
} catch (PDOException $e) {
$errors[] = 'Ошибка обновления: ' . $e->getMessage();
}
}
if (!empty($errors)) {
$_SESSION['update_errors'] = $errors;
header('Location: profile.php');
exit();
}
}
?>
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<base href="/cite_practica/">
<title>AETERNA - Мой профиль</title>
<link rel="stylesheet/less" type="text/css" href="style_for_cite.less">
<script src="https://cdn.jsdelivr.net/npm/less"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
<style>
body {
padding-bottom: 120px;
}
.profile-page-main {
min-height: calc(100vh - 0px);
}
.update-errors {
background: #f8d7da;
color: #721c24;
padding: 15px;
border-radius: 5px;
margin: 20px auto;
max-width: 800px;
border: 1px solid #f5c6cb;
}
.update-success {
background: #d4edda;
color: #155724;
padding: 15px;
border-radius: 5px;
margin: 20px auto;
max-width: 800px;
border: 1px solid #c3e6cb;
}
</style>
</head>
<body>
<?php include 'header_common.php'; ?>
<main class="profile-page-main">
<?php if (!empty($update_errors)): ?>
<div class="update-errors">
<h4><i class="fas fa-exclamation-circle"></i> Ошибки:</h4>
<ul>
<?php foreach ($update_errors as $error): ?>
<li><?= htmlspecialchars($error) ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<?php if (!empty($update_success)): ?>
<div class="update-success">
<i class="fas fa-check-circle"></i> <?= htmlspecialchars($update_success) ?>
</div>
<?php endif; ?>
<div class="profile-container">
<div class="profile-left-col">
<div class="logo" style="color: white;">AETERNA</div>
<div style="margin-top: 30px; color: rgba(255,255,255,0.8);">
<h3 style="margin-bottom: 15px; font-size: 18px;">Мой профиль</h3>
<p style="font-size: 14px; line-height: 1.5;">
Управляйте своими данными и настройками аккаунта
</p>
</div>
</div>
<div class="profile-right-col">
<div class="profile-form-block">
<h2>РЕДАКТИРОВАНИЕ ПРОФИЛЯ</h2>
<form class="profile-form" method="POST" id="profileForm">
<div class="input-group">
<label for="email">E-mail</label>
<input type="email" id="email" value="<?= htmlspecialchars($user['email']) ?>" disabled>
<small style="color: #666; font-size: 12px;">E-mail нельзя изменить</small>
</div>
<div class="input-group">
<label for="fio">ФИО *</label>
<input type="text" id="fio" name="fio" placeholder="Введите ваше ФИО"
value="<?= htmlspecialchars($user['full_name'] ?? '') ?>" required>
</div>
<div class="input-group">
<label for="city">Город *</label>
<input type="text" id="city" name="city" placeholder="Укажите ваш город"
value="<?= htmlspecialchars($user['city'] ?? '') ?>" required>
</div>
<div class="input-group">
<label for="phone">Телефон *</label>
<input type="tel" id="phone" name="phone" placeholder="+7(912)999-12-23"
value="<?= htmlspecialchars($user['phone'] ?? '') ?>" required>
</div>
<div class="input-group">
<label for="password">Новый пароль (оставьте пустым, если не хотите менять)</label>
<input type="password" id="password" name="password" placeholder="Придумайте новый пароль">
</div>
<div class="input-group">
<label for="confirm-password">Подтвердите новый пароль</label>
<input type="password" id="confirm-password" name="confirm-password" placeholder="Повторите пароль">
</div>
<button type="submit" class="btn primary-btn save-btn">
Сохранить изменения
</button>
</form>
</div>
</div>
</div>
</main>
<?php include 'footer.php'; ?>
<script>
$(document).ready(function() {
$('#phone').on('input', function() {
let value = $(this).val().replace(/\D/g, '');
if (value.length > 0) {
if (!value.startsWith('7') && !value.startsWith('8')) {
value = '7' + value;
}
if (value.startsWith('8')) {
value = '7' + value.substring(1);
}
let formatted = '+7';
if (value.length > 1) formatted += ' (' + value.substring(1, 4);
if (value.length > 4) formatted += ') ' + value.substring(4, 7);
if (value.length > 7) formatted += '-' + value.substring(7, 9);
if (value.length > 9) formatted += '-' + value.substring(9, 11);
$(this).val(formatted);
}
});
$('#profileForm').on('submit', function(e) {
const password = $('#password').val();
const confirmPassword = $('#confirm-password').val();
if (password && password !== confirmPassword) {
e.preventDefault();
alert('Пароли не совпадают');
return false;
}
if (password && password.length < 6) {
e.preventDefault();
alert('Пароль должен содержать минимум 6 символов');
return false;
}
});
});
</script>
</body>
</html>