<?php
header('Access-Control-Allow-Origin: *');

ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
$mail->CharSet = 'UTF-8';
$mail->isHTML(true); 

try {
     $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      
     $mail->isSMTP();                                           
     $mail->Host       = 'smtp.fvkorobkov.ru';                    
     $mail->Port       = 465;                                    
     $mail->SMTPAuth   = true;                                  
     $mail->Username   = 'ask@fvkorobkov.ru';                     
     $mail->Password   = 'G59UQwYaSl';                             
     $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;

    $mail->setFrom('ask@fvkorobkov.ru', 'Робот-Клиентправ');
    $mail->addAddress('help@clientright.ru', 'Заявка с сайта clientright.ru');
    $mail->addAddress('ftpl@yandex.ru', 'Заявка с сайта clientright.ru');

    $mail->isHTML(true); 

    $upload_urls=$_POST['upload_urls'];

    $files_names = $_POST['files_names'];

    $files_array = [];
    
    $appends=$_POST['appends'];

    $lastname = str_replace(' ', '',$_POST['lastname']);

    $new_post = array(
        '__vtrftk' => 'sid:ec649134ad232e44c3ad71bbd321cee986f05545,1688385374',
        'publicid' => '3ddc71c2d79ef101c09b0d4e9c6bd08b',
        'urlencodeenable' => '1',
        'name' => 'websiteticket', 
    );
    
    $files_array=array(); 
    if($upload_urls) {
        foreach($upload_urls as $index => $upload_url) {     
            if($upload_url) {
                $mail->addAttachment($upload_url);   
                $files_array = array_merge($files_array, array($files_names[$index] => new CURLFile(realpath($upload_url))));
                $new_post[$files_names[$index]] = $upload_url;  
            }           
        }
    }
        
    

    foreach($appends as $key => $itemjson){
        $item=json_decode($itemjson);
        $new_post[$item->crm_name] = $item->field_val; 
    }
       
    $final_post = array_merge($new_post, $files_array);

    $curl_connection =curl_init('https://crm.clientright.ru/modules/Webforms/capture.php');
    curl_setopt($curl_connection, CURLOPT_POST, 1);
    curl_setopt($curl_connection, CURLOPT_HEADER, 0);
    curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($curl_connection, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
    curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl_connection, CURLOPT_HTTPHEADER,array('Content-Type: multipart/form-data'));
    curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $final_post);

    $result = curl_exec($curl_connection);
    curl_close($curl_connection);


    $mail->Subject = 'Заявкас с сайта clientright.ru ';
    $mail->Body    = 'Пользователь заполнил форму! <br>';
    
    foreach($new_post as $key => $value) {
        $mail->Body .= $key.' : '.$value.'<br>';  
    }

    $mail->send();

} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

$files = glob('uploads/*'); 
foreach($files as $file){ 
  if(is_file($file)) {
    unlink($file); 
  }  
} 
