<?Lassoscript
// Last modified 2/25/10 by ECL, Landmann InterActive

// FUNCTIONALITY
// Reset password functionality

// CHANGE NOTES
// 12/12/07
// Recoded for CMS v. 3.0
// 5/20/08
// Recoded URL Links
// 7/23/09
// Added Robot Check
// 2/25/10
// Fixed bad call to nonexistent page_footer.inc

Include:'/siteconfig.lasso';

// Robot check
Include:($svLibsPath)'robotcheck.inc';

// Start the Admin session
Session_Start: -Name=$svSessionAdminName, -Expires=$svSessionTimeout;

// Convert action_params
Var:'vError'=(Action_Param:'Error');
Var:'vOption'=(Action_Param:'Option');
Var:'vUserEmail'=  (Action_Param:'UserEmail');
Var:'vQuestionID_Submit'=(Action_Param:'QuestionID');
Var:'vUserAnswer_Submit'=(Action_Param:'UserAnswer');

// Page head
Include:($svLibsPath)'page_header_admin.inc';

// Debugging
// Var:'svDebug' = 'Y';

// If Action_Param missing, kick out 1003 Required Information error
If: (Var:'vUserEmail') == '' || (Var:'vQuestionID_Submit') == ''|| (Var:'vUserAnswer_Submit') == '';
	Var:'vError' = '1003';

	If: (Var:'vUserEmail') == '';
		$vOption += 'Your E-mail, ';
	/If;
	If: (Var:'vQuestionID_Submit') == '';
		$vOption += 'Security Question, ';
	/If;
	If: (Var:'vUserAnswer_Submit') == '';
		$vOption += 'Answer';
	/If;

	$vOption->(RemoveTrailing:', ');

	LI_URLRedirect: -Page='/reset.lasso',-UseError='Y',-Error=$vError,-Option=$vOption,-UseArgs='Y';

// Made it through form checks, now check valid e-mail
Else: (Valid_Email:($vUserEmail)) == false;

	Var:'vError' = '3003';
	Var:'vOption' = '3003';

	LI_URLRedirect: -Page='/reset.lasso',-UseError='Y',-Error=$vError,-Option=$vOption,-UseArgs='Y';

// Made it through all checks, now get the record
Else;

	Var:'SQLQueryReset' = 'SELECT User_ID,User_Email,User_Active,User_FName,
		User_LName,User_LoginID,User_LoginPW,User_QuestionID,User_Answer
		FROM ' $svUsersTable '
		WHERE User_Email = "' $vUserEmail '"
		AND User_QuestionID = "' $vQuestionID_Submit '"
		AND User_Answer = "' $vUserAnswer_Submit '"';

	Inline: $IV_Users, -SQL=$SQLQueryReset;

		Var:'vQuestionID' = (Field:'User_QuestionID');
		Var:'vUserAnswer' = (Field:'User_Answer');
		Var:'vUser_LoginID' = (Field:'User_LoginID');
		Var:'vUser_PW' = (Field:'User_LoginPW');
		Var:'vUser_FName' = (Field:'User_FName');
		Var:'vUser_LName' = (Field:'User_LName');
		Var:'vUser_Email' = (Field:'User_Email');

		If: $svDebug == 'Y';
			'75: SQLQueryReset = ' $SQLQueryReset '<br>\n';
			'75: Error_CurrentError = ' (Error_CurrentError) '<br>\n';
		/If;

		// If User not found, kick out 3002 "Email failed" error
		If: (Found_Count) == 0;
			Var:'vError' = '3002';
			Var:'vOption' = '<strong>The requested user was not found</strong>';
		/If;
	
		// If user found, but answer is wrong, kick out 5015 "Answer Wrong" error
		If: (Found_Count) > 0  && ($vUserAnswer_Submit != $vUserAnswer);
			Var:'vError' = '3002';
			Var:'vOption' = '<strong>The answer to the security question is wrong</strong>';
		/If;
	
		If: $vError != '';
			LI_URLRedirect: -Page='/reset.lasso',-UseError='Y',-Error=$vError,-Option=$vOption,-UseArgs='Y';

		// Reset the password
		Else;

			// Get the User ID
			Var:'vUser_ID' = (Field:'User_ID');

			// Create new password
			Var:'NewCreateNewPWTemp' = Create_UID;
			Var:'NewCreateNewPW' = (Encrypt_MD5:$NewCreateNewPWTemp);

			// Update the user record
			Var:'SQLUpdateUser' = '
			UPDATE ' $svUsersTable ' SET
				User_LoginPW = "' $NewCreateNewPW '",
				DateModified = "'(Date_Format:(Date_GetCurrentDate),-DateFormat='%Q') '"
				WHERE User_ID = "' $vUser_ID '"';

			Inline: $IV_Users, -SQL=$SQLUpdateUser;

				If:(Error_CurrentError)==(Error_NoError);
			
					// If no error, Update was successful, dump out Error 1011 "Update Successful"
					Var:'vError' = '1011';
					Var:'vOption' = (Var:'vDisplayName');
					If: $svDebug == 'Y';
						'120: vError = ' $vError '<br>\n';
						'120: vDisplayName = ' $vDisplayName '<br>\n';
					/If;
			
				// There was an error
				Else;
					Var:'vError' = (Error_CurrentError);
				/If;
			
			/Inline;

			// If debug is on, send e-mail to Eric only, otherwise send to SiteAdmin and User
			If: $svDebug == 'Y';
		
				// Recoded for Webmail.us
				Email_Send:
					-Host=$svSMTP,
					-From=$svPostmasterEmail,
					-To=$svDeveloperEmail,
					-Username=$svAuthUsername,
					-Password=$svAuthPassword,
					-ReplyTo=$svAdminEmail,
					-Subject='CMS Login Information DEBUG ON',
					-Sender=(($svDomain)':reset_response'),
					-Body=(Include:($svLibsPath)'email_reset.txt'),
					-SimpleForm;

			Else;
				// Recoded for Webmail.us
				Email_Send:
					-Host=$svSMTP,
					-From=$svPostmasterEmail,
					-To=$vUser_Email,
					-Username=$svAuthUsername,
					-Password=$svAuthPassword,
					-ReplyTo=$svAdminEmail,
					-Subject='CMS Login Information',
					-Sender=(($svDomain)':reset_response'),
					-Body=(Include:($svLibsPath)'email_reset.txt'),
					-SimpleForm;
			/If;

		/If; 

		// Set error to 3001 "email sent"
		Var:'vError' = '3001';

	/Inline;

/If;
?>
<table width="780">
	<tr>
		<td width="170">
			[Include:($svLibsPath)'navbar_main.inc']
		</td>
		<td>
			<div class="contentcontainerwhite">
<?Lassoscript
	// Standard Error Table
	If: $vError != '';
		LI_ShowError3: -ErrNum=$vError, -Option=$vOption;
	/If;
?>
				<br>
				<a href="http://[$svDomain]/admin/">Login to the CMS Admin</a>
			</div>
		</td>
	</tr>
</table>
[OutputFooter]
</body>
</html>
