resend_confirm_address.php 2.7 KB

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