
<?php
session_start();
include('include/db.php');
require_once('include/constant.php');
require_once('include/functions.php');

/*************forgot password *********************************************/
if (isset($_REQUEST['search_account']) && !empty($_REQUEST['search_account'])) {
	
		if (isset($_REQUEST['email']) && $_REQUEST['email'] != "") {
		$email = strtolower(trim($_REQUEST['email']));
		$slt = "select user_email,user_name,user_mobile from registration where (user_mobile='$email' OR LOWER(user_email) = '".strtolower($email)."')";
		$connection = mysqli_query($conn, $slt);
		if (mysqli_num_rows($connection) > 0) {
			$getRowAssoc = mysqli_fetch_assoc($connection);
			print_r($getRowAssoc);
			//die;
			$org_pwd = substr(str_shuffle(str_repeat("0123456789", 15)), 0, 6);
			$pwd = base64_encode($org_pwd);
			$query = "update registration set user_pwd='" . $pwd . "' where LOWER(user_email) = '".strtolower($email)."' OR 	user_mobile='$email'";
			$qry = mysqli_query($conn, $query);
			if($qry)
			{
				$to = $email;
				$subject = "Your account password reset successfully";
				$txt = "\nAccount Password  - " .$org_pwd;
				$headers = "From: info@fairyindia.com";
				
				if(mail($to,$subject,$txt,$headers))
				{
					echo 1;
				}
				else{
					echo 2;
				}
				/************sms send*******/
				$template = get_sms_template('FORGOT_PASSWORD');
				$replaceArray = array(
				                            "[USER_NAME]"=>$getRowAssoc['user_name'],
				                            "[NEW_PASSWORD]" =>$org_pwd
				                            );
				echo $smsText = str_replace(array_keys($replaceArray),$replaceArray,$template);
				$checkSmsStatus = sendSmsDemo($getRowAssoc['user_mobile'],$smsText);
				/************sms send*******/
			}
		}
		else
			echo "not_found";
	}
	die;
}
/*************forgot password **********************************************************/



$login_id=isset($_REQUEST['email']) ? strtolower(trim($_REQUEST['email'])) : '';;
$login_pwd=isset($_REQUEST['pwd']) ? trim($_REQUEST['pwd']) : '';;

 $slt="select user_email, type from registration where (unique_user_id='$login_id' OR LOWER(user_email)='".strtolower(trim($login_id))."') AND user_pwd='".base64_encode($login_pwd)."' ";
$connection=mysqli_query($conn,$slt);

if(mysqli_num_rows($connection)>0)
{
    //  fetch user email through user or unique id and email verify or not code
    while($rows=mysqli_fetch_array($connection))
    {
        $get_email=$rows['user_email'];
        //  stored user email for anywhere use it
         $_SESSION['user_email']=$get_email;
        //   login secure page redirect session create
        //  stored ac type for redirecting
        $ac_type= strtolower(trim($rows['type']));
    }
	$session_security = md5(substr(str_shuffle(str_repeat("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 8)), 0, 8));  //  ganerate suffle string
	$slt2="update registration set login_session='".$session_security."' where unique_user_id='$login_id' OR user_email='$login_id'";
	$qry=mysqli_query($conn,$slt2);
	if($connection)
	{
	     if($ac_type=='user')
	     {
	         $_SESSION['account_type']=$ac_type;
	         echo 'user';
	     }
	     else if($ac_type=='admin')
	     {
	         $_SESSION['account_type']=$ac_type;
	         echo 'admin';
	     }
	     else if($ac_type=='mng')
	     {
	          $_SESSION['account_type']=$ac_type;
	          echo 'mng';
	     }
	     //  main session
         $_SESSION['login_id']=$session_security;


	}	
}
else
{
	echo 'err';
}







?>