2006-10-23 22:05:56 +00:00
|
|
|
#! /usr/bin/env php
|
|
|
|
|
2006-07-05 21:38:08 +00:00
|
|
|
<?php
|
2008-08-05 22:43:14 +00:00
|
|
|
// This file is part of BOINC.
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2008 University of California
|
|
|
|
//
|
|
|
|
// BOINC is free software; you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU Lesser General Public License
|
|
|
|
// as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2006-07-05 21:38:08 +00:00
|
|
|
|
2007-04-27 23:05:48 +00:00
|
|
|
// mass_email_script [--userid N] [--send] [--idfile X] [--batch N]
|
|
|
|
//
|
|
|
|
// send mass email. Options:
|
|
|
|
// --userid send only to the given user
|
|
|
|
// --send actually send email
|
|
|
|
// --show_email show what would be sent rather than sending it
|
|
|
|
// --explain show what we're doing
|
|
|
|
// --idfile read user ID from the given file; otherwise send to everyone.
|
|
|
|
// The IDs must be in increasing order
|
|
|
|
// --nocurrent Don't send to current users
|
|
|
|
// --batch N Do batches of N (default 1000)
|
|
|
|
//
|
|
|
|
// NOTE: a file "email_log" is used for checkpoint/restart.
|
|
|
|
// It stores a list of user IDs sent to.
|
|
|
|
// You must create this file (use touch) before starting
|
|
|
|
// (this is to prevent you from accidentally running this
|
|
|
|
// in the wrong directory and re-mailing a lot of people)
|
2006-07-05 21:38:08 +00:00
|
|
|
//
|
|
|
|
// see http://boinc.berkeley.edu/mass_email.php for info
|
|
|
|
|
2009-10-02 18:32:40 +00:00
|
|
|
$cli_only = true;
|
2006-07-05 21:38:08 +00:00
|
|
|
require_once('../project/project.inc');
|
|
|
|
require_once('../inc/email.inc');
|
|
|
|
require_once('../inc/db.inc');
|
2009-09-15 18:14:37 +00:00
|
|
|
require_once('../inc/util_ops.inc');
|
2006-07-05 21:38:08 +00:00
|
|
|
|
|
|
|
db_init();
|
|
|
|
set_time_limit(0);
|
|
|
|
|
2007-04-27 23:05:48 +00:00
|
|
|
$globals->send = false;
|
|
|
|
$globals->explain = false;
|
|
|
|
$globals->userid = 0;
|
|
|
|
$globals->nocurrent = false;
|
|
|
|
$globals->idfile = null;
|
|
|
|
$globals->batch = 1000;
|
|
|
|
$globals->lapsed_interval = 60*86400;
|
2006-10-23 22:05:56 +00:00
|
|
|
|
2007-04-27 23:05:48 +00:00
|
|
|
for ($i=1; $i<$argc; $i++) {
|
|
|
|
if ($argv[$i] == "--batch") {
|
|
|
|
$i++;
|
|
|
|
$globals->batch = $argv[$i];
|
|
|
|
} 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] == "--idfile") {
|
|
|
|
$i++;
|
|
|
|
$globals->idfile = $argv[$i];
|
|
|
|
} elseif ($argv[$i] == "--userid") {
|
|
|
|
$i++;
|
|
|
|
$globals->userid = $argv[$i];
|
|
|
|
} else {
|
|
|
|
echo "unrecognized option $argv[$i]\n";
|
|
|
|
echo "usage: mass_email_script.php [--userid N] [--show_mail] [--explain] [--send]\n";
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
2006-10-23 22:05:56 +00:00
|
|
|
|
2007-04-27 23:05:48 +00:00
|
|
|
$mass_email_log = 'email_log';
|
2006-10-23 22:05:56 +00:00
|
|
|
|
|
|
|
// File names for the various mail types.
|
|
|
|
// Change these here if you like.
|
|
|
|
|
2007-04-27 23:05:48 +00:00
|
|
|
$email_failed_html = 'newsletter/failed_html';
|
|
|
|
$email_failed_text = 'newsletter/failed_text';
|
|
|
|
$email_failed_subject = 'newsletter/failed_subject';
|
|
|
|
$email_lapsed_html = 'newsletter/lapsed_html';
|
|
|
|
$email_lapsed_text = 'newsletter/lapsed_text';
|
|
|
|
$email_lapsed_subject = 'newsletter/lapsed_subject';
|
|
|
|
$email_current_html = 'newsletter/current_html';
|
|
|
|
$email_current_text = 'newsletter/current_text';
|
|
|
|
$email_current_subject = 'newsletter/current_subject';
|
2006-07-20 20:27:21 +00:00
|
|
|
|
2006-07-05 21:38:08 +00:00
|
|
|
function read_files(&$item) {
|
|
|
|
$item['html'] = file_get_contents($item['html_file']);
|
|
|
|
if (!$item['html']) {
|
2006-10-23 22:05:56 +00:00
|
|
|
$x = $item['html_file'];
|
|
|
|
echo "file missing: $x\n";
|
|
|
|
exit();
|
2006-07-05 21:38:08 +00:00
|
|
|
}
|
|
|
|
$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() {
|
2007-04-27 23:05:48 +00:00
|
|
|
global $globals;
|
2006-07-05 21:38:08 +00:00
|
|
|
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);
|
2007-04-27 23:05:48 +00:00
|
|
|
if (!$globals->nocurrent) {
|
|
|
|
read_files($current);
|
|
|
|
}
|
2006-07-05 21:38:08 +00:00
|
|
|
$email_files['failed'] = $failed;
|
|
|
|
$email_files['lapsed'] = $lapsed;
|
|
|
|
$email_files['current'] = $current;
|
|
|
|
return $email_files;
|
|
|
|
}
|
|
|
|
|
2007-04-27 23:05:48 +00:00
|
|
|
function last_rpc_time($user) {
|
|
|
|
$x = 0;
|
2014-09-04 19:00:09 +00:00
|
|
|
$result = _mysql_query("select rpc_time from host where userid=$user->id");
|
|
|
|
while ($host = _mysql_fetch_object($result)) {
|
2007-04-27 23:05:48 +00:00
|
|
|
if ($host->rpc_time > $x) $x = $host->rpc_time;
|
|
|
|
}
|
2014-09-04 19:00:09 +00:00
|
|
|
_mysql_free_result($result);
|
2007-04-27 23:05:48 +00:00
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
|
2006-07-05 21:38:08 +00:00
|
|
|
function replace($user, $template) {
|
|
|
|
$pat = array(
|
|
|
|
'/<name\/>/',
|
|
|
|
'/<create_time\/>/',
|
|
|
|
'/<total_credit\/>/',
|
|
|
|
'/<opt_out_url\/>/',
|
2006-07-20 20:27:21 +00:00
|
|
|
'/<lapsed_interval\/>/',
|
2006-07-05 21:38:08 +00:00
|
|
|
);
|
|
|
|
$rep = array(
|
|
|
|
$user->name,
|
|
|
|
gmdate('d F Y', $user->create_time),
|
|
|
|
number_format($user->total_credit, 0),
|
2010-02-03 17:28:03 +00:00
|
|
|
opt_out_url($user),
|
2007-04-27 23:05:48 +00:00
|
|
|
floor((time() - $user->last_rpc_time) / 86400),
|
2006-07-05 21:38:08 +00:00
|
|
|
);
|
|
|
|
return preg_replace($pat, $rep, $template);
|
|
|
|
}
|
|
|
|
|
|
|
|
function mail_type($user, $email_file) {
|
2007-04-27 23:05:48 +00:00
|
|
|
global $globals;
|
|
|
|
|
2006-10-23 22:05:56 +00:00
|
|
|
$html = replace($user, $email_file['html']);
|
2006-07-05 21:38:08 +00:00
|
|
|
$text = replace($user, $email_file['text']);
|
2007-04-27 23:05:48 +00:00
|
|
|
if ($globals->show_email) {
|
2006-07-05 21:38:08 +00:00
|
|
|
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;
|
2007-04-27 23:05:48 +00:00
|
|
|
}
|
|
|
|
if ($globals->send) {
|
2024-08-02 04:10:21 +00:00
|
|
|
if (is_valid_email_syntax($user->email_addr)) {
|
2007-04-27 23:05:48 +00:00
|
|
|
send_email(
|
|
|
|
$user,
|
|
|
|
$email_file['subject'],
|
|
|
|
$text,
|
|
|
|
$html
|
|
|
|
);
|
2006-10-23 22:05:56 +00:00
|
|
|
} else {
|
2007-04-27 23:05:48 +00:00
|
|
|
if ($globals->explain) {
|
|
|
|
echo "invalid e-mail address\n";
|
|
|
|
}
|
2006-10-23 22:05:56 +00:00
|
|
|
}
|
2006-07-05 21:38:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-27 23:05:48 +00:00
|
|
|
function handle_user($user) {
|
|
|
|
global $email_files;
|
|
|
|
global $globals;
|
|
|
|
|
|
|
|
$user->last_rpc_time = last_rpc_time($user);
|
|
|
|
$lapsed = time() - $user->last_rpc_time > $globals->lapsed_interval;
|
|
|
|
|
2006-07-05 21:38:08 +00:00
|
|
|
if ($user->total_credit == 0) {
|
|
|
|
mail_type($user, $email_files['failed']);
|
2007-04-27 23:05:48 +00:00
|
|
|
if ($globals->explain) {
|
|
|
|
echo "sending failed email to $user->email_addr\n";
|
|
|
|
}
|
|
|
|
} else if ($lapsed) {
|
2006-07-05 21:38:08 +00:00
|
|
|
mail_type($user, $email_files['lapsed']);
|
2007-04-27 23:05:48 +00:00
|
|
|
if ($globals->explain) {
|
|
|
|
echo "sending lapsed email to $user->email_addr\n";
|
|
|
|
}
|
2006-07-05 21:38:08 +00:00
|
|
|
} else {
|
2007-04-27 23:05:48 +00:00
|
|
|
if (!$globals->nocurrent) {
|
|
|
|
mail_type($user, $email_files['current']);
|
|
|
|
if ($globals->explain) {
|
|
|
|
echo "sending current email to $user->email_addr\n";
|
|
|
|
}
|
2006-10-23 22:05:56 +00:00
|
|
|
}
|
2006-07-05 21:38:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-27 23:05:48 +00:00
|
|
|
function do_batch($startid, $n, $log) {
|
2014-09-04 19:00:09 +00:00
|
|
|
$result = _mysql_query(
|
2007-04-27 23:05:48 +00:00
|
|
|
"select * from user where id>$startid order by id limit $n"
|
2006-07-05 21:38:08 +00:00
|
|
|
);
|
2014-09-04 19:00:09 +00:00
|
|
|
while ($user = _mysql_fetch_object($result)) {
|
2007-04-27 23:05:48 +00:00
|
|
|
handle_user($user);
|
2006-07-05 21:38:08 +00:00
|
|
|
$startid = $user->id;
|
2006-10-23 22:05:56 +00:00
|
|
|
fputs($log, $user->id . "\n");
|
|
|
|
fflush($log);
|
|
|
|
}
|
2014-09-04 19:00:09 +00:00
|
|
|
_mysql_free_result($result);
|
2006-10-23 22:05:56 +00:00
|
|
|
return $startid;
|
|
|
|
}
|
|
|
|
|
2007-04-27 23:05:48 +00:00
|
|
|
function do_one($thisid, $log) {
|
2014-09-04 19:00:09 +00:00
|
|
|
$result = _mysql_query(
|
2006-10-23 22:05:56 +00:00
|
|
|
"select * from user where id=$thisid"
|
|
|
|
);
|
2014-09-04 19:00:09 +00:00
|
|
|
$user = _mysql_fetch_object($result);
|
2007-04-27 23:05:48 +00:00
|
|
|
if ($user) {
|
|
|
|
handle_user($user);
|
2006-10-23 22:05:56 +00:00
|
|
|
fputs($log, $user->id . "\n");
|
2006-07-05 21:38:08 +00:00
|
|
|
fflush($log);
|
|
|
|
}
|
2014-09-04 19:00:09 +00:00
|
|
|
_mysql_free_result($result);
|
2006-07-05 21:38:08 +00:00
|
|
|
return $startid;
|
|
|
|
}
|
|
|
|
|
|
|
|
function read_log() {
|
2006-10-23 22:05:56 +00:00
|
|
|
global $mass_email_log;
|
|
|
|
$f = fopen($mass_email_log, 'r');
|
2006-07-05 21:38:08 +00:00
|
|
|
if (!$f) {
|
2007-04-27 23:05:48 +00:00
|
|
|
echo "$mass_email_log not found - create empty file and run again\n";
|
2006-07-05 21:38:08 +00:00
|
|
|
exit();
|
|
|
|
}
|
|
|
|
$startid = 0;
|
2019-01-19 22:59:41 +00:00
|
|
|
while (fscanf($f, '%d', $startid)) {
|
2007-04-27 23:05:48 +00:00
|
|
|
// read to the last entry in the file
|
2006-07-05 21:38:08 +00:00
|
|
|
}
|
|
|
|
fclose($f);
|
|
|
|
return $startid;
|
|
|
|
}
|
|
|
|
|
2007-04-27 23:05:48 +00:00
|
|
|
function main() {
|
|
|
|
global $globals;
|
2006-10-23 22:05:56 +00:00
|
|
|
global $id_file;
|
|
|
|
global $mass_email_log;
|
2006-07-05 21:38:08 +00:00
|
|
|
$startid = read_log();
|
2006-10-23 22:05:56 +00:00
|
|
|
$f = fopen($mass_email_log, 'a');
|
|
|
|
if ($id_file == "") {
|
2007-04-19 22:11:25 +00:00
|
|
|
while (1) {
|
2007-04-27 23:05:48 +00:00
|
|
|
$new_startid = do_batch($startid, $globals->batch, $f);
|
2007-04-19 22:11:25 +00:00
|
|
|
if ($new_startid == $startid) break;
|
|
|
|
$startid = $new_startid;
|
|
|
|
}
|
2006-10-23 22:05:56 +00:00
|
|
|
} else {
|
2019-01-19 22:59:41 +00:00
|
|
|
$fid = fopen($id_file, 'r');
|
2007-04-19 22:11:25 +00:00
|
|
|
if (!$fid) {
|
2007-04-27 23:05:48 +00:00
|
|
|
echo $id_file . ' not found - create ID list and run again\n';
|
2007-04-19 22:11:25 +00:00
|
|
|
exit();
|
|
|
|
}
|
|
|
|
$thisid = 0;
|
2019-01-19 22:59:41 +00:00
|
|
|
while (fscanf($fid, '%d', $thisid)) {
|
2007-04-19 22:11:25 +00:00
|
|
|
if ($thisid > $startid) {
|
2007-04-27 23:05:48 +00:00
|
|
|
do_one($thisid, $f);
|
2007-04-19 22:11:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose($fid);
|
2006-07-05 21:38:08 +00:00
|
|
|
}
|
2006-10-23 22:05:56 +00:00
|
|
|
echo 'All done!' . "\n";
|
2006-07-05 21:38:08 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 22:57:52 +00:00
|
|
|
if (!function_exists('make_php_mailer')) {
|
2006-07-05 21:38:08 +00:00
|
|
|
echo "You must use PHPMailer.\n";
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
$email_files = read_email_files();
|
|
|
|
|
2007-04-27 23:05:48 +00:00
|
|
|
if ($globals->userid) {
|
2014-04-24 16:41:59 +00:00
|
|
|
$user = BoincUser::lookup_id($globals->userid);
|
2007-04-27 23:05:48 +00:00
|
|
|
if (!$user) {
|
|
|
|
echo "no such user\n";
|
|
|
|
} else {
|
|
|
|
$user->last_rpc_time = last_rpc_time($user);
|
|
|
|
mail_type($user, $email_files['failed']);
|
|
|
|
mail_type($user, $email_files['lapsed']);
|
|
|
|
if (!$globals->nocurrent) {
|
|
|
|
mail_type($user, $email_files['current']);
|
|
|
|
}
|
|
|
|
}
|
2006-07-05 21:38:08 +00:00
|
|
|
} else {
|
2007-04-27 23:05:48 +00:00
|
|
|
main();
|
2006-07-05 21:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|