<?php

	include "library.php";

	$cxn = start_code();

    exit_if_search_bot();

    exit_if_session_expired( $cxn );

	
			/* get variables */

	$email = val( $_POST, 'email' );

	$first_name = val( $_POST, 'first_name' );
	$last_name = val( $_POST, 'last_name' );
	$dob = val( $_POST, 'dob' );
    $date_format = val( $_POST, 'date_format' );


			/* start page */

	page_header( "", $cxn );


	if (! ($email != "" || ($first_name != "" && $last_name != "" && $dob != "" && $date_format != "")))
	{
		echo "You must enter an email address or the other fields on the 'forgotton password' screen.<br><br><br>";
	}
    else
    {
    	$first_name = prep_for_sql( $first_name );
    	$last_name = prep_for_sql( $last_name  );
    	$dob = prep_for_sql( $dob );
    
    
        if ($first_name != "" && $last_name != "" && $dob != "" && $date_format != "" )
        {
    
            $sql_date_of_birth = convert_date_of_birth( $dob, $date_format, $cxn );
    
    		$query = "SELECT handle, password FROM profile WHERE first_name = '".$first_name."' AND surname = '".$last_name."' AND date_of_birth = '".$sql_date_of_birth."' ORDER BY number_of_logins DESC";
    
    		$result = run_query( $query, $cxn );
    
    		if (query_num_rows($result) != 0)
    		{
    			$data = get_row( $result );
    
    			$password = $data['password'];
    
        	}
        }
    
    
    	$email = prep_for_sql( $email );
    
    	if ($email != "")
    	{
    		$query = "SELECT id, first_name, handle, password, email, returned_mail FROM profile WHERE email = '".$email."' ORDER BY number_of_logins DESC";	
    	}
    	else	
    	{
    		$query = "SELECT id, first_name, handle, password, email, returned_mail FROM profile WHERE first_name = '".$first_name."' AND surname = '".$last_name."' AND date_of_birth = '".$sql_date_of_birth."' ORDER BY number_of_logins DESC";
    	}
    	
    
    	$result = run_query( $query, $cxn );
    
    	if (query_num_rows($result) == 0)
    		echo black( "There is no member in the system with these details" );
    	else
    	{
    			/* send an email to the member with these details address */
    
    //			for ($i=0; $i < query_num_rows($result); $i++)
    
    		$data = get_row( $result );
        
    		if ($data['returned_mail'] == "yes")
    		{
    			page_header( "", $cxn );
    			
        		echo black( "<br>We have received a bounced email indicating that the email address that you have on file in your profile is invalid.<br><br>" );
        		echo black( " Please edit your profile and enter a valid email address that you check regularly.<br><br><br>" );
    //    		echo black( " Your email address is listed as " );
    //    		echo "<span class='gemail'><b>".$data['email']."</span></b><br><br><br>";
    
        		$_SESSION['id'] = $data['id'];
        		
    			echo show_link( "Edit Profile", "edit_profile.php", "newprofile", "false" )."<br><br>";
    		}
    		else
            {
        
        		$id = $data['id'];
        		$first_name = $data['first_name'];
        		$handle = $data['handle'];
        		$password = $data['password'];
        
        		$subject = sitename." site login details";
        
        		$text_body = "Dear ".name_case( first_name_or_handle( $data ) ).",\n\n";
        		$text_body .= "Your ".sitename." login details are:\n\n\n";
        		$text_body .= "Handle: ".$handle."\n\n";
        		$text_body .= "Password: ".$password."\n\n\n";
        
        		if (query_num_rows($result) > 1)
        		{
        			$text_body .= "Please note, there is more than one profile with these details\n\n";
        
        //					if ($i == 0)
        				$text_body .= "This profile is the most-used profile of these\n";
        		}
        
        
        		$html_body = "Dear ".name_case( first_name_or_handle( $data ) ).",<br><br>";
        		$html_body .= "Your ".sitename." login details are:<br><br><br>";
        		$html_body .= "Handle: ".$handle."<br><br>";
        		$html_body .= "Password: ".$password."<br><br><br>";
        
        		if (query_num_rows($result) > 1)
        		{
        			$html_body .= "Please note, there is more than one profile with these details<br><br>";
        
        //					if ($i == 0)
        				$html_body .= "This profile is the most-used profile<br>";
        		}
        
        		send_external( $id, $subject, $text_body, $html_body, $cxn, "live" );
        
        
        			/* display success on the screen */
        
        		echo black( "<br>Your handle and password have been emailed to you<br><br>" );
        		
        		echo black( "The email address that we have on file for you is " );
        		
        		echo "<span class='gemail'>".$data['email']."</span><br><br>";
        		
        		echo black( "If you have changed email address then you will need to contact site administration.<br><br><br>");
            }
    	}
    }



	echo show_home_link();

    echo "<br><br><br>";
    
    

    echo "</body>";
	
    echo "</html>";


?>