<?php 
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/vendor/autoload.php';
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;

$str = trim($_GET['id']);
if (empty($_GET['id']) || !isset($_GET['id']))
{
	die ("Required parameter is missing");
}

$str = base64_decode($_GET['id']);
$strArr 	= explode(";", $str);
$campId 	= intval($strArr[0]);
$yourName	= $strArr[1];
$yourEmail	= $strArr[2];

if (!isset($_POST['formtype']))
{
	$_POST['yourName'] = $yourName;
	$_POST['yourEmail'] = $yourEmail;
}

if (isset($_POST['formtype']) && $_POST['formtype'] == 'forward')
{
	$friendEmail 		= trim($_POST['friendEmail']);
	
	$yourName 			= trim($_POST['yourName']);
	$yourEmail 			= trim($_POST['yourEmail']);
	$comments 			= trim(strip_tags($_POST['comments'])); // remove all HTML tags to make the string safe to use.
	
	$error = "";
	
	if (empty($friendEmail))
	{
		$error = "Friend's E-mail address is required.<br /> ";
	}
	else if(!validate_email($friendEmail))
	{
		$error .= "Your friend's e-mail address is invalid.<br /> ";
	}

	if (empty($yourName))
	{
		$error .= "Your name is required.<br /> ";
	}
	if (empty($yourEmail))
	{
		$error .= "Your e-mail address is required.<br /> ";
	}
	else if(!validate_email($yourEmail))
	{
		$error .= "Your e-mail address is invalid.<br /> ";
	}
	if (strlen($error))
	{
		$errorMsg = "<strong>Please correct the following error(s):</strong> <br />
		$error";
	}
	else
	{
		$query = "SELECT CONTENTS, CONTENTSPLAIN FROM ##campaigns WHERE ID = '$campId'";
		$resultSet = array();
		$resultSet = $funcObj->CustomQuery($query);
		
		$query = "SELECT * FROM ##contacts WHERE ADDR_ID = '{$campId}' ORDER BY EMAIL LIMIT 1";
		$contactResult = $funcObj->CustomQuery($query);
		

		/* Update the newsletter content 
		by replacing the MARKERS with the 
		actual values
		*/
		$contents = $resultSet[0]['CONTENTS'];
		$code = sha1($campId);
		$mailsSent = 0;
		$mailsFailed = 0;
		$message = "";
		
		
		$mail = new PHPMailer(true);
		//include_once('classes/class.phpmailer.php');


		// $mail       	= new PHPMailer();
		$resultSetSMTP = array();
		$conntion = $sqlObj->mysqlClass();
		$query = "SELECT *	FROM ##smtp WHERE `DEFAULT` = '1'";
		$resultSetSMTP = array();
		$resultSetSMTP = $funcObj->CustomQuery($query);
		
		if (count($resultSetSMTP))
		{
			$defHOST 		= $resultSetSMTP[0]['HOST'];
			$defPORT 		= $resultSetSMTP[0]['PORT'];
			$defUSER 		= $resultSetSMTP[0]['USER'];
			$defPassword	= base64_decode(base64_decode($resultSetSMTP[0]['PASSWORD']));
			$defAuth		= ($resultSetSMTP[0]['AUTHENTICATION'] == '1') ? 'true' : 'false';
			$mail->IsSMTP();				
			$mail->SMTPAuth   = $defAuth;   
			$mail->Host       = $defHOST;   
			$mail->Port       = $defPORT;   
			$mail->Username   = $defUSER;  
			$mail->Password   = $defPassword;
			$mail->SetFrom($defUSER);
			$mail->SMTPSecure = 'tls'; 
			$mail->isHTML(true); 
		}else{
			 require_once ('configuration.mailer.php');
		}
		//
			
			
		if (!empty($comments))
		{
			$comments = nl2br($comments);
			$comments = $comments . '<br /><br /><br />';
			$contents = $comments . $contents;
		}
		
		$hash = sha1($friendEmail);
		
		$liveContent = stripslashes($contents);
		$replaceContent = array(
			'$UNSUB$' 		=> $trackURL . "?email={$friendEmail}&camid={$campId}&hash={$hash}&action=unsubscribe&friend=1&redirect=",
			'$CANTREAD$' 	=> $trackURL . "?email={$friendEmail}&camid={$campId}&code={$code}&action=cantread&friend=1&redirect=",
			'$LINKTRACK$' 	=> $trackURL . "?email={$friendEmail}&camid={$campId}&action=track&friend=1&redirect=",	 
			'@EMAIL@' 		=> $friendEmail, 
			'@FIRSTNAME@' 	=> 'Friend',	 
			'@LASTNAME@'	=> '',	 
			'@MISC_1@' 		=> '', 
			'@MISC_2@' 		=> '', 
			'@MISC_3@' 		=> '',
			'</html>'		=> "</html><img src='" . $trackURL . "?email={$friendEmail}&camid={$campId}&action=view&friend=1&redirect=' border='0' />" 
		);
		
		$contentsPlain = $resultSet[0]['CONTENTSPLAIN'];

		$replaceContent2 = array(
		'$UNSUB$' 		=> $trackURL . "?email={$friendEmail}&camid={$campId}&hash={$hash}&action=unsubscribe&friend=1&redirect=",
		'$CANTREAD$' 	=> $trackURL . "?email={$friendEmail}&camid={$campId}&code={$code}&action=cantread&friend=1&redirect=",
		'$LINKTRACK$' 	=> $trackURL . "?email={$friendEmail}&camid={$campId}&action=track&friend=1&redirect=",	 
		'@EMAIL@' 		=> $friendEmail, 
		'@FIRSTNAME@' 	=> 'Friend',	 
		'@LASTNAME@'	=> '',	 
		'@MISC_1@' 		=> '', 
		'@MISC_2@' 		=> '', 
		'@MISC_3@' 		=> ''
		);
		
		$liveContentPlain = trim($funcObj->replaceAll($replaceContent2, $contentsPlain));
		
		if (empty($liveContentPlain))
		{
			$liveContentPlain = 'To view the message, please use an HTML compatible email viewer!';
		}	
		
		$body = $funcObj->replaceAll($replaceContent, $liveContent);
		// $mail->IsSendmail();
		$mail->Subject    = "$yourName is forwarding an email to you";
		
		$mail->MsgHTML($body);
		$mail->AltBody    = $liveContentPlain; // optional, comment out and test
		
		$mail->AddAddress($friendEmail);
	
		//$mail->SMTPDebug = SMTP::DEBUG_SERVER; 
	
		if($mail->Send()) 
		{
		
			$message .= "<strong style='color: green'>Email sent successfully.</strong><br />";
			
			/*
			If the newsletter is successfully forwarded to a friend
			Then update the increment in database to track
			how many emails have been forwarded.
			*/
			$query = "UPDATE ##campaigns SET TIMES_FORWARDED = (TIMES_FORWARDED + 1) WHERE ID = '{$campId}'";
			$funcObj->updateQuery($query);
			
		} 
		else 
		{
			
			$message .= "<strong style='color: red'>Email failed. Please try again later.</strong><br />";
		}
	}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Forward to a Friend</title>
<script type="text/javascript" src="includes/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="includes/js/common.jquery.js"></script>
<script type="text/javascript" src="includes/js/index.jquery.js"></script>
<link rel="stylesheet" type="text/css" href="includes/css/segnantmailer.css" />
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="79" class="headerbg">
    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="headeritems">
  <tr>
    <td width="32%" rowspan="3" style="padding-left:10px;"><img src="images/logo.png" alt="" border="0" /></td>
    <td width="68%" align="right" style="padding-right:10px;" class="white">&nbsp;</td>
  </tr>
  <tr>
    <td height="42">&nbsp;</td>
  </tr>
  <tr>
    <td align="right" style="padding-right:10px;" class="size14 bold white">&nbsp;</td>
  </tr>
</table>
    </td>
  </tr>

  <tr>
    <td class="canvas" height="300" valign="top"><table width="100%" border="0" cellspacing="10" cellpadding="4">
      <tr>
        <td colspan="2" height="20">&nbsp;</td>
      </tr>
      <tr>
        <td colspan="2" align="center">
        <?php
		 if(!empty($errorMsg)){
			   echo "<center>";
			   errorMessage($errorMsg);
			   echo "</center>";
		 } 
   if (!empty($message))
   		echo "$message";
   ?>
    <div id="formTableDiv">
    <form name="forward" action="forward.php?id=<?php echo $_GET['id']?>" method="post" style="margin: 0">
    <table width="400" border="0" cellspacing="0" cellpadding="6" align="center" class="formtable">
  <tr>
    <td height="40" class="size16 bold" width="400">Forward Our Email To A Friend</td>
    </tr>
  <tr>
    <td width="50%" valign="bottom" height="29"><strong>Your Friends Email:</strong> <small>(Mandatory)</small></td>
  </tr>
  <tr>
    <td valign="top"><input type="text" name="friendEmail" maxlength="65" size="65" value="<?=isset($_POST['friendEmail']) ? $_POST['friendEmail'] : ''?>" class="textbox" /></td>
  </tr>
  <tr>
    <td height="29"><strong>Your Name:</strong> <small>(Mandatory)</small></td>
  </tr>
  <tr>
    <td><input type="text" name="yourName" maxlength="250" size="65" value="<?=stripslashes($_POST['yourName'])?>" class="textbox" /></td>
  </tr>
  <tr>
    <td height="29"><strong>Your Email:</strong> <small>(Mandatory)</small></td>
  </tr>
  <tr>
    <td><input type="text" name="yourEmail" maxlength="250" size="65" value="<?=$_POST['yourEmail']?>" class="textbox" /></td>
  </tr>
  <tr>
    <td height="29"><strong>A brief, personal note to this friend:</strong> <small>(Optional)</small></td>
  </tr>
  <tr>
    <td><textarea name="comments" class="textbox" style="height: 150px; width: 344px;" ><?=stripslashes(isset($_POST['comments'])? $_POST['comments'] : '')?></textarea></td>
  </tr>
  <tr>
    <td><input type="submit" name="submit" value="Send Email To Friend" class="button size2" /></td>
  </tr>
  <tr>
    <td height="2"></td>
  </tr>
</table>
<input type="hidden" name="formtype" value="forward" />
</form>
</div>
        </td>
      </tr>
      <tr>
        <td width="50%" valign="top">&nbsp;</td>
        <td height="100" width="50%" valign="top">&nbsp;</td>
      </tr>
    </table></td>
  </tr>
 <? require_once 'includes/inc.footer.php' ?>
</table>
</body>
</html>
