<?php
include 'pagename.php';
include $session;
Session::checkSession_app();
include '../config.php';

$evp_options_sql = "SELECT option_name, option_value FROM evp_options";
$evp_options_read = $db->select($evp_options_sql);
if ($evp_options_read) {
  $evp_options_check = mysqli_num_rows($evp_options_read);
  if ($evp_options_check > 0) {
    while ($row = $evp_options_read->fetch_assoc()) {
      $option_name[$row['option_name']] = $row['option_value'];
    }
    $evp_options_read->free();
    $s_url = $option_name['site_url'];
    $a_url = $option_name['app_url'];
    $s_path = $option_name['script_path'];
    $site_title = $option_name['site_title'];
  }
}
function test_input($data)
{
  $db = new database();
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  $data = strtolower($data);
  $data = mysqli_real_escape_string($db->link, $data);
  return $data;
}
if (isset($_POST['filename']) && isset($_POST['user_id'])) {
  $or_std = false;
  $all_std = false;
  $and_std = false;
  $filename = $_POST['filename'];
  $user_id = $_POST['user_id'];
  $dataL = '';
  $dataC = [];
  if (!empty($cc_data)) {
    function xtrad1($cc_data)
    {
      $data = [];
      foreach ($cc_data as $val) {
        $data[] = ucwords(str_replace('_', ' ', $val));
      }
      return $data;
    }
    function xtrad2($cc_data)
    {
      $data = '';
      foreach ($cc_data as $val) {
        $data .= "$val, ";
      }
      return $data;
    }
    $dataC = xtrad1($cc_data);
    $dataL = xtrad2($cc_data);
  }

  $sql = "SELECT $dataL email_name, email_status, safe_to_send, verification_response, score, bounce_type, email_type, email_acc, email_dom, mail_box, domain_dns, mx_server, mx_response FROM evp_email_list WHERE ";

  $csv_new_name_result = '';
  $csv_new_name_sql = "SELECT `csv_new_name` FROM evp_email_list_metadata WHERE csv_file_name = '$filename' AND user_id = '$user_id'";
  $get_csv_new_name = $db->select($csv_new_name_sql);
  if ($get_csv_new_name && mysqli_num_rows($get_csv_new_name) > 0) {
    $csv_new_name_result = $get_csv_new_name->fetch_assoc();
    $csv_new_name_result = $csv_new_name_result['csv_new_name'];
  }

  if (!empty($csv_new_name_result)) {
    $csv_name = $csv_new_name_result . ".csv";
  } else {
    $csv_name = "csv_" . uniqid() . ".csv";
  }

  if (!empty($_POST['all'])) {
    $all_std = true;
  } else {
    if (!empty($_POST['valid'])) {
      if ($or_std) {
        $sql .= "OR ";
      } else {
        $sql .= "( ";
      }
      $sql .= "email_status = 'valid' ";
      $or_std = true;
    }
    if (!empty($_POST['invalid'])) {
      if ($or_std) {
        $sql .= "OR ";
      } else {
        $sql .= "( ";
      }
      $sql .= "email_status = 'invalid' OR email_status = 'unknown' OR email_status = 'skipped'";
      $or_std = true;
    }
    if (!empty($_POST['catchall'])) {
      if ($or_std) {
        $sql .= "OR ";
      } else {
        $sql .= "( ";
      }
      $sql .= "email_status = 'catch all' ";
      $or_std = true;
    }
    if (!empty($_POST['free'])) {
      if ($or_std && !$and_std) {
        $sql .= ") AND ";
      }
      if ($and_std) {
        $sql .= "OR ";
      } else {
        $sql .= "( ";
      }
      $sql .= "email_type = 'Free Account' ";
      $and_std = true;
    }
    if (!empty($_POST['role'])) {
      if ($or_std && !$and_std) {
        $sql .= ") AND ";
      }
      if ($and_std) {
        $sql .= "OR ";
      } else {
        $sql .= "( ";
      }
      $sql .= "email_type = 'Role Account' ";
      $and_std = true;
    }
    if (!empty($_POST['disposable'])) {
      if ($or_std && !$and_std) {
        $sql .= ") AND ";
      }
      if ($and_std) {
        $sql .= "OR ";
      } else {
        $sql .= "( ";
      }
      $sql .= "email_type = 'Disposable Account' ";
      $and_std = true;
    }
    if (!empty($_POST['syntax'])) {
      if ($or_std && !$and_std) {
        $sql .= ") AND ";
      }
      if ($and_std) {
        $sql .= "OR ";
      } else {
        $sql .= "( ";
      }
      $sql .= "verification_response = 'syntax error' ";
      $and_std = true;
    }
  }
  if ($or_std || $and_std) {
    $sql .= ") AND ";
  }
  $sql .= "csv_file_name = '" . $filename . "' AND user_id = '" . $user_id . "' ";
  if ($all_std || $or_std || $and_std) {
    $read22 = $db->select($sql);
    if ($read22) {
      header('Content-type:text/csv;charset=utf-8'); //declear content-type;
      header('Content-Disposition: attachment; filename=' . $csv_name); //assign file name to csv file;
      echo "\xEF\xBB\xBF"; // BOM header UTF-8
      $file = fopen("php://output", "w"); //write to csv file;
      ob_clean();
      $arr = array('Email', 'Verification Status', 'Safe to send', 'Verification Response', 'Score', 'Bounce type', 'Account Type', 'Email Account', 'Email Domain', 'Mailbox', 'Domain DNS', 'MX Server', 'MX Response');
      if (!empty($cc_data)) {
        $arr = array_merge($dataC, $arr);
      }
      fputcsv($file, $arr, ",", "\"", "\\"); //headers name
      while ($row22 = $read22->fetch_assoc()) { //write data;
        fputcsv($file, $row22, ",", "\"", "\\");
      }
      fclose($file); //close file;
    }
  } else {
    header("Location: $a_url/" . $error_404_page);
  }
} else {
  header("Location: $a_url/" . $error_404_page);
}
?>