<?php
require('includes/global.php');
include_once('includes/ssp.class.php');
Header("Cache-Control: no-cache");

$num_errors = 0;
$dateTimeNow = dateTimeNow();
$username = fetch_postget_val("username", $_POST, $_GET);
$emel = fetch_postget_val("email", $_POST, $_GET);

$indicator_Query = "SELECT A.id_pengguna, A.nama
				  FROM t_pengguna A
				  WHERE A.login = ? AND A.emel = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $username, PDO::PARAM_STR);
$PDO_stmt->bindValue(2, $emel, PDO::PARAM_STR);
$PDO_stmt->execute();
$indicator_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

$result = [];
if($indicator_List['id_pengguna'] > 0)//valid
{
	try
	{
		$PASS_COUNTER = 0; //COUNTER FOR DOUBLE CONFIRM
		$PASS_VALUE = 1; //NUMBER OF QUERY TO EXECUTE. IMPORTANT FOR CHECKING!!

		//PDO INIT
		$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		$PDO->beginTransaction();

		$new_pwd = generatePassword();
		$indicator_Array = array();
		$indicator_Array[] = array('katalaluan', sha1($new_pwd), PDO::PARAM_STR,1);
		$indicator_Array[] = array('temp_pwd', 1, PDO::PARAM_INT);
		$indicator_Array[] = array('aktif', 1, PDO::PARAM_INT);
		$indicator_Array[] = array('status_blok', 0, PDO::PARAM_INT);
		$indicator_Array[] = array('bil_percubaan', 0, PDO::PARAM_INT);
		$indicator_Array[] = array('id_kemaskini', 0, PDO::PARAM_INT);
		$indicator_Array[] = array('tkh_kemaskini', $dateTimeNow, PDO::PARAM_STR);
		
		//*****************************************
		//UPDATE t_masterpengguna
		//*****************************************
		$whereCondition = "WHERE id_pengguna = ".$indicator_List['id_pengguna'];
		$PASS_COUNTER += PDO_UPDATE($PDO, 't_pengguna', $indicator_Array, $whereCondition);
		
		//*****************************************
		//COMMIT if PASS_COUNTER == PASS_VALUE
		//***************************************** 
		if ($PASS_COUNTER == $PASS_VALUE)
		{
			$PDO->commit();
			$result['status'] = 1;
			$result['message'] = 'Kata laluan anda telah dihantar kepada emel anda. Sila log masuk dengan kata laluan baru anda.';
			$result['redirect'] = 'login.php';
			//echo $PASS_COUNTER;
					
			//EMAIL TO user FUNCTION START 
			
			require_once('classes/php-mailer/PHPMailerAutoload.php');
			$subject = $TITLEFORGET;
			
			$message = 'Salam Sejahtera '.$indicator_List['nama'];
			$message .= '<br><br>';
			$message .= 'Tetapan semula kata laluan iSejahtera</b>.';
			$message .= '<br><br>';
			$message .= str_replace('<KATA LALUAN>', $new_pwd, $CONTENTFORGET);
			$message .= '<br><br>';
			$message .= $TQ;
			$message .= '<br>';
			$message .= $SUK1;
			$message .= '<br>';
			$message .= $SUK2;
			
			$mail = new PHPMailer(true);
			try {

				$mail->IsSMTP();                    						// Set mailer to use SMTP
				$mail->Host = $SMTP_HOST;									// Specify main and backup server
				$mail->SMTPAuth = $SMTP_AUTH;								// Enable SMTP authentication
				$mail->Username = $SMTP_USER;								// SMTP username
				$mail->Password = $SMTP_PASS;								// SMTP password
				//$mail->SMTPSecure = 'tls';								// Enable encryption, 'ssl' also accepted
				$mail->Port = $SMTP_PORT;									// TCP port to connect to
				$mail->addAddress($emel);									// Add recipient
				$mail->SetFrom($SMTP_FROM, $MAILFROM);

				$mail->IsHTML(true);										// Set email format to HTML
				$mail->CharSet = 'UTF-8';
				$mail->Subject = $subject;
				$mail->Body    = $message;
				$mail->Send();
				$arrResult = array ('response'=>'success');

			} 
			catch (phpmailerException $e) {
				$arrResult = array ('response'=>'error','errorMessage'=>$e->errorMessage());
			} 
			catch (Exception $e) {
				$arrResult = array ('response'=>'error','errorMessage'=>$e->getMessage());
			}
			
			//EMAIL user FUNCTION END	
		}
		else
		{
			$PDO->rollBack();
			echo -1;
		}
	}
	catch (Exception $e) 
	{
		$PDO->rollBack();
		echo "Failed: " . $e->getMessage();
	}
}
else
{
	$result['status'] = -1;
	$result['errorMsg'] = 'Maklumat anda tidak dijumpai di dalam sistem. Sila masukkan maklumat yang tepat.';
}

echo json_encode($result);
?>