<?
header("Content-type: text/html; charset=windows-1251");
$service = $_POST['service'];
$id = intval($_POST['id']);

IF($_POST['LMI_PREREQUEST']==1)
{
	echo "YES";
}else{
	$secret_key="yde83hec7h";

	$common_string = $_POST['LMI_PAYEE_PURSE'].$_POST['LMI_PAYMENT_AMOUNT'].$_POST['LMI_PAYMENT_NO'].
	$_POST['LMI_MODE'].$_POST['LMI_SYS_INVS_NO'].$_POST['LMI_SYS_TRANS_NO'].
	$_POST['LMI_SYS_TRANS_DATE'].$secret_key.$_POST['LMI_PAYER_PURSE'].$_POST['LMI_PAYER_WM'];

	$hash = strtoupper(md5($common_string));

	if($hash!=$_POST['LMI_HASH']) exit;

	$oplata=$_POST['LMI_PAYMENT_AMOUNT'];
	$wmipoc = $_POST['LMI_PAYER_WM'];

	if($service=="advbanner")
	{
		require('config.php');
		$res=mysql_query("select * from tb_addbanner where id='$id'");
		$row=mysql_fetch_assoc($res);
		$urlsite=$row["urlsite"]; $urlpic=$row["urlpic"]; $t=time(); $plan=$row["plan"]; $wmid=$row["wmid"];
$joindate=date("d.m.Y");
$timenow=time();

		$res=mysql_query("select price from tb_config where item='advbanner'");
		$res=mysql_fetch_assoc($res);
		$nado=$res["price"]*$plan;
		$nado=round($nado,2);

		if($nado<=$oplata)
		{
		mysql_query("INSERT INTO tb_advban (urlsite,urlpic,begindate,numdays,wmid) VALUES('$urlsite','$urlpic','$t','$plan','$wmid')");
mysql_query("INSERT INTO tb_zar (wmid, kol, amount, date, date2, tip) VALUES('$wmipoc', '$plan дней', '$nado', '$timenow', '$joindate', 'Баннер 468x60')") or die(mysql_error());

		mysql_query("DELETE FROM tb_addbanner WHERE id='$id'");

		$res=mysql_query("select * from tb_comp where param='6'");
		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=='0')
					{
						$res1=mysql_query("select username from tb_users where wmid='$wmipoc'");
						if(mysql_num_rows($res1)>0){$res1=mysql_fetch_assoc($res1); $lidername=$res1["username"];}else{$lidername='';}
					}else{
						$lidername=$wmipoc;
					}
					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"]+$nado;
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("update tb_compdata set resvalue='$resvalue', purse='$purse' where id='".$res1["id"]."'");
						}else{
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("insert into tb_compdata (idk,user,resvalue,purse) values ('$id','$lidername','$nado','$purse')");
						}
					}
				}
			}
		}
		}
		mysql_close($con);
	}

if($service=="advbannerclick")
 {
 require('config.php');
 $res=mysql_query("select * from tb_advbanclick where id='$id'");
 $row=mysql_fetch_assoc($res);
 $money=$row["money"];
 $plan = $row["plan"];
 $wmid=$row["wmid"];
$joindate=date("d.m.Y");
$timenow=time();
 $nado=round($money,2);

 if($nado<=$oplata)
 {
 mysql_query("update tb_advbanclick set status='1' where id='$id' ");
 mysql_query("INSERT INTO tb_zar (wmid, kol, amount, date, date2, tip) VALUES('$wmipoc', '$plan кликов', '$nado', '$timenow', '$joindate', 'Баннер 468x60')") or die(mysql_error());

 		$res=mysql_query("select * from tb_comp where param='6'");
		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=='0')
					{
						$res1=mysql_query("select username from tb_users where wmid='$wmipoc'");
						if(mysql_num_rows($res1)>0){$res1=mysql_fetch_assoc($res1); $lidername=$res1["username"];}else{$lidername='';}
					}else{
						$lidername=$wmipoc;
					}
					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"]+$nado;
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("update tb_compdata set resvalue='$resvalue', purse='$purse' where id='".$res1["id"]."'");
						}else{
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("insert into tb_compdata (idk,user,resvalue,purse) values ('$id','$lidername','$nado','$purse')");
						}
					}
				}
			}
		}
		}
   mysql_close($con);
  }

