<?

/*
FlexCMS (http://www.flexcms.com)
Flexible, Powerful, Affordable.

Contents of this file are protected by the copyright
notice found at http://www.flexcms.com/flex/pages/copyright.html,
and its usage is subject to the terms of the licensing agreement 
available at http://www.flexcms.com/flex/pages/license.html
*/



	// ========================================================================================
	// Lost Password Unlock
	// ========================================================================================
	
	$PageTitle = $MsgText[0][506];
	$BrowserTitle = $MsgText[0][506];
	$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > '.$MsgText[0][506];
	
	ReadFormArguments();
	Global $FormArguments;
	
	if ($FormArguments['UsernameToEdit'] != '' && $FormArguments['ResetCode'] != '') {
		
		$query = "select Username,ResetCode from `".$Settings['DBPrefix']."core-Users` where Username='".addslashes($FormArguments['UsernameToEdit'])."' LIMIT 1";
		$result = mysql_query($query) or die (mysql_error());
	
		$row = mysql_fetch_array($result);
	
		if ($row['ResetCode'] == $FormArguments['ResetCode'] && $row['ResetCode'] != '') {
		
			if ($FormArguments['Password1'] != '' && $FormArguments['Password1'] == $FormArguments['Password2']) {
						
				$FinalPassword = '';
				// encrypt the password, correcting for the fact that php only encrypts the first eight characters
				$EncryptedUsername = crypt($row['Username'],substr($row['Username'],0,2));
				$EncryptedUsername = substr($EncryptedUsername,0,8);
				$LoopCount = ceil(strlen($FormArguments['Password1'])/8);
				for ($i = 1; $i <= $LoopCount; $i++) {
					$FinalPassword .= crypt(substr($FormArguments['Password1'],($i*8)-8,8),substr($FormArguments['Password1'],0,2));
				}
				$FinalPassword = $EncryptedUsername.$FinalPassword;
										
				
				$query = "update `".$Settings['DBPrefix']."core-Users` set ResetCode='', Password='".$FinalPassword."',SessionLength='10' where Username='".$row['Username']."' LIMIT 1";
				$result = mysql_query($query) or die (mysql_error());
			
			
				sleep(1);
											
				ob_start();
				
				$CookieData = $row['Username'] . '==' . $FinalPassword;
				
				setcookie ($CookieName, $CookieData, 0, '/');
				
				header('Location: ' . $MainURL);
						
				ob_end_flush();
					
			}
			else {
				sleep(1);
				$PageContents = $MsgText[0][94];
			} 
			
				
		}
		else {
			sleep(7);
			$PageContents = $MsgText[0][515];
		}
		
	}
	else {
		sleep(3);
		$PageContents = $MsgText[0][516];	
	}
	
	
	// ========================================================================================




?>