<?php 
session_start();
require 'authenticate.user.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;
$table = "##addressbook";
$curFile = basename(__FILE__);
$ACTION				= 'add';

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

$id = intval($_GET['id']);
if (empty($id))
	die("Required parameter is missing");


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

$query = "SELECT DISTINCT(A.NAME) AS NAME, A.ID AS CAMPAIGNID 
FROM ##addressbook AS A,
##campaign_send_stats AS B
WHERE B.ADDR_ID = A.ID
AND B.SESSION_ID = '".session_id()."'
AND B.CAMPAIGN_ID = '{$id}'";
$AddrResultSet = array();
$AddrResultSet = $funcObj->CustomQueryArray($query);

$campIds = array();
foreach($AddrResultSet as $row)
{
	$campIds[] = $row['CAMPAIGNID'];
}
$campIdsStr = implode(",", $campIds);

$_SESSION['CAMPAIGNID'] = $AddrResultSet[0]['CAMPAIGNID'];
/* ACTIVATE THE TAB */
$activeTab = 'Campaigns';

?>
<!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>Segnant Mailer - Manage Campaigns</title>
<link rel="stylesheet" type="text/css" href="includes/css/segnantmailer.css" />
<link rel="stylesheet" type="text/css" href="includes/css/paging.css"  />
<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">
$(document).ready(function()
{
	$('#extLinks').click(function()
	{
		$('#extlinksdiv').slideToggle('slow');

	})
});

function noDefaultSMTP()
{
	alert("There is no default mailer.\nPlease visit Administration TAB and set one mailer as default.\nThe default mailer will be used to send the mails.");
	return false;
}

</script>
<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"><?php require_once 'includes/inc.header.php' ?>    
    </td>
  </tr>

  <tr>
    <td class="canvas" height="300" valign="top"><table width="100%" border="0" cellspacing="10" cellpadding="4">
      <tr>
        <td width="100%" height="50" valign="top" class="heading">Send Campaign</td>
        </tr>
      <tr>
        <td><table width="100%" border="0" cellspacing="1" cellpadding="1">
              <?php if (!empty($message))
					  echo "
                      <tr>
                        <td class='message'>$message</td>
                      </tr>
					  ";
					  ?>
              <tr>
                <td><form name="frmListing" id="frmListing" action="send3.php?id=<?=$id?>" method="post" style="margin:0; padding: 0">
                    <?php
					$searchTxt =isset($searchTxt) ? $searchTxt : '';
					$searchArray =isset($searchArray) ? $searchArray : '';
			  //$resultSet = $funcObj->FetchRecords("$table",$searchTxt,$searchArray,'NAME ASC', $pageObj->start, $pageObj->limit);
			   $resultSet = $funcObj->FetchRecords("$table",$searchTxt,$searchArray,'NAME ASC', '', '');
			  ?>
                    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="inactiveTable">
                      <tr class="inactiveTab">
                        <td width="30" align="center"><img src="images/10.png" alt="" hspace="8" border="0" /></td>
                        <td width="90%"><span><strong>Campaign:</strong> select data sources for "
                          <?=$campaignName?>
                          "</span></td>
                        <td width="10%" nowrap="nowrap" align="right" style="padding-right:10px"><a href="send.php?&id=<?=$id?>&action=edit">Edit Settings</a></td>
                      </tr>
                    </table>
                    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="activeTable" style="margin-top: 3px;">
                      <tr class="activeTab">
                        <td align="left"><img src="images/21.png" alt="" hspace="8" border="0" align="absmiddle" /> <span><strong>Campaign:</strong> summary for "
                          <?=$campaignName?>
                          "</span></td>
                      </tr>
                      <tr>
                        <td colspan="3" height="40" style="padding: 8px;">Please review your send options before continuing.</td>
                      </tr>
                      <tr>
                        <td colspan="3" height="60" align="center"><table width="99%" cellpadding="0" cellspacing="2" style="margin: 8px; background-color:#FAE7E7; border:1px solid #edcccc">
                            <tr>
                              <td width="2%"><img src="images/messagebox_warning-48.png" align="left" alt="" hspace="5" vspace="5" /></td>
                              <td align="center" style="text-align:left"><span class="size15 bold orange">Please note</span><br />
                                <span class="orange">You should test your email to a variety of email clients to ensure they look and function as expected.</span> </td>
                            </tr>
                          </table></td>
                      </tr>
                      <tr>
                        <td colspan="3" height="40" style="padding: 8px;"><table width="100%" border="0" cellspacing="3" cellpadding="2">
                            <tr>
                              <td width="33%" valign="top"><strong>Campaign Name</strong><br />
							<?=$campaignName?>	</td>
                              <td width="33%" valign="top"><strong>Subject</strong><br />
                              <?php
                                $query = "SELECT DISTINCT(EMAIL), FIRSTNAME, LASTNAME, MISC_1, MISC_2, MISC_3 FROM ##contacts WHERE ADDR_ID IN ($campIdsStr) AND IS_DELETED = 0 ORDER BY EMAIL ASC
