PageRenderTime 61ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/src/boinc-7.0.36/html/ops/mass_email_script.php

https://github.com/matszpk/native-boinc-for-android
PHP | 329 lines | 263 code | 27 blank | 39 comment | 48 complexity | d1a51afd3a54c13ba784c7123a792e48 MD5 | raw file
  1. #! /usr/bin/env php
  2. <?php
  3. // This file is part of BOINC.
  4. // http://boinc.berkeley.edu
  5. // Copyright (C) 2008 University of California
  6. //
  7. // BOINC is free software; you can redistribute it and/or modify it
  8. // under the terms of the GNU Lesser General Public License
  9. // as published by the Free Software Foundation,
  10. // either version 3 of the License, or (at your option) any later version.
  11. //
  12. // BOINC is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. // See the GNU Lesser General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU Lesser General Public License
  18. // along with BOINC. If not, see <http://www.gnu.org/licenses/>.
  19. // mass_email_script [--userid N] [--send] [--idfile X] [--batch N]
  20. //
  21. // send mass email. Options:
  22. // --userid send only to the given user
  23. // --send actually send email
  24. // --show_email show what would be sent rather than sending it
  25. // --explain show what we're doing
  26. // --idfile read user ID from the given file; otherwise send to everyone.
  27. // The IDs must be in increasing order
  28. // --nocurrent Don't send to current users
  29. // --batch N Do batches of N (default 1000)
  30. //
  31. // NOTE: a file "email_log" is used for checkpoint/restart.
  32. // It stores a list of user IDs sent to.
  33. // You must create this file (use touch) before starting
  34. // (this is to prevent you from accidentally running this
  35. // in the wrong directory and re-mailing a lot of people)
  36. //
  37. // see http://boinc.berkeley.edu/mass_email.php for info
  38. $cli_only = true;
  39. require_once('../project/project.inc');
  40. require_once('../inc/email.inc');
  41. require_once('../inc/db.inc');
  42. require_once('../inc/util_ops.inc');
  43. db_init();
  44. set_time_limit(0);
  45. $globals->send = false;
  46. $globals->explain = false;
  47. $globals->userid = 0;
  48. $globals->nocurrent = false;
  49. $globals->idfile = null;
  50. $globals->batch = 1000;
  51. $globals->lapsed_interval = 60*86400;
  52. for ($i=1; $i<$argc; $i++) {
  53. if ($argv[$i] == "--batch") {
  54. $i++;
  55. $globals->batch = $argv[$i];
  56. } elseif ($argv[$i] == "--show_email") {
  57. $globals->show_email = true;
  58. } elseif ($argv[$i] == "--explain") {
  59. $globals->explain = true;
  60. } elseif ($argv[$i] == "--send") {
  61. $globals->send = true;
  62. } elseif ($argv[$i] == "--idfile") {
  63. $i++;
  64. $globals->idfile = $argv[$i];
  65. } elseif ($argv[$i] == "--userid") {
  66. $i++;
  67. $globals->userid = $argv[$i];
  68. } else {
  69. echo "unrecognized option $argv[$i]\n";
  70. echo "usage: mass_email_script.php [--userid N] [--show_mail] [--explain] [--send]\n";
  71. exit (1);
  72. }
  73. }
  74. $mass_email_log = 'email_log';
  75. // File names for the various mail types.
  76. // Change these here if you like.
  77. $email_failed_html = 'newsletter/failed_html';
  78. $email_failed_text = 'newsletter/failed_text';
  79. $email_failed_subject = 'newsletter/failed_subject';
  80. $email_lapsed_html = 'newsletter/lapsed_html';
  81. $email_lapsed_text = 'newsletter/lapsed_text';
  82. $email_lapsed_subject = 'newsletter/lapsed_subject';
  83. $email_current_html = 'newsletter/current_html';
  84. $email_current_text = 'newsletter/current_text';
  85. $email_current_subject = 'newsletter/current_subject';
  86. function read_files(&$item) {
  87. $item['html'] = file_get_contents($item['html_file']);
  88. if (!$item['html']) {
  89. $x = $item['html_file'];
  90. echo "file missing: $x\n";
  91. exit();
  92. }
  93. $item['text'] = file_get_contents($item['text_file']);
  94. if (!$item['text']) {
  95. $x = $item['text_file'];
  96. echo "file missing: $x\n";
  97. exit();
  98. }
  99. $item['subject'] = file_get_contents($item['subject']);
  100. if (!$item['subject']) {
  101. $x = $item['subject'];
  102. echo "file missing: $x\n";
  103. exit();
  104. }
  105. }
  106. function read_email_files() {
  107. global $globals;
  108. global $email_failed_html;
  109. global $email_failed_text;
  110. global $email_failed_subject;
  111. global $email_lapsed_html;
  112. global $email_lapsed_text;
  113. global $email_lapsed_subject;
  114. global $email_current_html;
  115. global $email_current_text;
  116. global $email_current_subject;
  117. $failed['html_file'] = $email_failed_html;
  118. $failed['text_file'] = $email_failed_text;
  119. $failed['subject'] = $email_failed_subject;
  120. $lapsed['html_file'] = $email_lapsed_html;
  121. $lapsed['text_file'] = $email_lapsed_text;
  122. $lapsed['subject'] = $email_lapsed_subject;
  123. $current['html_file'] = $email_current_html;
  124. $current['text_file'] = $email_current_text;
  125. $current['subject'] = $email_current_subject;
  126. read_files($failed);
  127. read_files($lapsed);
  128. if (!$globals->nocurrent) {
  129. read_files($current);
  130. }
  131. $email_files['failed'] = $failed;
  132. $email_files['lapsed'] = $lapsed;
  133. $email_files['current'] = $current;
  134. return $email_files;
  135. }
  136. function last_rpc_time($user) {
  137. $x = 0;
  138. $result = mysql_query("select rpc_time from host where userid=$user->id");
  139. while ($host = mysql_fetch_object($result)) {
  140. if ($host->rpc_time > $x) $x = $host->rpc_time;
  141. }
  142. mysql_free_result($result);
  143. return $x;
  144. }
  145. function replace($user, $template) {
  146. $pat = array(
  147. '/<name\/>/',
  148. '/<create_time\/>/',
  149. '/<total_credit\/>/',
  150. '/<opt_out_url\/>/',
  151. '/<lapsed_interval\/>/',
  152. );
  153. $rep = array(
  154. $user->name,
  155. gmdate('d F Y', $user->create_time),
  156. number_format($user->total_credit, 0),
  157. opt_out_url($user),
  158. floor((time() - $user->last_rpc_time) / 86400),
  159. );
  160. return preg_replace($pat, $rep, $template);
  161. }
  162. function mail_type($user, $email_file) {
  163. global $globals;
  164. $html = replace($user, $email_file['html']);
  165. $text = replace($user, $email_file['text']);
  166. if ($globals->show_email) {
  167. echo "\nSending to $user->email_addr:\n";
  168. echo "------- SUBJECT ----------\n";
  169. echo $email_file['subject'];
  170. echo "\n------- HTML ----------\n";
  171. echo $html;
  172. echo "\n------- TEXT ----------\n";
  173. echo $text;
  174. }
  175. if ($globals->send) {
  176. if (is_valid_email_addr($user->email_addr)) {
  177. send_email(
  178. $user,
  179. $email_file['subject'],
  180. $text,
  181. $html
  182. );
  183. } else {
  184. if ($globals->explain) {
  185. echo "invalid e-mail address\n";
  186. }
  187. }
  188. }
  189. }
  190. function handle_user($user) {
  191. global $email_files;
  192. global $globals;
  193. $user->last_rpc_time = last_rpc_time($user);
  194. $lapsed = time() - $user->last_rpc_time > $globals->lapsed_interval;
  195. if ($user->total_credit == 0) {
  196. mail_type($user, $email_files['failed']);
  197. if ($globals->explain) {
  198. echo "sending failed email to $user->email_addr\n";
  199. }
  200. } else if ($lapsed) {
  201. mail_type($user, $email_files['lapsed']);
  202. if ($globals->explain) {
  203. echo "sending lapsed email to $user->email_addr\n";
  204. }
  205. } else {
  206. if (!$globals->nocurrent) {
  207. mail_type($user, $email_files['current']);
  208. if ($globals->explain) {
  209. echo "sending current email to $user->email_addr\n";
  210. }
  211. }
  212. }
  213. }
  214. function do_batch($startid, $n, $log) {
  215. $result = mysql_query(
  216. "select * from user where id>$startid order by id limit $n"
  217. );
  218. while ($user = mysql_fetch_object($result)) {
  219. handle_user($user);
  220. $startid = $user->id;
  221. fputs($log, $user->id . "\n");
  222. fflush($log);
  223. }
  224. mysql_free_result($result);
  225. return $startid;
  226. }
  227. function do_one($thisid, $log) {
  228. $result = mysql_query(
  229. "select * from user where id=$thisid"
  230. );
  231. $user = mysql_fetch_object($result);
  232. if ($user) {
  233. handle_user($user);
  234. fputs($log, $user->id . "\n");
  235. fflush($log);
  236. }
  237. mysql_free_result($result);
  238. return $startid;
  239. }
  240. function read_log() {
  241. global $mass_email_log;
  242. $f = fopen($mass_email_log, 'r');
  243. if (!$f) {
  244. echo "$mass_email_log not found - create empty file and run again\n";
  245. exit();
  246. }
  247. $startid = 0;
  248. while (fscanf($f, '%d', &$startid)) {
  249. // read to the last entry in the file
  250. }
  251. fclose($f);
  252. return $startid;
  253. }
  254. function main() {
  255. global $globals;
  256. global $id_file;
  257. global $mass_email_log;
  258. $startid = read_log();
  259. $f = fopen($mass_email_log, 'a');
  260. if ($id_file == "") {
  261. while (1) {
  262. $new_startid = do_batch($startid, $globals->batch, $f);
  263. if ($new_startid == $startid) break;
  264. $startid = $new_startid;
  265. }
  266. } else {
  267. $fid = fopen($id_file, 'r');
  268. if (!$fid) {
  269. echo $id_file . ' not found - create ID list and run again\n';
  270. exit();
  271. }
  272. $thisid = 0;
  273. while (fscanf($fid, '%d', &$thisid)) {
  274. if ($thisid > $startid) {
  275. do_one($thisid, $f);
  276. }
  277. }
  278. fclose($fid);
  279. }
  280. echo 'All done!' . "\n";
  281. }
  282. if (!$USE_PHPMAILER) {
  283. echo "You must use PHPMailer.\n";
  284. exit();
  285. }
  286. $email_files = read_email_files();
  287. if ($globals->userid) {
  288. $user = lookup_user_id($globals->userid);
  289. if (!$user) {
  290. echo "no such user\n";
  291. } else {
  292. $user->last_rpc_time = last_rpc_time($user);
  293. mail_type($user, $email_files['failed']);
  294. mail_type($user, $email_files['lapsed']);
  295. if (!$globals->nocurrent) {
  296. mail_type($user, $email_files['current']);
  297. }
  298. }
  299. } else {
  300. main();
  301. }
  302. ?>