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( '//', '//', '//', '//', '//', '//', ); $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", $user->id, floor((time() - $most_recent) / 86400), ); return preg_replace($pat, $rep, $template); } function mail_type($user, $email_files, $type) { global $testing; $email_file = $email_files[$type]; 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(); $ntype = 0; if $type == 'lapsed') $ntype = 2; if $type == 'failed') $ntype = 3; $query = "insert into sent_email values($user->id, $now, $ntype)"; mysql_query($query); } } 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 last_reminder_time($user) { $query = "select * from sent_email where $userid=$user->id"; $result = mysql_query($query); $t = 0; while ($r = mysql_fetch_object($result)) { if ($r->email_type !=2 && $r->email_type != 3) continue; if ($r->time_sent > $t) $t = $time_sent; } mysql_free_result($result); return $t; } 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 create_time<$max_create_time and expavg_credit < 10 order by id limit $n" ); while ($user = mysql_fetch_object($result)) { if (last_reminder_time($user) > $max_email_time) continue; 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); } ?>