<?PHP
include "../inc/var.php";
include "../inc/connect.php";
include $server_inc."/whm_functions.php";
include $server_inc."/client_functions.php";
include $server_inc."/login_functions.php";

if (isset($submit))
	{
	if (strlen(trim($subject))==0) { $err0=true; } else { $n0=true; }
	if (strlen(trim($body))==0) { $err1=true; } else { $n1=true; }
	if (isset($n0)&&isset($n1))
		{
		// timer class
		class Timer 
			{
			var $startTime,
				$endTime,
				$timeDifference;

			function start() {$this->startTime = $this->currentTime();}
			function finish() {$this->endTime = $this->currentTime();}
			function getTime() 
				{
				$this->timeDifference = $this->endTime - $this->startTime;
				return round($this->timeDifference, 5);
				}

			function currentTime() 
				{
				list($usec, $sec) = explode(' ',microtime());
				return ((float)$usec + (float)$sec);
				}
			} 

		$timer = new Timer(); 
		$timer->start(); 

		// build email headers
		$headers="From: ".$email_admin."\n";
		$headers.="Reply-To: ".$email_admin."\n";
		$headers.="X-Mailer: PHP/" . phpversion();

		if ($mailto==0) {$row=mysql_query("select email, first_name, uid from user where status!='69'");}
		else if ($mailto==1) {$row=mysql_query("select email, first_name, uid from user where reseller='1'");}
		else if ($mailto==2) {$row=mysql_query("select email, first_name, uid from user where reseller!='1' and status!='69'");}
		while ($rxx=mysql_fetch_row($row))
			{
			// define vars
			$email=stripslashes(trim($rxx[0]));
			$first_name=stripslashes(trim($rxx[1]));
			$uid=stripslashes(trim($rxx[2]));
			$bit=0;
			
			// loop the hosting_order db looking for match on posted whm_id's
			$row_p=mysql_query("select whm_id from hosting_order where uid='".addslashes(trim($uid))."'");
			while ($rs_p=mysql_fetch_row($row_p))
				{
				foreach ($whm_id as $line_num => $line)
					{
					if ($whm_id[$line_num]==$rs_p[0]) { $bit=1; }
					}
				}
			
			// bit is high, send email
			if ($bit==1)
				{
				// parse out {{first_name}}
				$subject=stripslashes($subject);
				$body=stripslashes($body);
				$mail_subject=str_replace("{{first_name}}", $first_name, $subject);
				$mail_body=str_replace("{{first_name}}", $first_name, $body);
				mail($email, $mail_subject, $mail_body, $headers);

				// set the bit to low
				$bit=0;
				}

			unset($mail_subject);
			unset($mail_body);
			unset($email);
			unset($first_name);
			unset($uid);
			usleep(5000);
			set_time_limit(0);
			}
		$done=true;
		}
	}

include $server_admin_inc."/header.php";

if (isset($done))
	{
	$timer->finish();
	echo("
		<form action='".$PHP_SELF."' method='POST'>
		<input type='hidden' name='sid' value='".trim($sid)."'>
		<table width='725' cellpadding='0' cellspacing='0' border='0'>
			<tr>
				<td width='1%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='375'></td>
				<td width='99%' align='left' valign='top'>
				<table width='100%' cellpadding='2' cellspacing='0' border='0'>
					<tr>
						<td><img src='".$http_images."/menu_arrow.gif'> <b>Email Current Users</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<a href='".$http_admin."/client_email.php?sid=".trim($sid)."'><font color='#990000'>Back</font></a>]</td>
					</tr>
					<tr>
						<td><img src='".$http_images."/space.gif' width='15' height='8'></td>
					</tr>
					<tr>
						<td>".$x." users were emailed in ".$timer->getTime()." seconds.</td>
					</tr>
					<tr>
						<td><hr color='#F3F3F3'></td>
					</tr>
					<tr>
						<td>Subject: ".trim($subject)."</td>
					</tr>
					<tr>
						<td><img src='".$http_images."/space.gif' width='15' height='4'></td>
					</tr>
					<tr>
						<td>Message:</td>
					</tr>
					<tr>
						<td>".nl2br(trim($body))."</td>
					</tr>
				</table>
				</td>
			</tr>
		</table>
		</form>
		");

	include $server_admin_inc."/footer.php";
	mysql_close($dblink);
	die;
	}

