<?php

session_start();

require 'configuration.php';

require_once 'includes/common.functions.php';

require_once ('classes/database.php');

require_once ('classes/functions.php');

$sqlObj = new mysqlClass;

$funcObj = new funcClass;

$table = "##super_user";

sleep($sleepTime);



function replaceEmailVars($body, $emailVar)

{

	

	$today = date("F j, Y, g:i A");

	$body		= str_replace('[ABSPATH]',				$emailVar['ABSPATH'],			$body);

	$body		= str_replace('[EMAIL]',				$emailVar['EMAIL'],				$body);

	$body		= str_replace('[EMAILHEADER]',			$emailVar['EMAILHEADER'], 		$body);

	$body		= str_replace('[PASSWORD]',				$emailVar['PASSWORD'], 			$body);

	$body		= str_replace('[FULLNAME]',				$emailVar['FULLNAME'],			$body);

	$body		= str_replace('[COMPANYNAME]',			$emailVar['COMPANYNAME'],		$body);



	return $body;

}





if ($_GET['formtype'] == 'resetpassword')

{

	$password1 		= trim($_GET['password1']);

	$password2 		= trim($_GET['password2']);

	$email 			= trim($_GET['email']);

	$code 			= trim($_GET['code']);

	

	$error = "";

	if (empty ($email))

	{

		$error = "Incorrect URL.";

	}

	else if (empty($password1))

	{

		$error = "Password cannot be left blank";

	}

	else if ($password1 != $password2)

	{

		$error 	= "Password Mismatch";

	}

	else if (sha1($email) != $code)

	{

		$error 	= "Security hack";

	}

	

	if (! empty ($error))

	{

		echo "<font color='red'><strong>{$error}</strong></font>";

		exit;

	}

	

	$query = "UPDATE $table SET PASSWORD = SHA1('$password1') WHERE EMAIL = '{$email}'";

	$updateId = $funcObj->updateQuery($query);

	

	$query = "SELECT FULLNAME FROM ##super_user WHERE EMAIL = '{$email}'";

	$ResultSet = $funcObj->CustomQuery($query);

	

	include_once('classes/class.phpmailer.php');



	$mail       = new PHPMailer();

	$body       = file_get_contents('templates/admin.resetpassword.tpl');

	

	/********* EMAIL SETUP ***************/

	global $absPath;

	$emailVar 	= array();

	

	$emailVar['ABSPATH'] 			= $absPath;

	$emailVar['EMAIL'] 				= $email;

	$emailVar['EMAILHEADER']		= 'images/email/header.jpg';

	$emailVar['FULLNAME']			= $ResultSet[0]->FULLNAME;

	$emailVar['PASSWORD']			= $password1;

	

	require_once ('configuration.mailer.php');

	$mail->Subject    = "Segnant Mailer - Password Reset";

	$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

	

	$mail->AddAddress($_GET['email']);

	$body = replaceEmailVars($body, $emailVar);

	$mail->MsgHTML($body);

	

	if($mail->Send()) 

	{

		echo '

		<strong style="color: green">Password successfully updated. Try <a href="index.php">login now</a></strong>

		';

	 

	} else {

		echo "

		<strong>Email failed but password successfully updated. Try <a href='index.php'>login now</a></strong>.

		";

	}

}

?>