<?php
/*************************************************************************** 
*	File Info: forgot_pass.php 
*	Purpose:   Generates New Password If Lost 
*	Updated:   31/07/2003 
****************************************************************************   
*	Developer: Alistar Brookbanks (Brooky.com) 
*	Copyright: (C)2003 http://www.brooky.com
*	Copyright: (C)2003 http://www.cubecart.com
*	This program is not "free" software and restrictions apply! 
*	Further Info: http://www.cubecart.com/license.php
*	Contact support@cubecart.com if any conditions are not clear. 
*
*	Licensees holding valid "CubeCart Licence Number" may edit
*	the (powered by CubeCart) from browser title and "Powered by CubeCart"
*	and "(c) Brooky.com" from the web page footer.
*
*	This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
*	THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
*	PURPOSE. This and all others in the download package can only be
*	redistributed with written permission from Alistair Brookbanks!
*
*	The "CubeCart License" is available to purchase at
*	https://secure.cubecart.com/
*	For pricing please contact us via e-mail at sales@cubecart.com 
***************************************************************************/

session_start();

include("admin/config.php");

include( "admin/settings.inc.php");

include( "header.inc.php");

include( "shoppingcart.php");

$cart = new Cart;

include("subheader.inc.php");

// start border
sb("100%",$la_fp_reset_pass,$colour_1,$bg_colour);

	// if customer has clicked submit reset
	if($reset)
		{
			// Generate random password
			$chars = array( 
				"a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H","i","I","j","J", "k","K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T", "u","U","v","V","w","W","x","X","y","Y","z","Z","1","2","3","4","5","6","7","8", 
				"9","0");
	
			$max_chars = count($chars) - 1;
			srand((double)microtime()*1000000);
	
	for($i = 0; $i < 8; $i++)
		{
			$new_pass = ($i == 0) ? $chars[rand(0, $max_chars)] : $new_pass . $chars[rand(0, $max_chars)];
		}


	// Check e-mail address is valid
	if (ereg("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $email))
		{
			$stop = 0;
		}
	else
		{
			print "<br><br><p align=\"center\"><b>$la_fp_fog_pass</b><Br><br><font color='red'>$la_customer_email_inval</font><br><br><a href=\"forgot_pass.php?session=$session\" target=\"_self\">$la_customer_try_again</a></p><br><br>";
			$stop = 1;

eb($bg_colour,$colour_1);

include( "subfooter.inc.php");

exit;

}

// Check email address is held in database
$result = mysql_query ("select email from ".$prefix."store_customer where email = '$email'");
$count_results = mysql_num_rows($result);

if ($count_results > 0)
	{
		$stop = 0;
	}	else
	{
		$stop = 1;
		echo"<br><br><p align=\"center\"><b>$la_fp_fog_pass</b><Br><br><b>$email</b> $la_fp_email_no_exist<Br><Br>$la_fp_email_ent_cor<br><br><a href=\"forgot_pass.php?session=$session\" target=\"_self\">$la_customer_try_again</a></p><br><br>";
	}
	
	// If all is ok encrypt password in database and send new password to the users email address
	if ($stop <> 1)
	{
		$passwd = md5($new_pass);
		$res = mysql_query ("update ".$prefix."store_customer set password='$passwd' where email = '$email'");
	
	if ($res)
	{
		echo "<br><br><p align=\"center\"><b>$la_fp_fog_pass</b><br><br>$la_fp_email_new_temp <b>$email</b><Br>$la_fp_change_sec</p><br><br>";
		
		$sendto = $email;
		$from = $site_email;
		$subject = "$la_fp_fog_pass";
		$message = "$la_account_dear $email,\n\n$la_fp_mail_new_pass $new_pass. $la_fp_mail_change  \n\n$la_account_ending \n\n$site_business\n\n$la_fp_follow_link\n****************************************************************\n$site_url/new_pass.php?temp=$new_pass&email=$email&language=$session_lang\n*****************************************************************\n\n$la_fp_fail_link\n\n$site_url/account.php";
		$headers = "From: $site_email\r\n";
        // send e-mail
        
		mail($sendto, $subject, $message, $headers);
		} 
	}
}//end if submit

// reset password form before submit
if(!$reset)
	{
		echo"<div align=\"center\"><br><br> 
		<form method=\"post\" enctype=\"multipart/form-data\" action=\"forgot_pass.php\">
			<table border=\"0\" cellspacing=\"0\" cellpadding=\"2\" align=\"center\">
				<tr> 
					<td colspan=\"2\" width=\"400\" height=\"10\"><b>$la_fp_lost_pass</b></td>
				</tr>
				<tr> 
					<td colspan=\"2\" width=\"400\" height=\"10\">$la_fp_explain</td>
				</tr>
				<tr> 
					<td width=\"100\" height=\"10\">$la_customer_email</td>
					<td height=\"10\" width=\"300\"> 
					<input type=\"text\" class=\"textbox\" name=\"email\">
					</td>
				</tr>
				<tr> 
					<td width=\"100\" height=\"10\"></td>
					<td height=\"10\" width=\"300\">
					<input type=\"submit\" name=\"reset\" class=\"submit\" value=\"$la_send_pass\">
					</td>
				</tr>
				<tr>
					<td colspan=\"2\" height=\"10\">
					</td>
				</tr>
			</table>
		</form>
	</div>";

}// end if submit

eb($bg_colour,$colour_1);

include("subfooter.inc.php");

include("footer.inc.php");

?>