resend_confirm_address.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. try {
  25. $ca = Confirm_address::getByAddress($email, 'email');
  26. } catch (NoResultException $e) {
  27. print sprintf("Can't find %s address %s in %s table.\n", $e->obj->address_type, $e->obj->address, $e->obj->tableName());
  28. exit(1);
  29. }
  30. } elseif (have_option('a', 'all')) {
  31. $all = true;
  32. $ca = new Confirm_address();
  33. $ca->address_type = 'email';
  34. if (!$ca->find()) {
  35. print "confirm_address table contains no lingering email addresses\n";
  36. exit(0);
  37. }
  38. } else {
  39. print "You must provide an email (or --all).\n";
  40. exit(1);
  41. }
  42. if (!have_option('y', 'yes')) {
  43. print "About to resend confirm_address email to {$ca->N} recipients. Are you sure? [y/N] ";
  44. $response = fgets(STDIN);
  45. if (strtolower(trim($response)) != 'y') {
  46. print "Aborting.\n";
  47. exit(0);
  48. }
  49. }
  50. function mailConfirmAddress(Confirm_address $ca)
  51. {
  52. try {
  53. $user = User::getByID($ca->user_id);
  54. $profile = $user->getProfile();
  55. if ($profile->isSilenced()) {
  56. $ca->delete();
  57. return;
  58. }
  59. if ($user->email === $ca->address) {
  60. throw new AlreadyFulfilledException('User already has identical confirmed email address.');
  61. }
  62. } catch (AlreadyFulfilledException $e) {
  63. print "\n User already had verified email: "._ve($ca->address);
  64. $ca->delete();
  65. } catch (Exception $e) {
  66. print "\n Failed to get user with ID "._ve($user_id).', deleting confirm_address entry: '._ve($e->getMessage());
  67. $ca->delete();
  68. return;
  69. }
  70. mail_confirm_address($user, $ca->code, $user->getNickname(), $ca->address);
  71. }
  72. require_once(INSTALLDIR . '/lib/mail.php');
  73. if (!$all) {
  74. mailConfirmAddress($ca);
  75. } else {
  76. while ($ca->fetch()) {
  77. mailConfirmAddress($ca);
  78. }
  79. }
  80. print "\nDONE.\n";