<?php
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);
	
	
}


?>