<?php
require_once "../system/constant.php";
$mysqli = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
 

//Load Composer's autoloader
require '../phpmailer/vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);                        

if(isset($_POST['faction'])){
	$email = $_POST['email'];
	$check = $mysqli->query("select * from users where username = '$email'");
	$rowCount = $check->num_rows;
	if($rowCount > 0){
		$row = $check->fetch_assoc();
		$name = $row['name'];
		$id = $row['id'];
			$d=date ("d");
			$m=date ("m");
			$y=date ("Y");
			$t=time();
			$dmt=$d+$m+$y+$t;    
			$ran= rand(0,10000000);
			$dmtran= $dmt+$ran;
			$un=  uniqid();
			$dmtun = $dmt.$un;
			$mdun = md5($dmtran.$un);
			$sort=substr($mdun, 16); // if you want sort length code.
			$select = $mysqli->query("select * from resetpwd_token where email='$email'");
			$rowCount = $select->num_rows;
				if($rowCount > 0){
					$update = $mysqli->query("update resetpwd_token set token = '$mdun', time = '$t' where email = '$email'");
				}else{
					$insert = $mysqli->query("insert into resetpwd_token (email,uid,token,time)values('$email','$id','$mdun','$t')");
				}
			$resetPassLink = dirname(siteUrl)."/resetpass/resetpass.php?&token=".$mdun."";
			try {
			$mail->SMTPDebug = 0;  
			$mail->isSMTP();     
			$mail->Host = HOST; 
			$mail->SMTPAuth = true;    
			$mail->Username = UNAME;  
			$mail->Password = PASS;                        
			$mail->SMTPSecure = ENCRYPTION;        
			$mail->Port = PORT;        
			$mail->SMTPOptions = array(
			'ssl' => array(
				'verify_peer' => false,
				'verify_peer_name' => false,
				'allow_self_signed' => true
			)
			);
			//Recipients
			$mail->setFrom(REPLYEMAIL, TITLE);
			$mail->addAddress($email, $name);   
			$mail->addReplyTo(REPLYEMAIL, TITLE);
			//Content
			$mail->isHTML(true);                             
			$mail->Subject = 'Forget password';
			$mail->Body    = '<table class="email-body_inner" align="center" width="570" cellpadding="0" cellspacing="0" style="box-sizing: border-box; font-family: Arial,  Helvetica, sans-serif; margin: 0 auto; padding: 0; width: 570px;" bgcolor="#FFFFFF">
			<tr>
			<td class="content-cell" style="box-sizing: border-box; font-family: Arial, Helvetica, sans-serif; padding: 35px; word-break: break-word;">
			<h1 style="box-sizing: border-box; color: #2F3133; font-family: Arial, Helvetica, sans-serif; font-size: 19px; font-weight: bold; margin-top: 0;" align="left">
			Hi '.$name.',</h1>
			<p style="box-sizing: border-box; color: #74787E; font-family: Arial, Helvetica, sans-serif; font-size: 16px; line-height: 1.5em; margin-top: 0;" align="left">You recently requested to reset your password for your account. Use the button below to reset it. <strong style="box-sizing: border-box; font-family: Arial, Helvetica, sans-serif;">This password reset is only valid for the next 1 hour.</strong></p>

			<table class="body-action" align="center" width="100%" cellpadding="0" cellspacing="0" style="box-sizing: border-box; font-family: Arial, Helvetica, sans-serif; margin: 30px auto; padding: 0; text-align: center; width: 100%;">
			<tr>
			<td align="center" style="box-sizing: border-box; font-family: Arial, Helvetica, sans-serif; word-break: break-word;">

			<table width="100%" border="0" cellspacing="0" cellpadding="0" style="box-sizing: border-box; font-family: Arial, Helvetica, sans-serif;">
			<tr>
			<td align="center" style="box-sizing: border-box; font-family: Arial, Helvetica, sans-serif; word-break: break-word;">
			<table border="0" cellspacing="0" cellpadding="0" style="box-sizing: border-box; font-family: Arial, Helvetica, sans-serif;">
			<tr>
			<td style="box-sizing: border-box; font-family: Arial, Helvetica, sans-serif; word-break: break-word;">
			<a href="'.$resetPassLink.'" class="button button-green" target="_blank" style="-webkit-text-size-adjust: none; background: #22BC66; border-color: #22bc66; border-radius: 3px; border-style: solid; border-width: 10px 18px; box-shadow: 0 2px 3px rgba(0, 0, 0, 0.16); box-sizing: border-box; color: #FFF; display: inline-block; font-family: Arial, Helvetica, sans-serif; text-decoration: none;">Reset your password</a>
			</td>
			</tr>
			</table>
			</td>
			</tr>
			</table>
			</td>
			</tr>
			</table>
			<p style="box-sizing: border-box; color: #74787E; font-family: Arial, Helvetica, sans-serif; font-size: 16px; line-height: 1.5em; margin-top: 0;" align="left">Thanks,
			<br />'.TITLE.'</p>
			</td>
			</tr>
			</table>';
			$mail->send();
			echo '<h5 style="color:green;border: 1px solid;padding: 5px;font-size: 16px;text-align: justify;">Instructions for setting a new password have been sent to the email address associated with your account.</h5>';
			} catch (Exception $e) {
			echo '<h5 style="color:red;border: 1px solid;padding: 5px;font-size: 16px;text-align: justify;">'.$mail->ErrorInfo.'</h5>';
			}
	}else{
		echo '<h5 style="color:red;border: 1px solid;padding: 5px;font-size: 16px;text-align: justify;">No user with that email or username exists</h5>';
	}
	
}

?>