resend_confirm_address.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env php
  2. <?php
  3. define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
  4. $shortoptions = 'e::ay';
  5. $longoptions = array('email=', 'all', 'yes');
  6. $self = basename($_SERVER['PHP_SELF']);
  7. $helptext = <<<END_OF_HELP
  8. {$self} [options]
  9. resends confirmation email either for a specific email (if found) or for
  10. all lingering confirmations.
  11. NOTE: You probably want to do something like this to your database first so
  12. only relatively fresh accounts get resent this:
  13. DELETE FROM confirm_address WHERE modified < DATE_SUB(NOW(), INTERVAL 1 month);
  14. Options:
  15. -e --email e-mail address to send for
  16. -a --all send for all emails in confirm_address table
  17. -y --yes skip interactive verification
  18. END_OF_HELP;
  19. require_once INSTALLDIR.'/scripts/commandline.inc';
  20. $all = false;
  21. $ca = null;
  22. if (have_option('e', 'email')) {
  23. $email = get_option_value('e', 'email');
  24. $ca = Confirm_address::getAddress($email, 'email');
  25. if (!$ca instanceof Confirm_address) {
  26. print "Can't find email $email in confirm_address table.\n";
  27. exit(1);
  28. }
  29. } elseif (have_option('a', 'all')) {
  30. $all = true;
  31. $ca = new Confirm_address();
  32. $ca->address_type = 'email';
  33. if (!$ca->find()) {
  34. print "confirm_address table contains no lingering email addresses\n";
  35. exit(0);
  36. }
  37. } else {
  38. print "You must provide an email (or --all).\n";
  39. exit(1);
  40. }
  41. if (!have_option('y', 'yes')) {
  42. print "About to resend confirm_address email to {$ca->N} recipients. Are you sure? [y/N] ";
  43. $response = fgets(STDIN);
  44. if (strtolower(trim($response)) != 'y') {
  45. print "Aborting.\n";
  46. exit(0);
  47. }
  48. }
  49. function mailConfirmAddress(Confirm_address $ca)
  50. {
  51. try {
  52. $user = User::getByID($ca->user_id);
  53. $profile = $user->getProfile();
  54. if ($profile->isSilenced()) {
  55. $ca->delete();
  56. return;
  57. }
  58. if ($user->email === $ca->address) {
  59. throw new AlreadyFulfilledException('User already has identical confirmed email address.');
  60. }
  61. } catch (AlreadyFulfilledException $e) {
  62. print "\n User already had verified email: "._ve($ca->address);
  63. $ca->delete();
  64. } catch (Exception $e) {
  65. print "\n Failed to get user with ID "._ve($user_id).', deleting confirm_address entry: '._ve($e->getMessage());
  66. $ca->delete();
  67. return;
  68. }
  69. mail_confirm_address($user, $ca->code, $user->getNickname(), $ca->address);
  70. }
  71. require_once(INSTALLDIR . '/lib/mail.php');
  72. if (!$all) {
  73. mailConfirmAddress($ca);
  74. } else {
  75. while ($ca->fetch()) {
  76. mailConfirmAddress($ca);
  77. }
  78. }
  79. print "\nDONE.\n";