id");
while ($host = mysql_fetch_object($result)) {
if ($host->rpc_time > $x) $x = $host->rpc_time;
}
return $x;
}
function read_files(&$item) {
$item['html'] = @file_get_contents($item['html_file']);
if (!$item['html']) {
//$x = $item['html_file'];
//echo "file missing: $x\n";
//exit();
}
$item['text'] = @file_get_contents($item['text_file']);
if (!$item['text']) {
$x = $item['text_file'];
echo "file missing: $x\n";
exit();
}
$item['subject'] = @file_get_contents($item['subject']);
if (!$item['subject']) {
$x = $item['subject'];
echo "file missing: $x\n";
exit();
}
}
function read_email_files() {
global $failed_html;
global $failed_text;
global $failed_subject;
global $lapsed_html;
global $lapsed_text;
global $lapsed_subject;
$failed['html_file'] = $failed_html;
$failed['text_file'] = $failed_text;
$failed['subject'] = $failed_subject;
$lapsed['html_file'] = $lapsed_html;
$lapsed['text_file'] = $lapsed_text;
$lapsed['subject'] = $lapsed_subject;
read_files($failed);
read_files($lapsed);
$email_files['failed'] = $failed;
$email_files['lapsed'] = $lapsed;
return $email_files;
}
function replace($user, $template) {
$pat = array(
'//',
'//',
'//',
'//',
'//',
'//',
);
$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",
$user->id,
$user->lapsed_interval,
);
return preg_replace($pat, $rep, $template);
}
function mail_type($user, $email_file) {
global $testing;
if ($email_file['html']) {
$html = replace($user, $email_file['html']);
} else {
$html = null;
}
$text = replace($user, $email_file['text']);
if ($testing) {
if (true) {
echo "------- SUBJECT ----------\n";
echo $email_file['subject'];
echo "\n------- HTML ----------\n";
echo $html;
echo "\n------- TEXT ----------\n";
echo $text;
}
} else {
send_email(
$user,
$email_file['subject'],
$text,
$html
);
$now = time();
mysql_query("update user set posts=$now where id=$user->id");
}
}
// NOTE: we're using user.posts (a deprecated field)
// to store the time the user was last sent an email.
// At some point maybe we'll rename this field.
function handle_user($user, $email_files) {
global $lapsed_interval;
global $testing;
if ($testing) {
$x = (time() - $user->create_time)/86400;
echo "user $user->email_addr was created $x days ago\n";
}
if ($user->total_credit == 0) {
if ($testing) {
echo "zero credit, sending failed email\n";
}
mail_type($user, $email_files['failed']);
} else {
$t = last_rpc_time($user);
if ($t < time() - $lapsed_interval) {
$user->lapsed_interval = (time()-$t)/86400;
if ($testing) {
echo "nonzero credit, last RPC $user->lapsed_interval days ago, sending lapsed email\n";
}
mail_type($user, $email_files['lapsed']);
}
}
}
function do_batch($email_files, $startid, $n) {
global $email_interval;
global $start_interval;
$max_email_time = time() - $email_interval;
$max_create_time = time() - $start_interval;
$result = mysql_query(
"select * from user where id>$startid and send_email<>0 and posts<$max_email_time and create_time<$max_create_time and expavg_credit < 10 order by id limit $n"
);
while ($user = mysql_fetch_object($result)) {
handle_user($user, $email_files);
$startid = $user->id;
}
mysql_free_result($result);
return $startid;
}
function main($email_files) {
$startid = 0;
$n = 1000;
while (1) {
$new_startid = do_batch($email_files, $startid, $n, $f);
if ($new_startid == $startid) break;
$startid = $new_startid;
}
echo "All done!\n";
}
if (!$USE_PHPMAILER) {
echo "You must use PHPMailer.\n";
exit();
}
$email_files = read_email_files();
if ($userid) {
$user = lookup_user_id($userid);
if (!$user) {
echo "No such user: $userid\n";
exit();
}
mail_type($user, $email_files['failed']);
mail_type($user, $email_files['lapsed']);
} else {
main($email_files);
}
?>