# This will generate a new password if user has forgotton password

<?php
/***************************************************************************
 *			forgot2.php                   	                   *
 *                     	          				           *
 *   File Info:		  Forgot password                                  *
 *   Copyright:       Copyright 2003 http://www.128concepts.com            *
 *						                           *
 ***************************************************************************/
include ("config.php");
include ("./your-header.inc");
include ("./tests.php");
if ($_SERVER['REQUEST_METHOD'] == "POST") 
{
        # escape data and set variables
        $username = addslashes($_POST["username"]);
        $email = addslashes($_POST["email"]);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Generate temporary user password</TITLE>
</HEAD>
<BODY>
<?php
$table = "users";
$conn = @mysql_connect($server,$user,$password)
	or die("Unable to connect to SQL database...");

$rs = @mysql_select_db($database,$conn) 
	or die("Unable to select the database 'mydata'...Please press 'Create Database' to create new database.");
$sql = "select * from $table where username = '$username' and email = '$email'";
$rs = mysql_query($sql,$conn)
	or die("Could not execute SQL query!!! Possibly SQL query already been executed.");
$nrow = mysql_num_rows($rs);
if ($nrow == '0')
{
echo "<br><br><center><img src=images/line.gif border=0 width=450 height=1><br><font face = verdana color = darkgreen size=2>Opps...unable to obtain password hint for username '$username'<br>Please make sure you entered this correctly. <img src=images/bullets.gif border=0><a href =forgot.php > Re input username</a></font><br><img src=images/line.gif border=0 width=450 height=1></center><br><br>";
}
else
{
	# generate a new password for user 
	
	$pchars = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9");
	$max = count($pchars)-1;
	srand((double)microtime()*1000000);
	for ($a=0;$a < 6; $a++)
	{
		$newpass = ($a == 0) ?$pchars[rand(0,$max)]:$newpass.$pchars[rand(0,$max)];
	}	
	$passwrd = md5($newpass);
	$sqlnew = "UPDATE $table set passwrd = '$passwrd' where username = '$username'";
	$res = mysql_query($sqlnew,$conn)
		or die("Opps...unable to update your password with temporary password!!!!");
	if ($res){
	echo("<br><br><center><img src=images/line.gif border=0 width=100% height=1><br><img src=images/mailme.gif border=0></center><p align=center><font face = Verdana color =navy size=2>Your new temporary password has been sent to <b>$email</b><br>Please ensure you change the password immediately for security reasons!!!</font><br><img src=images/line.gif border=0 width=100% height=1></p>");
		$msg = "********* IMPORTANT EMAIL CONFIRMATION ********\n\n";
		$msg.= "Thank you, you new temporary password has been created from website of $sitecompany.\n";
		$msg.="Your user details will be kept on our database for your user login.\n\n";
		$msg.="Your new temporary password are as follows:-\n\n";
		$msg.="---------------------------------------------------------------------\n";
		$msg.="Username: $username.\n";
		$msg.="Password: '$newpass'.\n";
		$msg.="---------------------------------------------------------------------\n\n";
		$msg.="N.B Your password will be encrpyted on our database.\n";
		$msg.="Please keep your password safe, do not disclose your password to anyone.\n\n";
		$msg.="Thank you for registering with $sitecompany.";
		$to = "$email <$email>";
		$extraheader.="From: $admin_name<$admin_mail>\n";
    		$extraheader.="Reply-To: $admin_mail\r\n";
    		$extraheader.="X-Mailer: $admin_name [admin_mail]";

		@mail($email, "Temporary Password Confirmation", $msg, $extraheader);}
}
echo "<br><br>";
include ("./your-footer.inc");
?>
</html>
</body>
