<?php

session_start();

require 'configuration.php';

require_once 'includes/common.functions.php';

require_once 'includes/url_encode_decode.php';

require_once ('classes/database.php');

require_once ('classes/functions.php');



$sqlObj = new mysqlClass;

$funcObj = new funcClass;



$oldpass		= $_GET['oldpass'];

$newpass		= $_GET['newpass'];

$confirmpass	= $_GET['confirmpass'];

$passwderror	= '';

$activeUser		= intval($_SESSION['SUPER_USER_ID']);



if (empty($_SESSION['SUPER_USER_ID']))

{

	echo 'Direct access to this file is not allowed';

	die();

}



if ($oldpass == '')

{

	$passwderror .= 'Old password is required.<br />';	

}

if (strlen($newpass) < 6)

{

	$passwderror .= 'New password must be atleast 6 characters long.<br />';	

}

else if ($newpass != $confirmpass)

{

	$passwderror .= 'Password mis-match.<br />';	

}



if (!empty($passwderror))

{

	echo $passwderror;

	die();

}

else

{

	/*

	Before updating the password make sure that the old password

	is correct.

	*/

	$resultSet = $funcObj->changepassword($activeUser, $oldpass);

	if ($resultSet == 0)

	{

		echo $passwderror = 'Incorrect old password, please try again.';

		exit;	

	}

	else //if the existing password is correct then change the password

	{

		$query = "UPDATE ##super_user SET PASSWORD=SHA1('$newpass') WHERE USER_ID='$activeUser'"; 

		

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

		if ($updateId == 1)

		{

			echo 1;

		}

		else

		{

			echo $passwderror = 'Password has not been updated. Please try with different password.';

		}

		exit;

	}

}



$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>");

}

