<?php

//$random_token = md5(uniqid(rand(), true));
	
$isAuthorized = false;
$blowfish_token = "keyforseletarmall124801597219038579102830195710831029y7029380uadfouqewfajsdfalsdfkjasdfq1r8";


if(!isset($_GET[token]) || $_GET[token] =='' || $_GET[token] ==null){
			
	echo 'You are not authorized.';
}else{
	 
	
				if($_GET[token]==$blowfish_token){
					$isAuthorized=true;
					//echo 'Same string';
				}
	//echo 'You are authorized.';
}


if($isAuthorized==true){

		$con = mysql_connect("localhost","ooffle_tsm_admin","tsm12345");

		if (!$con)

		  {

		  die('Could not connect: ' . mysql_error());

		  }



		mysql_select_db("ooffle_tsmballoons", $con);
	 $counterPriority1 =0;
	 $counterPriority2or3 =0;
	
	
	
	$resultCount=mysql_query("SELECT COUNT(*) as total FROM prizetable WHERE avail='0' AND DATE(`updated_at`) = DATE(CURDATE())");
	$data=mysql_fetch_assoc($resultCount);
	//echo 'TOTAL DAY REDEEMED: '. $data['total'];
	
	
	if($data['total'] <=45){
		$result= mysql_query("SELECT * FROM prizetable WHERE avail='1' AND priority='1' ORDER BY RAND() LIMIT 1");
      
		while($rows = mysql_fetch_array($result))
		{
			
			   $counterPriority1++;
				echo $rows['prize'];
				$sql2 = "UPDATE prizetable SET avail = '0', updated_at = now() WHERE id = '".$rows['id']."'";
				if (!mysql_query($sql2,$con)){die('Error: ' . mysql_error());}

		}

		if($counterPriority1==0){
			//echo 'No more priority 1 prizes!';

			$result2= mysql_query("SELECT * FROM prizetable WHERE avail='1' AND (priority='2' OR priority='3')  ORDER BY RAND() LIMIT 1");
			
			while($rows = mysql_fetch_array($result2))
			{
			
			   $counterPriority2or3++;
				echo $rows['prize'];
				$sql3 = "UPDATE prizetable SET avail = '0', updated_at = now() WHERE id = '".$rows['id']."'";
				if (!mysql_query($sql3,$con)){die('Error: ' . mysql_error());}

			}

			if($counterPriority2or3==0){
				echo 'No more prizes for entire campaign!';
			}
		}

	}else{
		echo 'No more prizes for the day!';
	}
		mysql_close($con);	
	
 
}
?>