<?php
session_start();

include("../includes/connect.php");
require '../includes/Exception.php';


function generateRandomString($length = 10) {
    return substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)) )),1,$length);
}

$pwd = generateRandomString(); 
/*require '../includes/PHPMailer.php';
require '../includes/SMTP.php';*/

//$dominio = "www.concorso-per-te.com"; // modificare per variare il link di conferma email

$valori = filter_input(INPUT_POST,'values',FILTER_DEFAULT,FILTER_REQUIRE_ARRAY);

//var_dump($valori);


$cid = $valori['cid'];
$eid = $valori['eid'];
$nome = $valori['firstName'];
$cognome= $valori['lastName'];
$sesso = $valori['gender'];
$nascita = $valori['birthDateYear'] . "-" . $valori['birthDateMonth'] . "-" . $valori['birthDateDay'];
$email = $valori['email'];
$email=strtolower($email);
$privacy = isset($valori['termsAgree']) ? $valori['termsAgree'] : 0;
$privacy2 = isset($valori['termsAgree2']) ? $valori['termsAgree2'] : 0;
$privacy3 = isset($valori['termsAgree3']) ? $valori['termsAgree3'] : 0;
$privacy4 = isset($valori['termsAgree4']) ? $valori['termsAgree4'] : 0;
$privacy5 = isset($valori['termsAgree5']) ? $valori['termsAgree5'] : 0;
$privacy6 = isset($valori['termsAgree6']) ? $valori['termsAgree6'] : 0;
$privacy7 = isset($valori['termsAgree7']) ? $valori['termsAgree7'] : 0;
$privacy8 = isset($valori['termsAgree8']) ? $valori['termsAgree8'] : 0;
$ip = $_SERVER['REMOTE_ADDR'];
$timestamp = time();

$tabellalead = "campaign" . $cid . "_leads";

//var_dump($tabellalead);

if((isset($_SESSION["aff_sub"]))&&(!empty($_SESSION["aff_sub"]))){
    
    $utm=1;
    $queryLead = $connection->prepare("INSERT INTO $tabellalead (campaignId,externalid,nome,cognome,sesso,nascita,email,privacy,privacy2,privacy3,privacy4,privacy5,privacy6,privacy7,privacy8,ip,timestamp,utmCampain) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");


    $queryLead->execute(["$cid","$eid","$nome","$cognome","$sesso","$nascita","$email","$privacy","$privacy2","$privacy3","$privacy4","$privacy5","$privacy6","$privacy7","$privacy8","$ip","$timestamp","$utm"]);
            
        
   
}else{

$queryLead = $connection->prepare("INSERT INTO $tabellalead (campaignId,externalid,nome,cognome,sesso,nascita,email,privacy,privacy2,privacy3,privacy4,privacy5,privacy6,privacy7,privacy8,ip,timestamp) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");


$queryLead->execute(["$cid","$eid","$nome","$cognome","$sesso","$nascita","$email","$privacy","$privacy2","$privacy3","$privacy4","$privacy5","$privacy6","$privacy7","$privacy8","$ip","$timestamp"]);
}
 $_SESSION["sale"]=$timestamp;
 $_SESSION["indir"]=$email;
 $_SESSION["eid"]=$eid;
     $_SESSION["cid"]=$cid;
        $_SESSION["nome"]=$nome;
 
if(($cid==43) || ($cid ==45) || ($cid ==49)|| ($cid ==50)|| ($cid ==51)|| ($cid == 54)){
    
    $sqlVerifica= $connection->prepare("SELECT count(*) as qta from account_leads where email=?");
    $sqlVerifica->execute(["$email"]);
    $countEmail = $sqlVerifica->fetchColumn();
    
    $count = $countEmail;
    
    
    if($count===0){
    
        $queryAccount = $connection->prepare("INSERT INTO account_leads (nome,cognome,email,motto,sesso) VALUES (?,?,?,md5(?),?)");
        $queryAccount->execute(["$nome","$cognome","$email","$pwd","$sesso"]);
      
        $_SESSION["motto"]=$pwd;
        $_SESSION["isRegist"]="no";
    
    
         
    }else{
        $_SESSION["isRegist"]="si";
    }
}else{
    
    $_SESSION["conc"]="1";
}