if($service=="mail_mess")
	{
		require('config.php');
		$sql="SELECT * FROM tb_adsmail2 WHERE id='$id'";
        $result=mysql_query($sql);
        $row=mysql_fetch_assoc($result);
        $nado=round($row[summ],2);
$joindate=date("d.m.Y");
$timenow=time();

		if($nado<=$oplata){
		 $add="INSERT INTO tb_adsmail (user, plan, members, link, title, text, time, pod) VALUES ('$row[user]', '$row[plan]', '0', '$row[link]', '$row[title]','$row[text]','$row[time]','$row[pod]')";
         mysql_query($add);
mysql_query("INSERT INTO tb_zar (wmid, kol, amount, date, date2, tip) VALUES('$wmipoc', '$row[plan] шт.', '$nado', '$timenow', '$joindate', 'Платное письмо')") or die(mysql_error());

		 mysql_query("delete from tb_adsmail2 where id='$id'");
		$res=mysql_query("select * from tb_comp where param='6'");
		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=='0')
					{
						$res1=mysql_query("select username from tb_users where wmid='$wmipoc'");
						if(mysql_num_rows($res1)>0){$res1=mysql_fetch_assoc($res1); $lidername=$res1["username"];}else{$lidername='';}
					}else{
						$lidername=$wmipoc;
					}
					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"]+$nado;
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("update tb_compdata set resvalue='$resvalue', purse='$purse' where id='".$res1["id"]."'");
						}else{
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("insert into tb_compdata (idk,user,resvalue,purse) values ('$id','$lidername','$nado','$purse')");
						}
					}
				}
			}
		}
		}

		mysql_close($con);
	}

if($service=="popol")
	{   require('config.php');
$joindate=date("d.m.Y");
$timenow=time();
        $money_cab = mysql_query("SELECT * FROM `still_cap_bal` WHERE `wmid_cab` = '$wmipoc'");
        if(mysql_num_rows($money_cab)==0){
          mysql_query("INSERT INTO `still_cap_bal` (`wmid_cab`, `balans`, `date`) VALUES ('$wmipoc', '$oplata', CURDATE())");
        }else{
          mysql_query("UPDATE `still_cap_bal` SET `balans` = `balans`+'$oplata' WHERE `wmid_cab` = '$wmipoc'");
          mysql_query("INSERT INTO tb_zar (wmid, kol, amount, date, date2, tip) VALUES('$wmipoc', '---', '$oplata', '$timenow', '$joindate', 'Пополнение кабинки рекламодателя')") or die(mysql_error());
        }

        $ball_cab = mysql_query("SELECT * FROM `tb_users` WHERE `wmid` = '$wmipoc'");
        if(mysql_num_rows($ball_cab) > 0){
         $timenow=time();
          require('karera/setup_ball.php');
          $ref_u = mysql_fetch_assoc($ball_cab);

          $ball_u = intval($oplata/10);

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

            $ball_r = $ball_u * $ball["ref_reclam"];
            mysql_query("UPDATE `tb_users` SET `ball` = `ball`+'$ball_r' WHERE `username` = '".$ref_u["referer"]."'");
            mysql_query("INSERT INTO tb_infoball (user, date, ball, referer, mesage) VALUES('".$ref_u["referer"]."', '$timenow', '+ $ball_r', '---', 'Реферер пополнил кабинет рекламодателя')") or die(mysql_error());
          }

          $ball_us = $ball_u * $ball["reclama"];
          mysql_query("UPDATE `tb_users` SET `ball` = `ball`+'$ball_us' WHERE `wmid` = '$wmipoc'");
          mysql_query("INSERT INTO tb_infoball (user, date, ball, referer, mesage) VALUES('$wmipoc', '$timenow', '+ $ball_us', '---', 'Вы пополнили кабинет рекламодателя')") or die(mysql_error());
        }


		$res=mysql_query("select * from tb_comp where param='6'");
		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=='0')
					{
						$res1=mysql_query("select username from tb_users where wmid='$wmipoc'");
						if(mysql_num_rows($res1)>0){$res1=mysql_fetch_assoc($res1); $lidername=$res1["username"];}else{$lidername='';}
					}else{
						$lidername=$wmipoc;
					}
					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"]+$oplata;
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("update tb_compdata set resvalue='$resvalue', purse='$purse' where id='".$res1["id"]."'");
						}else{
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("insert into tb_compdata (idk,user,resvalue,purse) values ('$id','$lidername','$oplata','$purse')");
						}
					}
				}
			}
		}
		mysql_close($con);
	}

