<?
session_start();

include('funciones.php');

if($_SESSION["endtime"]>time())
{
	echo "<span style=\"border: medium none; padding: 0pt; font-size: 10pt; font-family: Verdana; font-weight: bold; vertical-align: top; color: #ff0000;\">&nbsp;<img src=\"images/error.png\" align=\"middle\">&nbsp;Ошибка! Просмотр не засчитан</span>";
	exit();
}

if(isset($_SESSION["username"]) && isset($_SESSION["password"]))
{
	require('config.php');

	$user=text($_SESSION["username"]);
$checkuser = mysql_query("SELECT login FROM seelf_autoclicker WHERE login='$user'");
$username_exist = mysql_num_rows($checkuser);

if ($username_exist==2) {
echo "<br /><center><div class='error' style='width:500px;'>Вы нажали на ссылку для автокликеров 2 раза! Ваш Аккаунт Заблокирован!!!</div></center>";
exit();
}

$res=mysql_fetch_assoc(mysql_query("SELECT popal FROM tb_users WHERE username='$user'"));
if($res["popal"]=='ban')
{
  echo "<span style=\"border: medium none; padding: 0pt; font-size: 10pt; font-family: Verdana; font-weight: bold; vertical-align: top; color: #ff0000;\">";
  echo "<img src=\"images/error.png\" align=\"middle\"> Вы не можете просматривать ссылки,т.к. Вы забанены за использование сторонних программ в системе!</span>";
  exit();
}

	if(preg_match("|^[\d]*$|",$_SESSION['ad']))
	{
		$adse=intval($_SESSION["ad"]);
$chudoad=mysql_result(mysql_query("SELECT price FROM tb_config WHERE item='antiauto'"),0,0);
if($adse==$chudoad)
{
  $nowtime=time();
  mysql_query("UPDATE tb_users SET popal=popal+1, popadostime='$nowtime' WHERE username='$user'");
}

	}else{
		echo "<span style=\"border: medium none; padding: 0pt; font-size: 10pt; font-family: Verdana; font-weight: bold; vertical-align: top; color: #ff0000;\">&nbsp;<img src=\"images/error.png\" align=\"middle\">&nbsp;Ошибка! Просмотр не засчитан!</span>";
		exit();
	}

	$res=mysql_query("select dopsec from tb_ads where id='$adse'");
	$res=mysql_fetch_assoc($res);

	if($res["dopsec"]=='2')
	{
		$captcha=strtolower($_POST["code"]);
		$ver=strtolower($_SESSION["texto"]);
unset($_SESSION["texto"]);
	}

	if(!isset($_GET["ds"]))
	{
	   $vid=intval($_SESSION["vid"]);

	   if($vid == 0){
		 $checkad = mysql_query("SELECT id FROM tb_ads WHERE id='$adse' and tipo='ads' and members < plan and status = '0'");
       }else{
       	 $checkad = mysql_query("SELECT id FROM tb_ads WHERE id='$adse' and tipo='ads' and status = '0' and fechainicia + (plan*86400) > '".time()."'");
       }
		$ad_exist = mysql_num_rows($checkad);

		if ($ad_exist<1)
		{
			echo "<span style=\"border: medium none; padding: 0pt; font-size: 10pt; font-family: Verdana; font-weight: bold; vertical-align: top; color: #ff0000;\">&nbsp;<img src=\"images/error.png\" align=\"middle\">&nbsp;Ссылка не существует</span>";
			exit();
		}

		if($res["dopsec"]=='2')
		{
			?>
			 <form action='vls.php?view=ok&ds=clicked' method='post'>
      <table border="0" cellpadding="0" cellspacing="0" align="left" style="padding:3px 0px 0px 3px;"><tr>
      <td valign="middle" style="padding-right:4px"><img src="image.php?<?php echo $res; ?>" class="imgcap" alt="" /></td>
      <td valign="middle"><input type="hidden" name="code" value="0">
       <input class="serfnum" name="code" value="1" onclick="vernum(this.form,1); submit();" type="submit">
       <input class="serfnum" name="code" value="2" onclick="vernum(this.form,2); submit();" type="submit">
       <input class="serfnum" name="code" value="3" onclick="vernum(this.form,3); submit();" type="submit">
       <input class="serfnum" name="code" value="4" onclick="vernum(this.form,4); submit();" type="submit"><br />
       <input class="serfnum" name="code" value="5" onclick="vernum(this.form,5); submit();" type="submit">
       <input class="serfnum" name="code" value="6" onclick="vernum(this.form,6); submit();" type="submit">
       <input class="serfnum" name="code" value="7" onclick="vernum(this.form,7); submit();" type="submit">
       <input class="serfnum" name="code" value="8" onclick="vernum(this.form,8); submit();" type="submit"></td>
     </tr></table>
</form><?

			exit();
	}

	}

	if(!isset($_GET["ds"]) or ($_GET["ds"]=='clicked' and $ver==$captcha))
{

		$adse=intval($_SESSION["ad"]);

		$querye = mysql_query("SELECT * FROM tb_ads WHERE user = '$user' and ident= '$adse'") or die(mysql_error());
		$rowe = mysql_fetch_assoc($querye);

		$time=$rowe['visitime'];

		$crok1 = date(time());
		$crok2 = date($time + 86400);

		if($crok1 >= $crok2)
		{
			$checkvisit = mysql_query("SELECT * FROM tb_ads WHERE user='$user' and ident='$adse'");
			$referer_visit = mysql_num_rows($checkvisit);
                        $vid=intval($_SESSION["vid"]);
                                 if($vid == 0){
        $checkad = mysql_query("SELECT id FROM tb_ads WHERE id='$adse' and tipo='ads' and members < plan and status = '0'");
            }else{
              $checkad = mysql_query("SELECT id FROM tb_ads WHERE id='$adse' and tipo='ads' and status = '0' and fechainicia + (plan*86400) > '".time()."'");
            }
      $ad_exist = mysql_num_rows($checkad);

      if ($ad_exist<1)
   {
    echo "<span style=\"border: medium none; padding: 0pt; font-size: 10pt; font-family: Verdana; font-weight: bold; vertical-align: top; color: #ff0000;\">&nbsp;<img src=\"images/error.png\" align=\"middle\">&nbsp;Ссылка не существует</span>";
    exit();
   }
			$sqlz = "SELECT * FROM tb_ads WHERE id='$adse'";
			$resultz = mysql_query($sqlz);
			$myrowz = mysql_fetch_assoc($resultz);
			$numero=$myrowz["members"];

			$jo=$myrowz["plan"];



			if ($referer_visit>0)
			{
				$queryzx = "UPDATE tb_ads SET visitime='$crok1' WHERE user='$user' and ident='$adse' and tipo='visit'";
				mysql_query($queryzx) or die(mysql_error());
			}else{
				$queryzz = "INSERT INTO tb_ads (user, tipo, ident, visitime) VALUES('$user','visit','$adse','$crok1')";
				mysql_query($queryzz) or die(mysql_error());
			}

			require('karera/setup_status.php');

			$referer = mysql_query("SELECT * FROM `tb_users` WHERE `username` = '$user'");
			$refer = mysql_fetch_assoc($referer);

			$click = mysql_query("SELECT * FROM tb_config WHERE item='click' and howmany='1'");
            $cl = mysql_fetch_assoc($click);
            $cl_price = $cl["price"];
            $nas4et = 0;

            if($refer["blockpay"] == 1){

              echo "<span style=\"border: medium none; padding: 0pt; font-size: 10pt; font-family: Verdana; font-weight: bold; vertical-align: top; color: #ff0000;\"> <img src=\"images/error.png\" align=\"middle\"> Вы нарушили правила системы и не можете больше зарабатывать!</span>";
              exit();

            }

            if($refer["referer"] != ""){

              $referer1 = mysql_query("SELECT * FROM `tb_users` WHERE `username` = '".$refer["referer"]."'");
			  $refer1 = mysql_fetch_assoc($referer1);

			  $doxod_ref1 = $cl_price * ($st_users[$refer1["ball_status"]]["dox_ref"]/100);

			  if ($refer["ref_percent"] == "0"){

                $percent = $refer["ref_per_all"];

              }else{

                $percent = $refer["ref_percent"];

              }

              if ($percent == "0"){

                $money_refer1 = $doxod_ref1;

              } else {

                $nas4et = ($doxod_ref1/100) * $percent;
                $money_refer1 = $doxod_ref1 - $nas4et;

              }

              mysql_query("UPDATE tb_users
              SET `referalvisits` = `referalvisits` + '1',  `money` = `money` + '$money_refer1'
              WHERE username='".$refer["referer"]."'");

              if($refer1["referer"] != ""){

                $referer2 = mysql_query("SELECT * FROM `tb_users` WHERE `username` = '".$refer1["referer"]."'");
			    $refer2 = mysql_fetch_assoc($referer2);

			    $doxod_ref2 = $cl_price * ($st_users[$refer2["ball_status"]]["dox_ref2"]/100);

                mysql_query("UPDATE `tb_users`
                SET `referalvisits2` = `referalvisits2` + '1', `money` = `money` + '$doxod_ref2'
                WHERE `username` = '".$refer1["referer"]."'");

              }

            }

            $pluss = $nas4et + $cl_price;
            mysql_query("UPDATE `tb_users`
            SET `visits` = `visits` + '1', `doxod` = `doxod` + '$money_refer1' + '$doxod_ref2', `money` = `money` + '$pluss'
            WHERE username='$user'");

                  //Конкурс серфинг
			      $refer_kon=mysql_query("SELECT id
                  FROM still_konkurs_ref
                  WHERE idref=(SELECT id FROM tb_users WHERE username=(SELECT referer FROM tb_users WHERE username='$user')) and vid='2'
                  and time_start < '".time()."' and time_start+(time_end*86400) > '".time()."' LIMIT 1");
                  if(mysql_num_rows($refer_kon) > 0){
                    $rk = mysql_fetch_assoc($refer_kon);

                    $top_kon=mysql_query("SELECT id
                    FROM still_top_kon WHERE idk='".$rk["id"]."' and  idu=(SELECT id FROM tb_users WHERE username='$user') LIMIT 1");
                    if(mysql_num_rows($top_kon) > 0){
                      $tk = mysql_fetch_assoc($top_kon);

                      mysql_query("UPDATE still_top_kon SET danie=danie+'1' WHERE id='".$tk["id"]."'");

                    }else{

                      mysql_query("insert into still_top_kon (idu,danie,idk) values ((SELECT id FROM tb_users WHERE username='$user'),'1','".$rk["id"]."')");

                    }

                  }

			$sqlex = "UPDATE tb_ads SET members='$numero'+1 WHERE id='$adse'";
			$resultex = mysql_query($sqlex);


			$res=mysql_query("select * from tb_comp where param='3'");
			if(mysql_num_rows($res)>0)
			{
				while($row=mysql_fetch_assoc($res))
				{
					$id=$row["id"];
					$lidertype=$row["lidername"];
					$t=time();
					$sd=strtotime($row["startdate"]);
					$ed=strtotime($row["enddate"]);
					if($t>$sd && $t<$ed)
					{
						if($lidertype=='1')
						{
							$res1=mysql_query("select wmid from tb_users where username='$user'");
							if(mysql_num_rows($res1)>0){$res1=mysql_fetch_assoc($res1); $lidername=$res1["wmid"];}else{$lidername='';}
						}else{
							$lidername=$user;
						}
						if($lidername!='')
						{
							$res1=mysql_query("select * from tb_compdata where idk='$id' and user='$lidername'");
							if(mysql_num_rows($res1)>0)
							{
								$res1=mysql_Fetch_assoc($res1);
								$resvalue=$res1["resvalue"]+1;
								mysql_query("update tb_compdata set resvalue='$resvalue' where id='".$res1["id"]."'");
							}else{
								mysql_query("insert into tb_compdata (idk,user,resvalue) values ('$id','$lidername','1')");
							}
						}
					}
				}
			}



echo "
<table>
<tr>
<td><img src=\"images/ok.png\" align=\"absmiddle\" style='padding-right: 3px'></td><td><span style='color:green; font-size: 10pt;'> Спасибо за посещение!</span><br /><span style='color:#818181; font-size: 8pt;'> Просмотр засчитан!</span></td></tr></table>";
		?>
<script type="text/javascript">
window.top.location.replace("<?=$_SESSION["urlsite"]; ?>");
</script> 
		<? }else{
			echo "<span style=\"border: medium none; padding: 0pt; font-size: 10pt; font-family: Verdana; font-weight: bold; vertical-align: top; color: #ff0000;\">&nbsp;<img src=\"images/error.png\" align=\"middle\">&nbsp;Вы уже просматривали эту ссылку!</span>";
		}
	}else{
		echo "<span style=\"border: medium none; padding: 0pt; font-size: 10pt; font-family: Verdana; font-weight: bold; vertical-align: top; color: #ff0000;\">&nbsp;<img src=\"images/error.png\" align=\"middle\">&nbsp;Ошибка! Просмотр не засчитан</span>";
	}
	mysql_close($con);
}else{

	$juaz=date("n/j/Y H:i:s", $crok1);
	$juaze=date("n/j/Y H:i:s", $crok2);

	echo "<span style=\"border: medium none; padding: 0pt; font-size: 10pt; font-family: Verdana; font-weight: bold; vertical-align: top; color: #ff0000;\">&nbsp;<img src=\"images/error.png\" align=\"middle\">&nbsp;Вам необходимо авторизоваться</span>";
}
?>
<?php
$a = rand(1,20);
$b = rand(1,20);
$_SESSION["itog"] = $a + $b;
?>