<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Programmer<programmer@armia.com>        		              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems, Inc and iScripts.com � 2005                |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
?>
<?php
include "includes/config.php";
include "includes/session.php";
include "includes/functions.php";
include_once "classes/cls_store.php";
include_once "classes/cls_user.php";

if (get_magic_quotes_gpc()) {
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
}

$txtEmail = $_POST["txtEmail"];
if (isset($_POST["btnResetPassword"])) {

    $message = "" ;

	if (!isNotNull($txtEmail)) {
        $message .= REGISTRATION_REQUIRED_MSG." <br>";
    }else{
		if (!isValidEmail($txtEmail)) {
	        $message .= NOT_EMAIL_REGISTERED_MSG." <br>";
	    }else{
			$sql = "SELECT first_name,last_name,user_name, user_id FROM ".$tableprefix."users WHERE email = '".addslashes($txtEmail)."' ";
			$res = mysql_query($sql);
			if(mysql_num_rows($res)!=0){
				$row = mysql_fetch_array($res);
				$firstname = $row["first_name"];
				$lastname = $row["last_name"];
				$username = $row["user_name"];
				$userid = $row["user_id"];

				$randnum1 = mt_rand();
				if(strlen($randnum1) > 6){
					$randnum1 = substr($randnum1,0,6);
				}
				$newpass = $randnum1;
				$pass = md5($newpass);


                                //**********************************************Multicart3.0 Additions********************
// //Check Seller Privelege added to this buyer account

    $sellerAccObj   =    User::is_seller_account_activated($userid);

    $seller_activated_status        =   'N';
    //echopre($sellerAccObj);

        if($sellerAccObj->status==SUCCESS)
            {
                    $sellerAccObjVal            =   $sellerAccObj->data;
                    $seller_activated_status    =   $sellerAccObjVal->is_seller;
                    $activated_seller_id        =   $sellerAccObjVal->seller_id;
            }

            if($seller_activated_status=='Y')  {
                        $sellerInfoVo               =    new stdClass();
                        $sellerInfoVo->seller_id    =   $activated_seller_id;
                        $sellerInfoVo->password     =   $pass;
                        $selleifId                  =   Store::updateSellerPassword($sellerInfoVo);
            }

                        $buyerInfoVo               =        new stdClass();
                        $buyerInfoVo->buyer_id     =        $userid;
                        $buyerInfoVo->password     =        $pass;
                        $buyerId                   =        User::updateBuyerPassword($buyerInfoVo);

//**********************************************Multicart3.0 Additions********************
				//$sqlreset = "UPDATE ".$tableprefix."users SET password = '$pass' WHERE user_id = '".addslashes($userid)."' ";

				$userfullname = $firstname;
				if($lastname != ""){
					$userfullname .= " ". $lastname;
				}
				$to = $txtEmail;
				$subject = MAIL_ADMIN_FORGOT_PASSWORD_SUBJECT_1." ".htmlentities(stripslashes(SITE_NAME)) . " ".MAIL_ADMIN_FORGOT_PASSWORD_SUBJECT_2;
				
/*----------Multicart 2.0 Upgradation---------------*/				
				
				$mailmessage = readEmailTemplate('user_forgotpassword');
				
				$mailmessage = str_replace('[name]',$userfullname,$mailmessage);
				$mailmessage = str_replace('[user_name]',$username,$mailmessage);
				$mailmessage = str_replace('[password]',$newpass,$mailmessage);
				
				$mailmessage = nl2br($mailmessage);
				
				$mail_send_details = "<html><head><link href='".SITE_URL."/style/template1-admin.css' rel='stylesheet' type='text/css'></head><body><center><div align='left' id='layout'><div align='left' class='header'><div align='left' class='logo'><img src=".$logofolder."/".$logoimage." width='125' height='39'/></div></div><div>".$mailmessage."</div></div><div align='left' class='clear_float'><img src=".$imagefolder."/clear.gif width=1 height=1/></div></div></div></center></body></html>";
							
				$headers  = "MIME-Version: 1.0" . "\r\n";
				$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
				$headers .= "From: ".SITE_EMAIL."<".SITE_EMAIL.">" . "\r\n";
				$mailsent = @mail($to, $subject, $mail_send_details, $headers);
				
/*----------Multicart 2.0 Upgradation---------------*/												
				
				$successmessage="";
				if($mailsent){
					//mysql_query($sqlreset);
					$successmessage .= PASSWORD_SEND_TO_MSG;
				}else{
					$message .= RETRY_MSG." <br>";
				}

			}else{
				$message .= EMAIL_NOT_REGISTERED_TEXT." <br>";
			}
		}

	}




    if ($message != "") { // error
        $message = "<br>".CORRECT_ERRORS_TEXT."<br>" . $message;
    }
    if($successmessage !="")
    $message=$successmessage;
}

/*display the active template*/

$active_template = displayTemplate();


include "includes/htmltop.php";

/* Top File Name */

include_once ("includes/".$active_template[0]);
?>


<!--mainbody area start -->
<div class="main_body">
	<div class="container">
		<div class="row">

		 <div id="item_details">
            <div class="col-sm-12 col-lg-4 col-md-4"></div>
            <div class="col-sm-12 col-lg-4 col-md-4">
				<div id="items_top_area">
                            <div align="center" id="items_top_area_ttl">
                                    <?php echo FORGOT_PASSWORD_TEXT;?>
                                    <div class="clear"></div>
                            </div>
            	</div>
				<div class="form_wrapper">
					<div class="register_form_outer">
					<div class="registrationform_container">
						<form name="frmForgotPassword" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, 'UTF-8')?>" method="POST">
						<input type="hidden" name="userid" value="<?php echo $userid;?>">
						<div class="text_information" align="center"><?php echo $message?></div>
						<div class="field_wrapper formfield_row">
								<label><?php echo REGISTRATION_EMAIL_TEXT?></label>
								<input type="text" class="form-control" value="" name="txtEmail" placeHolder="<?php echo REGISTERED_EMAIL_TEXT?>" size="55" maxlength="100" >
						</div>
						<div class="field_wrapper formfield_row">
							<input type="submit" value="<?php echo RESET_PASSWORD_BUTTON;?>" name="btnResetPassword" class="button">
						</div>
						</form>
					</div>
					</div>
				</div>
			</div>
			<div class="col-sm-12 col-lg-4 col-md-4"></div>
		</div>
		<div align="left" class="clear"></div>		
 </div>
</div>
</div>	
<!--mainbody area end -->
<div class="clear"></div>




				

<?php
include "includes/".$active_template[2];	/* Bottom File Name */
?>																																													