<?php
/**** 
Email Verifier Pro - 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 '../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'];
    $seed_token = $option_name['license_key'];
    $access_id = $option_name['license_id'];
  }
}

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

  $update_ver = $_POST['update_ver'];

  // get latest files
  $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' => "$seed_token",
    'lic_id' => "$access_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);
  }


  // 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 extract update $update_ver file. 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_POST, 1);
  curl_setopt($ch_install, CURLOPT_POSTFIELDS, $pass_data);
  curl_setopt($ch_install, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch_install, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch_install, CURLOPT_VERBOSE, true);
  $get_install = curl_exec($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));
}
?>