<?php

/*
Here we set the username cookie which will be used to remember the
username for the change_password page because we'll replace hidden
fields in this page with cookies to remember the state from 
this page (update_alumni) to the next one (change_password).
*/

function set_username_cookie($username)
{
	if (!isset($_COOKIE['username']))
		setcookie('username',$username);
}

/* The following form is no longer needed because 
the username will be obtained from the cookie instead
from the form below.
*/
function display_form_change_password($username)
{
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 display_form($username,$password,$first_name,$last_name,$email,$phone,$address,$major,$graduation_year,$date)
{
	global $count;
	echo <<< Begin
		<html>
		<head>
		<title></title>
		</head>
		<body onload="document.Form2.reset(); document.Form2.First_Name.focus();">

		<H2 align="center">Your Information ( $count visits.)</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'>
	<!-- The following hidden fields are intended for this page not the next one-->
	
		<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 does_user_exist($username,$password,&$k)
{
	@$d = mysql_connect('localhost','abu-jeib','iyad911');
	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','abu-jeib','iyad911');
	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','abu-jeib','iyad911');
	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.");
}

/*
The following cookie will be used by the enxt page which is the one 
used for changing the password. We use cookies to remember data from 
one page to another. In this case, we want to remember the user name
to use it in change_password instead of promting the user for the 
username again or passing hidden field.
*/

set_username_cookie($username);

// Counter Cookie
$count = $_COOKIE['counter'];
if ($count == "")
	$count = 1;
else
	$count++;
setcookie('counter',$count);

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

$k = 0;
$flag = does_user_exist($username,$password,$k);
if (!$flag)
	exit("<p>can't process your request at this time</p>\n");
if($k > 0)
{	
	$flag = read_from_db($username,$password,$first_name,$last_name,$email,$phone,$address,$major,$graduation_year,$date);
	if (!$flag)
		exit("<p>can't process your request at this time</p>\n");
	if (!isset($_POST['submitted']))
	{
		display_form($username,$password,$first_name,$last_name,$email,$phone,$address,$major,$graduation_year,$date);
		//display_form_change_password($username);
		echo "<center><a href='change_password_alumni_cookies.php'>Change Password</a></center></br>";
	}
	
	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("<p>can't process your request at this time</p>\n");
		echo "<h2 align='center'>Your information has been updated. Thank you.</h2>";
		//display_form_change_password($username);
		echo "<center><a href='change_password_alumni_cookies.php'>Change Password</a></center></br>";
						
	} // if (form submitted)

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

