<?php
/**** 
Email Verifier Pro - Domain Filter List Updater
Version : 1.0
Script URL: https://emailverifierpro.app
Author: Tuhin Bhuiyan
Author URL: https://tuhinbhuiyan.com
****/
include('../functions/pagename.php');
include('../functions/' . $session);
Session::checkSession_app();
include('../db/db.cnf.php');
include('../db/database.Class.php');
$db = new database();

// sql check site meta
$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'];
		$s_path = $option_name['script_path'];
		$seed_token = $option_name['license_key'];
	}
}

if (isset($_POST['df_updater']) && !empty($_POST['df_updater'])) {

	$df_filter_list = $_POST['df_updater'];

	// get latest files
	$get_UpdateFile = "https://emailverifierpro.app/df_updates/";
	$ch_update = curl_init();
	$UpdateFile = "$s_path/tmp/df_filter_list.zip";
	$update_resource = fopen($UpdateFile, "w");
	$pass_data_download = array(
		'df_updater' => $df_filter_list,
		's_url' => $s_url,
		'lic_key' => $seed_token
	);

	$pass_data_download = http_build_query($pass_data_download);

	curl_setopt($ch_update, CURLOPT_URL, $get_UpdateFile);
	curl_setopt($ch_update, CURLOPT_FAILONERROR, true);
	curl_setopt($ch_update, CURLOPT_HEADER, 0);
	curl_setopt($ch_update, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)');
	curl_setopt($ch_update, CURLOPT_POSTFIELDS, $pass_data_download);
	curl_setopt($ch_update, CURLOPT_POST, true);
	curl_setopt($ch_update, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($ch_update, CURLOPT_AUTOREFERER, true);
	curl_setopt($ch_update, CURLOPT_BINARYTRANSFER, true);
	curl_setopt($ch_update, CURLOPT_TIMEOUT, 400);
	curl_setopt($ch_update, CURLOPT_SSL_VERIFYHOST, 0);
	curl_setopt($ch_update, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch_update, CURLOPT_VERBOSE, true);
	curl_setopt($ch_update, CURLOPT_FILE, $update_resource);
	$get_update = curl_exec($ch_update);
	if (!$get_update) {
		echo "Error :- " . curl_error($ch_update);
	}


	// extract zip
	$zip = new ZipArchive;
	$extractPath = "$s_path/tmp/";
	if ($zip->open($UpdateFile) != "true") {
		// return error	

		$result = array('title' => 'File Corrupted!', 'text' => "Unable to extract file", 'type' => 'error');
		return print_r(json_encode($result));
	} else {
		$zip->extractTo($extractPath);
		unlink("$s_path/tmp/df_filter_list.zip");
	}
	$zip->close();

	// execute latest update
	$exc_update = "$s_url/tmp/df_filter_list/update.php";
	$ch_install = curl_init();
	$pass_data = array(
		's_path' => $s_path,
		'df_updater' => $df_filter_list
	);

	$pass_data = http_build_query($pass_data);
	curl_setopt($ch_install, CURLOPT_URL, $exc_update);
	curl_setopt($ch_install, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch_install, CURLOPT_HEADER, 0);
	curl_setopt($ch_install, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)');
	curl_setopt($ch_install, CURLOPT_POSTFIELDS, $pass_data);
	curl_setopt($ch_install, CURLOPT_POST, true);
	curl_setopt($ch_install, CURLOPT_SSL_VERIFYHOST, 0);
	curl_setopt($ch_install, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch_install, CURLOPT_VERBOSE, false);
	$get_install = curl_exec($ch_install);



	// return success
	$result = array('title' => 'Sync Started', 'text' => 'updating in background...', 'type' => 'success');
	return print_r(json_encode($result));
} else {
	// return error	
	$result = array('title' => 'Error', 'text' => 'Update failed', 'type' => 'error');
	return print_r(json_encode($result));
}
?>