<?php
if (!defined('webid')) {
    exit();
}
/// проверяем прошли ли сутки с момента последнего запуска крона, если да то запускаем его повторно.
    $kron_if =  mysql_fetch_array( mysql_query( "SELECT timekron FROM tb_payx WHERE `id` = '1'" ));
	$dewff = $kron_if['timekron'];
    if ($dewff < (time()-86400)) { 
/// ---------------------------------------------------------------------------------------------------------------
/// Удаляем не активных пользователей болие 60 дней
    $user_del_sql = mysql_query("SELECT `id` FROM `tb_users` WHERE `lastlogdate` < '".(time()-5259486)."' AND `lastlogdate` != ''  LIMIT 500" );
    while ($user_del_row = mysql_fetch_assoc($user_del_sql)) 
    {
        mysql_query("UPDATE tb_users SET referer = 0 WHERE referer = '" . $user_del_row['id'] . "'");
        mysql_query("DELETE FROM tb_users WHERE id = '" . $user_del_row['id'] . "'");
        mysql_query("DELETE FROM tb_forum_mes WHERE userid = '" . $user_del_row['id'] . "'");
        mysql_query("DELETE FROM tb_forum_topic WHERE avtor = '" . $user_del_row['id'] . "'");
        mysql_query("DELETE FROM tb_wall WHERE fromus = '" . $user_del_row['id'] . "'");
        mysql_query("DELETE FROM tb_history WHERE user = '" . $user_del_row['id'] . "'");
        mysql_query("DELETE FROM masref WHERE userid = '" . $user_del_row['id'] . "'");
        mysql_query("DELETE FROM seo_refbirj WHERE rid = '" . $user_del_row['id'] . "'");
        mysql_query("DELETE FROM tb_allhistory WHERE idus = '" . $user_del_row['id'] . "'");
        mysql_query("DELETE FROM tb_statistic WHERE username = '" . $user_del_row['id'] . "'");
        mysql_query("DELETE FROM seo_mails WHERE toid = '" . $user_del_row['id'] . "'");
        mysql_query("DELETE FROM seo_taskcomms WHERE fromid = '" . $user_del_row['id'] . "'");
        mysql_query("DELETE FROM tb_gift WHERE tous = '" . $user_del_row['id'] . "'");
        mysql_query("DELETE FROM tb_gift WHERE fromus = '" . $user_del_row['id'] . "'");
        mysql_query("DELETE FROM konkurs_temp WHERE iduser = '" . $user_del_row['id'] . "'");
    }
/// --------------------------------------------------------------------------------------------------------------

/// Удаляем не вошедших в аккаунт пользователей болие 60 дней
    $user_del_sql = mysql_query("SELECT `id` FROM `tb_users` WHERE `joindate` < '".(time()-5259486)."' AND `lastlogdate` = ''  LIMIT 500" );
    while ($user_del_row = mysql_fetch_assoc($user_del_sql)) 
    {
        mysql_query("UPDATE tb_users SET referer = 0 WHERE referer = '" . $user_del_row['id'] . "'");
        mysql_query("DELETE FROM tb_users WHERE id = '" . $user_del_row['id'] . "'");
    }
/// ---------------------------------------------------------------------------------------------------------------

/// Удаляем нулевую статистику
    mysql_query("DELETE FROM tb_allhistory WHERE idus = '0'");
/// ---------------------------------------------------------------------------------------------------------------

/// Находим не активные площадки и пишем дату начала неактивности
    ###Серфинг
    $ads_insert_sql = mysql_query("SELECT `id`,`stopdate` FROM `tb_ads` WHERE `paused` = '1' AND `stopdate` = '' LIMIT 500" );
    while ($ads_insert_row = mysql_fetch_assoc($ads_insert_sql)) 
    {   
        mysql_query("UPDATE tb_ads SET stopdate = ".(time())." WHERE id = '" . $ads_insert_row['id'] . "'");
    }
    ###Оплачиваемые письма
    $paymails_insert_sql = mysql_query("SELECT `id`,`stopdate` FROM `seo_paymails` WHERE `paused` = '1' AND `stopdate` = '' LIMIT 500" );
    while ($paymails_insert_row = mysql_fetch_assoc($paymails_insert_sql)) 
    {   
        mysql_query("UPDATE seo_paymails SET stopdate = ".(time())." WHERE id = '" . $paymails_insert_row['id'] . "'");
    }
    ###Задания
    $task_insert_sql = mysql_query("SELECT `id`,`stopdate` FROM `seo_tasks` WHERE `paused` = '1' AND `stopdate` = '' LIMIT 500" );
    while ($task_insert_row = mysql_fetch_assoc($task_insert_sql)) 
    {   
        mysql_query("UPDATE seo_tasks SET stopdate = ".(time())." WHERE id = '" . $task_insert_row['id'] . "'");
    }
    ###Контекстные ссылки
    $kontext_insert_sql = mysql_query("SELECT `id`,`stopdate` FROM `tb_statlinks` WHERE `paused` = '1' AND `stopdate` = '' LIMIT 500" );
    while ($kontext_insert_row = mysql_fetch_assoc($kontext_insert_sql)) 
    {   
        mysql_query("UPDATE tb_statlinks SET stopdate = ".(time())." WHERE id = '" . $kontext_insert_row['id'] . "'");
    }
    ###Баннеры 468x60
    $ban4_insert_sql = mysql_query("SELECT `id`,`stopdate` FROM `tb_advban` WHERE `paused` = '1' AND `stopdate` = '' LIMIT 500" );
    while ($ban4_insert_row = mysql_fetch_assoc($ban4_insert_sql)) 
    {   
        mysql_query("UPDATE tb_advban SET stopdate = ".(time())." WHERE id = '" . $ban4_insert_row['id'] . "'");
    }   
	###Баннеры 100x100
    $ban1_insert_sql = mysql_query("SELECT `id`,`stopdate` FROM `tb_advban_100` WHERE `paused` = '1' AND `stopdate` = '' LIMIT 500" );
    while ($ban1_insert_row = mysql_fetch_assoc($ban1_insert_sql)) 
    {   
        mysql_query("UPDATE tb_advban_100 SET stopdate = ".(time())." WHERE id = '" . $ban1_insert_row['id'] . "'");
    }    
	###Баннеры 200x300
    $ban2_insert_sql = mysql_query("SELECT `id`,`stopdate` FROM `tb_advban_300` WHERE `paused` = '1' AND `stopdate` = '' LIMIT 500" );
    while ($ban2_insert_row = mysql_fetch_assoc($ban2_insert_sql)) 
    {   
        mysql_query("UPDATE tb_advban_300 SET stopdate = ".(time())." WHERE id = '" . $ban2_insert_row['id'] . "'");
    }
	###Рекламная цепочка
    $cep_insert_sql = mysql_query("SELECT `id`,`stopdate` FROM `cep_links` WHERE `paused` = '1' AND `stopdate` = '' LIMIT 500" );
    while ($cep_insert_row = mysql_fetch_assoc($cep_insert_sql)) 
    {   
        mysql_query("UPDATE cep_links SET stopdate = ".(time())." WHERE id = '" . $cep_insert_row['id'] . "'");
    }
/// ---------------------------------------------------------------------------------------------------------------

/// Находим активные площадки и стираем дату начала неактивности
    ###Серфинг
    $ads_up_sql = mysql_query("SELECT `id` FROM `tb_ads` WHERE `paused` = '0' LIMIT 500" );
    while ($ads_up_row = mysql_fetch_assoc($ads_up_sql)) 
    {
        mysql_query("UPDATE tb_ads SET stopdate = '' WHERE id = '" . $ads_up_row['id'] . "'");
    }
    ###Оплачиваемые письма
    $paymails_up_sql = mysql_query("SELECT `id` FROM `seo_paymails` WHERE `paused` = '0' LIMIT 500" );
    while ($paymails_up_row = mysql_fetch_assoc($paymails_up_sql)) 
    {
        mysql_query("UPDATE seo_paymails SET stopdate = '' WHERE id = '" . $paymails_up_row['id'] . "'");
    }
    ###Задания
    $task_up_sql = mysql_query("SELECT `id` FROM `seo_tasks` WHERE `paused` = '0' LIMIT 500" );
    while ($task_up_row = mysql_fetch_assoc($task_up_sql)) 
    {
        mysql_query("UPDATE seo_tasks SET stopdate = '' WHERE id = '" . $task_up_row['id'] . "'");
    }
    ###Контекстные ссылки
    $kontext_up_sql = mysql_query("SELECT `id` FROM `tb_statlinks` WHERE `paused` = '0' LIMIT 500" );
    while ($kontext_up_row = mysql_fetch_assoc($kontext_up_sql)) 
    {
        mysql_query("UPDATE tb_statlinks SET stopdate = '' WHERE id = '" . $kontext_up_row['id'] . "'");
    }
    ###Баннеры 468x60
    $ban4_up_sql = mysql_query("SELECT `id` FROM `tb_advban` WHERE `paused` = '0' LIMIT 500" );
    while ($ban4_up_row = mysql_fetch_assoc($ban4_up_sql)) 
    {
        mysql_query("UPDATE tb_advban SET stopdate = '' WHERE id = '" . $ban4_up_row['id'] . "'");
    }   
	###Баннеры 100x100
    $ban1_up_sql = mysql_query("SELECT `id` FROM `tb_advban_100` WHERE `paused` = '0' LIMIT 500" );
    while ($ban1_up_row = mysql_fetch_assoc($ban1_up_sql)) 
    {
        mysql_query("UPDATE tb_advban_100 SET stopdate = '' WHERE id = '" . $ban1_up_row['id'] . "'");
    }    
	###Баннеры 200x300
    $ban2_up_sql = mysql_query("SELECT `id` FROM `tb_advban_300` WHERE `paused` = '0' LIMIT 500" );
    while ($ban2_up_row = mysql_fetch_assoc($ban2_up_sql)) 
    {
        mysql_query("UPDATE tb_advban_300 SET stopdate = '' WHERE id = '" . $ban2_up_row['id'] . "'");
    }
	###Рекламная цепочка
    $cep_up_sql = mysql_query("SELECT `id` FROM `cep_links` WHERE `paused` = '0' LIMIT 500" );
    while ($cep_up_row = mysql_fetch_assoc($cep_up_sql)) 
    {
        mysql_query("UPDATE cep_links SET stopdate = '' WHERE id = '" . $cep_up_row['id'] . "'");
    }
/// ---------------------------------------------------------------------------------------------------------------

/// Удаление не активной рекламы
    ###Серфинг 
    $ads_del_sql = mysql_query("SELECT `id` FROM `tb_ads` WHERE `stopdate` < '".(time()-5259486)."' AND `stopdate` != '' AND `paused` != '0' LIMIT 500" );
    while ($ads_del_row = mysql_fetch_assoc($ads_del_sql)) 
    {
        mysql_query("DELETE FROM tb_ads WHERE id = '" . $ads_del_row['id'] . "'");
	    mysql_query("DELETE FROM tb_visits WHERE ident = '" . $ads_del_row['id'] . "'");
    }
    ###Оплачиваемые письма
    $paymails_del_sql = mysql_query("SELECT `id` FROM `seo_paymails` WHERE `stopdate` < '".(time()-5259486)."' AND `stopdate` != '' AND `paused` != '0' LIMIT 500" );
    while ($paymails_del_row = mysql_fetch_assoc($paymails_del_sql)) 
    {
        mysql_query("DELETE FROM seo_paymails WHERE id = '" . $paymails_del_row['id'] . "'");
	    mysql_query("DELETE FROM seo_paymailsviews WHERE ident = '" . $paymails_del_row['id'] . "'");
    }
    ###Задания 
    $task_del_sql = mysql_query("SELECT `id` FROM `seo_tasks` WHERE `stopdate` < '".(time()-5259486)."' AND `stopdate` != '' AND `paused` != '0' LIMIT 500" );
    while ($task_del_row = mysql_fetch_assoc($task_del_sql)) 
    {
        mysql_query("DELETE FROM seo_tasks WHERE id = '" . $task_del_row['id'] . "'");
        mysql_query("DELETE FROM seo_taskcomms WHERE ident = '" . $task_del_row['id'] . "'");
        mysql_query("DELETE FROM seo_taskstats WHERE ident = '" . $task_del_row['id'] . "'");
		mysql_query("DELETE FROM seo_favtasks WHERE ident = '" . $task_del_row['id'] . "'");
    }
    ###Контекстные ссылки
    $kontext_del_sql = mysql_query("SELECT `id` FROM `tb_statlinks` WHERE `stopdate` < '".(time()-5259486)."' AND `stopdate` != '' AND `paused` != '0' LIMIT 500" );
    while ($kontext_del_row = mysql_fetch_assoc($kontext_del_sql)) 
    {
        mysql_query("DELETE FROM tb_statlinks WHERE id = '" . $kontext_del_row['id'] . "'");
    }
    ###Баннеры 468x60
    $ban4_del_sql = mysql_query("SELECT `id` FROM `tb_advban` WHERE `stopdate` < '".(time()-5259486)."' AND `stopdate` != '' AND `paused` != '0' LIMIT 500" );
    while ($ban4_del_row = mysql_fetch_assoc($ban4_del_sql)) 
    {
        mysql_query("DELETE FROM tb_advban WHERE id = '" . $ban4_del_row['id'] . "'");
    }    
	###Баннеры 100x100
    $ban1_del_sql = mysql_query("SELECT `id` FROM `tb_advban_100` WHERE `stopdate` < '".(time()-5259486)."' AND `stopdate` != '' AND `paused` != '0' LIMIT 500" );
    while ($ban1_del_row = mysql_fetch_assoc($ban1_del_sql)) 
    {
        mysql_query("DELETE FROM tb_advban_100 WHERE id = '" . $ban1_del_row['id'] . "'");
    }    
	###Баннеры 200x300
    $ban2_del_sql = mysql_query("SELECT `id` FROM `tb_advban_300` WHERE `stopdate` < '".(time()-5259486)."' AND `stopdate` != '' AND `paused` != '0' LIMIT 500" );
    while ($ban2_del_row = mysql_fetch_assoc($ban2_del_sql)) 
    {
        mysql_query("DELETE FROM tb_advban_300 WHERE id = '" . $ban2_del_row['id'] . "'");
    }
	###Рекламная цепочка
    $cep_del_sql = mysql_query("SELECT `id` FROM `cep_links` WHERE `stopdate` < '".(time()-5259486)."' AND `stopdate` != '' AND `paused` != '0' LIMIT 500" );
    while ($cep_del_row = mysql_fetch_assoc($cep_del_sql)) 
    {
        mysql_query("DELETE FROM cep_links WHERE id = '" . $cep_del_row['id'] . "'");
    }
/// ---------------------------------------------------------------------------------------------------------------

/// Удаление старых выплат
    $pay_del_sql = mysql_query("SELECT `id` FROM `tb_history` WHERE `date` < '".(time()-259200)."'" );
    while ($pay_del_row = mysql_fetch_assoc($pay_del_sql)) 
    {
        mysql_query("DELETE FROM tb_history WHERE id = '" . $pay_del_row['id'] . "'");
    } 
/// ---------------------------------------------------------------------------------------------------------------

/// Удаление устарелой истории  аккаунта
    $statistika_del_sql = mysql_query("SELECT `id` FROM `tb_allhistory` WHERE `data` < '".(time()-5259486)."' " );
    while ($statistika_del_row = mysql_fetch_assoc($statistika_del_sql)) 
    {
        mysql_query("DELETE FROM tb_allhistory WHERE id = '" . $statistika_del_row['id'] . "'");
    }
/// ---------------------------------------------------------------------------------------------------------------

/// Удаление старых писем 
    $poshta_del_sql = mysql_query("SELECT `id` FROM `seo_mails` WHERE `timemessage` < '".(time()-5259486)."'" );
    while ($poshta_del_row = mysql_fetch_assoc($poshta_del_sql)) 
    {
        mysql_query("DELETE FROM seo_mails WHERE id = '" . $poshta_del_row['id'] . "'");
    }
/// ---------------------------------------------------------------------------------------------------------------

/// Удаление статистики конкурса доскипочета 
    $doska_del_sql = mysql_query("SELECT `id` FROM `doska_stat` WHERE `dateend` < '".(time()-604800)."' AND `banks` < '1'" );
    while ($doska_del_row = mysql_fetch_assoc($doska_del_sql)) 
    {
        mysql_query("DELETE FROM doska_stat WHERE id = '" . $doska_del_row['id'] . "'");
    }
/// ---------------------------------------------------------------------------------------------------------------

/// Блокировка штрафника по истечению 10 суток 
    $fine_del_sql = mysql_query("SELECT `id` FROM `tb_users` WHERE `datefine` < '".(time()-864000)."' AND `finestatus` = '1'" );
    while ($fine_del_row = mysql_fetch_assoc($fine_del_sql)) 
    {
        mysql_query("UPDATE tb_users SET ban = 1 WHERE id = '".$fine_del_row['id']."'");
    }
/// ---------------------------------------------------------------------------------------------------------------

/// Ну чтож наш крон чистки базы выполнен и теперь мы запишем дату для отсчета от нее даты следующего запуска. Можно сказать у нас успех!
    mysql_query("UPDATE tb_payx SET timekron = '".time()."' WHERE id = '1'");
}
/// ---------------------------------------------------------------------------------------------------------------