<?php
//Получает XML-ответ
$xml = base64_decode($_POST["operation_xml"]);

//Разбираем данные
$xmlres = simplexml_load_string($xml);

//Получаем и обрабатываем ответ
$order_id=strval($xmlres->order_id);
$status=strval($xmlres->status);
$amount=strval($xmlres->amount);

require('config.php');
$liq=mysql_query("select * from tb_liqpay where id='1'");
$ld=mysql_fetch_assoc($liq);
mysql_close($con);

$merchant_password = $ld['podp'];

$sign = base64_encode(sha1($merchant_password.$xml.$merchant_password,1));

#проверка подписи
if ($_POST["signature"] == $sign){
  #проверка статуса
  if ($status == "success"){
    $oplata=$amount;
    $order_id = str_replace(" ","",$order_id);
    $order_id = explode("_", $order_id);
    $id=$order_id[1];
    $service=$order_id[0];

    require('config.php');

    if($service=="advdynlink")
	{
	    $res=mysql_query("select money,plan from tb_ads where id='$id' and status='1'");
		$row=mysql_fetch_assoc($res);

      if($row["plan"]!=""){
	    if($row["money"]<=$oplata)
		{
		  mysql_query("update tb_ads set status='0', fechainicia='".time()."' where id='$id'";

		  $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')");
		  }
		}
	  }
	}

	if($service=="advdynlink1")
	{
		$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);

      if($plan!=""){
		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')");

		  $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')");
		  }
        }
	  }
	}

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

        $res=mysql_query("select * from tb_psevdoreq where id='$id'");
        $row=mysql_fetch_assoc($res);
        $urlsite=$row["url"];$description=$row["description"];$wmid=$row["wmid"];$plan=$row["plan"];$t=time();

        $nado=$plan*$psevdoweek1;
        $nado = round($nado,2);

      if($plan!=""){
        if($nado<=$oplata)
        {
          $query = "INSERT INTO tb_psevdolinks (url,description,wmid,begindate,numdays) VALUES('$urlsite','$description','$wmid','$t','$plan')";
          mysql_query($query) or die(mysql_error());

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

    if($service=="advstatlink")
	{
		$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);

      if($plan!=""){
		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($query) or die(mysql_error());

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

    if($service=="advlink")
	{
		$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;

		$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($plan!=""){
		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($query) or die(mysql_error());

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

	if($service=="advbanner")
	{
		$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"];

		$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($plan!=""){
		if($nado<=$oplata)
		{
		  mysql_query("INSERT INTO tb_advban (urlsite,urlpic,begindate,numdays,wmid) VALUES('$urlsite','$urlpic','$t','$plan','$wmid')");

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

	if($service=="advbanner200")
	{
		$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"];

		$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($plan!=""){
		if($nado<=$oplata)
		{
		  mysql_query("INSERT INTO tb_advban200 (urlsite,urlpic,begindate,numdays,wmid) VALUES('$urlsite','$urlpic','$t','$plan','$wmid')");

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


	if($service=="banner")
	{
		$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"];

		$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($plan!=""){
		if($nado<=$oplata)
		{
		  mysql_query("INSERT INTO tb_ban (urlsite,urlpic,begindate,numdays,wmid) VALUES('$urlsite','$urlpic','$t','$plan','$wmid')");

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

	if($service=="advframeads")
	{
		$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;

		$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($plan!=""){
		if($nado<=$oplata)
		{
		  $query = "INSERT INTO tb_frameads (wmid,link,text,enddate) VALUES('$wmid','$linksite','$text','$t')";
		  mysql_query($query) or die(mysql_error());

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

	if($service=="advtextads")
	{
		$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);

      if($plan!=""){
		if($nado<=$oplata)
		{
		  $query = "INSERT INTO tb_textads (wmid,url,text,enddate) VALUES('$wmid','$urlsite','$text','$t')";
		  mysql_query($query) or die(mysql_error());

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

	if($service=='advpacket')
    {
      $row=mysql_fetch_assoc(mysql_query("SELECT * FROM tb_packetreq WHERE id='$id'"));
      $cena=round($row["cena"],2);$url=$row["url"];$urlimage=$row["urlpic"];$ops=$row["ops"];$wmid=$row["wmid"];$idpk=$row["idpk"];

      $res=mysql_fetch_assoc(mysql_query("SELECT * FROM tb_packet WHERE id='$idpk'"));
      $banner=$res["baner"];$dyn=$res["dyn"];$timer=$res["time"];$frame=$res["frame"];$stat=$res["stat"];

      if($idpk!=""){
        if($oplata >= $cena)
        {
          $time=time();
          mysql_query("UPDATE tb_packetreq SET status='0',starttime='$time' WHERE id='$id'");

          mysql_query("INSERT INTO tb_advban (urlsite, urlpic, begindate, numdays, curview, wmid) VALUES('$url','$urlimage','$time','$banner', '1', '$wmid')");

          mysql_query("INSERT INTO tb_ads (paypalemail, plan, url, description, tipo, timer, dopsec, vip)
          VALUES('$wmid','$dyn','$url','$ops','ads', '$timer', '2','0')");

          $t=$time+$frame*24*3600;
          mysql_query("INSERT INTO tb_frameads (wmid,link,text,enddate) VALUES('$wmid','$url','$ops','$t')");

          mysql_query("INSERT INTO tb_statlinks (url,description,wmid,leftblock,begindate,numdays) VALUES('$url','$ops','$wmid','0','$time','$stat')");
          mysql_query("DELETE FROM tb_packetreq WHERE id='$id'");

          $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')");
		  }

		}
      }
	}

	if($service=="mailmess")
	{
		$sql="SELECT * FROM tb_adsmail2 WHERE id='$id'";
        $result=mysql_query($sql);
        $row=mysql_fetch_assoc($result);
        $nado=round($row[summ],2);

      if($row["plan"]!=""){
		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("delete from tb_adsmail2 where id='$id'");
		}
	  }
	}

	if($service=="mta")
	{
	  $res=mysql_query("select * from tb_mta where id='$id'");
	  $row=mysql_fetch_assoc($res);

	  $amount=$row["amount"];
	  $user=$row["user"];
      $nado=round($amount,2);

      if($row["amount"]!=""){
	    if($nado<=$oplata) {$amount=$oplata;
	      $sql="update tb_users set money=money+'$amount' where username='$user'";
	      mysql_query($sql);


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

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

          $ball_u = intval($oplata/10);
          $timenow=time();
          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 `user` = '$user'");
          mysql_query("INSERT INTO tb_infoball (user, date, ball, referer, mesage) VALUES ('$user', '$timenow', '+ $ball_us', '---', 'Вы пополнили рекламный счет')") or die(mysql_error());
        }




          $date = time();
	      mysql_query("insert into still_balans (user,date,money) values ('$user','$date','$oplata')");

	      mysql_query("delete from tb_mta where id='$id'");
	    }
	  }
    }

    mysql_close($con);

  }elseif($status == "wait_secure"){
    $error = "<font style='color:#0000dd;'><b>Заявка в режиме ожидания. После проверки данных реклама появится автоматом</b></font><br><br>";
  }else{
    $error = "<font style='color:#dd0000;'><b>Ваш заказ не прошол проверку или небыл оплачен</b></font><br><br>";
  }

}else{
 $error = "<font style='color:#dd0000;'><b>Оплата отклонена, подписи не совпали.</b></font><br><br>";
}
?>