if($service=="banner")
	{
		require('config.php');
		$res=mysql_query("select * from tb_addban where id='$id'");
		$row=mysql_fetch_assoc($res);
		$urlsite=$row["urlsite"]; $urlpic=$row["urlpic"]; $t=time(); $plan=$row["plan"]; $wmid=$row["wmid"];
$joindate=date("d.m.Y");
$timenow=time();
		$res=mysql_query("select price from tb_config where item='banner'");
		$res=mysql_fetch_assoc($res);
		$nado=$res["price"]*$plan;
		$nado=round($nado,2);

		if($nado<=$oplata)
		{
		mysql_query("INSERT INTO tb_ban (urlsite,urlpic,begindate,numdays,wmid) VALUES('$urlsite','$urlpic','$t','$plan','$wmid')");
mysql_query("INSERT INTO tb_zar (wmid, kol, amount, date, date2, tip) VALUES('$wmipoc', '$plan  дней', '$nado', '$timenow', '$joindate', 'Баннер 100x100')") or die(mysql_error());


		mysql_query("DELETE FROM tb_addban WHERE id='$id'");

		$res=mysql_query("select * from tb_comp where param='6'");
		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=='0')
					{
						$res1=mysql_query("select username from tb_users where wmid='$wmipoc'");
						if(mysql_num_rows($res1)>0){$res1=mysql_fetch_assoc($res1); $lidername=$res1["username"];}else{$lidername='';}
					}else{
						$lidername=$wmipoc;
					}
					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"]+$nado;
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("update tb_compdata set resvalue='$resvalue', purse='$purse' where id='".$res1["id"]."'");
						}else{
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("insert into tb_compdata (idk,user,resvalue,purse) values ('$id','$lidername','$nado','$purse')");
						}
					}
				}
			}
		}
		}
		mysql_close($con);
	}

if($service=="paket_adv")
   {
     require('config.php');
     $pak_adv=mysql_query("select * from tb_paket_adv where id='$id'");
     $p_adv=mysql_fetch_assoc($pak_adv);

     $paket=mysql_query("select * from tb_paket where id='".$p_adv["paket"]."'");
     $pak=mysql_fetch_assoc($paket);
     $nado=round($pak["money"],2);
$joindate=date("d.m.Y");
$timenow=time();
     if($nado<=$oplata)
     {
       $dinam = explode("*_*_*", $p_adv["dinam"]);
       $banner468 = explode("*_*_*", $p_adv["banner468"]);
       $psevdo = explode("*_*_*", $p_adv["psevdo"]);
       $frame = explode("*_*_*", $p_adv["frame"]);
       $statlink = explode("*_*_*", $p_adv["statlink"]);
       $textob = explode("*_*_*", $p_adv["textob"]);
       //Динамика
       mysql_query("INSERT INTO tb_ads (`fechainicia`, `paypalemail`, `plan`, `url`, `description`, `tipo`, `highlight`, `timer`, `dopsec`, `vip`)
       VALUES('".time()."','".$p_adv["schet"]."','".$pak["dinam"]."','".$dinam[0]."','".$dinam[1]."','ads','0', '".$pak["t_dinam"]."', '2','".$pak["r_dinam"]."')");
       //Статистика динамики
       $tim_z=date("d.m.Y", time());
       $dob_z=mysql_query("SELECT count(id) as kol FROM tb_adsdate WHERE data='$tim_z'");
       $dob=mysql_fetch_assoc($dob_z);
       if($dob["kol"]>0){mysql_query("UPDATE tb_adsdate SET kolvo=kolvo+1 WHERE data='$tim_z'");}else{mysql_query("INSERT INTO tb_adsdate (`data`, `kolvo`) VALUES ('$tim_z','1')");}
       //Баннер 468х60
       mysql_query("INSERT INTO tb_advban (`urlsite`, `urlpic`, `begindate`, `numdays`, `wmid`)
       VALUES('".$banner468[0]."','".$banner468[1]."','".time()."','".$pak["banner468"]."','".$p_adv["schet"]."')");
       //Псевдо ссылки
       mysql_query("INSERT INTO tb_psevdolinks (`url`, `description`, `wmid`, `begindate`, `numdays`)
       VALUES('".$psevdo[0]."','".$psevdo[1]."','".$p_adv["schet"]."','".time()."','".$pak["psevdo"]."')");
       //Реклама во фрейме
       $f_plan=time()+($pak["frame"]*86400);
       mysql_query("INSERT INTO tb_frameads (`wmid`, `link`, `text`, `enddate`)
       VALUES('".$p_adv["schet"]."','".$frame[0]."','".$frame[1]."','$f_plan')");
       //Статические ссылки
       mysql_query("INSERT INTO tb_statlinks (`url`, `description`, `wmid`, `leftblock`, `begindate`, `numdays`)
       VALUES('".$statlink[0]."','".$statlink[1]."','".$p_adv["schet"]."','0','".time()."','".$pak["statlink"]."')");
       //Текстовое объявление
       $t_plan=time()+($pak["textob"]*86400);
       mysql_query("INSERT INTO tb_textads (`wmid`, `url`, `text`, `enddate`)
       VALUES('".$p_adv["schet"]."','".$textob[0]."','".$textob[1]."','$t_plan')");
       mysql_query("INSERT INTO tb_zar (wmid, kol, amount, date, date2, tip) VALUES('$wmipoc', '".$p_adv["paket"]."', '$nado', '$timenow', '$joindate', 'Пакет рекламы')") or die(mysql_error());
       mysql_query("DELETE FROM tb_paket_adv WHERE id='$id'");


		$res=mysql_query("select * from tb_comp where param='6'");
		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=='0')
					{
						$res1=mysql_query("select username from tb_users where wmid='$wmipoc'");
						if(mysql_num_rows($res1)>0){$res1=mysql_fetch_assoc($res1); $lidername=$res1["username"];}else{$lidername='';}
					}else{
						$lidername=$wmipoc;
					}
					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"]+$nado;
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("update tb_compdata set resvalue='$resvalue', purse='$purse' where id='".$res1["id"]."'");
						}else{
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("insert into tb_compdata (idk,user,resvalue,purse) values ('$id','$lidername','$nado','$purse')");
						}
					}
				}
			}
		}
		    //конец
     }
   }


