2007-04-19 17:06:02 +00:00
|
|
|
#! /usr/bin/env php
|
|
|
|
|
2006-07-13 23:40:10 +00:00
|
|
|
<?php
|
|
|
|
|
2007-04-27 23:05:48 +00:00
|
|
|
// remind.php [--lapsed | --failed] [--show_email] [--userid ID]
|
2007-04-19 17:06:02 +00:00
|
|
|
//
|
2007-04-27 23:05:48 +00:00
|
|
|
// --lapsed
|
|
|
|
// Send emails to lapsed user (see below)
|
|
|
|
// --failed
|
|
|
|
// Send emails to failed user (see below)
|
|
|
|
// --userid
|
|
|
|
// Send both "lapsed" and "failed" emails to the given user,
|
|
|
|
// regardless of whether they are due to be sent.
|
|
|
|
// The --lapsed and --failed options are ignored.
|
|
|
|
// (for testing)
|
|
|
|
// --show_email
|
|
|
|
// Show the text that would be mailed
|
|
|
|
// --explain
|
|
|
|
// Show which users would be sent email and why
|
|
|
|
// --send
|
|
|
|
// Actually send emails (this is an option to encourage
|
|
|
|
// you to do thorough testing before using it)
|
|
|
|
//
|
|
|
|
// This program sends "reminder" emails to
|
2006-07-13 23:40:10 +00:00
|
|
|
// - failed users: those who
|
2007-04-27 23:05:48 +00:00
|
|
|
// 1) were created at least $start_interval seconds ago,
|
2006-07-13 23:40:10 +00:00
|
|
|
// 2) have zero total credit
|
|
|
|
// 3) haven't been sent an email in at least $email_interval seconds.
|
|
|
|
// These people typically either had a technical glitch,
|
|
|
|
// or their prefs didn't allow sending them work,
|
|
|
|
// or the app crashed on their host.
|
|
|
|
// The email should direct them to a web page that helps
|
|
|
|
// them fix the startup problem.
|
|
|
|
//
|
|
|
|
// Set $start_interval according to your project's delay bounds
|
|
|
|
// e.g. (1 or 2 weeks).
|
|
|
|
/// $email_interval should be roughly 1 month -
|
|
|
|
// we don't want to bother people too often.
|
|
|
|
//
|
|
|
|
// - lapsed users: those who
|
|
|
|
// 1) have positive total credit,
|
|
|
|
// 2) haven't done a scheduler RPC within the past
|
|
|
|
// $lapsed_interval seconds, and
|
|
|
|
// 3) haven't been sent an email in at least $email_interval seconds.
|
|
|
|
// The email should gently prod them to start running the project again.
|
|
|
|
//
|
|
|
|
|
2007-04-27 23:05:48 +00:00
|
|
|
$globals->start_interval = 14*86400;
|
|
|
|
$globals->email_interval = 30*86400;
|
|
|
|
$globals->lapsed_interval = 60*86400;
|
|
|
|
$globals->do_failed = false;
|
|
|
|
$globals->do_lapsed = false;
|
|
|
|
$globals->show_email = false;
|
|
|
|
$globals->send = false;
|
|
|
|
$globals->explain = false;
|
|
|
|
$globals->userid = 0;
|
2006-07-13 23:40:10 +00:00
|
|
|
|
2007-04-27 23:05:48 +00:00
|
|
|
for ($i=1; $i<$argc; $i++) {
|
|
|
|
if ($argv[$i] == "--failed") {
|
|
|
|
$do_failed = true;
|
|
|
|
} elseif ($argv[$i] == "--lapsed") {
|
|
|
|
$do_lapsed = true;
|
|
|
|
} elseif ($argv[$i] == "--show_email") {
|
|
|
|
$globals->show_email = true;
|
|
|
|
} elseif ($argv[$i] == "--explain") {
|
|
|
|
$globals->explain = true;
|
|
|
|
} elseif ($argv[$i] == "--send") {
|
|
|
|
$globals->send = true;
|
|
|
|
} elseif ($argv[$i] == "--userid") {
|
|
|
|
$i++;
|
|
|
|
$globals->userid = $argv[$i];
|
|
|
|
} else {
|
|
|
|
echo "unrecognized option $argv[$i]\n";
|
|
|
|
echo "usage: remind.php [--failed ] [--lapsed] [--userid N] [--show_mail] [--explain] [--send]\n";
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
2007-04-19 17:06:02 +00:00
|
|
|
|
2006-07-13 23:40:10 +00:00
|
|
|
require_once('../project/project.inc');
|
|
|
|
require_once("../inc/db.inc");
|
|
|
|
require_once("../inc/email.inc");
|
|
|
|
|
|
|
|
db_init();
|
|
|
|
set_time_limit(0);
|
|
|
|
|
|
|
|
// File names for the various mail types.
|
|
|
|
// Change these here if needed.
|
2007-04-27 23:05:48 +00:00
|
|
|
//
|
2006-07-13 23:40:10 +00:00
|
|
|
$dir = "remind_email";
|
|
|
|
$failed_html = "$dir/failed_html";
|
|
|
|
$failed_text = "$dir/failed_text";
|
|
|
|
$failed_subject = "$dir/failed_subject";
|
|
|
|
$lapsed_html = "$dir/lapsed_html";
|
|
|
|
$lapsed_text = "$dir/lapsed_text";
|
|
|
|
$lapsed_subject = "$dir/lapsed_subject";
|
|
|
|
|
|
|
|
// return time of last scheduler RPC from this user,
|
|
|
|
// or zero if they're never done one
|
|
|
|
//
|
|
|
|
function last_rpc_time($user) {
|
|
|
|
$x = 0;
|
2007-04-27 23:05:48 +00:00
|
|
|
$result = mysql_query("select rpc_time from host where userid=$user->id");
|
2006-07-13 23:40:10 +00:00
|
|
|
while ($host = mysql_fetch_object($result)) {
|
|
|
|
if ($host->rpc_time > $x) $x = $host->rpc_time;
|
|
|
|
}
|
2007-04-19 17:06:02 +00:00
|
|
|
mysql_free_result($result);
|
2006-07-13 23:40:10 +00:00
|
|
|
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(
|
|
|
|
'/<name\/>/',
|
|
|
|
'/<create_time\/>/',
|
|
|
|
'/<total_credit\/>/',
|
|
|
|
'/<opt_out_url\/>/',
|
2006-07-20 20:27:21 +00:00
|
|
|
'/<user_id\/>/',
|
|
|
|
'/<lapsed_interval\/>/',
|
2006-07-13 23:40:10 +00:00
|
|
|
);
|
|
|
|
$rep = array(
|
|
|
|
$user->name,
|
|
|
|
gmdate('d F Y', $user->create_time),
|
|
|
|
number_format($user->total_credit, 0),
|
2007-11-18 22:42:47 +00:00
|
|
|
URL_BASE."opt_out.php?code=".salted_key($user->authenticator)."&userid=$user->id",
|
2006-07-20 20:27:21 +00:00
|
|
|
$user->id,
|
2007-04-27 23:05:48 +00:00
|
|
|
floor ((time() - $user->last_rpc_time) / 86400),
|
2006-07-13 23:40:10 +00:00
|
|
|
);
|
|
|
|
return preg_replace($pat, $rep, $template);
|
|
|
|
}
|
|
|
|
|
2007-04-27 23:05:48 +00:00
|
|
|
function mail_type($user, $type) {
|
|
|
|
global $globals;
|
|
|
|
global $email_files;
|
|
|
|
|
2007-04-11 18:05:26 +00:00
|
|
|
$email_file = $email_files[$type];
|
2006-07-13 23:40:10 +00:00
|
|
|
if ($email_file['html']) {
|
|
|
|
$html = replace($user, $email_file['html']);
|
|
|
|
} else {
|
|
|
|
$html = null;
|
|
|
|
}
|
|
|
|
$text = replace($user, $email_file['text']);
|
2007-04-27 23:05:48 +00:00
|
|
|
if ($globals->show_email) {
|
|
|
|
echo "------- SUBJECT ----------\n";
|
|
|
|
echo $email_file['subject'];
|
|
|
|
echo "\n------- HTML ----------\n";
|
|
|
|
echo $html;
|
|
|
|
echo "\n------- TEXT ----------\n";
|
|
|
|
echo $text;
|
|
|
|
}
|
|
|
|
if ($globals->send) {
|
|
|
|
echo "sending to $user->email_addr\n";
|
|
|
|
echo send_email(
|
2006-07-13 23:40:10 +00:00
|
|
|
$user,
|
|
|
|
$email_file['subject'],
|
|
|
|
$text,
|
|
|
|
$html
|
|
|
|
);
|
|
|
|
$now = time();
|
2007-04-11 18:05:26 +00:00
|
|
|
$ntype = 0;
|
2007-04-19 17:06:02 +00:00
|
|
|
if ($type == 'lapsed') $ntype = 2;
|
|
|
|
if ($type == 'failed') $ntype = 3;
|
2007-04-11 18:05:26 +00:00
|
|
|
$query = "insert into sent_email values($user->id, $now, $ntype)";
|
|
|
|
mysql_query($query);
|
2006-07-13 23:40:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-11 18:05:26 +00:00
|
|
|
function last_reminder_time($user) {
|
2007-04-19 17:06:02 +00:00
|
|
|
$query = "select * from sent_email where userid=$user->id";
|
2007-04-11 18:05:26 +00:00
|
|
|
$result = mysql_query($query);
|
|
|
|
$t = 0;
|
|
|
|
while ($r = mysql_fetch_object($result)) {
|
|
|
|
if ($r->email_type !=2 && $r->email_type != 3) continue;
|
2007-04-19 17:06:02 +00:00
|
|
|
if ($r->time_sent > $t) $t = $r->time_sent;
|
|
|
|
|
2007-04-11 18:05:26 +00:00
|
|
|
}
|
|
|
|
mysql_free_result($result);
|
|
|
|
return $t;
|
|
|
|
}
|
|
|
|
|
2007-04-27 23:05:48 +00:00
|
|
|
function handle_user($user, $do_type) {
|
|
|
|
global $globals;
|
2006-07-13 23:40:10 +00:00
|
|
|
global $email_interval;
|
|
|
|
|
2007-04-27 23:05:48 +00:00
|
|
|
if ($user->send_email == 0) {
|
|
|
|
if ($globals->explain) {
|
|
|
|
echo "user: $user->id send_email = 0\n";
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2006-07-13 23:40:10 +00:00
|
|
|
$max_email_time = time() - $email_interval;
|
2007-04-27 23:05:48 +00:00
|
|
|
if (last_reminder_time($user) > $max_email_time) {
|
|
|
|
if ($globals->explain) {
|
|
|
|
echo "user: $user->id sent too recently\n";
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($globals->explain) {
|
|
|
|
$x = (time() - $user->create_time)/86400;
|
|
|
|
$t = last_rpc_time($user);
|
|
|
|
$show_lapsed_interval = (time()-$t)/86400;
|
|
|
|
echo "user $user->id ($user->email_addr) was created $x days ago\n";
|
|
|
|
echo " total_credit: $user->total_credit; last RPC $show_lapsed_interval days ago\n";
|
|
|
|
echo " sending $do_type email\n";
|
|
|
|
}
|
|
|
|
mail_type($user, $do_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
function do_failed() {
|
|
|
|
global $globals;
|
|
|
|
|
|
|
|
$max_create_time = time() - $globals->start_interval;
|
|
|
|
$result = mysql_query(
|
|
|
|
"select * from user where send_email<>0 and create_time<$max_create_time and total_credit = 0;"
|
|
|
|
);
|
|
|
|
while ($user = mysql_fetch_object($result)) {
|
|
|
|
handle_user($user, 'failed');
|
|
|
|
}
|
|
|
|
mysql_free_result($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
function do_lapsed() {
|
|
|
|
global $globals;
|
|
|
|
$max_last_rpc_time = time() - $globals->lapsed_interval;
|
|
|
|
|
|
|
|
// the following is an efficient way of getting the list of
|
|
|
|
// users for which no host has done an RPC recently
|
|
|
|
//
|
|
|
|
$result = mysql_query(
|
|
|
|
"select userid from host group by userid having max(rpc_time)<$max_last_rpc_time;"
|
|
|
|
);
|
|
|
|
while ($host = mysql_fetch_object($result)) {
|
|
|
|
$uresult = mysql_query("select * from user where id = $host->userid;");
|
|
|
|
$user = mysql_fetch_object($uresult);
|
|
|
|
mysql_free_result($uresult);
|
|
|
|
if (!$user) {
|
|
|
|
echo "Can't find user $host->userid\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
handle_user($user, 'lapsed');
|
2006-07-13 23:40:10 +00:00
|
|
|
}
|
2007-04-27 23:05:48 +00:00
|
|
|
mysql_free_result($result);
|
2006-07-13 23:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$USE_PHPMAILER) {
|
2007-04-27 23:05:48 +00:00
|
|
|
echo "You must use PHPMailer (http://phpmailer.sourceforge.net)\n";
|
2006-07-13 23:40:10 +00:00
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
$email_files = read_email_files();
|
|
|
|
|
2007-04-27 23:05:48 +00:00
|
|
|
if ($globals->userid) {
|
|
|
|
$user = lookup_user_id($globals->userid);
|
2006-07-13 23:40:10 +00:00
|
|
|
if (!$user) {
|
2007-04-27 23:05:48 +00:00
|
|
|
echo "No such user: $globals->userid\n";
|
2006-07-13 23:40:10 +00:00
|
|
|
exit();
|
|
|
|
}
|
2007-04-27 23:05:48 +00:00
|
|
|
$user->last_rpc_time = last_rpc_time($user);
|
|
|
|
mail_type($user, 'failed');
|
|
|
|
mail_type($user, 'lapsed');
|
2006-07-13 23:40:10 +00:00
|
|
|
} else {
|
2007-04-27 23:05:48 +00:00
|
|
|
if ($do_failed) {
|
|
|
|
do_failed();
|
|
|
|
}
|
|
|
|
if ($do_lapsed) {
|
|
|
|
do_lapsed();
|
|
|
|
}
|
2006-07-13 23:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|