<?php
session_start();
include("../includes/connect.php");
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require '../includes/Exception.php';
require '../includes/PHPMailer.php';
//require "PHPMailerAutoload.php";
require '../includes/SMTP.php';

require_once "../includes/Mobile_Detect.php";

function generateRandomString($length = 10) {
    return substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)) )),1,$length);
}
$openFile = fopen("cdata.txt", "r") or die("Unable to open file!");
$campaignData = fread($openFile,filesize("cdata.txt"));
fclose($openFile);

$campaignInfo = explode(PHP_EOL,$campaignData);
$campaignid = $campaignInfo[0];

$datiCampagna = $connection->query("SELECT meta_key,meta_value FROM campaigns_meta a WHERE campaign_id = '$campaignid' UNION ALL select 'txtLang' as meta_key,left(code,2) as meta_value from languages where id=(select meta_value from campaigns_meta where campaign_id='$campaignid' and meta_key='language');");

$campaignData = $datiCampagna->fetchAll(PDO::FETCH_KEY_PAIR);

$tabellaLead = "campaign" . $campaignid . "_leads";
$ts = $_GET['sanit'];
$eid = $_GET['e'];

$linguaCorrenteCampagna = $campaignData['txtLang'];

$sqlVerifica= $connection->prepare("SELECT validata from $tabellaLead where externalid = ?");
$sqlVerifica->execute(["$eid"]);
$Validata = $sqlVerifica->fetchColumn();

$nValidata = $Validata;

