<?php

include('init.php');
include('libs/functions.php');


print_header();


if (isset($_POST['uorp']) && isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['email']) ) {
$uorp = scrub_white_list($_POST['uorp'],'NUMBERONLY');
$fname = scrub_sql(scrub_white_list($_POST['fname'],'USER'),20);
$lname = scrub_sql(scrub_white_list($_POST['lname'],'USER'),30);
$email = scrub_sql(scrub_white_list($_POST['email'],'USER'),150);
$sql = "SELECT user_id, username, status FROM tb_user WHERE fname='$fname' AND lname='$lname' AND email='$email' AND (status=1 OR status=2 OR status=7 OR status=8)";
$rs = $db->Execute($sql);
//echo $sql;
$record_count = $rs->RecordCount();
if ($record_count == 1) {
$user_id = $rs->fields('user_id');
$user = $rs->fields('username');
$status = $rs->fields('status');
if ($uorp == 1) {

$sql = "INSERT INTO tb_audit (user_id, event, event_date) VALUES ($user_id, 'Username for this user was emailed to $email via forgot username function', getdate())";
$rs = $db->Execute($sql);



$message = ("



<HTML>
<BODY>
		$fname,
		<BR><BR>
		We have recieved your request to retrieve your account username. <br><br> The information associated with this email address is:<br><br>
		<center><table border='2'><tr><td style='padding-left: 15px; padding-right: 15px;' bgcolor='#CCCCCC'><b>Username:</b></td><td align='center' style='padding-left: 10px; padding-right: 10px;'>$user</td></tr></table></center>
		<BR><BR>
		If you did not request this information, or you are still experiencing issues accessing this account, please visit the <a href='http://test.vrss.domain/support.php'>VRSS Support</a> page.</center>
		<br><br>
		Thank you,
		<BR>
		VRSS Administrator
		<BR><HR><BR>
		</BODY></HTML>
");



}
else {


$newpass = createRandom(7);
$encnewpass = md5("gingerbreadcookies".$newpass);

if ($status == 1) {
	$newstatus = 7;
	}
else if ($status == 2) {
	$newstatus = 8;
	}
else {
	$newstatus = $status;
}

$sql = "UPDATE tb_user SET password='$encnewpass', status=$newstatus WHERE user_id=$user_id";
$rs = $db->Execute($sql);

$sql = "UPDATE tb_password_update SET temp_pass_exp='0', temp_exp_date=NULL WHERE user_id=$user_id";
$rs = $db->Execute($sql);

$sql_FLA = "UPDATE tb_user SET failed_login_attempt=0 WHERE user_id=$user_id";
$rs_FLA = $db->Execute($sql_FLA);

$sql = "INSERT INTO tb_audit (user_id, event, event_date) VALUES ($user_id, 'Password for this user was reset via forgot password function', getdate())";
$rs = $db->Execute($sql);


$message = ("

<HTML>
<BODY>

	$fname,
	<BR><BR>
	Your VRSS password has been temporarily reset to the following: <br><br>

	<center><table border='2'><tr><td style='padding-left: 15px; padding-right: 15px;' bgcolor='#CCCCCC'><b>New password:</b></td><td align='center' style='padding-left: 10px; padding-right: 10px;'>$newpass</td></tr></table></center><br>
	<br>Please log in to the Veterans Re-Entry Search Service to reset your password.
	<BR><BR>
	If you did not request this information, or you are still experiencing issues accessing this account, please visit the <a href='http://test.vrss.domain/support.php'>VRSS Support</a> page.</center>
	<br><br>
	Thank you,
	<BR>
	VRSS Administrator
	<BR><HR><BR>
	</BODY></HTML>

");


}

$headers = "From: Veterans Re-Entry Search Service Admin <noreply@domain>\n" . "MIME-Version: 1.0\n" . "Content-type: text/html; charset=iso-8859-1";

mail( $email, "Veterans Re-Entry Search Service Lost Login",
	$message, $headers );


		print "<H1 align=center>Lost Username/Password Sent</H1><BR><HR><CENTER>The requested information has been sent to <b>$email</b>. <br><br>
		<a href='login.php'>Back to Login</a>
		</CENTER>";

}
else {

		print "<H1 align=center>Error!</h1><br><h3 align='center'>The information provided did not match our records.</h3><br><br>
			<center><a href='login.php'>Back to Login</a> | <a href='support.php'>VRSS Support</a></center>";

}


}
else {print "<H1 align=center>Error. Incomplete form.</H1>";}

?>




<?php

print_footer();


?>