<html>
<head>
<title></title>
</head>
<body onload="document.Form2.reset(); document.Form2.First_Name.focus();">

<?php

function display_form($username,$password,$first_name,$last_name,$email,$phone,$address,$major,$graduation_year,$date)
{
	echo <<< Begin
		<H2 align="center">Your Information</H2>
        	</center>
        	<p></p>
        	<CENTER>
        	<form action="{$_SERVER['PHP_SELF']}" name="Form2" method="POST">

		<table bgcolor="lightgrey" border="1" bordercolor="gray">
            
		<TR>
        	<TD><B>Your First Name:</B></TD>
        	<TD><INPUT NAME="First_Name" SIZE="50" value="$first_name"></TD>
        	</TR>
		
		<TR>
        	<TD><B>Your Last Name:</B></TD>
        	<TD><INPUT NAME="Last_Name" SIZE="50" value="$last_name"></TD>
        	</TR>

		<TR>
        	<TD><B>Your Email:</B></TD>
        	<TD><INPUT NAME="Email" SIZE="50" value="$email"></TD>
        	</TR>

		<TR>
        	<TD><B>Phone:</B></TD>
        	<TD><INPUT NAME="Phone" size="50" value="$phone"></TD>
       	 </TR>
	
	 	<TR>
        	<TD><B>Address:</B></TD>
        	<TD><INPUT NAME="Address" size="50" value="$address"></TD>
        	</TR>

        	<TR>
       	 <TD><B>Major:</B></TD>
        	<TD><INPUT NAME="Major" size="50" value="$major"></TD>
       	 </TR>

        	<TR>
       	 <TD><B>Graduation Year:</B></TD>
        	<TD><INPUT NAME="Graduation_Year" size="50" value="$graduation_year"></TD>
       	 </TR>
			
		<TR>
       	 <TD colspan="2" align="center"><B>Last updated:</B> $date.</TD>
       	 </TR>

		
       	<input name='submitted' value='TRUE' type='hidden'>
		<input name='Username' value='$username' type='hidden'>
		<input name='Password' value='$password' type='hidden'>


       	 </TABLE>

        	<TABLE>
		<tr><td colspan="2" bgcolor="white" height="10"></td></tr>
		<tr><td colspan="2" align="center" BGCOLOR="lightgrey">
		If you update the information, press the submit button.
		</td></tr>
        	<TR>
        	<TD align="center" BGCOLOR="lightgrey"><INPUT TYPE="submit" VALUE="SUBMIT">
       	 </TD>
       	 <TD align="center" BGCOLOR="lightgrey">
       	 <INPUT TYPE="reset" VALUE="RESET">
        	</TD>
        	</TR>
       	 </table>

       	 </form></center>
Begin;
}

function display_form_change_password($username)
{
// Form for changing the password
echo <<< Begin
        <p>
        <CENTER>
        <FORM ACTION="change_password_alumni.php" NAME="Form3" METHOD="POST">
        <CENTER>
	  <input name="Username" type="hidden" value="$username" size="30">
        <P>
	  <INPUT TYPE="submit" VALUE="Change Password?">
	  <P>
        </FORM>
	  </CENTER>
Begin;

}

function does_user_exist($username,$password,&$k)
{
	@$d = mysql_connect('localhost','database_username','database_password');
	if (!d)
	{
		echo "Sorry can't process your information at this time.<BR>";
		return FALSE;
	}
	if (empty($username))
	{
		echo "You can't leave the username empty.<br>\n";
		return FALSE;
	}
	@$select = mysql_select_db('alumni');
	if (!$select)
	{
		echo "Sorry can't process your information at this time.<BR>";
		return FALSE;
	}

	$q = "select count(*) from users where username='$username' and password=sha1('$password')";
	@$r = mysql_query($q);
	if (!$r)
	{
		echo "Sorry can't process your information at this time.<BR>";
		@mysql_close($d);
		return FALSE;
	}

	@$row = mysql_fetch_row($r);
	$k = $row[0];

	@mysql_free_result($r);
	@mysql_close($d); 
	return TRUE;
}

