<?php 
include("postget.php");
include_once("common.php");
checklevel(2,$_REQUEST['clientid']);


include("head_template.php");

$now=time()+$config[31][1]*3600;

if ($QUEUEid AND $QUEUEq==1) {
	$query= "SELECT * FROM hd_emailqueue WHERE id=$QUEUEid";
	$output=mysql_query($query,$db_link);
	$list=mysql_fetch_array($output);

	$incluirlogo = 1;
	if (strstr($list[body],$config[87][1])) {$incluirlogo = 0;}

	mail2client($list[subject],$list[body],'admin',$list[email],$incluirlogo);

	$query="DELETE FROM hd_emailqueue WHERE id='$QUEUEid'";
	if(!mysql_query($query,$db_link)) {
		messagetop(_ERROR,mysql_error(),'emailqueue.php',0);
	}

	$query="UPDATE hd_config SET valor=$now WHERE ref=60";
	if(!mysql_query($query,$db_link)) {
		messagetop(_ERROR,mysql_error(),'emailqueue.php',0);
	}
	$ultimoenvio=$now;
}


if ($QUEUEq>1) {
	$query= "SELECT * FROM hd_emailqueue LIMIT 0,$QUEUEq";
	$output=mysql_query($query,$db_link);
	while($list=mysql_fetch_array($output)) {

		$incluirlogo = 1;
		if (strstr($list[body],$config[87][1])) {$incluirlogo = 0;}

		mail2client($list[subject],$list[body],'admin',$list[email],$incluirlogo);

		$query="DELETE FROM hd_emailqueue WHERE id='$list[id]'";
		if(!mysql_query($query,$db_link)) {
			messagetop(_ERROR,mysql_error(),'emailqueue.php',0);
		}

		$query="UPDATE hd_config SET valor=$now WHERE ref=60";
		if(!mysql_query($query,$db_link)) {
			messagetop(_ERROR,mysql_error(),'emailqueue.php',0);
		}
		$ultimoenvio=$now;
	}
}





$QUEUEid=null;
$QUEUEq=null;

if (!$ultimoenvio) {$ultimoenvio=$config[60][1];}


$then=getdate();
$tonight=mktime(23,59,59,$then[mon],$then[mday],$then[year]);

$query= "SELECT * FROM hd_emailqueue ORDER BY datetime";
$output=mysql_query($query,$db_link);


echo '
<div class="container">
	<div class="row">

';


print("<div>
    <b>".mysql_numrows($output)." "._EMAILS." "._IN." "._QUEUE."</b> 	
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>"._LAST_MAILING.": ".date($df.' H:i:s',$ultimoenvio)." (".TimeAgo($config[60][1],time()+$config[31][1]*3600)." "._AGO.")
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<a href='".$serverurl."/emailqueue.php'>"._REFRESH."</a>
	</b></div>");

graybutton("emailqueue.php?QUEUEq=10",""._SEND." 10 "._EMAILS."");
graybutton("emailqueue.php?QUEUEq=20",""._SEND." 20 "._EMAILS."");
graybutton("emailqueue.php?QUEUEq=50",""._SEND." 50 "._EMAILS."");
graybutton("emailqueue.php?QUEUEq=100",""._SEND." 100 "._EMAILS."");
graybutton("emailqueue.php?QUEUEq=9999999999",""._SEND." "._ALL."");

print("
<table class='table table-striped'>");


print("<thead><tr>
 	<th>"._SEND."</th> 	
	<th>"._SUBJECT."</th> 
	<th>"._SEND_TO."</th>
 	<th>"._REPLY_TO."</th> 	
 	<th>"._SEND_COPY_TO."</th> 	
	<th>"._DATE."</th>");

print("</tr></thead><tbody>");



$bgc=true;
$now=time()+$config[31][1]*3600;
while($list=mysql_fetch_array($output)) {
	$list0=$list;
	print("<tr>");
	print("<td align='left'><a href='".$serverurl."/emailqueue.php?QUEUEid=$list0[id]&QUEUEq=1'>"._SEND."</a></font></td>");
	print("<td align='left'>$list0[subject]</td>");
	print("<td align='left'>$list0[email]</td>");
	print("<td align='left'>".$list0[replyto]."</td>");
	print("<td align='left'>".$list0[bcc]."</td>");
	print("<td align='left'>".date($df.' H:i:s',$list0[datetime])."</td>");
	print("</tr>");
}

echo '
		</td>
		</tr></tbody>
		</table>
</div>
</div>
';

include("foot_template.php");

?> 
