<?php
/**** 
Email Verifier Pro - Updater
Version : 1.0
Script URL: https://emailverifierpro.app
Author: Tuhin Bhuiyan
Author URL: https://tuhinbhuiyan.com
****/
error_reporting(0); 
ini_set('display_errors', 0); 
ini_set('display_startup_errors', 0); 
ini_set('log_errors', 0);
include('../functions/pagename.php');
include '../config.php';
// 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'];		
        $a_url =  $option_name['app_url'];		
		$s_path =  $option_name['script_path'];
		$site_title =  $option_name['site_title'];
		$license_title =  $option_name['license_title'];
		$lic_key =  $option_name['license_key'];
		$license_support =  $option_name['license_support'];
		$lic_id =  $option_name['license_id'];
		$s_ver =  $option_name['script_ver'];
	}
}
if(!empty($_GET['update_ver'])):
$base_url = (( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == "on" ) ? "https" : "http");
$base_url .= "://" . $_SERVER['HTTP_HOST'];
$base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), "", $_SERVER['SCRIPT_NAME']);
$base_url = dirname($base_url);
$update_ver = $_GET['update_ver'];
$get_UpdateFile = "https://emailverifierpro.app/release/";
$ch_update= curl_init();
$UpdateFile = "$s_path/tmp/$update_ver.zip";
$update_resource = fopen($UpdateFile, "w");
$fetch_data = array(
			'website' => "$s_url",
			'dir_path' => "$s_path",
			'version' => "$s_ver",
			'license_key' => "$lic_key",
			'license_title' => "$license_title",
			'license_support' => "$license_support",
			'update_ver' => "$update_ver"
);
$fetch_data = http_build_query($fetch_data);
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_REFERER, $base_url); 
curl_setopt($ch_update, CURLOPT_POSTFIELDS, $fetch_data);
curl_setopt($ch_update, CURLOPT_POST, true);
curl_setopt($ch_update, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch_update, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch_update, CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch_update, CURLOPT_TIMEOUT, 400);
curl_setopt($ch_update, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch_update, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch_update, CURLOPT_VERBOSE, false);
curl_setopt($ch_update, CURLOPT_FILE, $update_resource);
$get_update = curl_exec($ch_update);
curl_close($ch_update);
// extract zip
$zip = new ZipArchive;
$extractPath = "$s_path/tmp/$update_ver/";
if($zip->open($UpdateFile) != "true"){
// return error
} else {
$zip->extractTo($extractPath);
unlink("$s_path/tmp/$update_ver.zip");
}
$zip->close();
// execute latest update
if(!empty($_POST['s_path'])){
	$s_path = $_POST['s_path'];
} else {
	global $s_path;
	$phpver = (float)phpversion();
	if ($phpver >= 7.0) {
	   $s_path = dirname(__DIR__, 1);
	 } elseif ($phpver <= 5.6) {
	   $s_path = realpath(__DIR__ . '/..');
	 } elseif ($phpver <= 5.3) {
	   $s_path = realpath(dirname(__FILE__) . '/..');
	}
}
$exc_update = "$s_url/tmp/$update_ver/update.php";
$ch_install = curl_init();
$pass_data = array(
	's_path' => $s_path,
	'update_ver' => $update_ver
);

$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);
curl_close($ch_install);

// Delete tmp files
function recursive_rmdir($dir) {
    if( is_dir($dir) ) { 
      $objects = array_diff( scandir($dir), array('..', '.') );
      foreach ($objects as $object) { 
        $objectPath = $dir."/".$object;
        if( is_dir($objectPath) )
          recursive_rmdir($objectPath);
        else
          unlink($objectPath); 
      } 
      rmdir($dir); 
    } 
}
recursive_rmdir("$s_path/tmp/$update_ver");
endif;
if(isset($_POST['update_ver']) && !empty($_POST['update_ver'])){
$update_ver = $_POST['update_ver'];
$get_UpdateFile = "https://emailverifierpro.app/updates/";
$ch_update = curl_init();
$UpdateFile = "$s_path/tmp/$update_ver.zip";
$update_resource = fopen($UpdateFile, "w");
$pass_data_download = array(
	'update_ver' => "$update_ver",
	's_url' => "$s_url",
	'lic_key' => "$lic_key",
	'lic_id' => "$lic_id"
);
$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, false);
curl_setopt($ch_update, CURLOPT_FILE, $update_resource);
$get_update = curl_exec($ch_update);
if(!$get_update)
{
 echo "Error :- ".curl_error($ch_update);
}
curl_close($ch_update);

// extract zip
$zip = new ZipArchive;
$extractPath = "$s_path/tmp/$update_ver/";
if($zip->open($UpdateFile) != "true"){
// return error	
$result = array('title' => 'Update Error','text' => "Unable to download update $update_ver file. Check if Archive/ZIP package installed in PHP Or try clean installation of $update_ver version.",'type' =>'error');
return print_r(json_encode($result));
} else {
$zip->extractTo($extractPath);
unlink("$s_path/tmp/$update_ver.zip");
}
$zip->close();

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

$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);
curl_close($ch_install);

// Delete tmp files
function recursive_rmdir($dir) {
    if( is_dir($dir) ) { 
      $objects = array_diff( scandir($dir), array('..', '.') );
      foreach ($objects as $object) { 
        $objectPath = $dir."/".$object;
        if( is_dir($objectPath) )
          recursive_rmdir($objectPath);
        else
          unlink($objectPath); 
      } 
      rmdir($dir); 
    } 
}
recursive_rmdir("$s_path/tmp/$update_ver");

// return success
$result = array('title' => 'Success','text' => 'Update Successful','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));
}
?>