]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/EmailReminder/scripts/sendemailreminder.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / EmailReminder / scripts / sendemailreminder.php
1 #!/usr/bin/env php
2 <?php
3 /*
4 * StatusNet - a distributed open-source microblogging tool
5 * Copyright (C) 2011, StatusNet, Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
22
23 $shortoptions = 't:e:auo';
24 $longoptions = array('type=', 'email=', 'all', 'universe', 'onetime');
25
26 $helptext = <<<END_OF_SENDEMAILREMINDER_HELP
27 sendemailreminder.php [options]
28 Send an email summary of the inbox to users
29
30  -t --type     type of reminder to send (register | invite | all)
31  -e --email    email address to send reminder to
32  -a --all      send reminder to all addresses
33  -u --universe send reminder to all addresses on all sites
34  -o --onetime  send one-time reminder to older addresses
35
36 END_OF_SENDEMAILREMINDER_HELP;
37
38 require_once INSTALLDIR . '/scripts/commandline.inc.php';
39
40 $quiet = have_option('q', 'quiet');
41
42 $types = array(
43     // registration confirmation reminder
44     'register' => array(
45         'type'       => 'register',
46         'className'  => 'Confirm_address',
47         'utransport' => 'uregrem'
48      ),
49     // invitation confirmation reminder
50     'invite'   => array(
51         'type'       => 'invite',
52         'className'  => 'Invitation',
53         'utransport' => 'uinvrem'
54     )
55     // ... add more here
56 );
57
58 $type = null;
59 $opts = array(); // special options like "onetime"
60
61 if (have_option('t', 'type')) {
62     $type = trim(get_option_value('t', 'type'));
63     if (!in_array($type, array_keys($types)) && $type !== 'all') {
64        print _m("Unknown reminder type: {$type}.\n");
65        exit(1);
66     }
67 } else {
68    show_help();
69    exit(1);
70 }
71
72 if (have_option('o', 'onetime')) {
73     $opts['onetime'] = true;
74     if (!$quiet) { print "Special one-time reminder mode.\n"; }
75 }
76
77 $reminders = array();
78
79 switch($type) {
80 case 'register':
81     $reminders[] = $types['register'];
82     break;
83 case 'invite':
84     $reminders[] = $types['invite'];
85     break;
86 case 'all':
87     $reminders = $types;
88     break;
89 }
90
91 if (have_option('u', 'universe')) {
92     $sn = new Status_network();
93     try {
94         if ($sn->find()) {
95             while ($sn->fetch()) {
96                 try {
97                     $server = $sn->getServerName();
98                     GNUsocial::init($server);
99                     // Different queue manager, maybe!
100                     $qm = QueueManager::get();
101                     foreach ($reminders as $reminder) {
102                         extract($reminder);
103                         $qm->enqueue(array($type, $opts), 'siterem');
104                         if (!$quiet) { print "Sent pending {$type} reminders for {$server}.\n"; }
105                     }
106                 } catch (Exception $e) {
107                     // keep going
108                     common_log(LOG_ERR, "Couldn't init {$server}.\n", __FILE__);
109                     if (!$quiet) { print "Couldn't init {$server}.\n"; }
110                     continue;
111                 }
112             }
113            if (!$quiet) { print "Done! Reminders sent to all unconfirmed addresses in the known universe.\n"; }
114         }
115     } catch (Exception $e) {
116         if (!$quiet) { print $e->getMessage() . "\n"; }
117         common_log(LOG_ERR, $e->getMessage(), __FILE__);
118         exit(1);
119     }
120 } else {
121     $qm = QueueManager::get();
122     try {
123         // enqueue reminder for specific email address or all unconfirmed addresses
124         if (have_option('e', 'email')) {
125             $address = trim(get_option_value('e', 'email'));
126             foreach ($reminders as $reminder) {
127                 // real bad voodoo here
128                 extract($reminder);
129                 $confirm = new $className;
130                 $confirm->address = $address;
131                 $result = $confirm->find(true);
132                 if (empty($result)) {
133                     throw new Exception("No confirmation code found for {$address}.");
134                 }
135                 $qm->enqueue(array($confirm, $opts), $utransport);
136                 if (!$quiet) { print "Sent all pending {$type} reminder to {$address}.\n"; }
137             }
138         } else if (have_option('a', 'all')) {
139             foreach ($reminders as $reminder) {
140                 extract($reminder);
141                 $qm->enqueue(array($type, $opts), 'siterem');
142                 if (!$quiet) { print "Sent pending {$type} reminders to all unconfirmed addresses on the site.\n"; }
143             }
144         } else {
145             show_help();
146             exit(1);
147         }
148     } catch (Exception $e) {
149         if (!$quiet) { print $e->getMessage() . "\n"; }
150         common_log(LOG_ERR, $e->getMessage(), __FILE__);
151         exit(1);
152     }
153 }