<link rel="stylesheet" href="stylesheets/precision_tracking.css" type="text/css" />
<style>body {background-image:none;}</style>

<?php

include('../pg-connect.php'); // Good practice to keep db connection files out of web root
include('../generate_random_string.php');


// Form for entering username to reset password
$form = '
    <h3 style="background-color:#51b759;">Do you really want to reset your password?</h3>
    <p>Please enter your username below. An email with instructions on how to reset your password will be sent to the email address we have on file.</p>
    <form action="forgot_password.php" method="post">
    <input type="text" name="username" autofocus placeholder="Please enter your username" style="margin-bottom:5px;width:180px;" />
    <button type="submit" name="submit">Reset password</button>
    </form>
';



if ($_SERVER['REQUEST_METHOD'] == 'POST') // Handle the form submission.
{ 
    // Validate and secure the form data:
	$problem = FALSE;
    
	if (!empty($_POST['username']) ) 
    {
        
        $username = trim(strip_tags($_POST['username'])); // Strip HTML and potentially dangerous scripts
        
    } 
    else 
    {
        
		$problem = TRUE;
        print $form;
        print '<p class="error">Please enter your username</p>';
	}
    
    
    // Execute if there are no problems with the form
    if (!$problem) 
    {
        
        // Generate random URL string
        $reset_code = generateRandomString(40);
        
        // Update table with random URL parameter string - also insert an expiry date of 4 hours from now
        $query = "SELECT client_update_reset_code ('".$reset_code."','".$username."')";
        
        if ($r = pg_query($dbc,$query))     
        { // Run the query.
            
            while ($row = pg_fetch_row($r)) 
            { 
                                
                // Retrieve email address and companyid - create variables for use in sending email
                $client_query = "SELECT companyid, email FROM Client WHERE username = '".$username."'";
                    $client_row = pg_query($dbc,$client_query);
                        $row = pg_fetch_array($client_row);
                            $company_id = $row[0];
                            $dest_address = $row[1];
                
                            
                            // Stop process if username not on file 
                            if ($row[0] == '') 
                            {
                                print $form;
                                print '<p class="error">Please ensure you have entered a valid username.</p>';
                                break;
                            }
                
                            // Stop process if no recovery email address on file 
                            if ($row[1] == '') 
                            {
                                print $form;
                                print '<p class="error">Unfortunately we don&#39;t have your email address. Please <a href="http://precisiontracking.co.nz/contact_us.html" target="_blank">contact us</a> by phone on 0800 GPS 001 or via email at <a href="mailto:admin@precisiontracking.co.nz">admin@precisiontracking.co.nz</a>.</p>';
                                break;
                            }
                
                
                // Construct HTML email message
                $html_message = '
                <p>Precision Tracking received a request to reset the password for your account. To reset your password, click on the button below:</p>
                <a href="http://www.precisiontracking.co.nz/reset_password.php?id='.$reset_code.'"><button style="padding:10px;background-color:#51b759;color:#fff;cursor:pointer;font-weight:bold;">RESET PASSWORD</button></a>
                <p>Or copy and paste the following URL into your browser:</p>
                <p>http://www.precisiontracking.co.nz/reset_password.php?id='.$reset_code.'</p>
                <p>If you experience any difficulties, or you did not request your password to be reset, please do not hesitate to <a href="http://www.precisiontracking.co.nz/contact_us.html" target="_blank">contact us</a> by phone on 0800 GPS 001 or via email at <a href="mailto:admin@precisiontracking.co.nz">admin@precisiontracking.co.nz</a>.
                ';
                
                
                // Insert email send details into email_outbox table where it will automatically be sent
				$email_query = "INSERT INTO email_outbox ( src_address, dest_address, company_id, subject, text_message, html_message )
								VALUES ( 'info@precisiontracking.co.nz','$dest_address','$company_id','Reset password','','$html_message')";
				
				$email_insert = pg_query($dbc, $email_query);
                
                    if (pg_affected_rows($email_insert) == 1)
                        
                    {
                   
                    // Success message for screen
                    print '<p>An email with instructions on how to reset your password has been sent to the email address we have on file for <strong>';
                    print $username;
                    print '</strong>.</p><p>You will need to reset your password within 4 hours.</p><p>If you experience any difficulties, please do not hesitate to <a href="contact_us.html" target="_blank">contact us</a>.</p>';
                    }
                
                    else
                    {
                    print '<p class="error">We are unable to reset your password at the moment. Please <a href="http://www.precisiontracking.co.nz/contact_us.html" target="_blank">contact us</a> by phone on 0800 GPS 001 or via email at <a href="mailto:admin@precisiontracking.co.nz">admin@precisiontracking.co.nz</a>.</p>';
                    }
            
            } // End while
                
        } // End of query IF.
    
    } // No problem!

} // End of form submission IF.
else // Print form as user has loaded page for first time
{
	print $form;
}


?>
