<?
DEFINE("ROOT_DIR", realpath(dirname(__FILE__).'/../') );
require(ROOT_DIR."/config.php");
require_once(ROOT_DIR."/funciones.php");
$vremy = time()-604800;

mysql_query("UPDATE `tb_ads` SET `paused`='1' WHERE `modered`=2");
mysql_query("UPDATE `tb_ads` SET `paused`='1' WHERE `balance`<price");
mysql_query("UPDATE `seo_paymails` SET `paused`='1' WHERE `balance`<price");
mysql_query("UPDATE `tb_advban` SET `paused`='1' WHERE `balance`<price");
mysql_query("UPDATE `b_users` SET `skidka`='0',`vremy`='0' WHERE `vremy`<'$vremy'");

$sqlcw = mysql_query("SELECT `ident`, COUNT(ident) AS `cnt_w` FROM `seo_taskstats` WHERE `status`='0' GROUP BY `ident`") or die(mysql_error());
if(mysql_num_rows($sqlcw) > 0){
while($rowcw = mysql_fetch_assoc($sqlcw)){
mysql_query("UPDATE `seo_tasks` SET `waits`='".$rowcw["cnt_w"]."' WHERE `id`='".$rowcw["ident"]."'")or die(mysql_error());
}
}
mysql_query("UPDATE `seo_tasks` SET `waits`='0' WHERE `id` NOT IN (SELECT `ident` FROM `seo_taskstats` WHERE `status`='0')")or die(mysql_error());


$sqlcws = mysql_query("SELECT `ident`, COUNT(ident) AS `cnt_ws` FROM `seo_taskstats` WHERE `status`='3' GROUP BY `ident`") or die(mysql_error());
if(mysql_num_rows($sqlcws) > 0){
while($rowcws = mysql_fetch_assoc($sqlcws)){
mysql_query("UPDATE `seo_tasks` SET `works`='".$rowcws["cnt_ws"]."' WHERE `id`='".$rowcws["ident"]."'")or die(mysql_error());
}
}
mysql_query("UPDATE `seo_tasks` SET `works`='0' WHERE `id` NOT IN (SELECT `ident` FROM `seo_taskstats` WHERE `status`='3')")or die(mysql_error());



$sql=mysql_query("SELECT * FROM `seo_taskstats` WHERE `status`='3'");
while ($roww = mysql_fetch_array($sql)) {
$id = $roww["ident"];
$sqli=mysql_query("SELECT * FROM `seo_tasks` WHERE `id`='$id'");
$rowss=mysql_fetch_assoc($sqli);
if($roww["starttime"]+$rowss["maxtime"] < time())
{
mysql_query("UPDATE `seo_tasks` SET `works`=`works`-'1' WHERE `id`='$id'");
}
}

$sqlii = mysql_query("SELECT * FROM `seo_taskstats` WHERE `status`='0' AND `anstime`<'".(time()-3600*260)."'");
while ($row = mysql_fetch_array($sqlii)) {
$sqliz = mysql_query("SELECT * FROM `seo_tasks` WHERE `id`='".$row["ident"]."'");
$rows = mysql_fetch_assoc($sqliz);
$taskbal = $rows["price"]*5/6;
$iduser = $rows["fromid"];
$r_sql = mysql_query("SELECT `referer` FROM `b_users` WHERE `id` = '".$row["fromid"]."'") or die(mysql_error());
if(mysql_num_rows($r_sql) > 0){
$ref_task = mysql_result($r_sql,0,0);
$ref_reyt = mysql_result(mysql_query("SELECT `reyting` FROM `b_users` WHERE `id`='$ref_task'"),0,0);
$pl_r_r1 = user_status($ref_reyt,4);
$r_task_bal = $taskbal*$pl_r_r1/100;
mysql_query("UPDATE `b_users` SET `money`=`money`+'$r_task_bal' WHERE `id`='$ref_task'");
}
$b_task = mysql_result(mysql_query("SELECT `price` FROM `b_config` WHERE `name`='task'"),0);
mysql_query("UPDATE `seo_tasks` set `waits`=`waits`-'1',`goods`=`goods`+'1',`balance`=`balance`-`price` WHERE `id`='".$rows["id"]."' AND `fromid`='$iduser'");
mysql_query("UPDATE `seo_taskstats` set `status`='1' where `id`='".$row["id"]."' and `ident`='".$row["ident"]."'");
mysql_query("UPDATE `b_users` SET `money`=`money`+'$taskbal',`taskreyt`=`taskreyt`+'1', `reyting`=`reyting`+'$b_task' WHERE `id` = '".$row["fromid"]."'");
///Отправка письма о выполнении
$ssd = mysql_query("SELECT * FROM `b_users` WHERE `id`='".$row["fromid"]."'");
$ressd = mysql_fetch_assoc($ssd);
mysql_query("INSERT INTO `b_mails` (`text`,`fromid`,`fromus`,`toid`,`tous`,`timemessage`,`read`) VALUES ('Выполнение задания № ".$rows["id"]."','2','Sustem','".$row["fromid"]."','".$ressd["username"]."','".time()."','5')") or die(mysql_error());
$idente = mysql_result(mysql_query("SELECT `id` FROM `b_mails` ORDER BY `id` DESC LIMIT 1"),0);
mysql_query("INSERT INTO `b_mails` (`text`,`fromid`,`fromus`,`toid`,`tous`,`timemessage`,`ident`) VALUES ('Поздравляем, ваше задание № ".$rows["id"]." автоматически подтвердилось по истечении 260 часов. Вам начислено $taskbal руб.','2','Sustem','".$row["fromid"]."','".$ressd["username"]."','".time()."','$idente')") or die(mysql_error());
///Отправка письма о выполнении

#конкурс от реферера
$sql_cr = mysql_query("SELECT `id` FROM `b_ref_comp` WHERE `status`='1' AND `id_user`='$ref_task' AND `start_comp`<='".time()."' AND `type`='3'") 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`='".$row["fromid"]."', `count`='1', `time`='".time()."' ON DUPLICATE KEY UPDATE `count`=`count`+'1'") or die(mysql_error());
}
#конкурс от реферера
#общий конкурс от реферера
$sql_cr = mysql_query("SELECT `id` FROM `b_ref_comp` WHERE `status`='1' AND `id_user`='$ref_task' 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`='".$row["fromid"]."', `count`='3', `time`='".time()."' ON DUPLICATE KEY UPDATE `count`=`count`+'3'") or die(mysql_error());
#общий конкурс от реферера
}
$varstr = date("w");
$day = NULL;
if($varstr==1) $day='mon';
if($varstr==2) $day='tue';
if($varstr==3) $day='wed';
if($varstr==4) $day='thu';
if($varstr==5) $day='fri';
if($varstr==6) $day='sat';
if($varstr==0) $day='sun';
mysql_query("UPDATE `b_statistic` SET `total`=`total`+'1', ".$day."=".$day."+'1' WHERE `user_id`='".$row["fromid"]."' AND `typ`='1'");
}
mysql_query("DELETE FROM `seo_taskcomms` WHERE `ident` NOT IN (SELECT `id` FROM `seo_tasks`)") or die(mysql_error());
mysql_query("DELETE FROM `seo_taskcomms` WHERE `fromid` NOT IN (SELECT `id` FROM `b_users`)") or die(mysql_error());
mysql_query("DELETE FROM `seo_favtasks` WHERE `ident` NOT IN (SELECT `id` FROM `seo_tasks`)") or die(mysql_error());

?>