<?php
/*
	@package : themeson.com
	author : Themeson
	Don't touch baby!
*/

if ($_POST['newwpsafelink']) {
	$linktarget = json_decode(base64_decode($_POST['newwpsafelink']), true);
	$_GET['newsafelink'] = $linktarget;
}

function newwpsafelink_top()
{
	$code = $_GET['newsafelink'];
	if ($code) {
		$wpsaf = json_decode(get_settings('wpsaf_options'));

		$code['delaytext'] = str_replace('<span id=\"wpsafe-time\">', '<span id="wpsafe-time">', $code['delaytext']);
		$code['ads1'] = str_replace('\"', '"', $code['ads1']);
		?>
<style>
	body {
		background-color: #ffffff;
		color: #1d1d1d;
		font-family: Montserrat, sans-serif;
		margin: 0;
		padding: 0;
	}

	.wpsafe-container {
		max-width: 1200px;
		margin: 0 auto;
		padding: 20px;
	}

	/* Advertisement Container */
	.ad-container {
		text-align: center;
	}

	.ad-label {
		font-size: 12px;
		color: gray;
	}

	/* Button Styles */
	.adi-btn, .adi-btn-2 {
		border-radius: 100px;
		margin: 10px auto;
		font-weight: 700;
		letter-spacing: 1px;
		cursor: pointer;
		text-shadow: 1px 1px 1px rgba(0,0,0,.14);
		text-transform: uppercase;
		box-shadow: 0 4px 9px 0 rgba(0,0,0,.2);
		transition: color .15s ease-in-out, background-color .15s ease-in-out, 
					border-color .15s ease-in-out, box-shadow .15s ease-in-out;
		color: #fff !important;
		border: none;
		outline: none;
	}

	.adi-btn {
		font-size: 18px;
		padding: 17px 39px !important;
	}

	.adi-btn-2 {
		font-size: 15px;
		padding: 6px 15px !important;
	}

	.adi-blue {
		background: linear-gradient(135deg, #673AB7 0, #2196F3 50%, #3F51B5 51%, #565bd8 71%, #00BCD4 100%);
		background-size: 400% 400%;
		animation: AnimationName 3s ease infinite;
	}

	@keyframes AnimationName {
		0% { background-position: 0 31% }
		50% { background-position: 100% 70% }
		100% { background-position: 0 31% }
	}

	.adi-green {
		background-color: #28a745;
	}

	.adi-green:hover {
		background-color: #218838;
		transform: translateY(-1px);
		box-shadow: 0 4px 12px rgba(0,0,0,0.2);
	}

	/* Timer and Status */
	.status-container {
		margin: 20px 0;
		padding: 20px;
		background-color: #f8f9fa;
		border-radius: 10px;
		border: 2px solid #e9ecef;
	}

	.step-text {
		font-size: 18px;
		font-weight: 600;
		margin-bottom: 15px;
		color: #495057;
	}

	.countdown-timer {
		font-size: 16px;
		font-weight: 600;
		color: #dc3545;
	}

	/* Scroll Instructions - Copied from likethis.txt */
	.scroll-instruction {
    display: inline-block;
    margin: 0 auto;
    padding: 6px 18px;
    background: #28a745;
    color: #fff !important;
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    text-align: center;
    letter-spacing: 0.5px;
    white-space: nowrap;
    line-height: 1.3;
    border-radius: 50px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
    cursor: pointer;
    transition: all 0.3s ease;
}

/* Hover */
.scroll-instruction:hover {
    background-color: #218838;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}

/* Active */
.scroll-instruction:active {
    transform: translateY(0);
    box-shadow: 0 2px 4px rgba(0,0,0,0.15);
}

/* Mobile fix */
@media (max-width: 600px) {
    .scroll-instruction {
        font-size: 11px;
        padding: 5px 14px;
    }
}

	/* Hidden elements */
	.hidden {
		display: none !important;
	}

	/* reCAPTCHA Container */
	.recaptcha-container {
		margin: 20px 0;
		display: none;
	}

	.g-recaptcha {
		display: inline-block;
	}

	/* Get Link Button - Copied from likethis.txt */
	.get-link-container {
		display: none;
		text-align: center;
	}

	.get-link-btn {
		border-radius: 100px;
		margin: 10px auto;
		font-weight: 700;
		letter-spacing: 1px;
		cursor: pointer;
		text-shadow: 1px 1px 1px rgba(0,0,0,.14);
		text-transform: uppercase;
		box-shadow: 0 4px 9px 0 rgba(0,0,0,.2);
		transition: color .15s ease-in-out, background-color .15s ease-in-out, 
					border-color .15s ease-in-out, box-shadow .15s ease-in-out;
		color: #fff !important;
		border: none;
		outline: none;
		font-size: 15px;
		padding: 6px 15px !important;
		background: linear-gradient(135deg, #673AB7 0, #2196F3 50%, #3F51B5 51%, #565bd8 71%, #00BCD4 100%);
		background-size: 400% 400%;
		animation: AnimationName 3s ease infinite;
		text-decoration: none;
		display: inline-block;
	}

	@keyframes AnimationName {
		0% { background-position: 0 31% }
		50% { background-position: 100% 70% }
		100% { background-position: 0 31% }
	}

	.get-link-btn:hover {
		transform: translateY(-1px);
		box-shadow: 0 4px 12px rgba(0,0,0,0.2);
	}

	.get-link-btn:disabled {
		background: #6c757d;
		cursor: not-allowed;
		transform: none;
		animation: none;
	}

	/* Hidden elements */
	.hidden {
		display: none !important;
	}

	/* Responsive */
	@media (max-width: 768px) {
		.wpsafe-container {
			padding: 10px;
		}
		
		.scroll-instruction {
			font-size: 16px;
		}
	}

	/* Disable content interaction during timer */
	.content-disabled {
		pointer-events: none;
		opacity: 0.7;
	}
</style>

<div class="wpsafe-container">
	<!-- Advertisement 1 -->
	<div class="ad-container">
		<center><?php echo $wpsaf->ads1; ?></center>
	</div>

<h5 style="color:red; text-align:center; font-weight:bold;">Click on any <img draggable="false" role="img" class="emoji" src="https://s.w.org/images/core/emoji/16.0.1/svg/261d.svg"> Ad <img draggable="false" role="img" class="emoji"  src="https://s.w.org/images/core/emoji/16.0.1/svg/1f447.svg"> &amp; wait 20 seconds for link</h5>
	
	<!-- Advertisement 2 -->
	<div class="ad-container">
		<center><?php echo $wpsaf->ads2; ?></center>
	</div>

	<center>
		<div id="timer-container">
			<button id="timer-btn" class="adi-btn-2 adi-blue">
				Please Wait <span id="timer-count"><?php echo isset($code['delay']) ? $code['delay'] : 10; ?></span> Seconds...
			</button>
		</div>
	</center>

	<!-- Advertisement 3 -->
	<div class="ad-container">
		<center><?php echo $wpsaf->ads3; ?></center>
	</div>

<div style="text-align:center;">
  <p id="scroll-instruction" class="scroll-instruction hidden">
    ⬇️ Scroll down to the bottom to unlock your Get link! ⬇️
  </p>
</div>

	<!-- Advertisement 4 -->
	<div class="ad-container">
		<center><?php echo $wpsaf->ads4; ?></center>
	</div>
<script type="text/javascript">
	var timerCount = <?php echo $code['delay']; ?>;
	var originalTimerCount = timerCount;
	var timerInterval;
	var recaptchaCompleted = false;

	window.addEventListener('load', function() {
		startTimer();
	});

	function startTimer() {
		var timerElement = document.getElementById('timer-count');
		var timerBtn = document.getElementById('timer-btn');
		
		timerInterval = setInterval(function() {
			timerCount--;
			timerElement.textContent = timerCount;
			
			if (timerCount <= 0) {
				clearInterval(timerInterval);
				timerCompleted();
			}
		}, 1000);
	}

	function timerCompleted() {
		document.getElementById('timer-container').style.display = 'none';
		
		document.getElementById('scroll-instruction').style.display = 'block';
		document.getElementById('scroll-instruction').classList.remove('hidden');
		
		<?php if($wpsaf->recaptcha_enable == 1): ?>
		document.getElementById('recaptcha-container').style.display = 'block';
		<?php else: ?>
		enableGetButton();
		<?php endif; ?>
	}

	function enableGetButton() {
		recaptchaCompleted = true;
		if (typeof showGetLinkButton === 'function') {
			showGetLinkButton();
		}
	}

	function redirectToLink() {
		<?php if($wpsaf->recaptcha_enable == 1): ?>
		if (!recaptchaCompleted) {
			alert('Please complete the reCAPTCHA verification first.');
			return;
		}
		<?php endif; ?>
		
		var targetLink = '<?php echo isset($code['linkr']) ? $code['linkr'] : '#'; ?>';
		if (targetLink && targetLink !== '#') {
			window.location.href = targetLink;
		}
	}

	document.addEventListener('contextmenu', function(e) {
		e.preventDefault();
	});

	document.addEventListener('keydown', function(e) {
		if (e.keyCode === 123 || 
			(e.ctrlKey && e.keyCode === 85) ||
			(e.ctrlKey && e.shiftKey && e.keyCode === 73) ||
			(e.ctrlKey && e.shiftKey && e.keyCode === 67) ||
			(e.ctrlKey && e.shiftKey && e.keyCode === 74)) {
			e.preventDefault();
		}
	});

	document.getElementById('scroll-instruction').addEventListener('click', function() {
		document.getElementById('recaptcha-container').scrollIntoView({
			behavior: 'smooth'
		});
	});
</script>

		<?php
	}
}

function newwpsafelink_bottom()
{
	$code = $_GET['newsafelink'];
	if ($code) {
		$wpsaf = json_decode(get_settings('wpsaf_options'));
		?>
	<!-- Advertisement 5 -->
	<div class="ad-container">
	<center><?php echo $wpsaf->ads5; ?></center>
	</div>
</div>

	<!-- reCAPTCHA Container -->
	<center><div id="recaptcha-container" class="recaptcha-container">
		<?php if($wpsaf->recaptcha_enable == 1): ?>
		<script src="https://www.google.com/recaptcha/api.js" async defer></script>
		<div class="g-recaptcha" data-sitekey="<?php echo $wpsaf->recaptcha_site_key; ?>" data-callback="enableGetButton"></div>
		<?php endif; ?>
		</div></center>

	<!-- Advertisement 6 -->
	<div class="ad-container">
		<center><?php echo $wpsaf->ads6; ?></center>
		</div>

<!-- Get Link Button -->
<center>
<div id="get-link-container" class="get-link-container">
	<button id="get-link-btn" class="get-link-btn" disabled onclick="redirectToLink()">
		Get Link
	</button>
</div>
</center>
	<!-- Advertisement 7 -->
	<div class="ad-container">
		<center><?php echo $wpsaf->ads7; ?></center>
	</div>


<script>
	// Function to show get link button (called from top function)
	function showGetLinkButton() {
		document.getElementById('get-link-container').style.display = 'block';
		document.getElementById('get-link-btn').disabled = false;
	}
	
	// Check if timer is already completed when bottom loads
	if (typeof recaptchaCompleted !== 'undefined') {
		<?php if($wpsaf->recaptcha_enable == 1): ?>
		if (recaptchaCompleted) {
			showGetLinkButton();
		}
		<?php else: ?>
		// If no recaptcha and timer completed, show button
		if (timerCount <= 0) {
			showGetLinkButton();
		}
		<?php endif; ?>
	}
	
	console.log("SafeLink loaded successfully");
</script>

		<?php
	}
}

// Add anti-adblock detection (optional)
function add_anti_adblock_script() {
	?>
	<script>
	// Anti-adblock detection
	(function(){
		const n=document,o=n.head;
		var t="pointer-events: none; height: 1px; width: 0; opacity: 0; visibility: hidden; position: fixed; bottom: 0;";
		const a=n.createElement("div"),s=n.createElement("div"),d=n.createElement("ins");
		a.id="div-gpt-ad-test",a.style=t,s.className="textads banner-ads banner_ads ad-unit ad-zone ad-space adsbox ads",s.style=t,d.className="adsbygoogle",d.style="display: none;";
		
		const i={allowed:null,elements:[a,s,d]};
		
		this.checkAdsStatus=function(t){
			const e=n.body;
			if("function"==typeof t){
				if("boolean"==typeof i.allowed){
					t(i);
				} else {
					e.appendChild(a),e.appendChild(s),e.appendChild(d);
					setTimeout(function(){
						if(0===a.offsetHeight||0===s.offsetHeight||d.firstElementChild){
							i.allowed=!1,t(i);
						} else {
							i.allowed=!0,t(i);
						}
						a.remove(),s.remove(),d.remove();
					},40);
				}
			}
		};
	}).call(this);

	function antiAdBlockerHandler() {
		window.checkAdsStatus(function(ads) {
			if (!ads.allowed) {
				console.log("AdBlock detected");
				// You can add adblock detection popup here
			} else {
				console.log("Ads allowed");
			}
		});
	}

	if (document.readyState === "complete" || document.readyState !== "loading") {
		antiAdBlockerHandler();
	} else {
		document.addEventListener("DOMContentLoaded", antiAdBlockerHandler);
	}
	</script>
	<?php
}

// Hook to add anti-adblock script to footer
add_action('wp_footer', 'add_anti_adblock_script');

?>