<?php
require 'sql.php';
$query = mysql_query("SELECT * FROM user");
echo '<pre>';
echo 0;

while($row = mysql_fetch_assoc($query)){
	echo '1<br/>';
	$q1 = mysql_query('SELECT * FROM planos WHERE id_user='.$row['id'].' LIMIT 1');
	$resultado = mysql_fetch_assoc($q1);

	$dias = $resultado['dias_restantes'] - 1;
	mysql_query('UPDATE planos SET dias_restantes = '.$dias.' WHERE id_user = '.$row['id']);


	if($dias == '5'){
		$subject = 'Titulo 5 DIAS';
		$message = '
		Mensagem de 5 dias
		';
		$headers[] = 'MIME-Version: 1.0';
		$headers[] = 'Content-type: text/html; charset=iso-8859-1';
		$headers[] = 'To: '.$row['nome'].' <'.$row['email'].'>';
		$headers[] = 'From: PainelDNS <noreply@paineldns.com>';
		mail($row['email'], $subject, $message, implode("\n", $headers));
	}

	if($dias == '3'){
		$subject = 'Titulo 3 DIAS';
		$message = '
		Mensagem de 3 dias
		';
		$headers[] = 'MIME-Version: 1.0';
		$headers[] = 'Content-type: text/html; charset=iso-8859-1';
		$headers[] = 'To: '.$row['nome'].' <'.$row['email'].'>';
		$headers[] = 'From: PainelDNS <noreply@paineldns.com>';
		mail($row['email'], $subject, $message, implode("\n", $headers));
	}

	if($dias <= '-5'){
		mysql_query('DELETE FROM tsdns WHERE id_user='.$row['id']);
		mysql_query('DELETE FROM jtsdns WHERE id_user='.$row['id']);
		mysql_query('DELETE FROM user WHERE id='.$row['id']);
	}

}