if($service=="advbanner200")
	{
		require('config.php');
		$res=mysql_query("select * from tb_addbanner200 where id='$id'");
		$row=mysql_fetch_assoc($res);
		$urlsite=$row["urlsite"]; $urlpic=$row["urlpic"]; $t=time(); $plan=$row["plan"]; $wmid=$row["wmid"];
$joindate=date("d.m.Y");
$timenow=time();
		$res=mysql_query("select price from tb_config where item='advbanner200'");
		$res=mysql_fetch_assoc($res);
		$nado=$res["price"]*$plan;
		$nado=round($nado,2);

		if($nado<=$oplata)
		{
		mysql_query("INSERT INTO tb_advban200 (urlsite,urlpic,begindate,numdays,wmid) VALUES('$urlsite','$urlpic','$t','$plan','$wmid')");
		mysql_query("INSERT INTO tb_zar (wmid, kol, amount, date, date2, tip) VALUES('$wmipoc', '$plan  дней', '$nado', '$timenow', '$joindate', 'Баннер 200x300')") or die(mysql_error());
mysql_query("DELETE FROM tb_addbanner200 WHERE id='$id'");

		$res=mysql_query("select * from tb_comp where param='6'");
		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=='0')
					{
						$res1=mysql_query("select username from tb_users where wmid='$wmipoc'");
						if(mysql_num_rows($res1)>0){$res1=mysql_fetch_assoc($res1); $lidername=$res1["username"];}else{$lidername='';}
					}else{
						$lidername=$wmipoc;
					}
					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"]+$nado;
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("update tb_compdata set resvalue='$resvalue', purse='$purse' where id='".$res1["id"]."'");
						}else{
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("insert into tb_compdata (idk,user,resvalue,purse) values ('$id','$lidername','$nado','$purse')");
						}
					}
				}
			}
		}
		}
		mysql_close($con);
	}