if($nValidata != 1){
    
    $connection->query("UPDATE $tabellaLead SET validata='1' WHERE timestamp = '$ts' AND externalid = '$eid'");
        
    
    
    
    if($campaignid>=65){

            // in questo caso, mandiamo le credenziali via posta all'indirizzo email
            
        $gatto = generateRandomString(); 
        $q= $connection->prepare("SELECT email FROM $tabellaLead WHERE externalid=?");
        $q->execute(["$eid"]);
        $topo = $q->fetchColumn();  
        $q= $connection->prepare("SELECT nome FROM account_leads WHERE email=?");
        $q->execute(["$topo"]);
        $nome = $q->fetchColumn();
        $queryAccount = $connection->prepare("UPDATE account_leads SET motto=(md5(?)) where email=?");
        $queryAccount->execute(["$gatto","$topo"]);

        if(($linguaCorrenteCampagna=="it")){
            
             $mailHtml = "Ciao $nome! <br> <br>
            Abbiamo riservato tanti contenuti esclusivi sui nostri portali. <br>
            Dai un'occhiata alla LaBakeca.eu, una città virtuale in cui abbiamo creato un ambiente ricco di attività! 
             <br><br>Per visitarla, <a href='https://labakeca.eu'>clicca qua!</a> <br><br>
                Di seguito, ecco i tuoi dati di accesso: <br>Username: ".$topo."<br>Password: ".$gatto."<br>";
            
            $altMail = "Gentile visitatore! <br>
                    Grazie per esserti registrato! 
                    Abbiamo riservato tanti contenuti esclusivi sui nostri portali. <br>
                    Dai un'occhiata alla LaBakeca.eu, in cui abbiamo cercato di creare un ambiente con molteplici attività!<br> 
                    Per visitarla, clicca qua! <br>
                        Di seguito, ecco i tuoi dati di accesso: <br>Username: ".$topo."<br>Password: ".$gatto."<br><em> <br>";

            $txtoggetto="$topo, ecco le tue credenziali per LaBakeca.eu";

            $setFrom='  Dati di accesso LaBakeca.eu';

            $txtTitleComunicazione="Indirizzo email confermato, grazie!";

            $txtBodyComunicazione="Il tuo indirizzo e-mail è stato confermato correttamente! <br> Ti abbiamo inviato una mail con le credenziali di accesso al nostro portale! <br><em>Se non dovessi trovarla, Verifica anche nello spam!</em> ";
            
        }elseif(($linguaCorrenteCampagna=="es")){
                 $mailHtml = "Hola $nome <br> <br>
            Gracias, confirmamos tu suscripción a labakeca.eu. ¡Con tu cuenta, tendrás acceso a muchas ofertas y contenido exclusivo!
             <br><br>Para visitarlo, <a href='https://labakeca.eu'>pulse aquí!</a> <br><br>
                Aquí tienes tus datos de acceso: <br>Username: ".$topo."<br>Password: ".$gatto."<br>";
            
            $altMail = "Hola visitante! <br>                    Grazie per esserti registrato!                     Gracias, confirmamos tu suscripción a labakeca.eu. <br> ¡Con tu cuenta, tendrás acceso a muchas ofertas y contenido exclusivo!                    Para visitarlo pulse aquí! <br>Aquí tienes tus datos de acceso: <br>Username: ".$topo."<br>Password: ".$gatto."<br><em> <br>";
            

            $txtoggetto="$topo, aquí están sus credenciales para LaBakeca.eu";

            $setFrom=' Datos de acceso a LaBakeca.eu';

            $txtTitleComunicazione="Dirección de correo electrónico confirmada, ¡gracias!";

            $txtBodyComunicazione="Su dirección de correo electrónico se ha confirmado correctamente. <br>Le hemos enviado un correo electrónico con las credenciales de acceso a nuestro portal<br><em>y si no lo encuentras, ¡por favor comprueba tu carpeta de spam!</em> ";


        }elseif(($linguaCorrenteCampagna=="en")){
                 $mailHtml = "Hi $nome! <br> <br>
           Thank you, we confirm your subscription to labakeca.eu. With your account, you will have access to many offers and exclusive content!
             <br><br>To visit it, <a href='https://labakeca.eu'>click here!</a> <br><br>
               Here are your login details: <br>Username: ".$topo."<br>Password: ".$gatto."<br>";
            
            $altMail = "Dear visitor! <br>
                    Thank you, we confirm your subscription to labakeca.eu. With your account, you will have access to many offers and exclusive content!<br> 
                    To visit it,click here <br>
                        Here are your login details: <br>Username: ".$topo."<br>Password: ".$gatto."<br><em> <br>";
 
            $txtoggetto="$topo, here are your credentials for LaBakeca.eu";

            $setFrom=' LaBakeca.eu login details';

            $txtTitleComunicazione="Email address confirmed, thank you!";

            $txtBodyComunicazione   ="Your email address has been successfully confirmed! <br>We have sent you an e-mail with the login credentials for our portal<br><em>and if you don't find it, please check your spam folder!</em> ";        
        }
       
        
        $emailInviatore='info@labakeca.eu';
        
        
        #$emailBcc = "webmaster@elenchitelefonici.it";
         try {
        
            // configurazione invio email
            $mail = new PHPMailer(true);
          
            $mail->isSMTP();
            //$mail->SMTPDebug = 4;
            $mail->Mailer = "smtp";
            $mail->Host       = 'mail.labakeca.eu';                    // Set the SMTP server to send through
            $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
            $mail->SMTPAutoTLS = true; 
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
            #$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
            $mail->Username   = 'back@labakeca.eu';                     // SMTP username
            $mail->Password   = 'MGX7W7UCFJL';                               // SMTP password
            $mail->Port = 587; // or 465                                 // TCP port to connect to
        
            $mail->setFrom($emailInviatore,$setFrom);
            $mail->addReplyTo($emailInviatore,$setFrom);
            $mail->addAddress($topo);
            $mail->isHTML(true);
            $mail->charset ='UTF-8';
            $mail->Subject = utf8_decode($txtoggetto);
            $mail->Body = utf8_decode($mailHtml);
            $mail->altBody = $altMail;
            #$message->addBcc($emailBcc);
            $mail->send();
           // echo 'Message has been sent';
        } catch (Exception $e) {
            //echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
        } 
        
    
 
 
       
}
}
?>
<!DOCTYPE html>
<html lang="<?php echo $campaignData['txtLang']; ?>">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="keywords" content="" />
    <meta name="description" content="<?php echo $campaignData['campaign_description']; ?>" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
    <!-- <link rel="shortcut icon" href="assets/1_66bce83c4ac77e50c0afcad5ee7bf433.ico?v=39884" />
        <link rel="apple-touch-icon" href="assets/1_66bce83c4ac77e50c0afcad5ee7bf433.ico?v=39884"> -->
    <title>
        <?php echo $campaignData['campaign_title']; ?>
    </title>
    <meta property="og:title" content="<?php echo $campaignData['campaign_title']; ?>" />
    <meta property="og:description" content="<?php echo $campaignData['campaign_description']; ?>" />
    <link href="../campaignsAssets/css/common.css" rel="stylesheet">
    <link href="../campaignsAssets/css/voucherzone_style.css" rel="stylesheet">
    <link href="../campaignsAssets/css/layout.css" rel="stylesheet">
    <link href="../campaignsAssets/css/white.css" rel="stylesheet">
    <link href="../campaignsAssets/css/branding.css" rel="stylesheet">
