<?php 
	session_start();

	if (get_magic_quotes_gpc()) die ('get_magic_quotes_gpc');
	$location=$_SERVER["REMOTE_ADDR"];
	$username=$_POST['username'];
	$password=$_POST['password'];

	//Password
	if (!isset($_SESSION['$username']))
	{
		//IF MYSQLI
		// $db=new mysqli('localhost', 'inters_php', 'flask3301', 'inters_Flask');
		// if (mysqli_connect_errno()) {die ('Cannot connect to package database.  Please try again later.' . mysql_error());}
		//ELSE
		 $link = mysql_connect('localhost', 'inters_php', 'flask3301') or die('Could not connect: ' . mysql_error());
		 mysql_select_db('inters_Flask') or die('Could not select database');
		//ENDIF
	    $query="select count(*) as COUNT from Registration where User='".addslashes($username)."' and Password='".addslashes($password)."' ";
		//IF MYSQLI
		// $result=$db->query($query);
		// $numresult=$result->num_rows;
		//ELSE
		 $result=mysql_query($query);
		 $numresult=mysql_num_rows($result);
		//ENDIF
		if ($numresult==1)
		{
			//IF MYSQLI
			//$row=$result->fetch_assoc();
			//ELSE
			 $row=mysql_fetch_assoc($result);
			//ENDIF
			$count=$row['COUNT'];
			if ($count==1)
			{ //Set authorization and username
				$_SESSION['$username']=$username;
			}
			$row=null; $count=null;
		}
		$numresult=null;
		$result=null;
		$query=null;
	}

	//Session should be setup now
	$username=$_SESSION['$username'];
	if (!isset($username)) die ('Username or Password not recognized.');
	$timestampnew=$_SESSION['$timestampnew'];
	$timestampnewp=null;
	if (isset($timestampnew))
	{
		$timestampnewp=$timestampnew;
	}
	$savemessage=' - Replace an old stamp with the new stamp and save changes.';

	//Query
	//IF MYSQLI
	//if (!isset($db)) $db=new mysqli('localhost', 'inters_php', 'flask3301', 'inters_Flask');
	//ELSE
	if (!isset($link))
	{
		$link = mysql_connect('localhost', 'inters_php', 'flask3301') or die('Could not connect: ' . mysql_error());
		mysql_select_db('inters_Flask') or die('Could not select database');
	}
	//ENDIF

	$query="select Guid,Slot,Timestamp,Active,Lastlocation,Lasttime from Package where user='".addslashes($username)."'";
	//IF MYSQLI
	//$result=$db->query($query);
	//ELSE
	$result=mysql_query($query);
	//ENDIF
	if (!$result) die ($query);

	//IF MYSQLI
	//$numresult=$result->num_rows;
	//ELSE
	$numresult=mysql_num_rows($result);
	//ENDIF
	if ($numresult!=5) die ('Unexpected number of slots: '.$numresult);

	for($i=0; $i<5; $i++)
	{
		//IF MYSQLI
		// $row=$result->fetch_assoc();
		//ELSE
		 $row=mysql_fetch_assoc($result);
		//ENDIF
		$guid=$row['Guid'];
		$slot=$row['Slot'];
		$timestamp=$row['Timestamp'];
		$active=$row['Active'];
		$lastlocation=$row['Lastlocation'];
		$lasttime=$row['Lasttime'];
		if ($timestamp=='') $timestamp='0000-00-00 00:00:00'; //format NULL
		if ($timestamp=='0000-00-00 00:00:00') $active=false;
		if ($timestampnew==$timestamp) $timestampnew=null; //already assigned, and no 0 above

		//echo 'DBG isset($timestampnew)='.isset($timestampnew);
		//echo 'DBG $timestamp==\'0000-00-00 00:00:00\'='.$timestamp=='0000-00-00 00:00:00';
		//echo 'DBG !$_SESSION[\'$timestampnewassigned\']='.!$_SESSION['$timestampnewassigned'];
		if (isset($timestampnew) && $timestamp=='0000-00-00 00:00:00' && !$_SESSION['$timestampnewassigned'])
		{
			$timestamp=$timestampnew;
			$timestampnew=null;
			$active=true;
			$slotnew=$slot;
			$savemessage=' - Click Save Changes to save the new stamp.';
			//echo 'DBG $slotnew='.$slotnew;
		}

		$slotvar='guid'.$slot;
		$$slotvar=$guid;

		$slotvar='timestamp'.$slot;
		$$slotvar=$timestamp;
		$slotvar='active'.$slot;
		$$slotvar=$active;
		$slotvar='lastlocation'.$slot;
		$$slotvar=$lastlocation;
		$slotvar='lasttime'.$slot;
		$$slotvar=$lasttime;
	}
	$result=null;
	
	//IF MYSQLI
	//$db->close();
	//ELSE
	mysql_close($link);
	//ENDIF

	$_SESSION['$guid']=$guid1;
	$_SESSION['$timestampnewassigned']=true;
	//echo 'timestampnewassigned:';
	//echo $_SESSION['$timestampnewassigned'];
