]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/invite.php
Use mail templates for invite text
[quix0rs-gnu-social.git] / actions / invite.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 // @todo XXX: Add documentation.
23 class InviteAction extends CurrentUserDesignAction
24 {
25     var $mode = null;
26     var $error = null;
27     var $already = null;
28     var $subbed = null;
29     var $sent = null;
30
31     function isReadOnly($args)
32     {
33         return false;
34     }
35
36     function handle($args)
37     {
38         parent::handle($args);
39         if (!common_config('invite', 'enabled')) {
40             // TRANS: Client error displayed when trying to sent invites while they have been disabled.
41             $this->clientError(_('Invites have been disabled.'));
42         } else if (!common_logged_in()) {
43             // TRANS: Client error displayed when trying to sent invites while not logged in.
44             // TRANS: %s is the StatusNet site name.
45             $this->clientError(sprintf(_('You must be logged in to invite other users to use %s.'),
46                                         common_config('site', 'name')));
47             return;
48         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
49             $this->sendInvitations();
50         } else {
51             $this->showForm();
52         }
53     }
54
55     function sendInvitations()
56     {
57         // CSRF protection
58         $token = $this->trimmed('token');
59         if (!$token || $token != common_session_token()) {
60             // TRANS: Client error displayed when the session token does not match or is not given.
61             $this->showForm(_('There was a problem with your session token. Try again, please.'));
62             return;
63         }
64
65         $user = common_current_user();
66         $profile = $user->getProfile();
67
68         $bestname = $profile->getBestName();
69         $sitename = common_config('site', 'name');
70         $personal = $this->trimmed('personal');
71
72         $addresses = explode("\n", $this->trimmed('addresses'));
73
74         foreach ($addresses as $email) {
75             $email = trim($email);
76             $valid = null;
77
78             if (Event::handle('StartValidateEmailInvite', array($user, $email, &$valid))) {
79                 $valid = Validate::email($email, common_config('email', 'check_domain'));
80                 Event::handle('EndValidateEmailInvite', array($user, $email, &$valid));
81             }
82
83             if (!$valid) {
84                 // TRANS: Form validation message when providing an e-mail address that does not validate.
85                 // TRANS: %s is an invalid e-mail address.
86                 $this->showForm(sprintf(_('Invalid email address: %s.'), $email));
87                 return;
88             }
89         }
90
91         $this->already = array();
92         $this->subbed = array();
93
94         foreach ($addresses as $email) {
95             $email = common_canonical_email($email);
96             $other = User::staticGet('email', $email);
97             if ($other) {
98                 if ($user->isSubscribed($other)) {
99                     $this->already[] = $other;
100                 } else {
101                     subs_subscribe_to($user, $other);
102                     $this->subbed[] = $other;
103                 }
104             } else {
105                 $this->sent[] = $email;
106                 $this->sendInvitation($email, $user, $personal);
107             }
108         }
109
110         $this->mode = 'sent';
111
112         $this->showPage();
113     }
114
115     function showScripts()
116     {
117         parent::showScripts();
118         $this->autofocus('addresses');
119     }
120
121     function title()
122     {
123         if ($this->mode == 'sent') {
124             // TRANS: Page title when invitations have been sent.
125             return _('Invitations sent');
126         } else {
127             // TRANS: Page title when inviting potential users.
128             return _('Invite new users');
129         }
130     }
131
132     function showContent()
133     {
134         if ($this->mode == 'sent') {
135             $this->showInvitationSuccess();
136         } else {
137             $this->showInviteForm();
138         }
139     }
140
141     function showInvitationSuccess()
142     {
143         if ($this->already) {
144             // TRANS: Message displayed inviting users to use a StatusNet site while the inviting user
145             // TRANS: is already subscribed to one or more users with the given e-mail address(es).
146             // TRANS: Plural form is based on the number of reported already subscribed e-mail addresses.
147             // TRANS: Followed by a bullet list.
148             $this->element('p', null, _m('You are already subscribed to this user:',
149                                          'You are already subscribed to these users:',
150                                          count($this->already)));
151             $this->elementStart('ul');
152             foreach ($this->already as $other) {
153                 // TRANS: Used as list item for already subscribed users (%1$s is nickname, %2$s is e-mail address).
154                 $this->element('li', null, sprintf(_m('INVITE','%1$s (%2$s)'), $other->nickname, $other->email));
155             }
156             $this->elementEnd('ul');
157         }
158         if ($this->subbed) {
159             // TRANS: Message displayed inviting users to use a StatusNet site while the invited user
160             // TRANS: already uses a this StatusNet site. Plural form is based on the number of
161             // TRANS: reported already present people. Followed by a bullet list.
162             $this->element('p', null, _m('This person is already a user and you were automatically subscribed:',
163                                          'These people are already users and you were automatically subscribed to them:',
164                                          count($this->subbed)));
165             $this->elementStart('ul');
166             foreach ($this->subbed as $other) {
167                 // TRANS: Used as list item for already registered people (%1$s is nickname, %2$s is e-mail address).
168                 $this->element('li', null, sprintf(_m('INVITE','%1$s (%2$s)'), $other->nickname, $other->email));
169             }
170             $this->elementEnd('ul');
171         }
172         if ($this->sent) {
173             // TRANS: Message displayed inviting users to use a StatusNet site. Plural form is
174             // TRANS: based on the number of invitations sent. Followed by a bullet list of
175             // TRANS: e-mail addresses to which invitations were sent.
176             $this->element('p', null, _m('Invitation sent to the following person:',
177                                          'Invitations sent to the following people:',
178                                          count($this->sent)));
179             $this->elementStart('ul');
180             foreach ($this->sent as $other) {
181                 $this->element('li', null, $other);
182             }
183             $this->elementEnd('ul');
184             // TRANS: Generic message displayed after sending out one or more invitations to
185             // TRANS: people to join a StatusNet site.
186             $this->element('p', null, _('You will be notified when your invitees accept the invitation and register on the site. Thanks for growing the community!'));
187         }
188     }
189
190     function showPageNotice()
191     {
192         if ($this->mode != 'sent') {
193             if ($this->error) {
194                 $this->element('p', 'error', $this->error);
195             } else {
196                 $this->elementStart('div', 'instructions');
197                 $this->element('p', null,
198                                // TRANS: Form instructions.
199                                _('Use this form to invite your friends and colleagues to use this service.'));
200                 $this->elementEnd('div');
201             }
202         }
203     }
204
205     function showForm($error=null)
206     {
207         $this->mode = 'form';
208         $this->error = $error;
209         $this->showPage();
210     }
211
212     function showInviteForm()
213     {
214         $this->elementStart('form', array('method' => 'post',
215                                            'id' => 'form_invite',
216                                            'class' => 'form_settings',
217                                            'action' => common_local_url('invite')));
218         $this->elementStart('fieldset');
219         // TRANS: Form legend.
220         $this->element('legend', null, 'Send an invitation');
221         $this->hidden('token', common_session_token());
222
223         $this->elementStart('ul', 'form_data');
224         $this->elementStart('li');
225         // TRANS: Field label for a list of e-mail addresses.
226         $this->textarea('addresses', _('Email addresses'),
227                         $this->trimmed('addresses'),
228                         // TRANS: Tooltip for field label for a list of e-mail addresses.
229                         _('Addresses of friends to invite (one per line).'));
230         $this->elementEnd('li');
231         $this->elementStart('li');
232         // TRANS: Field label for a personal message to send to invitees.
233         $this->textarea('personal', _('Personal message'),
234                         $this->trimmed('personal'),
235                         // TRANS: Tooltip for field label for a personal message to send to invitees.
236                         _('Optionally add a personal message to the invitation.'));
237         $this->elementEnd('li');
238         $this->elementEnd('ul');
239         // TRANS: Send button for inviting friends
240         $this->submit('send', _m('BUTTON', 'Send'));
241         $this->elementEnd('fieldset');
242         $this->elementEnd('form');
243     }
244
245     function sendInvitation($email, $user, $personal)
246     {
247         $profile = $user->getProfile();
248         $bestname = $profile->getBestName();
249
250         $sitename = common_config('site', 'name');
251
252         $invite = new Invitation();
253
254         $invite->address = $email;
255         $invite->address_type = 'email';
256         $invite->code = common_confirmation_code(128);
257         $invite->user_id = $user->id;
258         $invite->created = common_sql_now();
259
260         if (!$invite->insert()) {
261             common_log_db_error($invite, 'INSERT', __FILE__);
262             return false;
263         }
264
265         $confirmUrl = common_local_url('register', array('code' => $invite->code));
266
267         $recipients = array($email);
268
269         $headers['From'] = mail_notify_from();
270         $headers['To'] = trim($email);
271         $headers['Content-Type'] = 'text/html; charset=UTF-8';
272
273         // TRANS: Subject for invitation email. Note that 'them' is correct as a gender-neutral
274         // TRANS: singular 3rd-person pronoun in English. %1$s is the inviting user, $2$s is
275         // TRANS: the StatusNet sitename.
276
277         $headers['Subject'] = sprintf(_('%1$s has invited you to join them on %2$s'), $bestname, $sitename);
278
279         $title = (empty($personal)) ? 'invite' : 'invitepersonal';
280
281         $inviteTemplate = DocFile::fromTitle($title, DocFile::mailPaths());
282
283         $body = $inviteTemplate->toHTML(array('inviter' => $bestname,
284                                               'inviteurl' => $profile->profileurl,
285                                               'confirmurl' => $confirmUrl,
286                                               'personal' => $personal));
287
288         common_debug('Confirm URL is ' . common_local_url('register', array('code' => $invite->code)));
289
290         mail_send($recipients, $headers, $body);
291     }
292 }