<?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;



@sleep($sleepTime);



$email = $_GET['email'];

if (empty($email))

{

	die("<strong style='color: red'>Email address is required.</strong>");

}

else if (!validate_email ($email))

{

	die("<strong style='color: red'>Invalid e-mail address.");

}



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

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



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('[DATETIME]',				$emailVar['DATETIME'], 			$body);

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

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

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



	return $body;

}



// If there is a record then send an email

// otherwise show a message

if (count($ResultSet))

{

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



	$mail       = new PHPMailer();

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

	

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

	global $absPath;

	$emailVar 	= array();

	

	$emailVar['ABSPATH'] 			= $absPath;

	$emailVar['EMAIL'] 				= $email;

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

	$emailVar['DATETIME']			= date("F j, Y");

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

	$emailVar['RESETPASSWORDCODE']	= sha1($email);

	

	require_once ('configuration.mailer.php');

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

	$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">Email sent successfully</strong>

		';

	 

	} else {

		echo "

		<strong style='color: red'>Email failed. Problem in mail server.

		";

	}

}

else

{

	die("<strong style='color: red'>No such user available or is inactive.</strong>");

}

