<?

include "header.php";

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





if($task == "email_do") {

$em_sub = stripslashes($_POST['em_sub']);
$em_mess = stripslashes($_POST['em_mess']);
$em_from = $_POST['em_from'];

if($em_sub == "") {
$error = "You must enter a subject.";
}
if($em_mess == "" ) {
if($error != "") { $error .= "<br>"; }
$error .= "You must enter a message.";
}
if(!preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i", $em_from) | str_replace(" ", "", $em_from) == "") {
if($error != "") { $error .= "<br>"; }
$error .= "You must enter a valid \"from\" address.";
}

if($error != "") {
echo "
$header
<table cellpadding='0' cellspacing='0' width='500'>
<tr>
<td><b>An Error Has Occurred:</b></td>
</tr><tr>
<td>
$error<br><br>
<b>Please use your browser's back button to try again.</b>
</td>
</tr>
</table>
$footer";
exit;
}

if($admin_info[demo_feature] == "on") {
$users = mysql_query("SELECT * FROM ims WHERE is_approved='yes' AND email <> '$admin_info[demo_email]'");
} else {
$users = mysql_query("SELECT * FROM ims WHERE is_approved='yes'");
}

set_time_limit(500);
$total_users = mysql_num_rows($users);
$emails_at_a_time = 50;
$a = 0;
$b = 0;
echo $header;
echo "
<table cellpadding='0' cellspacing='0' width='500'>
<tr>
<td><b>Emailing Users</b></td>
</tr><tr>
<td>
Processing, please wait...<br>
</td>
</tr>
</table>
";
echo $footer;
ob_flush();
flush();
while($user_info = mysql_fetch_assoc($users)) {
if($a == $emails_at_a_time) {
$a = 0;
ob_flush();
flush();
sleep(2);
}
if($b == 500) {
set_time_limit(500);
$b = 0;
}
$fname = str_replace("&#39;", "'", $user_info[fname]);
$lname = str_replace("&#39;", "'", $user_info[lname]);
$em_sub1 = str_replace("<fullname>", "$fname $lname", $em_sub);
$em_mess1 = str_replace("<fullname>", "$fname $lname", $em_mess);
@mail("$user_info[email]", "$em_sub1", "$em_mess1", "From: $em_from");
$a = $a + 1;
$b = $b + 1;
}
echo "<META HTTP-EQUIV=\"refresh\" content=\"0;URL=email.php?task=success\">";
exit();
}




if($task == "success") {
echo $header;
echo "
<table cellpadding='0' cellspacing='0' width='500'>
<tr>
<td><b>Email All Users >> Success!</b></td>
</tr>
<tr>
<td>Your email has been sent to all users registered on this system.</td>
</tr>
</table>
";
echo $footer;
exit();
}








if($task == "email") {

if($admin_info[demo_feature] == "on") {
$total_users = mysql_num_rows(mysql_query("SELECT * FROM ims WHERE is_approved='yes' AND email <> '$admin_info[demo_email]'"));
} else {
$total_users = mysql_num_rows(mysql_query("SELECT * FROM ims WHERE is_approved='yes'"));
}

echo $header;

if($total_users > 0) {

echo "
<table cellpadding='0' cellspacing='0' width='500'>
<tr><form action='email.php' method='POST'>
<td><b>Email All Users</b><br>
Use this form to broadcast an email to every user registered on your IMS.<br>
If you would like a comma-delimited list of the user email addresses, <a href='email_list.php' title='Generate Email List' target='_blank'>click here</a>.<br>
Please be patient - more users require longer mailing time.<br>
</td>
</tr>
</table>
<br>
<table cellpadding='0' cellspacing='0' width='500'>
<tr>
<td align='right' style='padding-bottom: 3px;'>From &nbsp;</td>
<td style='padding-bottom: 3px;'><input type='text' name='em_from' size='45' value='$admin_info[email]'></td>
</tr>
<tr>
<td align='right' style='padding-bottom: 3px;'>Subject &nbsp;</td>
<td style='padding-bottom: 3px;'><input type='text' name='em_sub' size='45'></td>
</tr>
<tr>
<td align='right' valign='top'>Message &nbsp;</td>
<td><textarea rows='8' cols='60' name='em_mess'></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td style='padding-top: 3px;'>
<input type='hidden' name='task' value='email_do'>
<input class='button' type='submit' value='Send Email To All Users'>&nbsp;
<br><br>
Note: To insert the customer's full name, use <b>&lt;fullname&gt;</b>
</td>
</tr>
</form>
</table>
";

} else {

echo "
<table cellpadding='0' cellspacing='0' width='500'>
<tr>
<td><b>Email All Users</b></td>
</tr>
<tr>
<td>No users were found on the system.</td>
</tr>
</table>
";

}
echo $footer;
}

?>