<?php

if (!isset($_SESSION['FirstName'])) {
	session_start();
}
if ($_SESSION['FirstName'] == "") {
	echo 'You are not authorized to view this page. <br><br>You may <a href="index.php"><font color="blue">log in here.</font></a>';
	exit;
}

if (!isset($_POST['template']) || empty($_POST['template'])) {
	echo 'No template specified.';
	exit();
}

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

$template = trim($_POST['template']);
$template = getEmailTemplate($template);

if ($template) {
    $image_path = "http://".$_SERVER['HTTP_HOST']."/custom/images/email";
    $main_background_color = $template['main_background_color'];
    $background_color = $template['background_color'];
    $header_image_url = $template['header_image_url'];
    $footer_image_url = $template['footer_image_url'];
	$include_logo = $template['include_logo'];
	if($include_logo==1){
		$logo_url="<img src='http://".$_SERVER['HTTP_HOST']."/custom/images/header-logo.png'><br>";
	} else {
		$logo_url="";
	}
} else {
    $image_path = '';
    $background_color = '';
	$main_background_color = '';
    $header_image_url = '';
    $footer_image_url = '';
	$logo_url="";
}

if (isset($_POST['content'])) {
	$content = preg_replace("/\r\n|\r|\n/",'<br/>', $_POST['content']);
} else {
	$content = '<p>Donec rutrum congue leo eget malesuada. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec velit neque, auctor sit amet aliquam vel, ullamcorper sit amet ligula. Cras ultricies ligula sed magna dictum porta. Curabitur aliquet quam id dui posuere blandit. Donec sollicitudin molestie malesuada. Praesent sapien massa, convallis a pellentesque nec, egestas non nisi. Praesent sapien massa, convallis a pellentesque nec, egestas non nisi.</p><p>Proin eget tortor risus. Pellentesque in ipsum id orci porta dapibus. Proin eget tortor risus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec velit neque, auctor sit amet aliquam vel, ullamcorper sit amet ligula. Mauris blandit aliquet elit, eget tincidunt nibh pulvinar a. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus suscipit tortor eget felis porttitor volutpat.</p>';
}

$htmlmsg = "<center><table bgcolor='$main_background_color'><tr><td><center>$logo_url<table bgcolor='$background_color' width='800'>";
if(isset($header_image_url) && $header_image_url<>""){
	$htmlmsg .="<tr><td><center><image src='$image_path/$header_image_url'>";
}
$htmlmsg .= "<tr><td><table cellpadding='15' width='100%'><tr><td>$content</table>";

if(isset($footer_image_url) && $footer_image_url<>""){
	$htmlmsg .="<tr><td><center><image src='$image_path/$footer_image_url'>";
}

$htmlmsg .="</table></table>";

echo '<html>';
echo '<body>';
echo $htmlmsg;
echo '</body>';
echo '</html>';

function getEmailTemplate($templateName) {
	$sql = "SELECT * 
		FROM email_templates
		WHERE template_name = '".mysql_real_escape_string($templateName)."'
		LIMIT 1";

	$result = mysql_query($sql) or die(mysql_error());

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

	return mysql_fetch_assoc($result);
}

?>