<?
	#---------------------------------------------------------------------#
	# This source code is (c)2000 White Pajamas, Inc                      #
	# see http://www.stonekeep.com/license.html for licensing information #
	#---------------------------------------------------------------------#

	#----------------------------------------------------------------------
	# $Id: login.php3,v 1.40 2000/12/14 19:13:49 shevett Exp $
	#----------------------------------------------------------------------

	#----------------------------------------------------------------------
	
Function TimeStamp($username) {
	# This function timestamps the login so we have a log of when 
	#	this person last accesses Keystone.

	$now = time();
	$res = db_query("update people set lastlog=$now where login='$username'");
}

LogIt("DEBUG","login.php3","Login called.  Checking.");
if (IsSet($keycookie)) {
	LogIt("DEBUG","login.php3","'keycookie' is set, so there is a cookie in the browser called 'keycookie'");
	$foo		=	ereg("(.*)_(.*)",$keycookie,$clargs);
	LogIt("DEBUG","login.php3","Looking for user named '$clargs[1]'");
	$uinfo	=	db_query("select * from people where login='$clargs[1]'");
	$udata	=	db_fetch_array($uinfo);
	if (db_numrows($uinfo)) {
		LogIt("DEBUG","login.php3","Lookup successful.  user is '$udata[fullname]'");
	} else {
		LogIt("DEBUG","login.php3","Lookup FAILED.  no such user.  We should bail here.");
	}
	LogIt("DEBUG","login.php3","Browser cookie value is: $keycookie");
	LogIt("DEBUG","login.php3","MD5 section is --------: $clargs[2]");
	LogIt("DEBUG","login.php3","Building new md5 with -: $clargs[1]:$sys_serverkey:$udata[passwd]");
	$scalc 	=	md5("$clargs[1]:$sys_serverkey:$udata[passwd]");
	LogIt("DEBUG","login.php3","New md5 is ------------: $scalc");
	if ("$clargs[2]" != "$scalc") {
		LogIt("DEBUG","login.php3","Comparison FAILED.  Expiring cookie in browser.");
		setcookie("keycookie","",time(),"/");
		$f_func="";
		$f_process="";
		$logmsg	=	"Login incorrect ";
		$logmsg .= $sys_verbose ? "(scalc failed)" : "";
		LogIt("INFO","login.php3","scalc failed for user '$clargs[1]'.  Tried password $clargs[1].");
	} else {
		LogIt("DEBUG","login.php3","Comparison SUCCEEDED.  Access granted.");
		$curruser = "$udata[login]";
		TimeStamp($curruser);
	}
} else {
	LogIt("DEBUG","login.php3","No cookie named 'keycookie' found in the browser");
	if (! empty($login)) {
		LogIt("DEBUG","login.php3","'login' variable set.  Probably a post from the login screen, trying to log in.");

		# 
		# most probably someone trying to log in.
		#

		LogIt("DEBUG","login.php3","Looking for user '$login' in people table.");
		$uinfo	=	db_query("select * from people where login='$login'");
		$udata	=	db_fetch_array($uinfo);
		if (db_numrows($uinfo)) {
			LogIt("DEBUG","login.php3","User '$login' exists in the people table.  It's '$udata[fullname]'");
			LogIt("DEBUG","login.php3","Checking entered password of $password against stored password");
			if (md5($password) == $udata[passwd]) {
			
				#
				# success!
				#
			
				LogIt("DEBUG","login.php3","Login successful.");
				LogIt("DEBUG","login.php3","Cookie contains ------: $login:$sys_serverkey:$udata[passwd]");
			  $clkey = "$login"."_".md5("$login:$sys_serverkey:$udata[passwd]");
				LogIt("DEBUG","login.php3","Full cookie string is : $clkey");
			  setcookie("keycookie",$clkey,time()+36000,"/");
			  $curruser="$udata[login]";
				TimeStamp($curruser);
				
				#
				# do some checking.
				#
			
				$tmpres = db_query("select uid from state where uid='$curruser'");
				LogIt("DEBUG","login.php3","Checking if there is a state record.");
				if (db_numrows($tmpres) == 0) {
					# no state record!  da horrah!
					LogIt("DEBUG","login.php3","No state record.  Creating with userid $curruser, table $slips");
					$tmpres = db_query("insert into state (uid,mdataset) values ('$curruser','slips')");
				}
				LogIt("INFO","login.php3","User '$curruser' logged in.");
			
			} else {
				LogIt("DEBUG","login.php3","Entered password does not match md5()'d version in people table.");
				setcookie("keyuser","",time(),"/");
				$f_func="";
				$f_process="";
				$logmsg="Login incorrect ";
				$logmsg .= $sys_verbose ? "- Unmatched password" : "";
			}
		} else {
			LogIt("DEBUG","login.php3","No such user '$login' in people table.");
			$f_func="";
			$f_process="";
			$logmsg="Login incorrect ";
			$logmsg .= $sys_verbose ? "- no such user" : "";
			LogIt("INFO","login.php3","scalc failed for user '$clargs[1]'.  No such user.");
		}
	}
}
		
?>
