<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 7/3/2012	ELL
// 
// 
//
//

include ("check_session.php");
include("../" . BWB_LIB_DIR . "/mime_mail.inc");

$UserID = $_POST['UserID'];
if ($_POST["Active"] == "true")
	$Active = 1;
else
	$Active = 0;
$user_table = trim($_POST["user_table"]);
	
$query = "UPDATE " . $user_table . " SET Active=" . $Active . " WHERE UserID='" . $UserID . "'";
$cnt = $bwb_dbh->exec($query);

echo "Update complete";
	
if ($Active == 1)
{
	//// Send data over to mailform to send message to user.
	$query = "SELECT * FROM `" . $user_table . "` WHERE `UserID`='" . $UserID . "'";
	$result = $bwb_dbh->query($query);
	$row = $result->fetch(PDO::FETCH_ASSOC);
	$email = $row['email'];
	$password = $row['password'];
	$first_name = $row['first_name'];
	$last_name = $row['last_name'];
	$html_message_page = $row['welcome_message_page'];
	
	/////////////////////////////////////////////
	//Decrypt password for comparison:
	$enc_text = stripslashes($password);
	$key = stripslashes($UserID);
	
	//Convert key to codes and pad as necessary.
	for( $i = 0; $i < (strlen($enc_text))/2; $i++)
	{
		if ($i < strlen($key))
		{
			$c = $key{$i};
			$k[$i] = ord($c);
		}
		else
			$k[$i] = 65 + $i;
	}
	
	//Convert encrypted string to array of HEX uni-character codes.
	$j = 0;
	$q = 0;
	for( $i = 0; $i < strlen($enc_text); $i++)
	{
		if ($j == 0) {
			$c = $enc_text{$i};
			$j++;
		}
		else {
			$c = $c.$enc_text{$i};
			$e[$q] = hexdec($c);
			$q++;
			$j = 0;
		}
		
	}
	
	//Decrypt encrypted string with key to get original data.
	$decrypt_text = "";
	for( $i = 0; $i < (strlen($enc_text))/2; $i++)
   	{
   		$a[$i] = $e[$i] ^ $k[$i];
		$decrypt_text = $decrypt_text. chr($a[$i]);
   	}
	////////////////////////////////////////////////////
	
	// Send HTML message to inquirer if message is setup.
	//Mail fields to recipient.
	$mail = new mime_mail;
	
	$subject = "Your account has been activated";
	$html_message_page = $bwb_dbh->prepare($html_message_page);
	$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "page_data WHERE page_name = '". $html_message_page . "'";
	$result = $bwb_dbh->query($query);
	if ($row = $result->fetch(PDO::FETCH_ASSOC))
	{
		$html_msg = $row["page_content"];
		$html_msg = str_replace("[FIRST_NAME]", $first_name, $html_msg);
		$html_msg = str_replace("[LAST_NAME]", $last_name, $html_msg);
		$html_msg = str_replace("[USER_ID]", $UserID, $html_msg);
		$html_msg = str_replace("[PASSWORD]", $decrypt_text, $html_msg);
		
		$mail->from = $from;
		$mail->to = $email;
		$mail->replyto = $reply_to;
		$mail->subject = $subject;
		$mail->body = $html_msg;
		$mail->html = 1;
		
		$mail->send();
		
		$email_ext = substr($email, strlen($email) - 8);
	}
}
?>