if($service=="advpsevdo")
{
require('config.php');
$sql="select price from tb_config where item='psevdoweek1'";
$res=mysql_query($sql);
$psevdoweek1=mysql_result($res,0,0);

$sql="select price from tb_config where item='psevdoweek2'";
$res=mysql_query($sql);
$psevdoweek2=mysql_result($res,0,0);

$sql="select price from tb_config where item='psevdoweek3'";
$res=mysql_query($sql);
$psevdoweek3=mysql_result($res,0,0);

$sql="select price from tb_config where item='psevdoweek4'";
$res=mysql_query($sql);
$psevdoweek4=mysql_result($res,0,0);

$res=mysql_query("select * from tb_psevdoreq where id='$id'");
$row=mysql_fetch_assoc($res);
$joindate=date("d.m.Y");
$timenow=time();
$urlsite=$row["url"];
$description=$row["description"];
$wmid=$row["wmid"];
$plan=$row["plan"];
$t=time();

$nado=$plan*$psevdoweek1;
$nado = round($nado,2);
if($nado<=$oplata)
{
$query = "INSERT INTO tb_psevdolinks (url,description,wmid,begindate,numdays) VALUES('$urlsite','$description','$wmid','$t','$plan')";
mysql_query("INSERT INTO tb_zar (wmid, kol, amount, date, date2, tip) VALUES('$wmipoc', '$plan дней', '$nado', '$timenow', '$joindate', 'Псевдодинамическая ссылка')") or die(mysql_error());
mysql_query($query) or die(mysql_error());


$queryz = "DELETE FROM tb_psevdoreq WHERE id='$id'";
mysql_query($queryz) or die(mysql_error());

		$res=mysql_query("select * from tb_comp where param='6'");
		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=='0')
					{
						$res1=mysql_query("select username from tb_users where wmid='$wmipoc'");
						if(mysql_num_rows($res1)>0){$res1=mysql_fetch_assoc($res1); $lidername=$res1["username"];}else{$lidername='';}
					}else{
						$lidername=$wmipoc;
					}
					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"]+$nado;
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("update tb_compdata set resvalue='$resvalue', purse='$purse' where id='".$res1["id"]."'");
						}else{
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("insert into tb_compdata (idk,user,resvalue,purse) values ('$id','$wmid','$nado','$purse')");
						}
					}
				}
			}
		}

}
mysql_close();

}

	if($service=="advframeads")
	{
		require('config.php');
		$res=mysql_query("select * from tb_frameadsreq where id='$id'");
		$row=mysql_fetch_assoc($res);
		$wmid=$row["wmid"]; $linksite=$row["link"]; $text=$row["text"]; $plan=$row["plan"]; $t=time(); $t=$t+$plan*24*3600;
$joindate=date("d.m.Y");
$timenow=time();
		$res=mysql_query("select price from tb_config where item='wmframeads'");
		$res=mysql_fetch_assoc($res);
		$nado=$res["price"]*$plan;
		$nado=round($nado,2);

		if($nado<=$oplata)
		{
		$query = "INSERT INTO tb_frameads (wmid,link,text,enddate) VALUES('$wmid','$linksite','$text','$t')";
		mysql_query("INSERT INTO tb_zar (wmid, kol, amount, date, date2, tip) VALUES('$wmipoc', '---', '$nado', '$timenow', '$joindate', 'Фрейм ссылка')") or die(mysql_error());
mysql_query($query) or die(mysql_error());


		$queryz = "DELETE FROM tb_frameadsreq WHERE id='$id'";
		mysql_query($queryz) or die(mysql_error());

          $res=mysql_query("select * from tb_comp where param='6'");
          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=='0')
                         {
			 $res1=mysql_query("select username from tb_users where wmid='$wmipoc'");
                         if(mysql_num_rows($res1)>0){$res1=mysql_fetch_assoc($res1); $lidername=$res1["username"];}else{$lidername='';}
                         }else{
                         $lidername=$wmipoc;
                         }
                         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"]+$nado;
                                   $purse=$_POST['LMI_PAYEE_PURSE'];
                                   mysql_query("update tb_compdata set resvalue='$resvalue', purse='$purse' where id='".$res1["id"]."'");
                              }else{
                                   $purse=$_POST['LMI_PAYEE_PURSE'];
                                   mysql_query("insert into tb_compdata (idk,user,resvalue,purse) values ('$id','$lidername','$nado','$purse')");
                              }
                         }
                    }
               }
          }
          }
          mysql_close($con);
     }

	if($service=="advstatlink")
	{
		require('config.php');
		$res=mysql_query("select * from tb_statlinksreq where id='$id'");
		$row=mysql_fetch_assoc($res);
		$urlsite=$row["url"]; $description=$row["description"]; $wmid=$row["wmid"]; $lb=$row["leftblock"];  $cvet=$row["cvet"]; $t=time(); $plan=$row["plan"];

		$res=mysql_query("select price from tb_config where item='advstatlink'");
		$res=mysql_fetch_assoc($res);
		$nado=$res["price"]*$plan;

		$res=mysql_query("select price from tb_config where item='leftblock'");
		$res=mysql_fetch_assoc($res);
		$res=mysql_query("select price from tb_config where item='cvet_link_stat'");
		$res=mysql_fetch_assoc($res);
		$nado=$nado+$lb*$res["price"]+$cvet*$res["price"];
		$nado=round($nado,2);
$joindate=date("d.m.Y");
$timenow=time();
		if($nado<=$oplata)
		{
		$query = "INSERT INTO tb_statlinks (url,description,wmid,cvet,leftblock,begindate,numdays) VALUES('$urlsite','$description','$wmid','$cvet','$lb','$t','$plan')";
		mysql_query("INSERT INTO tb_zar (wmid, kol, amount, date, date2, tip) VALUES('$wmipoc', '$plan дней', '$nado', '$timenow', '$joindate', 'Статическая ссылка')") or die(mysql_error());
                mysql_query($query) or die(mysql_error());


		$queryz = "DELETE FROM tb_statlinksreq WHERE id='$id'";
		mysql_query($queryz) or die(mysql_error());

		$res=mysql_query("select * from tb_comp where param='6'");
		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=='0')
					{
						$res1=mysql_query("select username from tb_users where wmid='$wmipoc'");
						if(mysql_num_rows($res1)>0){$res1=mysql_fetch_assoc($res1); $lidername=$res1["username"];}else{$lidername='';}
					}else{
						$lidername=$wmipoc;
					}
					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"]+$oplata;
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("update tb_compdata set resvalue='$resvalue', purse='$purse' where id='".$res1["id"]."'");
						}else{
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("insert into tb_compdata (idk,user,resvalue,purse) values ('$id','$lidername','$oplata','$purse')");
						}
					}
				}
			}
		}
		}
		mysql_close($con);
	}

