<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Databse Testing File</title>
</head>

<body><?php

//testing function

function print_rows(){
	
	$link = mysql_connect("localhost", "hmhcs_atmusers", "umopapisdn") or die('Could not connect: ' . mysql_error());
	mysql_select_db("hmhcs_atmusers", $link) or die('Could not select database');
	
	
	$query = "SELECT * FROM `users`";
	
	$result = mysql_query($query, $link);
	
	while($row = mysql_fetch_assoc($result)){
		
		foreach($row as $colname => $col){
			print "$colname\t\t$col<br />\r\n";
		}
		
		print "<br />\r\n<br />\r\n<br />\r\n";

	}
	
	mysql_close($link);

	
}

////////////Database functions

function name_allowed($username, $server='both'){
	
	
	$link = mysql_connect("localhost", "hmhcs_atmusers", "umopapisdn") or die('Could not connect: ' . mysql_error());
	mysql_select_db("hmhcs_atmusers", $link) or die('Could not select database');
	
	//$query = "SELECT items.itemnum AS number, items.model, items.brand, items.price AS givingprice, items.ourprice, options.* FROM `items` LEFT JOIN `options` ON items.itemnum = options.itemnum WHERE items.stock > '0' ORDER BY `brand`, items.model, options.price";
	
	$query = ($server!='both') ? "SELECT * FROM `users` WHERE `username` = '$username' AND `$server` = '1'" : "SELECT * FROM `users` WHERE `username` = '$username'";
	
	$result = mysql_query($query, $link);
	
	if(mysql_num_rows($result)!=0){
		return TRUE;
	}
	else
		return FALSE;
	mysql_close($link);

}

function add_user($username, $server='both', $expires='2999-1-1'){
	

	if($server == 'efx'){
		$efx = 1;
		$datastream = 0;
	}
	elseif($server == 'datastream'){
		$efx = 0;
		$datastream = 1;
	}
	elseif($server == 'both'){
		$efx = 1;
		$datastream = 1;
	}
	else{
		die('server unrecognized');
	}
	
	$link = mysql_connect("localhost", "hmhcs_atmusers", "umopapisdn") or die('Could not connect: ' . mysql_error());
	mysql_select_db("hmhcs_atmusers", $link) or die('Could not select database');
	
	$isexistent = name_allowed($username);
	
	if(!$isexistent)
		$query = "INSERT INTO `users` (`username`, `efx`, `datastream`, `enabled`, `expires`) VALUES ('$username', '$efx', '$datastream', '1', '$expires')";
	else{
		$query = ($server!='both') ? "UPDATE `users` SET `$server` = '1' WHERE `username` = '$username';" : "UPDATE `users` SET `efx` = '1', `datastream` = '1' WHERE `username` = '$username';";
	}
	
	$result = mysql_query($query, $link);
	
	
	mysql_close($link);
	
	
}


function delete_user($username, $server='both'){
	
	$link = mysql_connect("localhost", "hmhcs_atmusers", "umopapisdn") or die('Could not connect: ' . mysql_error());
	mysql_select_db("hmhcs_atmusers", $link) or die('Could not select database');
	
	$isexistent = name_allowed($username);
	
	if($isexistent){
		if($server == 'both'){
			$query = "DELETE FROM `users` WHERE `username` = '$username'";
		}
		elseif($server == 'efx' || $server = 'datastream'){
			$query = "UPDATE `users` SET `$server` = '0' WHERE `username` = '$username';";
		}
		else{
			die('Server not recognized');
		}
	
		$result = mysql_query($query, $link);
	}
	else{
		die('User not found in Database');
	}
		
	
	mysql_close($link);
	
	
}


//print name_allowed("OmarButt") ? "yes" : "no";
print "<br />
";
//print "<br />\r\n".date("Y-m-d", mktime(0, 0, 0, 7, 1, 2012))."<br />\r\n";
/*
add_user("AccessOne", "both");
add_user("kittyatm", "efx");
add_user("rickp1", "efx");
add_user("Rtaichman1", "both");//, date("Y-m-d", mktime(0, 0, 0, 7, 1, 2012)) );
//print name_allowed("OmarButt", 'efx') ? "yes" : "no";
*/
print "<br />
";
print_rows();
//add_user("OmarButt");
print name_allowed("OmarButt", 'efx') ? "yes" : "no"; echo "<br />\r\n";
print name_allowed("OmarButt", 'datastream') ? "yes" : "no"; echo "<br />\r\n";
print name_allowed("OmarButt", 'both') ? "yes" : "no"; echo "<br />\r\n";
echo "<br />\r\n";
print name_allowed("AccessOne", 'efx') ? "yes" : "no"; echo "<br />\r\n";
print name_allowed("AccessOne", 'datastream') ? "yes" : "no"; echo "<br />\r\n";
print name_allowed("AccessOne", 'both') ? "yes" : "no"; echo "<br />\r\n";
echo "<br />\r\n";
print name_allowed("kittyatm", 'efx') ? "yes" : "no"; echo "<br />\r\n";
print name_allowed("kittyatm", 'datastream') ? "yes" : "no"; echo "<br />\r\n";
print name_allowed("kittyatm", 'both') ? "yes" : "no"; echo "<br />\r\n";
echo "<br />\r\n";
print name_allowed("rickp1", 'efx') ? "yes" : "no"; echo "<br />\r\n";
print name_allowed("rickp1", 'datastream') ? "yes" : "no"; echo "<br />\r\n";
print name_allowed("rickp1", 'both') ? "yes" : "no"; echo "<br />\r\n";
echo "<br />\r\n";
print name_allowed("Rtaichman1", 'efx') ? "yes" : "no"; echo "<br />\r\n";
print name_allowed("Rtaichman1", 'datastream') ? "yes" : "no"; echo "<br />\r\n";
print name_allowed("Rtaichman1", 'both') ? "yes" : "no"; echo "<br />\r\n";
echo "<br />\r\n";

//delete_user("OmarButt", 'efx');
 ?>
</body>
</html>
