80 lines
2.7 KiB
PHP
Executable File
80 lines
2.7 KiB
PHP
Executable File
<?php
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
require_once __DIR__ . '/../init.php';
|
|
require_once __DIR__ . '/../send_email.php';
|
|
|
|
if (is_logged_in()) header('Location: ../game/dashboard.php');
|
|
|
|
$errors = [];
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$email = filter_var($_POST['email'] ?? '', FILTER_VALIDATE_EMAIL);
|
|
$pass = $_POST['password'] ?? '';
|
|
$pass2 = $_POST['password_confirm'] ?? '';
|
|
|
|
if (!$email) $errors[] = 'Inserisci un indirizzo email valido.';
|
|
if (strlen($pass) < 8) $errors[] = 'La password deve essere almeno 8 caratteri.';
|
|
if ($pass !== $pass2) $errors[] = 'Le password non coincidono.';
|
|
|
|
if (empty($errors)) {
|
|
$pdo = getPDO();
|
|
$stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
|
|
$stmt->execute([$email]);
|
|
if ($stmt->fetch()) {
|
|
$errors[] = 'Email già registrata.';
|
|
} else {
|
|
$hash = password_hash($pass, PASSWORD_DEFAULT);
|
|
$stmt = $pdo->prepare("INSERT INTO users (email, password_hash) VALUES (?, ?)");
|
|
$stmt->execute([$email, $hash]);
|
|
|
|
// Invia mail di benvenuto
|
|
$subject = "Benvenuto su Diplomacy Web App";
|
|
$body = "<p>Ciao ".htmlspecialchars($email).",</p>
|
|
<p>Grazie per esserti registrato su Diplomacy Web App. Ora puoi iniziare a giocare con i tuoi amici!</p>
|
|
<p>— Il team di Diplomacy</p>";
|
|
sendEmail($email, $subject, $body);
|
|
|
|
// Login automatico
|
|
$_SESSION['user_id'] = $pdo->lastInsertId();
|
|
$_SESSION['user_email'] = $email;
|
|
header('Location: ../game/dashboard.php');
|
|
exit;
|
|
}
|
|
}
|
|
}
|
|
|
|
$page_title = 'Registrati';
|
|
include __DIR__ . '/../header.php';
|
|
?>
|
|
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-6">
|
|
<h2>Registrazione</h2>
|
|
|
|
<?php if(!empty($errors)): ?>
|
|
<div class="alert alert-danger">
|
|
<ul><?php foreach($errors as $e) echo "<li>".htmlspecialchars($e)."</li>"; ?></ul>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<form method="post" novalidate>
|
|
<div class="mb-3">
|
|
<label>Email</label>
|
|
<input type="email" name="email" class="form-control" required value="<?= htmlspecialchars($_POST['email'] ?? '') ?>">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label>Password</label>
|
|
<input type="password" name="password" class="form-control" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label>Conferma Password</label>
|
|
<input type="password" name="password_confirm" class="form-control" required>
|
|
</div>
|
|
<button class="btn btn-primary">Registrati</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include __DIR__ . '/../footer.php'; ?>
|