<?php
session_start();
header( 'Content-type: text/html; charset=windows-1251' );
error_reporting( 1 );
require($_SERVER['DOCUMENT_ROOT']."/sfb.php");
require($_SERVER['DOCUMENT_ROOT']."/funciones.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(); }

// ответ на тест
if($_POST['func'] == "answer_tests") {
$id = valideint($_POST["id"]);
$reply1 = valideint($_POST["reply1"]);
$reply2 = valideint($_POST["reply2"]);

$sql = mysql_query("SELECT * FROM `b_tests` WHERE `id`='$id' AND `balance`>`price` AND `fromus`<>'$id_user' LIMIT 1") or die(mysql_error());
if(mysql_num_rows($sql) == 0){ echo "1";exit();
}else{
$sqlv = mysql_query("SELECT `id` FROM `b_tests_view` WHERE `ident`='$id' AND `id_us`='$id_user' AND `date`>='".(time()-24*3600)."' AND `status`='0' LIMIT 1") or die(mysql_error());
if(mysql_num_rows($sqlv) > 0){ echo "2";exit();
}else{
$row = mysql_fetch_assoc($sql);
if($row["prv_1"] != $reply1) { 
mysql_query("INSERT INTO `b_tests_view` (`ident`, `id_us`, `date`, `status`) VALUES ('$id', '$id_user', '".time()."', '0')") or die(mysql_error());
mysql_query("UPDATE `b_tests` SET `bad`=`bad`+'1' WHERE `id`='$id'") or die(mysql_error());
echo "3";exit(); }
elseif($row["prv_2"] != $reply2){ 
mysql_query("INSERT INTO `b_tests_view` (`ident`, `id_us`, `date`, `status`) VALUES ('$id', '$id_user', '".time()."', '0')") or die(mysql_error());
mysql_query("UPDATE `b_tests` SET `bad`=`bad`+'1' WHERE `id`='$id'") or die(mysql_error());
echo "3";exit(); }
$money = $row["payforad"];
$user = $row["fromus"];
mysql_query("INSERT INTO `b_tests_view` (`ident`, `id_us`, `date`, `status`) VALUES ('$id', '$id_user', '".time()."', '1')") or die(mysql_error());
mysql_query("UPDATE `b_tests` SET `good`=`good`+'1', `balance`=`balance`-`price` WHERE `id`='$id'") or die(mysql_error());
mysql_query("UPDATE `b_users` SET `money`=`money`+'$money' WHERE `id`='$id_user'") or die(mysql_error());
mysql_query("UPDATE `b_tests` SET `paused`='1' WHERE `balance`<`price` AND `id`='$id'") or die(mysql_error());
#общий конкурс от реферера
$referer = mysql_result(("SELECT `referer` FROM `b_users` WHERE `id`='$id_user'"),0);
if($referer > 0){
$sql_cr = mysql_query("SELECT `id` FROM `b_ref_comp` WHERE `status`='1' AND `id_user`='$referer' AND `start_comp`<='".time()."' AND `type`='4'") or die(mysql_error());
if(mysql_num_rows($sql_cr) > 0){
$row_cr = mysql_fetch_assoc($sql_cr);
mysql_query("INSERT INTO `b_ref_comp_data` SET `id_comp`='".$row_cr["id"]."', `id_user`='$id_user', `count`='2', `time`='".time()."' ON DUPLICATE KEY UPDATE `count`=`count`+'2'") or die(mysql_error());
}
}
echo "4"; exit();
}
}
}
// жалоба на тесты
if($_POST['func'] == "add_abuse") {
$ident = valideint($_POST["id"]);
if(mysql_num_rows(mysql_query("SELECT * FROM `user_zal` WHERE `link_id`='$ident' AND `user`='$id_user' AND `type`='2'"))==0){
$abuse = iconv("UTF-8", "windows-1251", validetext($_POST["text"]));
mysql_query("INSERT INTO `b_complaints` (`link_id`,`user`,`date`,`text`,`type`) values ('$ident','$id_user','".time()."','$abuse','2')") or die(mysql_error());
mysql_query("UPDATE `b_tests` SET `all_zh`=`all_zh`+'1' WHERE `id`='$ident'") or die(mysql_error());
mysql_query("UPDATE `b_tests` SET `modered`='2' WHERE `all_zh`>'5' AND `id`='$ident'") or die(mysql_error());
echo "1";exit();
}else{echo "2";exit();}
}
?>