<?php
// compila con la tua e-mail
$your_email = 'admin@localhost';
// inserisci i tornei disponibili
$challenges = array('1000', '1001', '1002');
function show_errors()
{
global $errors;
echo <<<EOF
<html>
<head>
<title>Errore iscrizione</title>
</head>
<body>
<h1>Errore</h1>
<p>Si sono verificati degli errori:</p>
<ul>
EOF;
foreach($errors as $err)
echo "\t\t<li>{$err}</li>\n";
echo <<<EOF
</ul>
<p><a href="{$_SERVER['PHP_SELF']}">Torna indietro</a></p>
</body>
</html>
EOF;
exit();
}
if(isset($_POST['submit']))
{
$name = isset($_POST['name']) ? trim($_POST['name']) : '';
$surname = isset($_POST['surname']) ? trim($_POST['surname']) : '';
$city = isset($_POST['city']) ? trim($_POST['city']) : '';
$telephone = isset($_POST['telephone']) ? trim($_POST['telephone']) : '';
$email = isset($_POST['email']) ? trim($_POST['email']) : '';
$challenge = isset($_POST['challenge']) ? trim($_POST['challenge']) : '';
$message = isset($_POST['message']) ? trim($_POST['message']) : '';
$errors = array();
if($name == '')
$errors[] = 'Non hai inserito il tuo nome.';
if($surname == '')
$errors[] = 'Non hai inserito il tuo cognome.';
if($city == '')
$errors[] = 'Non hai inserito la tua città.';
if(!is_numeric($telephone))
$errors[] = 'Il numero di telefono non è numerico.';
if(!preg_match('/^[\w][\w\.-]*[\w]@[\w][\w\.-]*[\w]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/', $email))
$errors[] = 'L\'indirizzo e-mail inserito non è valido.';
if(!in_array($challenge, $challenges))
$errors[] = 'Il torneo inserito non &egrqave; valido.';
if(count($errors) > 0)
show_errors();
$subject = 'Nuova iscrizione';
$body = "Una nuova iscrizione è stata richiesta inserendo i seguenti dati:\n";
$body .= "------------------------------------------\n";
$body .= "Nome: {$name}\n";
$body .= "Cognome: {$surname}\n";
$body .= "Città: {$city}\n";
$body .= "Telefono: {$telephone}\n";
$body .= "Torneo: {$challenge}\n";
$body .= "------------------------------------------\n";
$body .= !empty($message) ? "Messaggio: {$message}\n" : '';
$body .= !empty($message) ? "------------------------------------------\n" : '';
$body .= "L'indirizzo IP da cui è stata effettuata la richiesta è {$_SERVER['REMOTE_ADDR']}.";
ob_start();
if(!mail($your_email, $subject, $message))
$errors[] = 'Impossibile inviare il messaggio. Riprova più tardi.';
ob_end_clean();
if(count($errors) > 0)
show_errors();
echo <<<EOF
<html>
<head>
<title>Iscrizione effettuata</title>
</head>
<body>
<h1>Iscrizione effettuata</h1>
<p>La tua iscrizione al torneo <b>{$challenge}</b> è stata effettuata con successo.</p>
</body>
</html>
EOF;
}
else
{
echo <<<EOF
<html>
<head>
<title>Iscrizione</title>
</head>
<body>
<h1>Iscrizione</h1>
<p>Per effettuare l'iscrizione compila i campi sottostanti.</p>
<form action="{$_SERVER['PHP_SELF']}" method="post">
<table cellspacing="10">
<tr>
<td><label>Nome:</label></td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td><label>Cognome:</label></td>
<td><input type="text" name="surname" /></td>
</tr>
<tr>
<td><label>Città:</label></td>
<td><input type="text" name="city" /></td>
</tr>
<tr>
<td><label>Telefono:</label></td>
<td><input type="text" name="telephone" /></td>
</tr>
<tr>
<td><label>E-mail:</label></td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td><label>Torneo:</label></td>
<td>
<select name="challenge">
<option value="1000">1000</option>
<option value="1001">1001</option>
<option value="1002">1002</option>
</select>
</td>
</tr>
<tr>
<td><label>Messaggio:</label></td>
<td><textarea name="message" cols="50" rows="5"></textarea></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Iscriviti" /></td>
<td><input type="reset" name="reset" value="Reimposta" /></td>
</tr>
</table>
</form>
</body>
</html>
EOF;
}
?>