<?
include_once '/home/krista02/misoft.store/www/engine/config.php'; /// подключение к бд

	#Удаление бана в чате
	mysql_query( "DELETE FROM chat_ban WHERE timeban < '".time()."' LIMIT 100" );
	
	#Удаляем онлайн чата
	mysql_query( "DELETE FROM chat_online WHERE time < '".time(). "'" );
	
	#Удаляем сообщения чата
	mysql_query( "DELETE FROM chat_mess WHERE data < '" . ( time() - 804800 ) . "'" ); 
	
	#Удаление сообщений
	mysql_query( "DELETE FROM messages WHERE from_del = 1 and to_del = 1 LIMIT 100" );
	
	#Сбрасываем колво отосланых смс для активации аккаунта через сутки
	mysql_query( "UPDATE user SET mobileSend = 0, mobileTimeActivate = 0 WHERE mobileTimeActivate + 86400 < '" . time() . "' and mobileSend >= 3 and mobileTimeActivate > 1000 LIMIT 100" ); 
	
	#Удаление корзины
	mysql_query( "DELETE FROM basket WHERE time < '" . time() . "' - 86400 LIMIT 1000" );
	
	#Удаляем неоплаченные рекламные площадки заказанные от неавторизированных пользователей
	 #Серфинг
	 mysql_query( "DELETE FROM dynlink WHERE date < '" . time() . "' - 86400 AND nouser = '1' AND paused = '0' AND balance = '0' LIMIT 100" );
	 #Авто-серфинг
	 mysql_query( "DELETE FROM autodynlink WHERE date < '" . time() . "' - 86400 AND nouser = '1' AND paused = '0' AND balance = '0' LIMIT 100" );
	 #Баннеры 200x300
	 mysql_query( "DELETE FROM banners WHERE date < '" . time() . "' - 86400 AND nouser = '1' AND balance < '" . $config['banner200x300'] . "' AND type = 0 LIMIT 100" );
	 #Баннеры 468x60
	 mysql_query( "DELETE FROM banners WHERE date < '" . time() . "' - 86400 AND nouser = '1' AND balance < '" . $config['banner468x60'] . "' and type = 1 LIMIT 100" );
	 #Контекстнные ссылки
	 mysql_query( "DELETE FROM klink WHERE date < '" . time() . "' - 86400 AND nouser = '1' AND balance < '" . $config['klink'] . "' LIMIT 100" );
	 #Статические ссылки (без цвета)
	 mysql_query( "DELETE FROM statlink WHERE date < '" . time() . "' - 86400 AND nouser = '1' AND balance < '" . $config['statlink'] . "' AND color = 0 LIMIT 100" );
	 #Статические ссылки (с цветом)
	 $pricestatlinksxz = ($config['statlink'] + $config['statlinkColorPrice']);
	 mysql_query( "DELETE FROM statlink WHERE date < '" . time() . "' - 86400 AND nouser = '1' AND balance < '" . $pricestatlinksxz . "' AND color = 1 LIMIT 100" );
	 #Рекламная цепочка (без цвета)
	 mysql_query( "DELETE FROM chain WHERE date < '" . time() . "' - 86400 AND nouser = '1' AND balance < '" . $config['chain'] . "' AND color = 0 LIMIT 100" );
	 #Рекламная цепочка (с цветом)
	 $priceChainsxz = ($config['chain'] + $config['chainColorPrice']);
	 mysql_query( "DELETE FROM chain WHERE date < '" . time() . "' - 86400 AND nouser = '1' AND balance < '" . $priceChainsxz . "' AND color = 1 LIMIT 100" );
	 #Тесты
	 mysql_query( "DELETE FROM tests WHERE date < '" . time() . "' - 86400 AND nouser = '1' AND balance < '" . $config['tests'] . "' LIMIT 100" );
	
	
	#Оплачиваемые задания
	 #Убираем V.I.P задания у которых истекло время
	 mysql_query( "UPDATE tasks SET vip = 0, viptime = 0 WHERE viptime <= '" . time() . "' and vip = 1 LIMIT 100" );
	
	 #удаляем выполнения которые начаты и истек срок времени исполнения
	 $tasks_nachalo = mysql_query( "SELECT ident FROM tasks_temp WHERE status in (0,3) and enddate <= '" . time() . "' LIMIT 1" );
	 if ( mysql_num_rows( $tasks_nachalo ) > 0 ) {
	   $tasks_nachalo_row = mysql_fetch_array( $tasks_nachalo );
	   mysql_query( "UPDATE tasks SET t_work = t_work - 1 WHERE id = '" . $tasks_nachalo_row['ident'] . "'" );
	   mysql_query( "DELETE FROM tasks_temp WHERE ident = '" . $tasks_nachalo_row['ident'] . "' and status in (0,3) and enddate <= '" . time() . "'" );
	 }
	
	 #Автоматическая оплата выполненных заданий
	 $tasks_autopay = mysql_query( "SELECT * FROM tasks_temp WHERE status in (4) and startdate + 86400 * 3 - 7200 <= '" . time() . "' and ident IN(SELECT id FROM tasks WHERE balance >= price / '" . $config['factor_tasks'] . "')" );
	 if ( mysql_num_rows( $tasks_autopay ) > 0 ) {
		$tasks_autopay_row = mysql_fetch_array( $tasks_autopay );
		
		$tasks_autopay_user = mysql_fetch_array( mysql_query( "SELECT referer, autoproc FROM user WHERE id = '" . $tasks_autopay_row['iduser'] . "'" ) );
		
		function moneyraund ( $money, $kol ) {
			if ( !is_numeric( $money ) ) {
				return $money = 0;
			} else
			if ( strpos( $money, '.') ) {
				$money = explode( '.', $money );
				return $money[0] . '.' . substr( $money[1], 0, $kol );
			} else {
				return $money;
			}
		}
		
		#зачисляем рефбек рефералу I уровня
		if ( $tasks_autopay_user['referer'] != 0 ) {
			$ref_1 = mysql_fetch_array( mysql_query( "SELECT reyting FROM user WHERE id = '" . $tasks_autopay_user['referer'] . "'" ) );
			
			if ( $ref_1['reyting'] >= 1 and $ref_1['reyting'] < 10 ) {
				$ref1_back = $tasks_autopay_row['price'] * $config['st13'];
			} else
			if ( $ref_1['reyting'] >= 10 and $ref_1['reyting'] < 100 ) {
				$ref1_back = $tasks_autopay_row['price'] * $config['st14'];
			} else
			if ( $ref_1['reyting'] >= 100 and $ref_1['reyting'] < 250 ) {
				$ref1_back = $tasks_autopay_row['price'] * $config['st15'];
			} else
			if ( $ref_1['reyting'] >= 250 and $ref_1['reyting'] < 600 ) {
				$ref1_back = $tasks_autopay_row['price'] * $config['st16'];
			} else
			if ( $ref_1['reyting'] >= 600 and $ref_1['reyting'] < 1000 ) {
				$ref1_back = $tasks_autopay_row['price'] * $config['st17'];
			} else
			if ( $ref_1['reyting'] >= 1000 ) {
				$ref1_back = $tasks_autopay_row['price'] * $config['st18'];
			} else {
				$ref1_back = 0;
			}
			
			if ( !is_numeric( $ref1_back ) ) $ref1_back = 0;
			$my_refback = $ref1_back * ( $tasks_autopay_user['autoproc'] / 100 );
			if ( !is_numeric( $my_refback ) ) $my_refback = 0;
			$ref1_refback = $ref1_back - $my_refback;
			if ( !is_numeric( $ref1_refback ) ) $ref1_refback = 0;
			mysql_query( "UPDATE user SET money = money + '" . $ref1_refback . "' WHERE id = '" . $tasks_autopay_user['referer'] . "'" );
		}
		
		$my_money = $tasks_autopay_row['price'] + $my_refback;
		
		mysql_query( "UPDATE user SET money = money + '" . $my_money . "', toref = toref + '" . $ref1_refback . "', tasks_view = tasks_view + 1 WHERE id = '" . $tasks_autopay_row['iduser'] . "'" );
		
		$viruchka = moneyraund( moneyraund( $tasks_autopay_row['price'] / $config['factor_tasks'], 2 ) - $ref1_refback - $my_money, 4 );
		mysql_query( "UPDATE viruchka SET money = money + '" . $viruchka . "' WHERE ident = 5" );
		
		mysql_query( "UPDATE tasks_temp SET status = 1, refuse = '' WHERE ident = '" . $tasks_autopay_row['ident'] . "' and iduser = '" . $tasks_autopay_row['iduser'] . "' and status = 4" );
		mysql_query( "UPDATE tasks SET t_wait = t_wait - 1, t_goods = t_goods + 1, balance = balance - '" . moneyraund( $tasks_autopay_row['price'] / $config['factor_tasks'], 2 ) . "' WHERE id = '" . $tasks_autopay_row['ident'] . "'" );
		
		#Создаем по днях статистику зароботка
		mysql_query( "INSERT INTO earnings_statistics SET iduser = '" . $tasks_autopay_row['iduser'] . "', summ = '1', date = '" . time() . "', ident = '" . $tasks_autopay_row['ident'] . "', type = '4'" );
	 }
	
	
	
	
	
	
	
	
	
	

?>