<?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 'phpmailler/class.phpmailer.php';  
$mail  = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.libero.it";
#Qui c'è il debug dell'smtp, se metti un commento non stampa diavolerie, ma è utile in fase di test per capire la natura di qualche errore
$mail->SMTPDebug  = "2";                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = "true";                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = "465";                   // set the SMTP port for the GMAIL server
$mail->Username   = "paperinik4@gmail.com";  // GMAIL username
$mail->Password   = "xxxxxxxxxxxxx";            // GMAIL password
 $mail->SetFrom($_POST['email']);
 
$mail->Subject    = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML('Questa è una prova in html <b>prova invio rmail</b>');
if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}
echo "<h1> <br>Utente registrato correttamente. E' stata inviata un'email di conferma per attivare l'account</h1>";
//e torno al form 
//header("Refresh: 2;URL=paginaris.php");     
header("Refresh: 2;URL=login.php");          
}
}
?>