#! /usr/bin/env php
/',
'//',
'//',
'//',
'//',
);
$most_recent = 0;
$result = mysql_query("select * from host where userid=" . $user->id);
while ($host = mysql_fetch_object($result)) {
if ($host->rpc_time > $most_recent) { $most_recent = $host->rpc_time; }
}
mysql_free_result($result);
$rep = array(
$user->name,
gmdate('d F Y', $user->create_time),
number_format($user->total_credit, 0),
URL_BASE."opt_out.php?code=".salted_key($user->authenticator)."&userid=$user->id",
floor((time() - $most_recent) / 86400),
);
return preg_replace($pat, $rep, $template);
}
function mail_type($user, $email_file) {
global $testing;
$html = replace($user, $email_file['html']);
$text = replace($user, $email_file['text']);
if ($testing) {
echo "\nSending to $user->email_addr:\n";
echo "------- SUBJECT ----------\n";
echo $email_file['subject'];
echo "\n------- HTML ----------\n";
echo $html;
echo "\n------- TEXT ----------\n";
echo $text;
} else {
echo $user->id . ": ";
if (is_valid_email_addr($user->email_addr)) {
send_email(
$user,
$email_file['subject'],
$text,
$html
);
echo "sent\n";
} else {
echo "invalid e-mail address\n";
}
}
}
function handle_user($user, $email_files) {
global $nocurrent;
if ($user->total_credit == 0) {
mail_type($user, $email_files['failed']);
} else if ($user->expavg_credit < 1000) {
mail_type($user, $email_files['lapsed']);
} else {
if ($nocurrent == 0) {
mail_type($user, $email_files['current']);
} else {
echo $user-> id . ": not doing current/active users\n";
}
}
}
function do_batch($email_files, $startid, $n, $log) {
$result = mysql_query(
"select * from user where id>$startid and id < 122080 order by id limit $n"
);
while ($user = mysql_fetch_object($result)) {
handle_user($user, $email_files);
$startid = $user->id;
fputs($log, $user->id . "\n");
fflush($log);
}
mysql_free_result($result);
return $startid;
}
function do_one($email_files, $thisid, $log) {
$result = mysql_query(
"select * from user where id=$thisid"
);
while ($user = mysql_fetch_object($result)) {
handle_user($user, $email_files);
fputs($log, $user->id . "\n");
fflush($log);
}
mysql_free_result($result);
return $startid;
}
function read_log() {
global $mass_email_log;
$f = fopen($mass_email_log, 'r');
if (!$f) {
echo $mass_email_log . ' not found - create empty file and run again\n';
exit();
}
$startid = 0;
while (fscanf($f, '%d', &$startid)) {
}
fclose($f);
return $startid;
}
function main($email_files) {
global $id_file;
global $mass_email_log;
$startid = read_log();
$f = fopen($mass_email_log, 'a');
$n = 10;
if ($id_file == "") {
while (1) {
$new_startid = do_batch($email_files, $startid, $n, $f);
if ($new_startid == $startid) break;
$startid = $new_startid;
}
} else {
$fid = fopen($id_file, 'r');
if (!$fid) {
echo $id_file . ' not found - create id list and run again\n';
exit();
}
$thisid = 0;
while (fscanf($fid, '%d', &$thisid)) {
if ($thisid > $startid) {
do_one($email_files, $thisid, $f);
}
}
fclose($fid);
}
echo 'All done!' . "\n";
}
function test($email_files) {
}
if (!$USE_PHPMAILER) {
echo "You must use PHPMailer.\n";
exit();
}
$email_files = read_email_files();
if ($userid) {
$user = lookup_user_id($userid);
mail_type($user, $email_files['failed']);
mail_type($user, $email_files['lapsed']);
if ($nocurrent == 0) { mail_type($user, $email_files['current']); }
} else {
main($email_files);
}
?>