<?php if (!isset($_SESSION['FirstName'])) {
	session_start();
}
//echo "Brand: ".$_SESSION['admin_brand'];
	if($_GET['skey']=="p987gjkjtyedghnbdd3421scdzx"){
		$_SESSION['iframed']=1;
	}
    include ('includes/db1config.php'); //db connection config file
    include ('../includes/mysql_connect.php'); //connect using selected db config file
	include_once 'includes/admin-config.php';	//get site options
	require_once 'includes/pwd_reset_functions.php';
	
	$action = 'reset_form';

	// Send email
	if (isset($_POST['action']) && $_POST['action'] == 'send_reset_link') {
		$email = isset($_POST['email']) ? $_POST['email'] : '';

		$admin = findAdminByEmail($email);

		if (empty($admin)) {
			$message = "No user found with the submitted email.";
			$_SESSION['reset_pwd_message'] = $message;
			header("Location: reset_pwd.php");
			exit();
		}

		$token = createAdminResetToken($admin);

		if (sendAdminResetEmail($admin, $token)) {
			$message = "A password reset link has been sent to your email.";
		} else {
			$message = 'Error sending email.';
		}

		$action = 'email_sent';
	}

	// Password reset link
	if (isset($_GET['token']) && !empty($_GET['token'])) {
		// Show the form
		$action = 'reset_password';
		$token = trim($_GET['token']);
		$passwordReset = findAdminPasswordResetToken($token);

		if (empty($passwordReset)) {
			$_SESSION['reset_pwd_message'] = "Invalid password reset link";
			header("Location: reset_pwd.php");
			exit();
		}

		if (isset($_POST['action']) && $_POST['action'] == 'reset_password') {
			// Reset the password
			$password = isset($_POST['password']) ? trim($_POST['password']) : '';
			$passwordConfirmation = isset($_POST['password_confirmation']) ? trim($_POST['password_confirmation']) : '';

			if (empty($password) || strlen($password) < 6) {
				// Invalid password
				$_SESSION['reset_pwd_message'] = 'The password you entered is invalid.';
				header("Location: reset_pwd.php?token=".$_GET['token']);
				exit();
			}

			if ($password != $passwordConfirmation) {
				$_SESSION['reset_pwd_message'] = 'The passwords do not match.';
				header("Location: reset_pwd.php?token=".$_GET['token']);
				exit();
			}

			$sql = "UPDATE admin 
					SET password = PASSWORD('".$password."')
					WHERE recordKey = '".mysql_real_escape_string($passwordReset['admin_id'])."'";
			mysql_query($sql) or die(mysql_error());

			deleteAdminResetTokensForAdmin($passwordReset['admin_id']);

			$action = 'password_updated';
		}
	}

	// Reset password

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<?php 
include('title.php'); 
echo'<link href="css/'.$stylesheet.'" rel="stylesheet" type="text/css" media="screen" />';	
echo'<link href="css/print_style.css" rel="stylesheet" type="text/css" media="print" />';
?>
<script type="text/javascript">
function setFocus()
{
document.getElementById("username").focus();
}
</script>

<!-- for popup windows -->
<link href="css/popup-style.css" rel="stylesheet" type="text/css" media="all" />
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"> </script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<script type="text/javascript" src="js/script.js"></script>
 <!-- end popup script-->

<script type="text/javascript" src="js/jquery.nestable.js"></script>
<link rel="stylesheet" type="text/css" href="css/nestable.css" />

</head>

<body>


<div id="wrapper">

<!-- start header -->
<?php include('includes/header.inc.php'); ?>
<!-- end header-->
<?php
if($full_width=="1"){
	$bg="-nobg";
	$col="-one-column";
}
?>

<!-- start page -->
<div id="page-container<?php echo $bg;?>">
<div id="page-bg-top">


<!-- start content -->
<div id="content<?php echo $col;?>">
<?php
if($full_width!="1"){
?>
<div id="main-content">
<?php
}
?>   
		<div class="post">
		  <div class="entry">
          
<br />

<p><strong>Site Admin Page:</strong>

 &bull; Home</p><br />

<h1>Site Admin: <?php include('seotitle.php'); ?></h1>

<?php if (isset($_SESSION['reset_pwd_message'])) : ?>
	<p><?php echo $_SESSION['reset_pwd_message']; unset($_SESSION['reset_pwd_message']); ?></p>
<?php endif; ?>

<?php if ($action == 'email_sent') : ?>
	<p><?php echo $message; ?></p>
<?php elseif($action == 'reset_password') : ?>
	<form method="post" action="/cmsadmin/reset_pwd.php?token=<?php echo $token; ?>">
		<input type="hidden" name="action" value="reset_password">
		<div>
			<label>New Password:</label>
			<input type="password" name="password">
		</div>
		<div>
			<label>Confirm Password:</label>
			<input type="password" name="password_confirmation">
		</div>

		<button type="submit">Submit</button>
	</form>
<?php elseif ($action == 'password_updated'): ?>
	<p>Your password has been updated. You can now <a href="/cmsadmin/">login here</a>.</p>
<?php else: ?>
	<form method="post">
		<input type="hidden" name="action" value="send_reset_link">
		<label>Email: </label>
		<input type="text" name="email">
		<button type="submit">Send Password Reset Link</button>
	</form>
<?php endif; ?>

<br />

</div><!-- end entry-->

<br />

</div><!-- end post-->
<?php
if($full_width!="1"){
?>
</div><!-- end main-content-->
<?php
}
?> 
</div><!-- end content -->
    
<!-- start sidebar -->
<?php 
if($full_width!="1"){
	include('includes/admin-sidebar.inc.php');
}
?>
<!--sidebar-->


<div style="clear: both;">&nbsp;</div>

<div id="page-bg-btm">
</div><!-- end page-bg-btm div -->
</div><!-- end page-bg-top div -->
</div><!-- end page-container -->



</div><!-- end wrapper div-->

<!-- end page -->


<!--footer-->



</body>
</html>
