<?php
//запуск раз в сутки
DEFINE("ROOT_DIR", realpath(dirname(__FILE__).'/../') );
require(ROOT_DIR."/config.php");
require_once(ROOT_DIR."/funciones.php");

#очистка статистики заказов рекламы
mysql_query("UPDATE `b_all_stats` SET `dyn`='0',`mail`='0',`task`='0',`test`='0',`kont`='0',`ban`='0' WHERE `date`='".date("w")."'") or die(mysql_error());

mysql_query("UPDATE `b_users` SET `r_money`=`r_money`+'10', `reyting`=`reyting`+'5' WHERE `user_status`='moder'");
mysql_query("UPDATE `b_users` SET `r_money`=`r_money`+'100' WHERE `user_status`='admin'");

$b_noactiv = mysql_result(mysql_query("SELECT `price` FROM `b_config` WHERE `name`='noactiv'"),0,0);
mysql_query("UPDATE `b_users` SET `bonus`='0' WHERE `bonus`='1'") or die(mysql_error());
//оптимизация таблиц
function get_database_tables() {
	$i=0;
	$sql = mysql_query("SHOW TABLES");

	if(mysql_num_rows($sql)>0) {
		while($row = mysql_fetch_assoc($sql, MYSQL_NUM)) {
			$i++;
			$tbl = $row[0];

			mysql_query("REPAIR TABLE `$tbl`") or die(mysql_error());
			mysql_query("OPTIMIZE TABLE `$tbl`") or die(mysql_error());
			mysql_query("ALTER TABLE `$tbl` ") or die(mysql_error());
		}
	}

	mysql_free_result($sql);
}
echo get_database_tables();

//очистка пользователей которые не заходили 60 дней
mysql_query("DELETE FROM `b_users` WHERE `lastlogdate_str` <= '".(time()-60*24*3600)."'") or die(mysql_error());
//очистка пользователей которые не заходили день после регистрации
mysql_query("DELETE FROM `b_users` WHERE `lastlogdate` = '' AND `joindate_str` <= '".(time()-24*3600)."'") or die(mysql_error());
//снятие реферера
$sql = mysql_query("SELECT `id`,`referer` FROM `b_users` WHERE `referer`>'0'");
if(mysql_num_rows($sql) > 0){
while($row = mysql_fetch_assoc($sql)){
$coll = mysql_num_rows(mysql_query("SELECT `id` FROM `b_users` WHERE `id`='".$row["referer"]."'"));
if($coll == 0){
mysql_query("UPDATE `b_users` SET `referer`='0' WHERE `id`='".$row["id"]."'") or die(mysql_error());
}
}
}
//снятие реферера 2ур.
$sql = mysql_query("SELECT `id`,`referer2` FROM `b_users` WHERE `referer2`>'0'");
if(mysql_num_rows($sql) > 0){
while($row = mysql_fetch_assoc($sql)){
$coll = mysql_num_rows(mysql_query("SELECT `id` FROM `b_users` WHERE `id`='".$row["referer2"]."'"));
if($coll == 0){
mysql_query("UPDATE `b_users` SET `referer2`='0' WHERE `id`='".$row["id"]."'") or die(mysql_error());
}
}
}
//очистка ярмарки
mysql_query("DELETE FROM `b_jarmarka` WHERE `rid` NOT IN (SELECT `id` FROM `b_users`)") or die(mysql_error());
mysql_query("DELETE FROM `b_jarmarka` WHERE `trid` NOT IN (SELECT `id` FROM `b_users`)") or die(mysql_error());
//снижение рефтинга за неактивность
mysql_query("UPDATE `b_users` SET `reyting` = `reyting`-'$b_noactiv' WHERE `reyting` > '0' AND `lastlogdate_str` < '".(time()-7*24*3600)."'") or die(mysql_error());
//обнуляем рейтинг если меньше 0
mysql_query("UPDATE `b_users` SET `reyting` = '0' WHERE `reyting` < '0'") or die(mysql_error());
//очистка конкурсов
mysql_query("DELETE FROM `b_compdata` WHERE `id_user` NOT IN (SELECT `id` FROM `b_users`)") or die(mysql_error());
mysql_query("DELETE FROM `b_comp` WHERE `id` NOT IN (SELECT `idk` FROM `b_compdata`)") or die(mysql_error());

for($i=0;$i<=3;$i++){
$sql = mysql_query("SELECT `id` FROM `b_comp` WHERE `type`='$i' AND `enddate`<'".time()."' AND `id` < (SELECT MAX(id) FROM `b_comp`)") or die(mysql_error());
while($row = mysql_fetch_assoc($sql)){
mysql_query("DELETE FROM `b_comp` WHERE `id`='".$row["id"]."'") or die(mysql_error());
mysql_query("DELETE FROM `b_compdata` WHERE `idk`='".$row["id"]."'") or die(mysql_error());
}
}

//обнуление статистики
$varstr = date('w');
$day = 0;
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 `date` = " . $varstr . " , " . $day . " = '0'") or die(mysql_error());

//очистка статистики
$sql = mysql_query("SELECT `user_id` FROM `b_statistic` WHERE `user_id` NOT IN (SELECT `id` FROM `b_users`) ORDER BY `id` ASC") or die(mysql_error());
while($row = mysql_fetch_assoc($sql)){
mysql_query("DELETE FROM `b_statistic` WHERE `user_id` = '".$row["id"]."'") or die(mysql_error());
}

//очистка выводов денег
mysql_query("DELETE FROM `tb_payme` WHERE `datetime`<='".(time()-2*24*3600)."'") or die(mysql_error());

//очистка выводов денег
mysql_query("DELETE FROM `tb_visits` WHERE `data`<='".(time()-2*24*3600)."'") or die(mysql_error());

//очистка жалоб
mysql_query("DELETE FROM `user_zal` WHERE `link_id` NOT IN (SELECT `id` FROM `tb_ads`)") or die(mysql_error());

//очистка приватных писем
mysql_query("DELETE FROM `b_mails` WHERE `fromdl`='1' AND `todl`='1'") or die(mysql_error());

//очистка рекламы
mysql_query("DELETE FROM `tb_advban` WHERE `fromus` NOT IN (SELECT `id` FROM `b_users`)") or die(mysql_error());

mysql_query("DELETE FROM `tb_statlinks` WHERE `fromus` NOT IN (SELECT `id` FROM `b_users`)") or die(mysql_error());

mysql_query("DELETE FROM `tb_ads` WHERE `fromus` NOT IN (SELECT `id` FROM `b_users`)") or die(mysql_error());
mysql_query("DELETE FROM `tb_visits` WHERE `ident` NOT IN (SELECT `id` FROM `tb_ads`)") or die(mysql_error());
mysql_query("DELETE FROM `tb_visits` WHERE `idus` NOT IN (SELECT `id` FROM `b_users`)") or die(mysql_error());

mysql_query("DELETE FROM `seo_tasks` WHERE `fromid` NOT IN (SELECT `id` FROM `b_users`)") or die(mysql_error());

mysql_query("DELETE FROM `seo_paymails` WHERE `fromid` NOT IN (SELECT `id` FROM `b_users`)") or die(mysql_error());
mysql_query("DELETE FROM `seo_paymailsviews` WHERE `ident` NOT IN (SELECT `id` FROM `seo_paymails`)") or die(mysql_error());
mysql_query("DELETE FROM `seo_paymailsviews` WHERE `fromid` NOT IN (SELECT `id` FROM `b_users`)") or die(mysql_error());
mysql_query("truncate table `tb_visits_1`") OR DIE(mysql_error());
mysql_close();
?>