<?
require('config.php');
function XMLfilter($rs, $tag) {            
   $rs = str_replace("\n", "", str_replace("\r", "", $rs));
   $tags = '<'.$tag.'>';
   $tage = '</'.$tag;
   $start = strpos($rs, $tags)+strlen($tags);
   $end = strpos($rs, $tage);
   return substr($rs, $start, ($end-$start)); 
}

$xmlin = base64_decode($_POST['operation_xml']);//Получаем данные и расшифровываем
$insig = $_POST['signature'];

$status = XMLfilter($xmlin, 'status');
$oplata = XMLfilter($xmlin, 'amount');
$deskr	=XMLfilter($xmlin, 'description');
$error	=XMLfilter($xmlin, 'code');
$deskr 	= explode('Oplata zakaza nomer ', $deskr);
$id		= (int)$deskr['1'];

$f=@fopen("1234.txt","a+") or
          die("error");
fputs($f,"id: $id;Summ: $oplata; status: $status;erro: $error\n");
fclose($f);

$f=@fopen("1.txt","a+") or
          die("error");
fputs($f,"$xmlin\n");
fclose($f);


$gensig = base64_encode(sha1($signature_l.$xmlin.$signature_l,1));
if(($status == 'success') and ($gensig == $insig)){
	
	require('config.php');
	$price=mysql_result(mysql_query("SELECT sum(price) FROM b_basket WHERE fromid='$id'"),0);
	if($oplata>=$price)
	{
			$sql=mysql_query("SELECT * FROm b_basket WHERE fromid='$id'");
		while($row=mysql_fetch_assoc($sql))
		{
			$ident=$row['ident'];
			$price=$row['price'];
			$type=$row['type'];
			switch($type)
			{
			case '1': $nowtime=time();
			mysql_query("InSERT InTO tb_allhistory (idus,data,doing,title) values ('$id','$nowtime','4','Пополнение баланса динамической ссылки № $ident')"); mysql_query("UPDATE tb_ads SET balance=balance+'$price' WHERE id='$ident' AND fromus='$id'");
			break;
									case '6': $nowtime=time();
			mysql_query("InSERT InTO tb_allhistory (idus,data,doing,title) values ('$id','$nowtime','4','Пополнение баланса письма № $ident')"); mysql_query("UPDATE seo_paymails SET balance=balance+'$price' WHERE id='$ident' AND fromid='$id'");
			break;
			case '2':$nowtime=time();
			mysql_query("InSERT InTO tb_allhistory (idus,data,doing,title) values ('$id','$nowtime','4','Пополнение баланса контекстной ссылки № $ident')");  mysql_query("UPDATE tb_statlinks SET balance=balance+'$price' WHERE id='$ident' AND fromus='$id'");
			break;
			case '3':$nowtime=time();
			mysql_query("InSERT InTO tb_allhistory (idus,data,doing,title) values ('$id','$nowtime','4','Пополнение баланса баннера № $ident')"); 
			mysql_query("UPDATE tb_advban SET balance=balance+'$price' WHERE id='$ident' AND fromus='$id'");
			break;
			case '4': $res=mysql_fetch_assoc(mysql_query("SELECT * FROM b_jarmarka WHERE rid='$ident'"));
			$rid=$res['rid'];
			$trid=$res['trid'];
			$price=$res['price'];
			mysql_query("UPDATE b_users SET referer='$id' WHERE id='$rid'");
			mysql_query("UPDATE b_users SET money=money+'$price' WHERE id='$trid'");
			mysql_query("DELETE FROM b_jarmarka WHERE rid='$ident'");			
			$nowtime=time();
			mysql_query("InSERT InTO tb_allhistory (idus,data,doing,title) values ('$trid','$nowtime','7','Ваш реферал # $rid Продан на бирже рефералов!')"); 
		break;
							case '5':$nowtime=time();
			mysql_query("InSERT InTO tb_allhistory (idus,data,doing,title) values ('$id','$nowtime','4','Пополнение баланса задания № $ident')"); 
			mysql_query("UPDATE seo_tasks SET balance=balance+'$price' WHERE id='$ident' AND fromid='$id'");
			break;
		};
		}
		mysql_query("DELETE FROM b_basket WHERE fromid='$id'");
	echo "OK\n";//ответ если все ОК
	}
}else{
	echo "Bad\n";//ответ в случае ошибки
}
?>