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 $email_failed_html; global $email_failed_text; global $email_failed_subject; global $email_lapsed_html; global $email_lapsed_text; global $email_lapsed_subject; global $email_current_html; global $email_current_text; global $email_current_subject; $failed['html_file'] = $email_failed_html; $failed['text_file'] = $email_failed_text; $failed['subject'] = $email_failed_subject; $lapsed['html_file'] = $email_lapsed_html; $lapsed['text_file'] = $email_lapsed_text; $lapsed['subject'] = $email_lapsed_subject; $current['html_file'] = $email_current_html; $current['text_file'] = $email_current_text; $current['subject'] = $email_current_subject; read_files($failed); read_files($lapsed); read_files($current); $email_files['failed'] = $failed; $email_files['lapsed'] = $lapsed; $email_files['current'] = $current; 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) { 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 { send_email( $user, $email_file['subject'], $text, $html ); } } function handle_user($user, $email_files) { if ($user->total_credit == 0) { mail_type($user, $email_files['failed']); } else if ($user->expavg_credit < 1) { $t = last_rpc_time($user); $user->lapsed_interval = (time()-$t)/86400; mail_type($user, $email_files['lapsed']); } else { mail_type($user, $email_files['current']); } } function do_batch($email_files, $startid, $n, $log) { $result = mysql_query( "select * from user where id>$startid 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 read_log() { $f = fopen($logfile, 'r'); if (!$f) { echo '$logfile 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) { $startid = read_log(); $f = fopen($logfile, 'w'); $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'; } 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']); mail_type($user, $email_files['current']); } else { main($email_files); } ?>