</head>

<body>
    <div class="body-wrap lang_it" style="background-image: url(assets/<?php echo $campaignData['bg']; ?>)">
        <!-- immagine grande di sfondo -->
        <div class="body-wrap-in">
            <div class="content">
                <div class="content-in">
                    <div class="panels">
                        <div class="panel" id="thanks">
                            <div class="container">
                                <div class="row content-row">
                                    <div class="position-info">
                                        <div class="position-text">
                                            <div class="position-title-wrap">
                                                <div class="title"></div>
                                                <div class="subtitle"></div>
                                            </div>
                                            <div class="position-image">
                                                <img src="assets/<?php echo $campaignData['front_desktop']; ?>" alt="" class="pure-pic packshot-large" /> <!-- immagine front desktop form -->
                                                <img src="assets/<?php echo $campaignData['front_mobile']; ?>" alt="" class="pure-pic packshot-small" /> <!-- immagine front mobile form -->
                                            </div>
                                        </div>
                                    </div>
                                    <div class="position-form">
                                        <div class="questions" id="question-container">
                                            <div class="questions-progress-wrap">
                                                <div class="questions-progress">
                                                    <div class="questions-almost ">
                                                        <div class="questions-almost-img">
                                                            <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="200px" height="141.406px" viewBox="100 29.297 200 141.406" enable-background="new 100 29.297 200 141.406" xml:space="preserve">
                                                                <path fill="#F30B0B" d="M137.578,37.578l-8.281-8.281C110.391,48.203,100,73.281,100,100s10.391,51.836,29.297,70.703l8.281-8.281
                                                    c-16.68-16.68-25.859-38.828-25.859-62.422S120.898,54.258,137.578,37.578z" />
                                                                <path fill="#F30B0B" d="M145.898,45.898C131.406,60.352,123.438,79.57,123.438,100s7.969,39.648,22.461,54.102l8.281-8.281
                                                    c-12.266-12.266-19.023-28.516-19.023-45.82s6.758-33.555,19.023-45.82L145.898,45.898z" />
                                                                <path fill="#F30B0B" d="M270.703,29.297l-8.281,8.281c16.68,16.68,25.859,38.867,25.859,62.422s-9.18,45.742-25.859,62.422
                                                    l8.281,8.281C289.609,151.797,300,126.719,300,100S289.609,48.203,270.703,29.297z" />
                                                                <path fill="#F30B0B" d="M254.102,45.898l-8.281,8.281c12.266,12.266,19.023,28.555,19.023,45.82c0,17.305-6.758,33.555-19.023,45.82
                                                    l8.281,8.281c14.492-14.453,22.461-33.672,22.461-54.102S268.594,60.352,254.102,45.898z" />
                                                                <path fill="#F30B0B" d="M163.945,119.922h72.07v9.18h-72.07V119.922z" />
                                                                <path fill="#F30B0B" d="M191.367,142.695c0,4.766,3.867,8.633,8.633,8.633s8.633-3.867,8.633-8.633v-1.875h-17.266V142.695z" />
                                                                <path fill="#F30B0B" d="M225.742,96.133V84.258c0-12.227-8.516-22.422-19.883-25.078V48.711h-11.719V59.18
                                                    c-11.406,2.656-19.883,12.891-19.883,25.078v11.875c0,4.258-1.094,8.398-3.047,12.031h57.656
                                                    C226.836,104.531,225.742,100.43,225.742,96.133z" />
                                                            </svg> </div>
                                                        <div class="questions-almost-text">
                                                            <div class="onLeadCreated">
                                                                <?echo $txtTitleComunicazione;?>
                                                            </div>
                                                            <div class="question-progress-impulse" style="background-color:#cecece!important;height:15rem!important;font-size:1.2rem;">
                                                                <?if($nValidata!=1)
                                                                    if(($campaignid>=65)){ 
                                                                        echo $txtBodyComunicazione;
                                                                    }?>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <p class="hide"></p>
                                                    <div class="question-progress">
                                                        <div id="question-progress-bar"></div>
                                                        <span class="question-progress-num"></span>
                                                    </div>
                                                </div>
                                                <div class="question-progress-impulse " style="background-color:#cecece!important;height:15rem!important;font-size:1.2rem;">
                                                    <?
                          if($nValidata!=1)
                          if(($campaignid>=65)){
                              
                                echo $txtBodyComunicazione;
                            }
                            
                    
                       ?>
                                                </div>
                                            </div>
                                            <div class="questions-in">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>

</html>