?>

<HTML>
<FORM action="cpaneludpdate.php" method="post">
	<H3>SQL Offline - Package Receipt</H3>
	<table>
		<tr><td>Sender&nbsp;Name:</td><td><?php echo $username; ?></td></tr>
		<tr><td>Sender&nbsp;ID:</td><td><?php echo $guid1; ?></td></tr>
	</table>
	<P></P>
	<!-- H3>Delivery Confirmation</H3 -->
	<br />
	<table>
		<tr bgColor="#b5bed6">
			<td align="center" width="15">&nbsp;Slot&nbsp;</td>
			<td align="center" width="150">&nbsp;Stamp&nbsp;</td>
			<td align="center" width="50">&nbsp;Active&nbsp;</td>
			<td align="center" width="100">&nbsp;Date&nbsp;Last&nbsp;Opened&nbsp;</td>
			<td align="center" width="100">&nbsp;Location&nbspOpened&nbsp;</td>
		</tr>
		<tr>
			<td>1</td>
			<td align="center"><input type="text" name="timestamp1" width=150 value="<?php echo $timestamp1; ?>" ></td>
			<td align="center"><input type="checkbox" name="active1" size="3" maxlength="3" <?php echo $active1?CHECKED:UNCHECKED ?> ></td>
			<td align="center"><?php echo $lasttime1; ?></td>
			<td align="center"><?php echo $lastlocation1; ?></td>
		</tr>
		<tr>
			<td>2</td>
			<td align="center"><input type="text" name="timestamp2" width=150 value="<?php echo $timestamp2; ?>" ></td>
			<td align="center"><input type="checkbox" name="active2" size="3" maxlength="3" <?php echo $active2?CHECKED:UNCHECKED ?> ></td>
			<td align="center"><?php echo $lasttime2; ?></td>
			<td align="center"><?php echo $lastlocation2; ?></td>
		</tr>
		<tr>
			<td>3</td>
			<td align="center"><input type="text" name="timestamp3" width=150 value="<?php echo $timestamp3; ?>" ></td>
			<td align="center"><input type="checkbox"  name="active3" size="3" maxlength="3" <?php echo $active3?CHECKED:UNCHECKED ?> ></td>
			<td align="center"><?php echo $lasttime3; ?></td>
			<td align="center"><?php echo $lastlocation3; ?></td>
		</tr>
		<tr>
			<td>4</td>
			<td align="center"><input type="text" name="timestamp4" width=150 value="<?php echo $timestamp4; ?>" ></td>
			<td align="center"><input type="checkbox" name="active4" size="3" maxlength="3" <?php echo $active4?CHECKED:UNCHECKED ?> ></td>
			<td align="center"><?php echo $lasttime4; ?></td>
			<td align="center"><?php echo $lastlocation4; ?></td>
		</tr>
		<tr>
			<td>5</td>
			<td align="center"><input type="text" name="timestamp5" width=150 value="<?php echo $timestamp5; ?>" ></td>
			<td align="center"><input type="checkbox" name="active5" size="3" maxlength="3" <?php echo $active5?CHECKED:UNCHECKED ?> ></td>
			<td align="center"><?php echo $lasttime5; ?></td>
			<td align="center"><?php echo $lastlocation5; ?></td>
		</tr>

		<!-- tr>
			<td colspan="5" align="center"><input type="submit" value="Save Changes" /></td>
		</tr -->
	</table>
	<P><strong><FONT color=green><?php echo $timestampnewp; ?></FONT></strong>
		<?php echo $timestampnewp?$savemessage:''; ?>
	</P>
	<P><input type="submit" value="Save Changes" /></P>
	<P><hr>Time:<?php echo date('H:i jS F'); ?>, from: <?php echo $location; ?>.</P>
</FORM>
</HTML>
