<?php
// Version Checker & Updater
if (isset($_SESSION['auth_log']) && $_SESSION['auth_log'] == true) {
	$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']);
	$ch_ver = curl_init();
	$vcheck = array('vercheck' => 'true', 'lic_key' => "$seed_token", 's_ver' => "$s_ver", 's_url' => "$s_url", 'lic_id' => "$access_id");
	$vcheck = http_build_query($vcheck);
	curl_setopt($ch_ver, CURLOPT_URL, 'https://emailverifierpro.app/release/');
	curl_setopt($ch_ver, CURLOPT_HEADER, 0);
	curl_setopt($ch_ver, 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_ver, CURLOPT_REFERER, $base_url);
	curl_setopt($ch_ver, CURLOPT_POSTFIELDS, $vcheck);
	curl_setopt($ch_ver, CURLOPT_POST, true);
	curl_setopt($ch_ver, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch_ver, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($ch_ver, CURLOPT_SSL_VERIFYHOST, 0);
	curl_setopt($ch_ver, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch_ver, CURLOPT_VERBOSE, 0);
	$update_ver = curl_exec($ch_ver);
	$update_ver = json_decode($update_ver);
	?>
	<?php if (!empty($update_ver->status)) { ?>
		<?php
		$suppress_license_msg = false;
		if ($update_ver->msg == 'Requires a valid license!' && isset($_SESSION['access_active']) && $_SESSION['access_active'] == 'true') {
			$suppress_license_msg = true;
		}
		if ($update_ver->msg && !$suppress_license_msg):
			echo '<div id="msg_info" class="alert alert-info" role="alert" style="text-align: center;"><p>' . $update_ver->msg . "</p></div>";
		endif;
		?>

		<?php if (!empty($update_ver->released_ver) && ($update_ver->released_ver > $s_ver) && ($update_ver->status == 'true')) { ?>
			<div id="ver_check" class="alert alert-warning" role="alert" style="text-align: center;">
				<p>
					You are using older version <span class="installed_ver" style="font-weight:bold;"><?php echo $s_ver; ?></span>.
					There is a new version <span class="update_ver"
						style="font-weight:bold;"><?php echo $update_ver->released_ver; ?></span> available! <a
						class="update_now d-sm-inline-block btn btn-sm btn-primary shadow-sm" href="#">Update Now!</a> <br />
					If there's an issue with updating, contact <a href="https://emailverifierpro.app/support" target="_blank"
						title="Email Verifier Pro Support">Email Verifier Pro - Support Panel</a>
				</p>
			</div>
		<?php } // ver check ?>


		<script>
			$(document).ready(function () {
				$("#ver_check .update_now").click(function () {
					var get_update_ver = $('#ver_check').find(".update_ver").text();
					Swal.fire({
						title: "Update Confirmation",
						html: "<p style='color: #009c5d;text-transform: uppercase;font-size: 16px;font-weight: bold;'>Would you like to upgrade in version <span style='color:#0063ff;'>" + get_update_ver + "</span>?</p> <p style='color: #e74a3b;font-size: 16px;font-weight: bold;'> Before updating always make sure to backup existing <span style='color:#0063ff;'>database</span> and <span style='color:#0063ff;'>script files</span> incase auto update fails. We won't be responsible for any type of data lose!</p>",
						icon: 'info',
						type: "info",
						showCancelButton: true,
						confirmButtonText: 'Yes!',
						showLoaderOnConfirm: true,
						preConfirm: function (text) {
							return new Promise(function (resolve) {
								$.ajax({
									url: "../functions/updater.php",
									method: 'POST',
									data: {
										update_ver: get_update_ver
									},
									dataType: "json"
								}).done(function (response) {
									if (response['title'] === 'Success') {
										Swal.fire({
											title: "Completed",
											showConfirmButton: true,
											icon: 'success',
											type: "success",
											html: 'Update successful',
										}).then((result) => {
											// Reload the Page
											location.reload();
										});
									} else if (response['title'] === 'Update Error') {
										Swal.fire({
											title: "File Corrupted!",
											showConfirmButton: true,
											icon: 'warning',
											type: "warning",
											html: 'Unable to extract update ' + get_update_ver + ' file. Try clean installation of ' + get_update_ver + ' version.',
										})
									} else if (response['title'] === 'Error') {
										Swal.fire({
											title: "Connection Error",
											showConfirmButton: true,
											icon: 'error',
											type: "error",
											html: 'Update failed',
										})
									}
								})
							})
						},
					})
					event.preventDefault();
				});
			});
		</script>
	<?php } // curl init ?>

	<script
		type="text/javascript">window.$sleek = []; window.SLEEK_PRODUCT_ID = 722167656; (function () { d = document; s = d.createElement("script"); s.src = "https://client.sleekplan.com/sdk/e.js"; s.async = 1; d.getElementsByTagName("head")[0].appendChild(s); })();</script>

<?php } // admin check ?>