if($service=="advlink")
	{
		require('config.php');
		$res=mysql_query("select * from tb_statlinkreq where id='$id'");
		$row=mysql_fetch_assoc($res);
		$urlsite=$row["url"]; $description=$row["description"]; $text=$row["text"]; $wmid=$row["wmid"]; $lb=$row["cvet"]; $t=time(); $plan=$row["plan"];

		$res=mysql_query("select price from tb_config where item='adtatlink'");
		$res=mysql_fetch_assoc($res);
		$nado=$res["price"]*$plan;
$joindate=date("d.m.Y");
$timenow=time();
		$res=mysql_query("select price from tb_config where item='cvet_link'");
		$res=mysql_fetch_assoc($res);
		$nado=$nado+$lb*$res["price"];
		$nado=round($nado,2);

		if($nado<=$oplata)
		{
		$query = "INSERT INTO tb_statlink (url,description,text,wmid,cvet,begindate,numdays) VALUES('$urlsite','$description','$text','$wmid','$lb','$t','$plan')";
		mysql_query("INSERT INTO tb_zar (wmid, kol, amount, date, date2, tip) VALUES('$wmipoc', '$plan дней', '$nado', '$timenow', '$joindate', 'Контекстная ссылка')") or die(mysql_error());
                mysql_query($query) or die(mysql_error());


		$queryz = "DELETE FROM tb_statlinkreq WHERE id='$id'";
		mysql_query($queryz) or die(mysql_error());

		$res=mysql_query("select * from tb_comp where param='6'");
		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=='0')
					{
						$res1=mysql_query("select username from tb_users where wmid='$wmipoc'");
						if(mysql_num_rows($res1)>0){$res1=mysql_fetch_assoc($res1); $lidername=$res1["username"];}else{$lidername='';}
					}else{
						$lidername=$wmipoc;
					}
					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"]+$nado;
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("update tb_compdata set resvalue='$resvalue', purse='$purse' where id='".$res1["id"]."'");
						}else{
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("insert into tb_compdata (idk,user,resvalue,purse) values ('$id','$lidername','$nado','$purse')");
						}
					}
				}
			}
		}
		}
		mysql_close($con);
	}

	if($service=="advtextads")
	{
		require('config.php');
		$res=mysql_query("select * from tb_textadsreq where id='$id'");
		$row=mysql_fetch_assoc($res);
		$wmid=$row["wmid"]; $urlsite=$row["url"]; $text=$row["text"]; $plan=$row["plan"]; $t=time(); $t=$t+$plan*24*3600;

		$res=mysql_query("select price from tb_config where item='textads'");
		$res=mysql_fetch_assoc($res);
		$nado=$res["price"]*$plan;
		$nado=round($nado,2);
$joindate=date("d.m.Y");
$timenow=time();
		if($nado<=$oplata)
		{
		$query = "INSERT INTO tb_textads (wmid,url,text,enddate) VALUES('$wmid','$urlsite','$text','$t')";
                mysql_query("INSERT INTO tb_zar (wmid, kol, amount, date, date2, tip) VALUES('$wmipoc', '---', '$nado', '$timenow', '$joindate', 'Текстовое объявление')") or die(mysql_error());
                mysql_query($query) or die(mysql_error());


		$queryz = "DELETE FROM tb_textadsreq WHERE id='$id'";
		mysql_query($queryz) or die(mysql_error());

		$res=mysql_query("select * from tb_comp where param='6'");
		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=='0')
					{
						$res1=mysql_query("select username from tb_users where wmid='$wmipoc'");
						if(mysql_num_rows($res1)>0){$res1=mysql_fetch_assoc($res1); $lidername=$res1["username"];}else{$lidername='';}
					}else{
						$lidername=$wmipoc;
					}
					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"]+$nado;
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("update tb_compdata set resvalue='$resvalue', purse='$purse' where id='".$res1["id"]."'");
						}else{
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("insert into tb_compdata (idk,user,resvalue,purse) values ('$id','$lidername','$nado','$purse')");
						}
					}
				}
			}
		}
		}
		mysql_close($con);
	}

	if($service=="advdynlink1")
	{
		require('config.php');

		$res=mysql_query("select * from tb_advertisers1 where id='$id'");
		$row=mysql_fetch_assoc($res);
		$pemail=$row["pemail"];
                $plan=$row["plan"];
                $url=$row["url"];
                $description=$row["description"];
                $highlight=$row["highlight"];
                $fechainicia=time();
                $timer=$row["timer"];
                $dopsec=$row["dopsec"];
                $vip=$row["vip"];
                $active=$row["act"];
		$nado=$row["summ"];
         	$nado=round($nado,2);
                $joindate=date("d.m.Y");
                $timenow=time();
		if($nado<=$oplata)
		{
		mysql_query("INSERT INTO tb_ads1 (fechainicia, act, paypalemail, plan, url, description, tipo, highlight, timer, dopsec, vip) VALUES('$fechainicia','$active','$pemail','$plan','$url','$description','ads','$highlight', '$timer', '$dopsec','$vip')");
                mysql_query("INSERT INTO tb_zar (wmid, kol, amount, date, date2, tip) VALUES('$wmipoc', '$plan визитов', '$nado', '$timenow', '$joindate', 'Мини ссылка')") or die(mysql_error());


		$queryz = "DELETE FROM tb_advertisers1 WHERE id='$id'";
		mysql_query($queryz) or die(mysql_error());

                $t=date("d.m.Y", time());
		$res=mysql_query("select count(id) as kol from tb_adsdate where data='$t'");
		$res=mysql_fetch_assoc($res);
		if($res["kol"]>0)
		{
			mysql_query("update tb_adsdate set kolvo=kolvo+1 where data='$t'");
		}else{
			mysql_query("insert into tb_adsdate (data,kolvo) values ('$t','1')");
		}

		$res=mysql_query("select * from tb_comp where param='6'");
		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=='0')
					{
						$res1=mysql_query("select username from tb_users where wmid='$wmipoc'");
						if(mysql_num_rows($res1)>0){$res1=mysql_fetch_assoc($res1); $lidername=$res1["username"];}else{$lidername='';}
					}else{
						$lidername=$wmipoc;
					}
					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"]+$nado;
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("update tb_compdata set resvalue='$resvalue', purse='$purse' where id='".$res1["id"]."'");
						}else{
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("insert into tb_compdata (idk,user,resvalue,purse) values ('$id','$lidername','$nado','$purse')");
						}
					}
				}
			}
		}
		}
		mysql_close($con);
	}



	if($service=="advdynlink")
	{
		require('config.php');
		$res=mysql_query("select money,plan,vid from tb_ads where id='$id' and status='1'");
		$row=mysql_fetch_assoc($res);
                $joindate=date("d.m.Y");
                $timenow=time();
                $nadop=$row["money"];
                $plan=$row["plan"];
                $plannn=$row["plan"];
                if($row['vid']==0){$vid_ads="визитов";}else{$vid_ads="дней";}

		if($row["money"]<=$oplata)
		{
		mysql_query("update tb_ads set status='0', fechainicia='".time()."' where id='$id'");
                mysql_query("INSERT INTO tb_zar (wmid, kol, amount, date, date2, tip) VALUES('$wmipoc', '$plannn $vid_ads', '$nadop', '$timenow', '$joindate', 'Обычная ссылка')");


		$t=date("d.m.Y", time());
		$res=mysql_query("select count(id) as kol from tb_adsdate where data='$t'");
		$res=mysql_fetch_assoc($res);
		if($res["kol"]>0)
		{
			mysql_query("update tb_adsdate set kolvo=kolvo+1 where data='$t'");
		}else{
			mysql_query("insert into tb_adsdate (data,kolvo) values ('$t','1')");
		}

		$res=mysql_query("select * from tb_comp where param='6'");
		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=='0')
					{
						$res1=mysql_query("select username from tb_users where wmid='$wmipoc'");
						if(mysql_num_rows($res1)>0){$res1=mysql_fetch_assoc($res1); $lidername=$res1["username"];}else{$lidername='';}
					}else{
						$lidername=$wmipoc;
					}
					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"]+$nadop;
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("update tb_compdata set resvalue='$resvalue', purse='$purse' where id='".$res1["id"]."'");
						}else{
							$purse=$_POST['LMI_PAYEE_PURSE'];
							mysql_query("insert into tb_compdata (idk,user,resvalue,purse) values ('$id','$lidername','$nadop','$purse')");
						}
					}
				}
			}
		}
		}
		mysql_close($con);
	}


	if($service=="mta")
	{
		require('config.php');

	  if($oplata > 0){
		  mysql_query("update tb_users set money_cr=money_cr+'$oplata' where id='$id'");

		  $user_n = mysql_query("SELECT username FROM `tb_users` WHERE `id` = '$id'");
          $u_s = mysql_fetch_assoc($user_n);

          $date = time();
		  mysql_query("insert into still_balans (user,date,money) values ('".$u_s["username"]."','$date','$oplata')");

        $ball_cab = mysql_query("SELECT * FROM `tb_users` WHERE `wmid` = '$wmipoc'");
        if(mysql_num_rows($ball_cab) > 0){

          require('karera/setup_ball.php');
          $ref_u = mysql_fetch_assoc($ball_cab);

          $ball_u = intval($oplata/10);

          if($ref_u["referer"] != ""){
            $timenow=time();
            $ball_r = $ball_u * $ball["ref_reclam"];
            mysql_query("UPDATE `tb_users` SET `ball` = `ball`+'$ball_r' WHERE `username` = '".$ref_u["referer"]."'");
            mysql_query("INSERT INTO tb_infoball (user, date, ball, referer, mesage) VALUES ('".$ref_u["referer"]."', '$timenow', '+ $ball_r', '---', 'Реферер пополнил рекламный счет')") or die(mysql_error());
          }
         $timenow=time();
          $ball_us = $ball_u * $ball["reclama"];
          mysql_query("UPDATE `tb_users` SET `ball` = `ball`+'$ball_us' WHERE `wmid` = '$wmipoc'");
          mysql_query("INSERT INTO tb_infoball (user, date, ball, referer, mesage) VALUES ('$wmipoc', '$timenow', '+ $ball_us', '---', 'Вы пополнили рекламный счет')") or die(mysql_error());
        }


		  mysql_close($con);

		}
	}

