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