<?php

function findAdminByEmail($email) {
	$sql = "SELECT *
			FROM admin
			WHERE email = '".mysql_real_escape_string($email)."'";
	
	$result = mysql_query($sql) or die(mysql_error());

	if (mysql_num_rows($result) < 1) {
		return null;
	}

	return mysql_fetch_assoc($result);
}

function createAdminResetToken($admin) {
	$token = sha1($admin['email'] . $admin['recordKey'] . time() + rand());

	deleteAdminResetTokensForAdmin($admin['recordKey']);

	$sql = "INSERT INTO admin_password_resets (
				admin_id,
				token
			) VALUES (
				'".mysql_real_escape_string($admin['recordKey'])."',
				'".mysql_real_escape_string($token)."'
			)";
	mysql_query($sql) or die(mysql_error());

	return $token;
}

function deleteAdminResetTokensForAdmin($adminId) {
	$sql = "DELETE FROM admin_password_resets
			WHERE admin_id = '".mysql_real_escape_string($adminId)."'";
	mysql_query($sql) or die(mysql_error());
}

function findAdminPasswordResetToken($token) {
	$sql = "SELECT *
			FROM admin_password_resets 
			WHERE token = '".mysql_real_escape_string($token)."'";
	$result = mysql_query($sql);

	if (mysql_num_rows($result) < 1) {
		return null;
	}

	return mysql_fetch_assoc($result);
}

function sendAdminResetEmail($admin, $token) {
	set_time_limit(0);

	require_once('../includes/phpmailer/class.phpmailer.php');
	//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

	$to = $admin['email'];
 
 	$subject = "Password Reset";
	$message = "You have requested to reset your password. If you feel this is in error, please do nothing and your password will not be changed. <br><br> To proceed with changing your password, please click on the following link and you will be prompted to enter your new password.<br><br>
		<a href='http://" .$_SERVER['HTTP_HOST']. "/cmsadmin/reset_pwd.php?token=". $token . "'>Reset My Password</a>";

	$mail             = new PHPMailer();

	$body = "<div align=center>Please do not reply to this email. It was sent from an automated system and you will not receive a response.</div><br><br>" . $message;

	//$body             = file_get_contents('contents.html');
	//$body             = preg_replace('/[\]/i','',$body);

	$mail->IsSMTP(); // telling the class to use SMTP
	//$mail->Host       = "helpme.interspire.smtp.com"; // SMTP server
	$mail->Host       = "port80.smtpcorp.com"; // SMTP server
	$mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
	                                           // 1 = errors and messages
	                                           // 2 = messages only
	$mail->SMTPAuth   = true;                  // enable SMTP authentication
	//$mail->Host       = "helpme.interspire.smtp.com"; // sets the SMTP server
	$mail->Port       = 80;                    // set the SMTP port for the GMAIL server
	//$mail->Port       = 2525;                    // set the SMTP port for the GMAIL server
	//$mail->Username   = "mailer@vpsends.com"; // SMTP account username
	//$mail->Password   = "mymail21";        // SMTP account password
	$mail->Username   = "pilotwebsolutions"; // SMTP account username
	$mail->Password   = "TasteSys12";        // SMTP account password

	$mail->SetFrom('ordersystem@restaurantonline.biz', 'RestaurantOnline! Password reset');

	//$mail->AddReplyTo("name@yourdomain.com","First Last");

	$mail->Subject    = $subject;

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

	$mail->MsgHTML($body);

	//$address = $notifyemail;
	$mail->AddAddress($to);
	//$mail->AddAddress($address, "John Doe");

	//$mail->AddAttachment("images/phpmailer.gif");      // attachment
	//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

	if(!$mail->Send()) {
		return false;
	  echo "Mailer Error: " . $mail->ErrorInfo;
	} else {
		return true;
	}
}