<?php
require_once("connetti.php");
if (!isset($_POST['reg'])) {
    ?>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Modulo d'iscrizone</title>
        <center>   <H1>BENVENUTO NEL SITO D'ISCIRIZIONE PROVA</H1>
            <form action= '' method="POST">
                <font face="Comic Sans MS">
                Nome:     <input type="text" name="nome"  maxlength="30" size ="32"> <br> <br>
                Cognome:  <input type="text" name="cognome"  maxlength="30" size ="32"> <br> <br>
                Username: <input type="text" name="username"  maxlength="30" size ="32"> <br> <br>
                 Email:    <input type="text" name="email"  maxlength="40" size ="42"> <br> <br>
                 Rip-Email: <input type="text" name="ripemail"  maxlength="40" size ="42"> <br> <br>
                 Password: <input type="password" name="password"  maxlength="12" size ="14"> <br> <br>
                 Rip-Passw: <input type="password" name="passw"  maxlength="12" size ="14"> <br> <br>
                </font>
                <center>
                    <input type="submit" name="reg" value="Invio iscrizione" />
                    <input type="submit" name="uscita" value="Uscita" />
                </center> </form>
        </head>
        <body>
    <?php
    
} else {
    $nome = mysql_real_escape_string(strtolower(trim ($_POST['nome'])));
    $cognome = mysql_real_escape_string(strtolower(trim ($_POST['cognome'])));
    $username = mysql_real_escape_string(strtolower(trim ($_POST['username'])));
    $password = mysql_real_escape_string(trim ($_POST['password']));
    $passw = mysql_real_escape_string(trim ($_POST['passw']));
    $email = mysql_real_escape_string(trim ($_POST['email']));
    $ripemail = mysql_real_escape_string(trim ($_POST['ripemail']));
//crittografa la passowrod
$pass = mysql_real_escape_string(md5(trim($_POST['password'])));
 
// verifico la presenza dei campi obbligator
    
$ok_ko ="";
 //controllo campi compilati
if($nome ==""){$ok_ko .=  "Attenzione: il campo nome deve essere compilato. <br> <br>";}
if($cognome ==""){$ok_ko .=  "Attenzione: il campo cognome deve essere compilato. <br> <br>";}
if($username ==""){$ok_ko .=  "Attenzione: il campo username deve essere compilato. <br> <br>";}
if($email ==""){$ok_ko .=  "Attenzione: il campo email deve essere compilato. <br> <br>";}
if($password ==""){$ok_ko .=  "Attenzione: il campo password deve essere compilato. <br><br>";}
 
 
 if($password != $passw){$ok_ko .=  "Attenzione: le due password non coincidono. <br> <br>";}
 
// Controllo se lo username è presente nel db
$checkusername=mysql_query("SELECT username FROM utenti WHERE username='$username'");
$vuota=mysql_num_rows($checkusername);
if($vuota > 0){
	echo "<br> <br> <h1> Attenzione: lo username: ".$username." è già presente nell'archivio. Si prega di cambiare email. Grazie !!! </h1>";
echo "<meta http-equiv='Refresh' content=4; URL=".htmlspecialchars($_SERVER['PHP_SELF'])."'>";
exit();
}
  
//controllo scrittura corretta dell'email
 
 if(!filter_var($email, FILTER_VALIDATE_EMAIL)||!filter_var($ripemail,FILTER_VALIDATE_EMAIL)){
     
             $ok_ko .= "indirizzo email non valido<br>";
 //   } //chiude il controllo scrittura corretta dell'email 
        // Controlla se le due email coincidono
if($email != $ripemail){$ok_ko .=  "Attenzione: le due email non coincidono. <br> <br>";}
} //chiude il controllo scrittura corretta dell'email
// Controllo se esiste l'email nel db
$checkemail=mysql_query("SELECT email FROM utenti WHERE email='$email'");
$vuota=mysql_num_rows($checkemail);
if($vuota > 0){
	echo "<br> <br> <h1> Attenzione: l'email: ".$email." è già presente nell'archivio. Si prega di cambiare email. Grazie !!! </h1>";
echo "<meta http-equiv='Refresh' content=4; URL=".htmlspecialchars($_SERVER['PHP_SELF'])."'>";
exit();
}
if($ok_ko !=""){
		echo "<h1>$ok_ko</h1>";
		echo "<meta http-equiv='Refresh' content='3; URL=".htmlspecialchars($_SERVER['PHP_SELF'])."'>";
exit();
}else{
 
 $query="INSERT INTO utenti(nome, cognome, username, email, password)  VALUES('$nome','$cognome','$username','$email','$pass')";
//var_dump($query);//qui dovresti vedere se la query si forma come dovrebbe
$q_i=mysql_query($query);//qui se registra, se bool false errore, se resuorce... è a posto
var_dump($q_i);
   
// Parte riguardante l'invio email
//require_once ("/class.phpmailler.php");
require_once 'phpmailer/class.phpmailer.php';  
$mail  = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Username   = "@gmail.com";  // GMAIL username
$mail->Password   = "";            // GMAIL password
$mail->SetFrom("no_replay@myserver.org");
 
$mail->Subject    = "Attivazione account.";
//$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->CharSet = "UTF-8";  //Content-Type" content="text/html;    
$mail->AddAddress($_POST['email']);
//$mail->MsgHTML('Grazie di esserti iscritto.  <a href="http://utenti/fabio/MegaLaboratorio/login.php?username=' . $_GET['username'] .'&password=' . $_GET['pass'].'">clicca qui per attivare</a></b>'." "."per attviare l'account. Grazie."); 
$mail->MsgHTML('Grazie di esserti iscritto.  <a href="http://utenti/fabio/MegaLaboratorio/login.php?username=' . $_POST['username'] .'&password=' . $_POST['pass'].'">clicca qui per attivare</a></b>'." "."per attviare l'account. Grazie."); 
if(!$mail->Send()) {
    echo "Errore";
} else {
    echo "<h1> <br>Utente registrato correttamente. E' stata inviata un'email di conferma per attivare l'account</h1>";
    header("Refresh: 10;URL=login.php");
}
      
}
}
?>