<?php
session_start();
header( 'Content-type: text/html; charset=windows-1251' );
error_reporting( 1 );
require($_SERVER['DOCUMENT_ROOT']."/sfb.php");

$id_user = (isset($_SESSION["iduser"]) && preg_match("|^[\d]{1,11}$|", trim($_SESSION["iduser"]))) ? intval(trim($_SESSION["iduser"])) : false;

if(!isset($_SESSION['iduser'])) { exit(); }
if(!isset($_POST['func'])) { exit(); }
if($_SERVER['HTTP_X_REQUESTED_WITH'] != "XMLHttpRequest") { exit(); }

$zp = valideint($_POST["zp"]);
switch($zp){
case 1: $zapros1 = "tb_ads"; $from1 = "fromus";$bask = '1';break;
case 2: $zapros1 = "seo_paymails"; $from1 = "fromid";$bask = '6';break;
case 3: $zapros1 = "seo_tasks"; $from1 = "fromid";$bask = '5';break;
case 4: $zapros1 = "tb_statlinks"; $from1 = "fromus";$bask = '2';break;
case 5: $zapros1 = "tb_advban"; $from1 = "fromus";$bask = '3';break;
case 6: $zapros1 = "b_tests"; $from1 = "fromus";$bask = '9';$pln = "`good`='0', `bad`='0'";break;
}


// Пополнение бюджета рекламной ссылки
if($_POST['func'] == "add_summ") {
$id = valideint($_POST["id"]);
$price = (isset($_POST["price"])) ? abs(floatval(str_replace(",",".",trim($_POST["price"])))) : false;
$sql=mysql_query("SELECT `price` FROM `$zapros1` WHERE `id`='$id' AND `modered`='1' AND `$from1`='$id_user'");
if(mysql_num_rows($sql) > 0){
mysql_query("INSERT INTO `b_basket` (`ident`,`price`,`fromid`,`time`,`type`) VALUES ('$id','$price','$id_user','".time()."','$bask')") or die(mysql_error());
}
}

// Удаление дин.ссылок
if($_POST['func'] == "deletes") {
$id = valideint($_POST["id"]);
$sql = mysql_query("SELECT `balance`, `paused` FROM `$zapros1` WHERE `$from1`='$id_user' AND `id`='$id'");
if(mysql_num_rows($sql)>0){
$row = mysql_fetch_assoc($sql);
$paused = $row["paused"];
if($paused == 1){
$balance = $row["balance"];
$dell_comis = 10 ;
if($dell_comis > 0) { $balance = $balance - (($balance*$dell_comis)/100); }
if($balance > 0) { mysql_query("UPDATE `b_users` SET `r_money`=`r_money`+'$balance' WHERE `id`='$id_user'") or die(mysql_error());}
mysql_query("DELETE FROM `$zapros1` WHERE `$from1`='$id_user' AND `id`='$id'") or die(mysql_error());
mysql_query("DELETE FROM `b_basket` WHERE `fromid`='$id_user' AND `ident`='$id'") or die(mysql_error());
echo "3";exit();
}else{ echo "2";exit();}
}else{ echo "1";exit();
}
}

// обнуление просмотров баннеров
if($_POST['func'] == "clear_ban") {
$id = valideint($_POST["id"]);
$plan = valideint($_POST["plan"]);
$sql = mysql_query("SELECT `id` FROM `tb_advban` WHERE `fromus`='$id_user' AND `id`='$id'");
if(mysql_num_rows($sql)>0){
if($plan > 0) {
mysql_query("UPDATE `tb_advban` SET `views`='0' WHERE `fromus`='$id_user' AND `id`='$id'") or die(mysql_error());
}
echo "2";exit();
}else{echo "1";exit();}
}

// обнуление просмотров дин.ссылок
if($_POST['func'] == "clear_dyn") {
$id = valideint($_POST["id"]);
$plan = valideint($_POST["plan"]);
$sql = mysql_query("SELECT `id` FROM `tb_ads` WHERE `fromus`='$id_user' AND `id`='$id'");
if(mysql_num_rows($sql)>0){
if($plan > 0) {
mysql_query("UPDATE `tb_ads` SET `plan`='0' WHERE `fromus`='$id_user' AND `id`='$id'") or die(mysql_error());
}
echo "2";exit();
}else{echo "1";exit();}
}

