<?php
/**
 * Processing Forget password form data:
 * check data then generate new password and send it through SMPT Email
 *
 * PHP version 7.3
 *
 * @package        SMS
 * @version        Release 1.0.0
 * @create_by      Mohammad aljaouni
 * @create_date    10-08-2020
 * @edited_by      Mohammad aljaouni
 * @last_edit_date 10-08-2020
 * @todo           Nothing
 */
ob_start();
session_start();
if(empty($_POST["name"])) {
    header("Location: forgetPassword.php?res=212");
    exit;
}
include("config/config.php");
include("conn.php");
include("functions/commonFunction.php");
include("includeInformation.php");
$helpText = "";
if(!empty($_POST["name"])){
    $_POST["name"]=mysqli_real_escape_string($conn, $_POST["name"]);
    $name = $_POST["name"];
    $queryNewUser  ="select * from user_login where `userName`='".$name."'";
    $resultNewUser = mysqli_query($conn, $queryNewUser);
    $numNewUser=mysqli_num_rows($resultNewUser);
}
if(empty($numNewUser)) {
	header("location: forgetPassword.php?res=9");
	exit;
}
$rowUser = mysqli_fetch_array($resultNewUser);
$inputArray1 = 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");
$inputArray2 = array("0","1","2","3","4","5","6","7","8","9");
$new_password="";
$new_password.= $inputArray1[array_rand($inputArray1)];
$new_password.= $inputArray2[array_rand($inputArray2)];
$new_password.= $inputArray1[array_rand($inputArray1)];
$new_password.= $inputArray1[array_rand($inputArray1)];
$new_password.= $inputArray2[array_rand($inputArray2)];
$new_password.= $inputArray1[array_rand($inputArray1)];
$md5Password = md5($new_password);
$sql="UPDATE `user_login` SET `password`='".$md5Password."' WHERE   userName='".$name."' ";
$res=mysqli_query($conn, $sql);

$queryInsertMove = "INSERT INTO `move_archive` (userId,`type`,`date`) VALUES ";
$queryInsertMove .= "('".$rowUser["id"]."','3','".time()."')";
$resultInsertMove = mysqli_query($conn, $queryInsertMove); 

$varEmail= 0;
$subject=$forgetLBL["forgetPassword"];
$msg=$forgetLBL["newPassword"]." : ".$new_password;
$message = "
	<table border=0 cellspacing=8 cellpadding=0 width=100% align=center nowrap id='table1'>
	 <tr><td>
	<div>
	<span class='gmail_quote'><br></span><font face='Tahoma' size='2'>
	<span lang='ar-sa'>&nbsp;&nbsp;&nbsp;</span></font><div align=center>
	<center>
	<table style='border-collapse:collapse' cellspacing=0 cellpadding=0 width='27%' border=0 id='table3'>
	<tbody>
	<tr>
	<td valign=center align=right width=95></td>
	<td align=middle width=111 bgcolor='#f00000'>
	<p dir='rtl'><font face='Simplified Arabic' color='#ffffff' size=2><b>
	<span lang='ar-sa'>".$forgetLBL["loginData"]."</span></b></font> </td>
	</tr></tbody></table></center></div>
	<div align=center>
	<center>
	<table style='border-collapse:collapse' cellspacing=0 cellpadding=6 width='31%' border=0 id='table4'>
	<tbody>
	<tr>
	<td style='border-right:#808080 1px solid;border-top:#808080 1px solid;border-left:#808080 1px solid;border-bottom:#808080 1px solid' width=211>
	<div align=center>
	<center>
	<table style='border-collapse:collapse' cellspacing=0 cellpadding=0 width='90%' border=0 id='table5'>
	<tbody>
	<tr>
	<td align=right><font face='Tahoma' size='2' color='#F00000'> ".$rowUser["userName"]." </font></td>
	<td align=right nowrap height=25><font face=Tahoma color='#f00000' size=2> : 
	<span lang='ar-sa'> ".$forgetLBL["username"]." </span></font></td></tr>
	<tr>
	<td align=right><font face='Tahoma' size='2' color='#F00000'> ".$new_password." </font><font face=Tahoma color='#f00000' size=2> </font></td>
	<td align=right width=90 height=25 nowrap><font face=Tahoma color='#f00000' size=2> </font>
	<span lang='ar-sa'><font face='Tahoma' size='2' color='#F00000'> ".$forgetLBL["newPassword"]." </font></span></td></tr></tbody></table></center></div>
	 </center></div>
	<p><br>
	<font color='#000000'></div>
	
	</td></tr>
	</table>  ";
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";

$headers .= "From:  ".$fromEmail." \r\n";

if ( !empty($host) and !empty($smtpUsername)  and !empty($smtpPassword)  and !empty($From) 
		and !empty($From)  and !empty($FromName))
{
//here
	include_once('class.phpmailer.php');	
	$mail = new PHPMailer(); // defaults to using php "mail()"
	$mail->IsSMTP(); // telling the class to use SMTP
	$mail->Host = $host; // SMTP server
	$mail->IsHTML(true);
	$mail->SMTPAuth = true;     // turn on SMTP authentication
	$mail->Username = $smtpUsername;  // SMTP username
	$mail->Password = $smtpPassword; // SMTP password
	$mail->From       = $From;
	$mail->FromName   = $FromName;
	$mail->AddAddress($rowUser["email"]);
	$mail->Subject    = $subject;
	$body =  str_replace('[\]','	',$message);
	$mail->Subject    = $subject;
	$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
	$mail->MsgHTML($body);
	$mail->CharSet = 'UTF-8';
	if($mail->Send()){
		$varEmail="1";
	}

}else{
	
	 if (@mail($rowUser["email"], $subject,$message, $headers)){
		$varEmail="1";
	 }  
}

if($varEmail) {
	header("location: forgetPassword.php?res=5");
	exit();
} else {
	  header("location: forgetPassword.php?res=2");
	  exit();
}			
			

ob_end_flush();
?>
