getMessage());
}
$user_id = $_SESSION['user_id'];
$error = '';
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/account_suspension.php';
$suspensionState = og_is_current_user_suspended($pdo);
if (!empty($suspensionState['is_suspended'])) {
header('Location: /account/settings/?error=' . urlencode('Twoje konto jest zawieszone. Zmiana adresu email jest zablokowana.'));
exit();
}
// Sprawdź czy konto nie jest zawieszone
try {
$suspendCheck = $pdo->prepare("SELECT account_suspended FROM users WHERE id = ? LIMIT 1");
$suspendCheck->execute([$user_id]);
$suspendRow = $suspendCheck->fetch(PDO::FETCH_ASSOC);
if ($suspendRow && (int)($suspendRow['account_suspended'] ?? 0) === 1) {
header('Location: /account/profile/?error=' . urlencode('Twoje konto jest zawieszone. Nie możesz zmieniać adresu email.'));
exit();
}
} catch (Throwable $e) {
// Ignoruj jeśli kolumna nie istnieje
}
// Pobranie danych użytkownika
$stmt = $pdo->prepare("SELECT email FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$userData = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$userData) {
die("Nie znaleziono użytkownika");
}
// Walidacja nowego emaila
function validateEmail($email) {
if (empty($email)) {
return "Email jest wymagany";
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
return "Nieprawidłowy format adresu email";
}
if (strlen($email) > 255) {
return "Email jest za długi (max 255 znaków)";
}
return null;
}
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$new_email = trim($_POST["new_email"] ?? "");
$validation_error = validateEmail($new_email);
if ($validation_error) {
$error = $validation_error;
} elseif (strtolower($new_email) === strtolower($userData['email'])) {
$error = "Nowy email nie może być taki sam jak obecny email.";
} else {
// Sprawdź czy email nie jest już zajęty
$check = $pdo->prepare("SELECT id FROM users WHERE LOWER(email) = LOWER(?) AND id != ?");
$check->execute([$new_email, $user_id]);
if ($check->fetch()) {
$error = "Ten adres email jest już zajęty.";
} else {
// Generowanie 6-cyfrowego kodu
$reset_code = str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
$reset_expires = date('Y-m-d H:i:s', strtotime('+15 minutes'));
// Zapisanie kodu w bazie
try {
$update = $pdo->prepare("UPDATE users SET email_change_code = ?, email_change_expires = ?, new_email = ? WHERE id = ?");
$update->execute([$reset_code, $reset_expires, $new_email, $user_id]);
} catch (PDOException $e) {
die("Błąd aktualizacji bazy: " . $e->getMessage() . "
Czy dodałeś kolumny email_change_code, email_change_expires i new_email do tabeli users?
Wykonaj w phpMyAdmin:
ALTER TABLE users\nADD COLUMN email_change_code VARCHAR(6) NULL,\nADD COLUMN email_change_expires DATETIME NULL,\nADD COLUMN new_email VARCHAR(255) NULL;"); } // Wysłanie emaila z kodem NA NOWY ADRES require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/smtp_helper.php'; $subject = "Kod weryfikacyjny - Wspólnie"; $message = "
Otrzymaliśmy prośbę o zmianę adresu email na to konto w serwisie Wspólnie.
Twój kod weryfikacyjny to:
Kod jest ważny przez 15 minut.
Jeśli to nie Ty zażądałeś tej zmiany, zignoruj tę wiadomość.
Wprowadź nowy adres email