<?php set_time_limit(0);
ini_set('display_errors', 1);

include 'pagename.php';

include $session;

Session::checkSession_app();

include '../config.php';

session_start();

$file_data = $_SESSION['file_data'];
$tmp_name = $_SESSION['file_name'];

unset($_SESSION['file_data']);
unset($_SESSION['file_name']);

function sanitize_text($data)
{

    //filter value function

  global $db;

    return mysqli_real_escape_string($db->link, htmlspecialchars(stripslashes(trim($data))));
}

function clear_quotes($strClear)
{
  return preg_replace('/^(\'(.*)\'|"(.*)")$/', '$2$3', $strClear);
}

$filename = sanitize_text($_POST['filename']);
$user_id = sanitize_text($_POST['uid']);
$check_mailbox = sanitize_text($_POST['check_mailbox']);

if($check_mailbox == 'true'){
	$mail_box = 'pending';
} elseif($check_mailbox == 'false'){
	$check_mailbox = NULL;
}

$duplicate        = $unsave        = $save        = 0;
$total = 0;
foreach ($file_data as $row) {
	$dataC = '';
    $dataV = '';

    if(!empty($cc_data)) {
        foreach($cc_data as $val) {
            $dataC .= "$val, ";
            
            $value = null;
         
            if (isset($_POST["$val"])) {
				$value = (isset($row[$_POST["$val"]])) ? sanitize_text($row[$_POST["$val"]]) : null;
    }

            $dataV .= "'".$value."', ";
    }
    }
			
    $emails       = explode(",", $row[$_POST['email_name']]);

    $em_status = "Not Verify";
	
    $emails           = array_filter($emails, 'strlen');
    $emails           = clear_quotes($emails);
    $total            = count($emails);
    $start_date       = date("Y-m-d h:i:s");

    $unique_emails    = array_unique($emails);
		
    $count_after_uniq = count($unique_emails);
    $duplicate        = $total - $count_after_uniq;
    $coma_check       = false;

    $save_data_sql = "INSERT INTO evp_email_list ( csv_file_name, $dataC email_name, email_status, mail_box, create_date, user_id ) values";

    foreach ($unique_emails as $email) {

        $email = sanitize_text($email);

        if (!empty($email)) {

    $save_data_sql .= $coma_check ? ',' : '';
    $coma_check = true;
            $save_data_sql .= " ('$filename',$dataV '$email','$em_status','$mail_box','$start_date','$user_id')";
        }
        $save++;
  }
	
    if ($coma_check) {
        $save_data_read = $db->insert($save_data_sql);
    }
}

    $listing_metadata_sql = "INSERT INTO evp_email_list_metadata ( csv_file_name, total, creation_date, scan_status, check_mailbox, user_id ) values ('$filename', '$save', '$start_date','pending','$check_mailbox','$user_id')";
	$listing_metadata_read = $db->insert($listing_metadata_sql);
	 
$end_date = date("Y-m-d h:i:s");
$total = count($file_data);
$unsave = $total - ($save + $duplicate);

$tmploc = realpath(dirname(dirname(__FILE__))).'/tmp/csv/';
$del_tmpfile = "$tmploc/$tmp_name";
unlink($del_tmpfile);

echo json_encode(['filename' => $filename, 'user_id', 'save' => $save, 'total' => $total, 'unsave' => $unsave, 'duplicate' => $duplicate, 'end_time' => $end_date, 'start_time' => $start_date]);

exit;