<?php
error_reporting(0);
//ini_set('display_errors', 1);
//ini_set('display_startup_errors', 1);
//error_reporting(E_ALL);

require_once ('assets/mail/Exception.php');
require_once ('assets/mail/PHPMailer.php');
require_once ('assets/mail/SMTP.php');

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Checking For Blank Fields..
if($_GET["name"]==""||$_GET["email"]==""||$_GET["message"]==""||$_GET['token']==""){
    header("Location: http://cityfiber.eu/?no_field#contact");
    exit;
}

define("RECAPTCHA_V3_SECRET_KEY", '6LdUIVsoAAAAAI6YQf-vUD5FnpwjL3UPL1J6wVZu');
$token = $_GET['token'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('secret' => RECAPTCHA_V3_SECRET_KEY, 'response' => $token)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$arrResponse = json_decode($response, true);

if($arrResponse["success"] == '1' && $arrResponse["score"] >= 0.5) {
    $subject = 'Order from webpage';
    $message[]=array();
    $message[]="Dear CITY FIBER";
    $message[]="";
    $message[]="We have one message:";
    $message[]="From: ".$_GET["name"];
    $message[]="Message: ".$_GET["message"];
    $message[]="e-mail: ".$_GET["email"];
    $message[]="";
    $message[]="Date of send: ".date("Y-m-d H:i:s");
    $message[]="";

    $mail = new PHPMailer(true);

//Server settings
    $mail->SMTPDebug = 0;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.ionos.es';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'web@cityfiber.eu';                 // SMTP username
    $mail->Password = '@Zer0c00l#';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    $mail->setFrom('web@cityfiber.eu', 'Web Robot');
    $mail->Subject = 'You have new message from our webpage';
    $mail->Body    = implode("<br>".PHP_EOL,$message);
    $mail->AltBody = implode("<br>".PHP_EOL,$message);
    $mail->addAddress('info@cityfiber.eu', 'New message from our webpage');
    $mail->send();

    header("Location: https://cityfiber.eu/?result=ok#contact");
    exit;
}

header("Location: https://cityfiber.eu/?no_capcha#contact");
exit;

?>