limit 0, 1";
$contactsResultSet = array();
$contactsResultSet = $funcObj->CustomQueryArray($query);
foreach ($contactsResultSet as $rowContactsResultSet)
{
    $code = sha1($id); 
	$hash = sha1($rowContactsResultSet['EMAIL']);
	$forwardStr 	= '';
	$forwardStr 	= $id . ';' . $rowContactsResultSet['FIRSTNAME'] . ' ' . $rowContactsResultSet['LASTNAME'] . ';' . $rowContactsResultSet['EMAIL'];
	$forwardStr		= base64_encode($forwardStr);
	
    $replaceContent = array(
	    '$UNSUB$' 		=> $trackURL . "?email={$rowContactsResultSet['EMAIL']}&camid={$id}&hash={$hash}&action=unsubscribe&friend=0&redirect=",
	    '$CANTREAD$' 	=> $trackURL . "?email={$rowContactsResultSet['EMAIL']}&camid={$id}&code={$code}&action=cantread&friend=0&redirect=",
	    '$LINKTRACK$' 	=> $trackURL . "?email={$rowContactsResultSet['EMAIL']}&camid={$id}&action=track&friend=0&redirect=",	 
	    '$FORWARD$' 	=> $absPath . "/forward.php?id={$forwardStr}",
	    '@EMAIL@' 		=> $rowContactsResultSet['EMAIL'], 
	    '@FIRSTNAME@' 	=> $rowContactsResultSet['FIRSTNAME'],	 
	    '@LASTNAME@'	=> $rowContactsResultSet['LASTNAME'],	 
	    '@MISC_1@' 		=> $rowContactsResultSet['MISC_1'], 
	    '@MISC_2@' 		=> $rowContactsResultSet['MISC_2'], 
	    '@MISC_3@' 		=> $rowContactsResultSet['MISC_3'],
	);
	$subject    	= trim($funcObj->replaceAll($replaceContent, $result[0]['SUBJECT']));
	?>
                              <?=$subject?>                              </td>
                              <td valign="top"><strong>From Address</strong><br />
                              <?=$result[0]['FROM_NAME'] ?> &lt;<?=$result[0]['FROM_EMAIL'] ?>&gt;                              </td>
                            </tr>
                            <tr>
                              <td>&nbsp;</td>
                              <td>&nbsp;</td>
                              <td>&nbsp;</td>
                            </tr>
                            <tr>
                              <td valign="top"><strong>Address Book(s)</strong><br />
                              <?php
							  foreach($AddrResultSet as $row)
							  {
							  	echo $row['NAME'];
								echo "<br />";
							  }
							  ?>                              </td>
                              <td valign="top"><strong>Internal / External Links<br />
                              </strong>
                              <?php
                              }
// USE THIS FUNCTION TO USE AUTOMATICALLY LinkExtractor CLASS
require( "classes/LinkExtractor.class.php" );

$ID = $_GET['id'];

$query = "SELECT CONTENTS FROM ##campaigns WHERE ID = '$ID'";
$resultSet = array();
$resultSet = $funcObj->CustomQuery($query);

//$resultSet[0]->CONTENTS;

