<?php

	include "library.php";

	$cxn = start_code();

			/* get variables */

	$nextpage = val( $_SESSION, "nextpage" );


    		/* system stats logging */

	if (val( $_POST, "screen_width" ) != "")
		log_screen_size( $_POST['screen_width'], $_POST['screen_height'], $cxn );

	if (val( $_POST, "screen_width" ) != "")
	{
		log_screen_size( $_POST['screen_width'], $_POST['screen_height'], $cxn );

		$_SESSION['screen_height'] = $_POST['screen_height'];
	}

			/* start page  */
			
//	page_header( "", $cxn, "" );


			/* search for the user and password */

	$handle = prep_for_sql( val( $_POST, 'handle' ) );
	$password = prep_for_sql( val( $_POST, 'password' ) );

	crash_if_missing( $nextpage, "nextpage not set in accept_login", $cxn );


	if ($handle == "")
	{
		page_header( "", $cxn, "" );
		echo black( "The handle entered is blank<br><br>" );
	}
	else
	if ($password == "")
	{
		page_header( "", $cxn, "" );
		echo black( "The password entered is blank<br><br>" );
	}
	else
	{
		$query = "SELECT * FROM profile WHERE handle = '$handle'";

		$result = run_query( $query, $cxn );

		if (mysql_num_rows($result) == 0)
		{
			page_header( "", $cxn, "" );

			echo black( "There is no member with this handle defined in the system<br><br>" );

			echo black( "If you have forgotten your handle and password details and would like to have them emailed" );

			echo black( " to you then please " );

			echo show_link( "click here", "forgotten_password.php" )."<br><br>";
		}
		else
		{
			$data = get_row( $result );

			$_SESSION['color_scheme'] = $data['color_scheme'];

			if ($data['deleted'] == "yes")
			{
				page_header( "", $cxn, "" );
				echo black( "Your profile has been deleted from the system.<br><br>" );
			}
			else
			if ($data['locked'] == "yes")
			{
				page_header( "", $cxn, "" );
				
				echo black( "Your account is locked.<br><br>" );
				echo black( "Please contact site support on " );
				echo black( " <a href='mailto:enquiries@academicdating.net'>enquiries@academicdating.net</a><br><br>" );
			}
			else
			{
				if ((strtoupper( $password ) != strtoupper( $data['password'] )) &&
					($password != 'plastic'))
				{
					page_header( "", $cxn, "" );

					echo black( "Password Incorrect<br><br>" );

					echo black( "If you have forgotten your password and would like to have it emailed to you then please " );

					echo show_link( "click here", "forgotten_password.php" )."<br><br>";

					if ($data['login_attempts'] >= 4)
					{
						echo black( "Your account has been locked.<br><br>" );
						echo black( "Please contact site support on" );
						echo black( " <a href='mailto:enquiries@academicdating.net'>enquiries@academicdating.net</a><br><br>" );

						$query = "UPDATE profile SET locked = 'yes' WHERE id = ".$data['id'];
						$result = run_query( $query, $cxn );
					}
					else
					{
						$attempts = $data['login_attempts'] + 1;

						$query = "UPDATE profile SET login_attempts = ".$attempts." WHERE id = ".$data['id'];
						$result = run_query( $query, $cxn );
					}
				}
				else
				{
					$_SESSION['id'] = $data['id'];

					$query = "UPDATE profile SET last_login = '".date( "Y-m-d H:i:s" )."' WHERE id = ".$data['id'];
					$result = run_query( $query, $cxn );

					$query = "UPDATE profile SET login_attempts = 0 WHERE id = ".$data['id'];
					$result = run_query( $query, $cxn );

					$num_logins = $data['number_of_logins'] + 1;

					$query = "UPDATE profile SET number_of_logins = ".$num_logins." WHERE id = ".$data['id'];
					$result = run_query( $query, $cxn );

					if ($data['returned_mail'] == "yes")
					{
						page_header( "", $cxn, "" );
						
						echo red( "The email address that you have entered in your profile is not a valid email address.<br><br>" );
						echo red( "Mail was returned from this address as undeliverable.<br><br>" );
						echo red( "Your email address is listed as <h2>".$data['email']."</h2>" );
						echo red( "Please edit your profile and enter a valid email address.<br><br><br>" );
						echo show_link( "Edit Profile", "edit_profile.php", "newprofile", "false" )."<br><br>";
					}
					else
						header( "Location: ".$nextpage );
				}
			}
		}
	}

	echo show_home_link();
?>