<?

require_once('altauth_lib.php');

function do_resetpass()
{
	$username = strtolower($_REQUEST['username']);
	
	if ( ! $username)
	{
		print '<center><b style="color:red">* Please fill in the Username field</b></center><br>';
		return 0;
	}

	if (has_user($username))
	{
		if (preg_match('/\\@/', $username))
		{
			$password = substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"), 0, 8);
			save_user($username, $password);
			
			$path = preg_replace('/\/[^\/]+$/', '', $_SERVER['PHP_SELF']); ;
			mail($username, 'Your new password', "URL: $_REQUEST[return_url]\nUsername: $username\nPassword: $password");
			
			print "<center><b style='color:blue'>Your new password is sent to your e-mail address!</b></center><br>";
			
			return 1;
		}
		else
		{
			print "<center><b style='color:red'>* Username $username does not look like an e-mail address</b></center><br>";
		}
	}
	else
	{
		print "<center><b style='color:red'>* User with username $username is not found in the DB</b></center><br>";
	}

	return 0;
}

//---------------------------------------- M A I N --------------------------------------
if (file_exists('altauth_header.php')) print file_get_contents('altauth_header.php');

print "<style type='text/css'>\n";
print file_get_contents('altauth_style.css');
print "</style>\n";

if( ! isset($_REQUEST['do_resetpass']) || ! do_resetpass() )
{
	include('altauth_resetpassscreen.php');
}

if (file_exists('altauth_footer.php')) print file_get_contents('altauth_footer.php');

print_copyright();

?>