$myLinks = new LinkExtractor(); // create a LinkExtractor Object
$myLinks->parseString( $resultSet[0]['CONTENTS'] ); // parse a string

$linkcount=$myLinks->getLinks();
$cntLinks='';
if(!empty($linkcount)){
	$cntLinks = count($myLinks->getLinks());
}


echo "<a href='#' id='extLinks'>{$cntLinks} links found</a> <br /><br />";

echo "<div style='display: none;' id='extlinksdiv' class='box'>";
$fetchLinks = $myLinks->getLinks();
if(!empty($fetchLinks)){
	for( $a = 0, $b = count( $fetchLinks ); $a < $b; $a++ ) {
		echo $fetchLinks[$a]."<br />";
	}
}


echo "</div>";

?>                              </td>
                              <td valign="top"><strong>Reply-To Address</strong><br />
								<?php
								if (empty($result[0]['REPLYTO_EMAIL']))
									echo '<em>Same as FROM address</em>';
								else
									echo $result[0]['REPLYTO_NAME'] . ' &lt;'	. $result[0]['REPLYTO_EMAIL'] . '&gt'; 
								?>
							</td>
                            </tr>
                            <tr>
                              <td>&nbsp;</td>
                              <td>&nbsp;</td>
                              <td>&nbsp;</td>
                            </tr>
                            <tr>
                              <td>
                                <?php
								$query = "SELECT *	FROM ##smtp WHERE `DEFAULT` = '1'";
								$resultSetSMTP = array();
								$resultSetSMTP = $funcObj->CustomQuery($query);
								$prohibitProceed='';
								if (count($resultSetSMTP))
								{
									if ($resultSetSMTP[0]['MAILER_TYPE'] == 'smtp')
									{
										echo "
										<strong>SMTP used ({$resultSetSMTP[0]['TITLE']})</strong><br />
										Host: {$resultSetSMTP[0]['HOST']} <br />
										Port: {$resultSetSMTP[0]['PORT']} <br />
										User: {$resultSetSMTP[0]['USER']} <br />
										Authentication Required: ";
										echo ($resultSetSMTP[0]['AUTHENTICATION'] == '1') ? 'Yes' : 'No';
									}
									else
									{
										echo "<strong>Mailer used: {$resultSetSMTP[0]['MAILER_TYPE']}</strong>";
									}
								}
								else
								{
									// Bind alert message with Goto Step 3 button
									$prohibitProceed = " onclick='return noDefaultSMTP()' ";
								}
								?></td>
                              <td>&nbsp;</td>
                              <td>&nbsp;</td>
                            </tr>
                        </table></td>
                      </tr>
                    </table>
                    <div style="height: 50px; border: 0px solid #000; vertical-align: middle; padding: 5px; text-align: right">
                  <input type="submit" value="Goto Step 3" class="button" <?=$prohibitProceed?> />
                </div>
                    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="inactiveTable" style="margin-top: 3px;">
                      <tr class="inactiveTab">
                        <td width="30" align="center"><img src="images/30.png" alt="" border="0" hspace="8" /></td>
                        <td width="90%"><span><strong>Campaign:</strong> live preview for "
                          <?=$campaignName?>
                          "</span></td>
                        <td width="10%" nowrap="nowrap">&nbsp;</td>
                      </tr>
                    </table>
                    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="inactiveTable" style="margin-top: 3px;">
                      <tr class="inactiveTab">
                        <td width="30" align="center"><img src="images/40.png" alt="" hspace="8" border="0" /></td>
                        <td width="90%"><span><strong>Campaign:</strong> send</span></td>
                        <td width="10%" nowrap="nowrap">&nbsp;</td>
                      </tr>
                    </table>
                    <input type="hidden" name="id" value="<?php echo isset($_GET['id']) ? $_GET['id'] : ''?>" />
                    <input type="hidden" name="formaction" value="<?php echo $ACTION?>" />
                  </form></td>
              </tr>
            </table></td>
      </tr>
    </table></td>
  </tr>
<?php require_once 'includes/inc.footer.php' ?>
</table>
</body>
</html>