// обнуление просмотров стат.ссылок
if($_POST['func'] == "clear_stat") {
$id = valideint($_POST["id"]);
$views = valideint($_POST["views"]);
$sql = mysql_query("SELECT `id` FROM `tb_statlinks` WHERE `fromus`='$id_user' AND `id`='$id'");
if(mysql_num_rows($sql)>0){
if($views > 0) {
mysql_query("UPDATE `tb_statlinks` SET `views`='0' WHERE `fromus`='$id_user' AND `id`='$id'") or die(mysql_error());
}
echo "2";exit();
}else{echo "1";exit();}
}

// старт-пауза дин.ссылок
if($_POST['func'] == "paused") {
$id = valideint($_POST["id"]);
$sql = mysql_query("SELECT `paused` FROM `$zapros1` WHERE `$from1`='$id_user' AND `id`='$id' AND `modered`='1'");
$paused = mysql_result($sql,0);
if($paused == 1) {
$paused = "0";
}else{
$paused = "1";
}
mysql_query("UPDATE `$zapros1` SET `paused`='$paused' WHERE `$from1`='$id_user' AND `id`='$id' AND `modered`='1' AND `balance`>'price'") or die(mysql_error());
echo $paused;exit();
}

// перевод на другую площадку
if($_POST['func'] == "tranfer_summ") {
$id = valideint($_POST["id"]);
$to = validetext($_POST["to"]);
$summ = (isset($_POST["summ"])) ? abs(floatval(str_replace(",",".",trim($_POST["summ"])))) : false;

if($to != ''){
$exp2 = explode(":", $to);
$pl_to2 = $exp2[0];
$id_to2 = $exp2[1];
switch($pl_to2){
case 1: $zapros2 = "tb_ads"; $from2= "fromus";break;
case 2: $zapros2 = "seo_paymails"; $from2 = "fromid";break;
case 3: $zapros2 = "seo_tasks"; $from2 = "fromid";break;
case 4: $zapros2 = "tb_statlinks"; $from2 = "fromus";break;
case 5: $zapros2 = "tb_advban"; $from2 = "fromus";break;
case 6: $zapros2 = "b_tests"; $from2 = "fromus";break;
}
$sql1 = mysql_query("SELECT `balance`,`paused` FROM `$zapros1` WHERE `$from1`='$id_user' AND `id`='$id'");
$row1 = mysql_fetch_assoc($sql1);
$summa = $row1["balance"];
$paused = $row1["paused"];
if($paused == 1){
$sql = mysql_query("SELECT * FROM `$zapros2` WHERE `$from2`='$id_user' AND `id`='$id_to2'");
if(mysql_num_rows($sql) > 0){
if($summ <= $summa){
mysql_query("UPDATE `$zapros1` SET `balance`=`balance`-'$summ' WHERE `$from1`='$id_user' AND `id`='$id' AND `balance`>='$summ'") or die(mysql_error());
mysql_query("UPDATE `$zapros2` SET `balance`=`balance`+'$summ' WHERE `$from2`='$id_user' AND `id`='$id_to2'") or die(mysql_error());
echo "3";exit();
}else{echo "2";exit();}
}else{echo "1";exit();}
}else{echo "4";exit();}
}else{echo "1";exit();}
}

// просмотр жалоб
if($_POST['func'] == "view-abuse") {
$id = valideint($_POST["id"]);
$ab_sql = mysql_query("SELECT `date`,`text` FROM `user_zal` WHERE `link_id`='$id'");
if(mysql_num_rows($ab_sql) > 0){
$i = 0;
while($ab_row = mysql_fetch_assoc($ab_sql)){
$i++;
echo "<br /><small style='color:#757272;'>".date("d.m.y H:i",$ab_row["date"])."</small> - <font color='#FF0000'>".$ab_row["text"]."</font><br>";
}
}else{ echo "1";exit();}
}
?>