<?php

$usingcode = $_POST["sendcode"];
$usingpass = md5($_POST["sendpass"]);

if(0 == 0) {// ALL FUNCTIONS
if(1 == 1) {//CHECK IF CAPTCHA ENABLED
$RCactive3 = "SELECT `rcactive` FROM `Web_Settings`";
$RCactive2 = mysql_query($RCactive3) or die($RCactive3."<br/><br/>".mysql_error());
$RCactive = mysql_result($RCactive2, 0);
if ($RCactive == 1){
	//CHECK CAPTCHA SITE KEY
	$RCsite3 = "SELECT `rcsitekey` FROM `Web_Settings`";
	$RCsite2 = mysql_query($RCsite3) or die($RCsite3."<br/><br/>".mysql_error());
	$RCsite = mysql_result($RCsite2, 0);
	//CHECK CAPTCHA SECRET KEY
	$RCsecret3 = "SELECT `rcsecretkey` FROM `Web_Settings`";
	$RCsecret2 = mysql_query($RCsecret3) or die($RCsecret3."<br/><br/>".mysql_error());
	$RCsecret = mysql_result($RCsecret2, 0);
	$lang = 'en';
}

//CHECK IF CAPTCHA ENABLED
$checkcaptchastatus1 = "SELECT `smactive` FROM `Web_Settings`";
$checkcaptchastatus2 = mysql_query($checkcaptchastatus1) or die($checkcaptchastatus1."<br/><br/>".mysql_error());
$checkcaptchastatus = mysql_result($checkcaptchastatus2, 0);
if ($checkcaptchastatus == 1){
	//CHECK CAPTCHA C-KEY
	$captchac1 = "SELECT `smckey` FROM `Web_Settings`";
	$captchac2 = mysql_query($captchac1) or die($captchac1."<br/><br/>".mysql_error());
	$captchac = mysql_result($captchac2, 0);
	//CHECK CAPTCHA V-KEY
	$captchav1 = "SELECT `smvkey` FROM `Web_Settings`";
	$captchav2 = mysql_query($captchav1) or die($captchav1."<br/><br/>".mysql_error());
	$captchav = mysql_result($captchav2, 0);
	//CHECK CAPTCHA H-KEY
	$captchah1 = "SELECT `smhkey` FROM `Web_Settings`";
	$captchah2 = mysql_query($captchah1) or die($captchah1."<br/><br/>".mysql_error());
	$captchah = mysql_result($captchah2, 0);
	
}

if ($checkcaptchastatus == 1 && $RCactive == 1){
	$luck = rand(0,1);
	if ($luck == 1){
		$RCactive = 1;
		$checkcaptchastatus = 0;
	}else{
		$RCactive = 0;
		$checkcaptchastatus = 1;
	}
}
}

if(2 == 2) {//GET LINK DATA
$query = mysql_query("SELECT * FROM Links_All WHERE code='$code'");
$numrows = mysql_num_rows($query);
$qqf = mysql_fetch_array($query);
$adctype = $qqf['advtype'];
$urlto = $qqf['url'];
$itemuser = $qqf['username'];
$password = $qqf['password'];
}

if(3 == 3) {//GET RATES
$getvalues = mysql_query("SELECT * FROM Web_Countries WHERE country='$country_name'");
$fgvalues = mysql_fetch_array($getvalues);
	$countryone_ppv_int = $fgvalues['ppv'];
	$countryone_cpv_int = $fgvalues['ppvadv'];
	$countryone_ppv_ban = $fgvalues['ppvb'];
	$countryone_cpv_ban = $fgvalues['ppvbadv'];
	$countryone_ppv_vid = $fgvalues['ppvvideo'];
	$countryone_cpv_vid = $fgvalues['cpvvideo'];
$getvaluesall = mysql_query("SELECT * FROM Web_Countries WHERE country='all'");
$allworldvalues = mysql_fetch_array($getvaluesall);
	$countryzer_ppv_int = $allworldvalues['ppv'];
	$countryzer_cpv_int = $allworldvalues['ppvadv'];
	$countryzer_ppv_ban = $allworldvalues['ppvb'];
	$countryzer_cpv_ban = $allworldvalues['ppvbadv'];
	$countryzer_ppv_vid = $allworldvalues['ppvvideo'];
	$countryzer_cpv_vid = $allworldvalues['cpvvideo'];
}

if(4 == 4) {//GET AD
			$checkforads = mysql_query("SELECT * FROM Ads_All WHERE adtype='$adctype' AND countries='$country_name' AND credits > 1 AND active = '1' AND approved = '1' ORDER BY RAND()");
			$numcrows = mysql_num_rows($checkforads); 
			if($numcrows != 0) {
				$fetchurl = mysql_fetch_array($checkforads);
  	    		$ADDATA1 = $fetchurl['url'];
  	    		$ADDATA2 = $fetchurl['imageurl'];
 	     		$ADDATA3 = $fetchurl['id'];
 	     		if($adctype == 'interstitial') {
     				$ADVprice = $countryone_cpv_int;
					$PUBprice = $countryone_ppv_int;
   			}
   			if($adctype == 'video') {
         		$ADVprice = $countryone_cpv_vid;
					$PUBprice = $countryone_ppv_vid;
   			}
   			if($adctype == 'banner') {
      			$ADVprice = $countryone_cpv_ban;
					$PUBprice = $countryone_ppv_ban;
   			}
   			$got = 1;
			}else{
			$checkforads2 = mysql_query("SELECT * FROM Ads_All WHERE adtype='$adctype' AND countries='ALL' AND credits > '1' AND active = '1' AND approved = '1' ORDER BY RAND()");
			$numcrows2 = mysql_num_rows($checkforads2); 
			if($numcrows2 != 0) {
				$fetchurl = mysql_fetch_array($checkforads);
  	    		$ADDATA1 = $fetchurl['url'];
  	    		$ADDATA2 = $fetchurl['imageurl'];
 	     		$ADDATA3 = $fetchurl['id'];
 	     		if($adctype == 'interstitial') {
     				$ADVprice = $countryone_cpv_int;
					$PUBprice = $countryone_ppv_int;
   			}
   			if($adctype == 'video') {
         		$ADVprice = $countryone_cpv_vid;
					$PUBprice = $countryone_ppv_vid;
   			}
   			if($adctype == 'banner') {
      			$ADVprice = $countryone_cpv_ban;
					$PUBprice = $countryone_ppv_ban;
   			}
   			$got = 1;
			}else {
				if(empty($ADDATA1)) {
					$ADDATA1 = $adctype;
				}
				if(empty($ADDATA2)) {
					$ADDATA2 = 'none';
				}
				if(empty($ADDATA3)) {
					$ADDATA3 = 'none';
				}
			}
			}
}

if(5 == 5) {
	if (mysql_real_escape_string($_POST['captchanext'])) {
		if ($checkcaptchastatus == 1 || $RCactive == 1){
			$recaptcha=$_POST['g-recaptcha-response'];
			$solvemediaresponde = $_POST["adcopy_response"];
			if($_POST['captchaservice'] == 'solvemedia'){
				if(empty($solvemediaresponde)) {
					$captchacorrect = 0;
					$step = 2;
				}
			}
			if($_POST['captchaservice'] == 'recaptcha'){
				if(empty($recaptcha)) {
					$captchacorrect = 0;
					$step = 2;
				}
			}
			if (isset($_POST["captchaservice"])) {
				$captchaservice = $_POST["captchaservice"];
				if ($captchaservice == 'solvemedia'){
					if (empty($solvemediaresponde)){
						echo"<script type='text/javascript'>window.location.replace('$mainurl$code'); </script>";
					}
				}else if ($captchaservice == 'recaptcha'){
					if (empty($recaptcha)){
						echo"<script type='text/javascript'>window.location.replace('$mainurl$code'); </script>";
					}
				}
			}
			if(!empty($recaptcha))
			{
				$google_url = "https://www.google.com/recaptcha/api/siteverify";
				$secret = $RCsecret;
				$ip = $_SERVER['REMOTE_ADDR'];
				$url = $google_url."?secret=".$secret."&response=".$recaptcha."&remoteip=".$ip;
				$res = getCurlData($url);
				$res = json_decode($res, true);
				if($res['success'])
				{
					$captchacorrect = 1;
					$step = 3;
				}else{
					$captchacorrect = 0;
					$step = 2;
				}
			}else{
				require_once("include/solvemedialib.php");
				$privkey = $captchav;
				$hashkey = $captchah;
				$solvemedia_response = solvemedia_check_answer($privkey,
						$_SERVER["REMOTE_ADDR"],
						$_POST["adcopy_challenge"],
						$_POST["adcopy_response"],
						$hashkey);
				if (!$solvemedia_response->is_valid) {
					$captchacorrect = 0;
					$step = 2;
				}
				else{
					$captchacorrect = 1;
					$step = 2;
				}
			}
		}
	}
//END ALL FUNCTIONS
}

if(6 == 6) {
	if (isset($_POST['step3complete'])) {
		$step = 4;
		if($adctype == 'banner'){
			$step = 5;
		}
	}
//END ALL FUNCTIONS
}

if(7 == 7) {
		//checkhash = $secretwebhash . $code . $userip . $md5password
	if (mysql_real_escape_string($_POST['step4complete'])) {
		if ($checkcaptchastatus == 1 || $RCactive == 1){
			$recaptcha=$_POST['g-recaptcha-response'];
			$solvemediaresponde = $_POST["adcopy_response"];
			if($_POST['captchaservice'] == 'solvemedia'){
				if(empty($solvemediaresponde)) {
					$captchacorrect = 0;
					$step = 5;
				}
			}
			if($_POST['captchaservice'] == 'recaptcha'){
				if(empty($recaptcha)) {
					$captchacorrect = 0;
					$step = 4;
				}
			}
			if (isset($_POST["captchaservice"])) {
				$captchaservice = $_POST["captchaservice"];
				if ($captchaservice == 'solvemedia'){
					if (empty($solvemediaresponde)){
						echo"<script type='text/javascript'>window.location.replace('$mainurl$code'); </script>";
					}
				}else if ($captchaservice == 'recaptcha'){
					if (empty($recaptcha)){
						echo"<script type='text/javascript'>window.location.replace('$mainurl$code'); </script>";
					}
				}
			}
			if(!empty($recaptcha))
			{
				$google_url = "https://www.google.com/recaptcha/api/siteverify";
				$secret = $RCsecret;
				$ip = $_SERVER['REMOTE_ADDR'];
				$url = $google_url."?secret=".$secret."&response=".$recaptcha."&remoteip=".$ip;
				$res = getCurlData($url);
				$res = json_decode($res, true);
				if($res['success'])
				{
					$captchacorrect = 1;
					$step = 5;
				}else{
					$captchacorrect = 0;
					$step = 4;
				}
			}else{
				require_once("include/solvemedialib.php");
				$privkey = $captchav;
				$hashkey = $captchah;
				$solvemedia_response = solvemedia_check_answer($privkey,
						$_SERVER["REMOTE_ADDR"],
						$_POST["adcopy_challenge"],
						$_POST["adcopy_response"],
						$hashkey);
				if (!$solvemedia_response->is_valid) {
					$captchacorrect = 0;
					$step = 4;
				}
				else{
					$captchacorrect = 1;
					$step = 5;
				}
			}
		}
	}
//END ALL FUNCTIONS
}

if(8 == 8) {
	if($adctype == 'none'){
		$step = 5;
	}
}

}

//STEP ASSIGN
	if(empty($step)){
		if(!empty($password)) {
			if(empty($usingpass)) {
				$step = 1;
			}else {
				if($password == $usingpass) {
					//ROVNAKE HESLA >> ĎALŠÍ KROK
					if($checkcaptchastatus == 1 || $RCactive == 1) {
						if(empty($step)) {					
							$step = 2;
							if($adctype != 'banner'){
								$step = 3;
							}
						}
					}else{
						$step = 3;
					}
				}else {
					if(empty($step)) {
						$step = 1;
						$error = 'badpassword';
					}
				}
			}
		}else if($checkcaptchastatus == 1 || $RCactive == 1) {
						if(empty($step)) {					
							$step = 2;
							if($adctype != 'banner'){
								$step = 3;
							}
						}
			}else{
						$step = 3;
			}
	}
//STEP ASSIGN