resend_confirm_address.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env php
  2. <?php
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. define('INSTALLDIR', dirname(__DIR__));
  18. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  19. $shortoptions = 'e::ay';
  20. $longoptions = ['email=', 'all', 'yes'];
  21. $self = basename($_SERVER['PHP_SELF']);
  22. $helptext = <<<END_OF_HELP
  23. {$self} [options]
  24. resends confirmation email either for a specific email (if found) or for
  25. all lingering confirmations.
  26. NOTE: You probably want to do something like this to your database first so
  27. only relatively fresh accounts get resent this:
  28. DELETE FROM confirm_address WHERE modified < (CURRENT_TIMESTAMP - INTERVAL '1' MONTH);
  29. Options:
  30. -e --email e-mail address to send for
  31. -a --all send for all emails in confirm_address table
  32. -y --yes skip interactive verification
  33. END_OF_HELP;
  34. require_once INSTALLDIR . '/scripts/commandline.inc';
  35. $all = false;
  36. $ca = null;
  37. if (have_option('e', 'email')) {
  38. $email = get_option_value('e', 'email');
  39. if (is_null($email)) {
  40. echo "You must provide an email.\n";
  41. exit(1);
  42. }
  43. try {
  44. $ca = Confirm_address::getByAddress($email, 'email');
  45. } catch (NoResultException $e) {
  46. echo sprintf(
  47. "Can't find %s address %s in %s table.\n",
  48. $e->obj->address_type,
  49. $e->obj->address,
  50. $e->obj->tableName()
  51. );
  52. exit(1);
  53. }
  54. } elseif (have_option('a', 'all')) {
  55. $all = true;
  56. $ca = new Confirm_address();
  57. $ca->address_type = 'email';
  58. if (!$ca->find()) {
  59. echo "confirm_address table contains no lingering email addresses\n";
  60. exit();
  61. }
  62. } else {
  63. echo "You must provide an email (or --all).\n";
  64. exit(1);
  65. }
  66. if (!have_option('y', 'yes')) {
  67. echo "About to resend confirm_address email to {$ca->N} recipients. Are you sure? [y/N] ";
  68. $response = fgets(STDIN);
  69. if (strcasecmp(trim($response), 'y') != 0) {
  70. echo "Aborting.\n";
  71. exit();
  72. }
  73. }
  74. function mail_confirm_address(Confirm_address $ca): void
  75. {
  76. try {
  77. $user = User::getByID($ca->user_id);
  78. $profile = $user->getProfile();
  79. if ($profile->isSilenced()) {
  80. $ca->delete();
  81. return;
  82. }
  83. if ($user->email === $ca->address) {
  84. throw new AlreadyFulfilledException('User already has identical confirmed email address.');
  85. }
  86. } catch (AlreadyFulfilledException $e) {
  87. echo "\n User already had verified email: " . _ve($ca->address);
  88. $ca->delete();
  89. } catch (Exception $e) {
  90. echo "\n Failed to get user with ID " . _ve($user_id)
  91. . ', deleting confirm_address entry: ' . _ve($e->getMessage());
  92. $ca->delete();
  93. return;
  94. }
  95. $ca->sendConfirmation();
  96. }
  97. require_once INSTALLDIR . '/lib/util/mail.php';
  98. if (!$all) {
  99. mail_confirm_address($ca);
  100. } else {
  101. while ($ca->fetch()) {
  102. mail_confirm_address($ca);
  103. }
  104. }
  105. echo "\nDONE.\n";