echo("
	<table width='725' cellpadding='0' cellspacing='0' border='0'>
		<tr>
			<td width='1%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='375'></td>
			<td width='99%' align='left' valign='top'>
			<form action='".$PHP_SELF."' method='POST'>
			<input type='hidden' name='sid' value='".trim($sid)."'>
			<table width='100%' cellpadding='2' cellspacing='0' border='0'>
				<tr>
					<td colspan='2' align='left' valign='top'><img src='".$http_images."/menu_arrow.gif'><b>Email Current Users</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<a href='".$http_admin."/index2.php?sid=".trim($sid)."'><font color='#990000'>Back</font></a>]</td>
				</tr>
				<tr>
					<td colspan='2' align='left' valign='top'><img src='".$http_images."/space.gif' width='1' height='8'></td>
				</tr>
	");
if (isset($err0))
	{
	echo("
				<tr>
					<td colspan='2' align='left' valign='top'><img src='".$switch."/error_arrow.gif'> You must enter a subject to continue.</td>
				</tr>
		");
	}
if (isset($err1))
	{
	echo("
				<tr>
					<td colspan='2' align='left' valign='top'><img src='".$switch."/error_arrow.gif'> You must enter a message to continue.</td>
				</tr>
		");
	}
echo("
				<tr>
					<td width='15%' align='left' valign='top'>Send Emails to:</td>
					<td width='85%' align='left' valign='top'><select name='mailto' ".$input_style.">
					<option value='0'>All Users</option>
					<option value='1'>Resellers Only</option>
					<option value='2'>Non Resold Clients Only</option>
					</select></td>
				</tr>	
				<tr>
					<td colspan='2' align='left' valign='top'>
					<table width='500' cellpadding='0' cellspacing='0' border='1' bordercolor='#E2E3E4'>
	");
$x=0;
$y=0;
$rowx=mysql_query("select whm_id, server_name from server_config order by server_name asc");
while ($rx=mysql_fetch_row($rowx))
	{
	if ($x==0)
		{
		echo ("
			<tr>
				<td width='50%' align='left' valign='middle'><input type='checkbox' name='whm_id[".$y."]' value='".$rx[0]."' checked> ".stripslashes(trim($rx[1]))."</td>
			");

		$x=1;
		}
	else
		{
		echo ("
				<td width='50%' align='left' valign='middle'><input type='checkbox' name='whm_id[".$y."]' value='".$rx[0]."' checked> ".stripslashes(trim($rx[1]))."</td>
			</tr>
			");

		$x=0;
		}
	$y++;
	}

if ($x%2!=0)
	{
	echo ("
			<td width='50%' align='center' valign='top'></td>
		</tr>
		");
	}

echo("
					</table>
					</td>
				</tr>	
				<tr>
					<td colspan='2' align='left' valign='top'><img src='".$http_images."/space.gif' width='1' height='6'></td>
				</tr>		
				<tr>
					<td colspan='2'><hr color='#F3F3F3'></td>
				</tr>	
				<tr>
					<td colspan='2' align='left' valign='top'><img src='".$http_images."/space.gif' width='1' height='6'></td>
				</tr>	
				<tr>
					<td width='15%' align='left' valign='top'>Email Subject:</td>
					<td width='85%' align='left' valign='top'><input ".$input_style." type='text' size='51' maxlength='255'name='subject' value='".$admin_email."'></td>
				</tr>		
				<tr>
					<td colspan='2' align='left' valign='top'><img src='".$http_images."/space.gif' width='1' height='4'></td>
				</tr>	
				<tr>
					<td colspan='2' align='left' valign='top'>Email Message: [Object available: {{first_name}}]</td>
				</tr>	
				<tr>
					<td colspan='2' align='left' valign='top'><textarea name='body' rows='18' cols='80' ".$input_style."></textarea></td>
				</tr>	
				<tr>
					<td colspan='2' align='left' valign='top'><img src='".$http_images."/space.gif' width='1' height='8'></td>
				</tr>		
				<tr>
					<td width='15%' align='left' valign='top'></td>
					<td width='85%' align='left' valign='top'><input ".$button_style." type='submit' name='submit' value='Send Email Now'></td>
				</tr>	
			</table>
			</form>
			</td>
		</tr>
	</table>
	");
include $server_admin_inc."/footer.php";
mysql_close($dblink);
?>