<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

class SMTPClass {

    private $smtp_host;
    private $smtp_port;
    private $smtp_username;
    private $smtp_password;
    private $smtp_from_name;

    public function __construct()
    {
        $this->smtp_host = 'smtp.office365.com';
        $this->smtp_port = 587;
        $this->smtp_username = 'comercial@vedicerca.pt';
        $this->smtp_password = 'H&857213048460ob';
        $this->smtp_from_name = 'Vedicerca';
        $this->smtp_from_email = 'comercial@vedicerca.pt';
    }

    public function sendEmail($name, $email, $subject, $email_to, $email_bcc, $data, $uploaded_files)
    {
        require_once('PHPMailer-6.8.0/src/Exception.php');
        require_once('PHPMailer-6.8.0/src/PHPMailer.php');
        require_once('PHPMailer-6.8.0/src/SMTP.php');

        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->Host = $this->smtp_host;
        $mail->SMTPDebug = false;
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = 'tls';
        $mail->Port = $this->smtp_port;
        $mail->Username = $this->smtp_username;
        $mail->Password = $this->smtp_password;

        // dados destinatário
        $mail->SetFrom($this->smtp_from_email, $this->smtp_from_name);
        $mail->AddAddress($email_to);
        $mail->addBCC($email_bcc);  
        $mail->AddReplyTo($email, $name);

        // dados mensagem
        $mail->IsHTML(true);
        $mail->CharSet = 'UTF-8';
        $mail->Subject = $subject;

        $form_email = $this->get_include_contents('views/email.php', $data);
        $mail->Body = $form_email;
        foreach ($uploaded_files as $i => $file) {
            $mail->addAttachment($file['path']);
        }
        //$mail->MsgHTML($message);

        // envio da mensagem
        if(!$mail->Send()) {
            //echo $mail->ErrorInfo;die;
            $response = false;
        } else {
            $response = true;
        }

	    $mail->ClearAllRecipients();

        return $response;
    }

    public function cleanString($str)
    {
        $text = htmlspecialchars($str);
        $text = strip_tags($text);
        
        return $text;
    }

    public function get_include_contents($filename, $data)
    {
        if (is_file($filename)) {
            ob_start();
          include $filename;
          return ob_get_clean();
        }
        return false;
    }

    public function gRecaptcha($key, $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' => $key, 'response' => $token, 'remoteip' => $_SERVER['REMOTE_ADDR'])));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        curl_close($ch);
    
        return json_decode($response, true);
    }

}

$act = $_GET['act'];
if($act == 'form_contact') {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $email_to = $_POST['email_to'];
    $email_bcc = $_POST['email_bcc'];
    $data = $_POST['data'];
    $uploaded_files = $_POST['uploaded_files'];

    $form = new SMTPClass;
    $send = $form->sendEmail($name, $email, $subject, $email_to, $email_bcc, $data, $uploaded_files);

    echo $send;
    exit;
}