<?php

$sLogFile = 'log';
$sSentMailFile = 'email_sent';

file_put_contents($sLogFile, "\n".'cron launched at '.date('d/m/Y H:i:s').'. Result: ', FILE_APPEND);

$sVpnPage = file_get_contents('https://billing.hideipvpn.com/cart.php?gid=1');
preg_match_all('/\<strong\>Free\ VPN\ (NL|UK\/US)\ PPTP\ Connection\<\/strong\>\ \<em\>\((.*)\ Available\)\<\/em\>/i', $sVpnPage, $asMatches);

$bAvailableAccount = false;
foreach($asMatches[2] as $iAccountNb)
{
	if($iAccountNb>0)
	{
		$bAvailableAccount = true;
	}
}

if($bAvailableAccount)
{
	$sResult = 'VPN available. ';
	if(false && file_exists($sSentMailFile))
	{
		$sResult .= 'Mail already sent';
	}
	else
	{
		$sHeaders = 'MIME-Version: 1.0'."\r\n".
					'Content-type: text/plain; charset=UTF-8'."\r\n".
					'From: webmaster@lutran.fr'."\r\n".
					'Reply-To: webmaster@lutran.fr'."\r\n".
					'X-Mailer: PHP/'.phpversion();
		if(mail('julien.lutran@gmail.com', 'VPN disponible', 'go get one ! https://billing.hideipvpn.com/cart.php?gid=1', $sHeaders))
		{
			$sResult .= 'Mail sent';
			file_put_contents($sSentMailFile, 'mail sent at '.date('d/m/Y H:i:s'));
		}
		else
		{
			$sResult .= 'Failed attempt to send mail';
		}
	}
}
else
{
	$sResult = 'No VPN available';
}
file_put_contents($sLogFile, $sResult.'.', FILE_APPEND);

?>