if($service=="auto")
{
require('config.php');
$id_zakaz = $_POST["id"];
$sql = mysql_query("SELECT `money` FROM `seelf_auto` WHERE `id`='$id_zakaz'");
if(mysql_num_rows($sql)) {
$row = mysql_fetch_assoc($sql);
$wmid = $row["money"];
$joindate=date("d.m.Y");
$timenow=time();
if(floatval($row["money"])!=floatval($_POST['LMI_PAYMENT_AMOUNT'])) {exit();}
mysql_query("UPDATE `seelf_auto` SET `status`='pay', `wmid`='$wmid', `date`='".time()."' WHERE `id`='$id_zakaz'") or die(mysql_error());
mysql_query("INSERT INTO tb_zar (wmid, kol, amount, date, date2, tip) VALUES('$wmipoc', '---', '".$_POST['LMI_PAYMENT_AMOUNT']."', '$timenow', '$joindate', 'Автосерфинг')") or die(mysql_error());

          $res=mysql_query("select * from tb_comp where param='6'");
          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=='0')
                         {
                              $res1=mysql_query("select username from tb_users where wmid='$wmipoc'");
                              if(mysql_num_rows($res1)>0){$res1=mysql_fetch_assoc($res1); $lidername=$res1["username"];}else{$lidername='';}
                         }else{
                              $lidername=$wmipoc;
                         }
                         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"]+$_POST['LMI_PAYMENT_AMOUNT'];
                                   $purse=$_POST['LMI_PAYEE_PURSE'];
                                   mysql_query("update tb_compdata set resvalue='$resvalue', purse='$purse' where id='".$res1["id"]."'");
                              }else{
                                   $purse=$_POST['LMI_PAYEE_PURSE'];
                                   mysql_query("insert into tb_compdata (idk,user,resvalue,purse) values ('$id','$lidername','".$_POST['LMI_PAYMENT_AMOUNT']."','$purse')");
                              }
                         }
                    }
               }
          }
}}

if($service)
{
$only_it = array("advdynlink","advdynlink1","mail_mess","paket_adv","advbanner","banner","advchatads","advstatlink","advtextads","advframeads", "advpsevdo" );
$adv_wmid = $_POST['LMI_PAYER_WM'];
$fta = time();
if(in_array($service, $only_it))
{

require("config.php");
$adv_list = mysql_query("SELECT id FROM spisok_adv WHERE wmid = '$adv_wmid'");
mysql_close($con);

if(mysql_num_rows($adv_list) > 0)
{

require("config.php");
$adv_list = mysql_query("UPDATE spisok_adv SET all_sum = all_sum + $oplata, all_adv = all_adv + 1, lost_adv = $fta WHERE wmid = '$adv_wmid'");
mysql_close($con);

}else{

require("config.php");
$adv_list = mysql_query("INSERT INTO spisok_adv (wmid, all_sum, all_adv, ferst_adv, lost_adv) VALUES ('$adv_wmid','$oplata','1','$fta','$fta')");
mysql_close($con);

}
}

}
}

?>