<?php 
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
session_start();
require 'authenticate.user.php';
sleep(2);
require 'configuration.php';
require_once 'includes/common.functions.php';
require_once ('classes/database.php');
require_once ('classes/functions.php');
$sqlObj = new mysqlClass;
$funcObj = new funcClass;
$table = "##addressbook";
$curFile = basename(__FILE__);

if ($_SESSION['IS_USER_CLIENT'])
{
	die ("Sorry, you are not authorized to access this page");
}

$id = intval($_GET['id']);
$type = $_GET['type'];

if (empty($id))
	die("Required parameter is missing");

$campaignID = $_SESSION['CAMPAIGNID'];
if (empty($campaignID))
	die ("Required data missing. Please try again from step 1");

$query = "SELECT CAMPAIGN_NAME, FROM_NAME, FROM_EMAIL, SUBJECT  FROM ##campaigns WHERE ID = '{$id}'";
$result = $funcObj->CustomQuery($query);
$campaignName = $result[0]['CAMPAIGN_NAME'];

$query = "SELECT * FROM ##contacts WHERE ADDR_ID = '{$campaignID}' ORDER BY EMAIL LIMIT 1";
$contactResult = $funcObj->CustomQuery($query);

switch ($type)
{
	case 'html':
		$query = "SELECT CONTENTS FROM ##campaigns WHERE ID = '$id'";
		$resultSet = array();
		$resultSet = $funcObj->CustomQuery($query);
		$liveContent = $contents = $resultSet[0]['CONTENTS'];
		break;

	case 'text':
		$query = "SELECT CONTENTSPLAIN FROM ##campaigns WHERE ID = '$id'";
		$resultSet = array();
		$resultSet = $funcObj->CustomQuery($query);
		$liveContent = $contents = $resultSet[0]['CONTENTSPLAIN'];
		break;
		
	default:
		$query = "SELECT CONTENTS FROM ##campaigns WHERE ID = '$id'";
		$resultSet = array();
		$resultSet = $funcObj->CustomQuery($query);
		$liveContent = $contents = $resultSet[0]['CONTENTS'];
		break;
	
}

$forwardStr 	= '';
$forwardStr 	= $id . ';' . $contactResult[0]['FIRSTNAME'] . ' ' . $contactResult[0]['LASTNAME'] . ';' . $contactResult[0]['EMAIL'];
$forwardStr		= base64_encode($forwardStr);
/*
Format for forward string = id;name;email
*/

$replaceContent = array(
'$UNSUB$' 		=> $trackURL . "?email={$contactResult[0]['EMAIL']}&camid={$id}&action=unsubscribe&redirect=",
'$CANTREAD$' 	=> $trackURL . "?email={$contactResult[0]['EMAIL']}&camid={$id}&action=cantread&redirect=",
'$LINKTRACK$' 	=> $trackURL . "?email={$contactResult[0]['EMAIL']}&camid={$id}&action=track&redirect=",
'$FORWARD$' 	=> $absPath . "/forward.php?id={$forwardStr}",	 
'@EMAIL@' 		=> $contactResult[0]['EMAIL'], 
'@FIRSTNAME@' 	=> $contactResult[0]['FIRSTNAME'],	 
'@LASTNAME@'	=> $contactResult[0]['LASTNAME'],	 
'@MISC_1@' 		=> $contactResult[0]['MISC_1'], 
'@MISC_2@' 		=> $contactResult[0]['MISC_2'], 
'@MISC_3@' 		=> $contactResult[0]['MISC_3']
);

$liveContent = $funcObj->replaceAll($replaceContent, $liveContent);

switch ($type)
{
	case 'html':
		echo $liveContent; 	
		break;

	case 'code':
		echo "<textarea style='width: 900px; height:390px; border: 0px solid #ccc;'>$liveContent</textarea>";
		break;
		
	case 'text':
		echo "<textarea style='width: 900px; height:390px; border: 0px solid #ccc;'>$liveContent</textarea>";
		break;

	default:
		echo $liveContent; 	
		break;
}
?>