function read_from_db($username,$password,&$first_name,&$last_name,&$email,&$phone,&$address,&$major,&$graduation_year,&$date)
{
	@$d = mysql_connect('localhost','database_username','database_password');
	if (!$d)
	{
		echo "Sorry can't process your information at this time.<BR>";
		return FALSE;
	}

	@$s = mysql_select_db('alumni');
	if (!$s)
	{
		echo "Sorry can't process your information at this time.<BR>";
		return FALSE;
	}
	
	$q = "select first_name, last_name, email, address, phone, major, graduation_year, date from users 
		where username='$username' and password=sha1('$password')";
	@$r = mysql_query($q);
	if (!$r)
	{
		echo "Sorry can't process your information at this time.<BR>";
		@mysql_close($d);
		return FALSE;
	}
	else
	{
		while ($row = mysql_fetch_row($r))
		{
			$first_name = $row[0];
			$last_name = $row[1];
			$email = $row[2];
			$address = $row[3];
			$phone = $row[4];
			$major = $row[5];
			$graduation_year = $row[6];
			$date = $row[7];
		}
	}
	@mysql_free_result($r);
	@mysql_close($d); 
	return TRUE;
}

function write_to_db($username,$password,$first_name,$last_name,$email,$phone,$address,$major,$graduation_year)
{
	$s = array($username,$password,$first_name,$last_name,$email,$phone,$address,$major,$graduation_year);
	foreach ($s as $v)
		if (empty($v))
		{
			echo "You have to fill out all the fields.<br>";
			return FALSE;
		}
	@$d = mysql_connect('localhost','database_username','database_password');
	if (!$d)
	{
		echo "Sorry can't process your information at this time.<BR>";
		return FALSE;
	}

	@$s = mysql_select_db('alumni');
	if (!$s)
	{
		echo "Sorry can't process your information at this time.<BR>";
		return FALSE;
	}

	$q = "update users set first_name='$first_name',last_name='$last_name', date = now(), 
		email='$email',phone='$phone',address='$address',major='$major',graduation_year='$graduation_year' 
		where username='$username' and password=sha1('$password')";
	@$r = mysql_query($q);
	if (!$r)
	{
		echo "Sorry can't process your information at this time.<BR>";
		@mysql_close($d);
		return FALSE;
	}

	// mysql_free_result($r); // NO, don't here.
	@mysql_close($d);
	return TRUE;
}

$username = trim(stripslashes($_POST['Username']));
if (empty($username))
{
	exit("The username is empty. This page can be accessed only by logged in users.");
}

$password = trim(stripslashes($_POST['Password']));

$k = 0;
$flag = does_user_exist($username,$password,$k);
if (!$flag)
	exit;
if($k > 0)
{	
	$flag = read_from_db($username,$password,$first_name,$last_name,$email,$phone,$address,$major,$graduation_year,$date);
	if (!$flag)
		exit;
	if (!isset($_POST['submitted']))
	{
		display_form($username,$password,$first_name,$last_name,$email,$phone,$address,$major,$graduation_year,$date);
		display_form_change_password($username);
	}
	
	if (isset($_POST['submitted']))
	{	
		$username = trim($_POST['Username']);
		$password = trim($_POST['Password']);
		$first_name = trim($_POST['First_Name']);
		$last_name = trim($_POST['Last_Name']);
		$email = trim($_POST['Email']);
		$phone = trim($_POST['Phone']);
		$address = trim($_POST['Address']);
		$major = trim($_POST['Major']);
		$graduation_year = trim($_POST['Graduation_Year']);
		$flag = write_to_db($username,$password,$first_name,$last_name,$email,$phone,$address,$major,$graduation_year);
		if (!$flag)
			exit;
		echo "<h2 align='center'>Your information has been updated. Thank you.</h2>";
		display_form_change_password($username);
						
	} // if (form submitted)

} // $k > 0
else
	echo "<center>Check the username and the password.</center><br>\n";
	
?>
</BODY></HTML>

