<?
$page = "admin_emailall.php";
include "admin_header.php";

if(isset($_POST['task'])) { $task = $_POST['task']; } elseif(isset($_GET['task'])) { $task = $_GET['task']; } else { $task = "main"; }






if($task == "dosend") {
$em_from = str_replace("'", "&#39;", stripslashes($_POST['from']));
$em_to = $_POST['to'];
$em_sub = str_replace("'", "&#39;", stripslashes($_POST['subject']));
$em_mess = str_replace("'", "&#39;", stripslashes($_POST['message']));
$emails_at_a_time = $_POST['emails_at_a_time'];

if(!isset($_POST['start']) OR $_POST['start'] == "") {
$start = 0;
} else {
$start = $_POST['start'];
}

$finish = $start + $emails_at_a_time;
$limit = "$start,$emails_at_a_time";

if($em_to == "all") {
$emailquery = "SELECT u_id, username, email, fname, lname FROM bhost_users ORDER BY u_id LIMIT $limit";
} else {
$emailquery = "SELECT u_id, username, email, fname, lname FROM bhost_users WHERE g_id=$em_to ORDER BY u_id LIMIT $limit";
}


$users = mysql_query($emailquery);
$totalinset = mysql_num_rows($users);

echo $head;
if($totalinset < $emails_at_a_time) {
echo "
<h2>$admin_emailall3</h2>
$admin_emailall4
<br><br>
<form action='admin_emailall.php' method='POST'>
<input type='submit' class='button' value='$admin_emailall5'>
</form>
";
} else {
$start1 = $start + 1;
$finish1 = $finish + 1;
echo "
<h2>$admin_emailall17</h2>
$admin_emailall18 <b>#$start1 - #$finish1</b><br>
$admin_emailall19
<br><br>
<form action='admin_emailall.php' name='nextset' method='POST'>
<input type='submit' class='button' value='Continue...'>
<input type='hidden' name='start' value='$finish'>
<input type='hidden' name='from' value='$em_from'>
<input type='hidden' name='to' value='$em_to'>
<input type='hidden' name='subject' value='$em_sub'>
<input type='hidden' name='message' value='$em_mess'>
<input type='hidden' name='emails_at_a_time' value='$emails_at_a_time'>
<input type='hidden' name='task' value='dosend'>
</form>

<script language='JavaScript'>
<!-- 
function window.onload() { 
setTimeout(\"document.nextset.submit()\", 2000);
} 
// -->
</script>
";
}

$em_from = str_replace("&#39;", "'", $em_from);
$em_sub = str_replace("&#39;", "'", $em_sub);
$em_mess = str_replace("&#39;", "'", $em_mess);

// SEND EMAILS TO THIS SET
while($user = mysql_fetch_assoc($users)) {
$fname = str_replace("&#39;", "'", $user[fname]);
$lname = str_replace("&#39;", "'", $user[lname]);
$em_sub1 = str_replace("<username>", "$user[username]", $em_sub);
$em_sub1 = str_replace("<fname>", "$fname", $em_sub1);
$em_sub1 = str_replace("<lname>", "$lname", $em_sub1);
$em_mess1 = str_replace("<username>", "$user[username]", $em_mess);
$em_mess1 = str_replace("<fname>", "$fname", $em_mess1);
$em_mess1 = str_replace("<lname>", "$lname", $em_mess1);
mail($user['email'], $em_sub1, $em_mess1, "From: $em_from");
}

echo $foot;
exit();
}














if($task == "main") {

// GENREATE LIST OF GROUPS
$groups = mysql_query("SELECT g_id, group_name FROM bhost_groups");
$grouplist = "
<select name='to'>
<option value='all'>$admin_emailall9</option>
";
if(mysql_num_rows($groups) > 1) {
while($group = mysql_fetch_assoc($groups)) {
$grouplist .= "<option value='$group[g_id]'>$group[group_name]</option>";
}}
$grouplist .= "</select>";


echo $head;
echo "
<h2>$admin_emailall6</h2>
$admin_emailall7 
<br><br>

<table cellpadding='5' cellspacing='0'>
<form action='admin_emailall.php' method='POST'>
<tr>
<td align='right'>$admin_emailall10</td>
<td><input type='text' class='text' name='from' size='40' value='$admin_info[fname] $admin_info[lname] &lt;$admin_info[email]&gt;'></td>
</tr>
<tr>
<td align='right'>$admin_emailall8</td>
<td>

$grouplist

</td>
</tr>
<tr>
<td align='right'>$admin_emailall11</td>
<td><input type='text' class='text' name='subject' size='40'></td>
</tr>
<tr>
<td align='right' valign='top'>$admin_emailall12</td>
<td><textarea name='message' rows='8' cols='60'></textarea></td>
</tr>
<tr>
<td align='right'>$admin_emailall14</td>
<td>
<select name='emails_at_a_time'>
<option value='1'>1 $admin_emailall15</option>
<option value='2'>2 $admin_emailall16</option>
<option value='5'>5 $admin_emailall16</option>
<option value='10'>10 $admin_emailall16</option>
<option value='20' SELECTED>20 $admin_emailall16</option>
<option value='30'>30 $admin_emailall16</option>
<option value='40'>40 $admin_emailall16</option>
<option value='50'>50 $admin_emailall16</option>
</select>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<input type='submit' class='button' value='$admin_emailall13'>
<input type='hidden' name='task' value='dosend'>
</td>
</tr>
</table>


</form>
";
echo $foot;
}
?>