<?php

	include "library.php";

	$cxn = start_code();

	$cxn_common = db_common_login();


			/* get variables */

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



			/* 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 );

        $login = true;

	if (val( $_GET, 'guest' ) == "true")
        {
             $handle = "guest";
             $password = "guest";
             $_SESSION['logged_in_as_guest'] = "true";
        }

	if ($handle == "")
	{
		page_header( "", $cxn, $cxn_common, "" );
		echo black( "The handle entered is blank<br><br>" );
                $login = false;
	}
	else
	if ($password == "")
	{
		page_header( "", $cxn, $cxn_common, "" );
		echo black( "The password entered is blank<br><br>" );
                $login = false;
	}

        if ($login)
	{
		$query = "SELECT * FROM profile WHERE handle = '$handle'";

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

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

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

					echo black( "Password Incorrect<br><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><br>";

					if ($data['login_attempts'] >= 10)
					{
						echo black( "Your account has been locked.<br><br>" );
						echo black( "Please contact site support on" );
						echo black( " <a ref='mailto:admin@academicdating.net'>admin@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
				{
				        if ($handle != "guest")
				                $_SESSION['logged_in_as_guest'] = "false";

					$_SESSION['id'] = $data['id'];
					
					$_SESSION['show_language_translation'] = $data['show_language_translation'];


            		$ip_country = get_ip_country( $cxn );

            		$ip_country = str_replace( "'", "''", $ip_country );
		                                           
					$num_logins = $data['number_of_logins'] + 1;


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


					$query = "INSERT INTO logins_log (member_id, login_datetime) VALUES (".$data['id'].", '".date( "Y-m-d H:i:s" )."')";
					$result = run_query( $query, $cxn );

            	    $ssex_and_seeking = val( $_SESSION, 'ssex_and_seeking' );
                	$smin_age = val( $_SESSION, 'smin_age' );
                	$smax_age = val( $_SESSION, 'smax_age' );
                	$scity = val( $_SESSION, 'scity' );
                	$scountry = val( $_SESSION, 'scountry' );
                	$sregion = val( $_SESSION, 'sregion' );	

                    if ($ssex_and_seeking != "" || $smin_age != "" || $smin_age != "" || $scity != "" || $scountry != "" || $sregion != "")       
                    {
    					$query = "UPDATE profile SET last_search_search_for = '".$ssex_and_seeking."', last_search_minimum_age = '".$smin_age."', last_search_maximum_age = '".$smax_age."', last_search_city = '".$scity."', last_search_country = '".$scountry."', last_search_region = '".$sregion."' WHERE id = ".$data['id'];
	    				$result = run_query( $query, $cxn );
                    }

                    if ($data['membership_type'] == "paid")
                    {
                        $date_now = date("Y-m-d H:i:s");
                        
                        if ($data['premium_membership_expiry'] < $date_now)
                        {
        					$query = "UPDATE profile SET membership_type = 'free' WHERE id = ".$data['id'];
					        $result = run_query( $query, $cxn );
					
    						page_header( "", $cxn, $cxn_common, "" );
						
                    		echo black( "<br>Your Paid membership has expired. To renew your membership please go to the 'Upgrade to Paid Membership' option on the 'Other' tab on the home page.<br><br><br>" );
                        }
                    }


					if ($data['returned_mail'] == "yes")
					{
						page_header( "", $cxn, $cxn_common, "" );
						
                		echo black( "<br>We have received a bounced email indicating that the email address that you have on file in your profile is not valid.<br><br>" );
                		echo black( " Please edit your profile and enter a valid email address that you check regularly.<br><br>" );
                		echo black( " Your email address is listed as " );
                		echo "<span class='gemail'><b>".$data['email']."</span></b><br><br><br>";
                		
						echo show_link( "Edit Profile", "edit_profile.php", "newprofile", "false" )."<br><br>";
					}
					else

                            // change back to http after login
                            
                            // possible problem here: Internet Explorer seems to drop session variables on going from http to https,
                            // so the logged in $_SESSION['nextpage'] is dropped. At present no browser seems to drop https to http
                            // but possible problem for the future
                            
                       session_write_close();                            
                    
//					header( "Location: city1.net/".$nextpage );

        			header( "Location: ".$nextpage );					
				}
			}
		}
	}

	echo show_home_link();
?>