]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/invite.php
Merge remote-tracking branch 'gitorious/1.0.x' into 1.0.x
[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 showNoticeForm()
32     {
33         return;
34     }
35
36     function isReadOnly($args)
37     {
38         return false;
39     }
40
41     function handle($args)
42     {
43         parent::handle($args);
44         if (!common_config('invite', 'enabled')) {
45             // TRANS: Client error displayed when trying to sent invites while they have been disabled.
46             $this->clientError(_('Invites have been disabled.'));
47         } else if (!common_logged_in()) {
48             // TRANS: Client error displayed when trying to sent invites while not logged in.
49             // TRANS: %s is the StatusNet site name.
50             $this->clientError(sprintf(_('You must be logged in to invite other users to use %s.'),
51                                         common_config('site', 'name')));
52             return;
53         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
54             $this->sendInvitations();
55         } else {
56             $this->showForm();
57         }
58     }
59
60     function sendInvitations()
61     {
62         // CSRF protection
63         $token = $this->trimmed('token');
64         if (!$token || $token != common_session_token()) {
65             // TRANS: Client error displayed when the session token does not match or is not given.
66             $this->showForm(_('There was a problem with your session token. Try again, please.'));
67             return;
68         }
69
70         $user = common_current_user();
71         $profile = $user->getProfile();
72
73         $bestname = $profile->getBestName();
74         $sitename = common_config('site', 'name');
75         $personal = $this->trimmed('personal');
76
77         $addresses = explode("\n", $this->trimmed('addresses'));
78
79         foreach ($addresses as $email) {
80             $email = trim($email);
81             $valid = null;
82
83             try {
84
85                 if (Event::handle('StartValidateUserEmail', array(null, $email, &$valid))) {
86                     $valid = Validate::email($email, common_config('email', 'check_domain'));
87                     Event::handle('EndValidateUserEmail', array(null, $email, &$valid));
88                 }
89
90                 if ($valid) {
91                     if (Event::handle('StartValidateEmailInvite', array($user, $email, &$valid))) {
92                         $valid = true;
93                         Event::handle('EndValidateEmailInvite', array($user, $email, &$valid));
94                     }
95                 }
96
97                 if (!$valid) {
98                     // TRANS: Form validation message when providing an e-mail address that does not validate.
99                     // TRANS: %s is an invalid e-mail address.
100                     $this->showForm(sprintf(_('Invalid email address: %s.'), $email));
101                     return;
102                 }
103             } catch (ClientException $e) {
104                 $this->showForm($e->getMessage());
105                 return;
106             }
107         }
108
109         $this->already = array();
110         $this->subbed = array();
111
112         foreach ($addresses as $email) {
113             $email = common_canonical_email($email);
114             $other = User::staticGet('email', $email);
115             if ($other) {
116                 if ($user->isSubscribed($other)) {
117                     $this->already[] = $other;
118                 } else {
119                     subs_subscribe_to($user, $other);
120                     $this->subbed[] = $other;
121                 }
122             } else {
123                 $this->sent[] = $email;
124                 $this->sendInvitation($email, $user, $personal);
125             }
126         }
127
128         $this->mode = 'sent';
129
130         $this->showPage();
131     }
132
133     function showScripts()
134     {
135         parent::showScripts();
136         $this->autofocus('addresses');
137     }
138
139     function title()
140     {
141         if ($this->mode == 'sent') {
142             // TRANS: Page title when invitations have been sent.
143             return _('Invitations sent');
144         } else {
145             // TRANS: Page title when inviting potential users.
146             return _('Invite new users');
147         }
148     }
149
150     function showContent()
151     {
152         if ($this->mode == 'sent') {
153             $this->showInvitationSuccess();
154         } else {
155             $this->showInviteForm();
156         }
157     }
158
159     function showInvitationSuccess()
160     {
161         if ($this->already) {
162             // TRANS: Message displayed inviting users to use a StatusNet site while the inviting user
163             // TRANS: is already subscribed to one or more users with the given e-mail address(es).
164             // TRANS: Plural form is based on the number of reported already subscribed e-mail addresses.
165             // TRANS: Followed by a bullet list.
166             $this->element('p', null, _m('You are already subscribed to this user:',
167                                          'You are already subscribed to these users:',
168                                          count($this->already)));
169             $this->elementStart('ul');
170             foreach ($this->already as $other) {
171                 // TRANS: Used as list item for already subscribed users (%1$s is nickname, %2$s is e-mail address).
172                 $this->element('li', null, sprintf(_m('INVITE','%1$s (%2$s)'), $other->nickname, $other->email));
173             }
174             $this->elementEnd('ul');
175         }
176         if ($this->subbed) {
177             // TRANS: Message displayed inviting users to use a StatusNet site while the invited user
178             // TRANS: already uses a this StatusNet site. Plural form is based on the number of
179             // TRANS: reported already present people. Followed by a bullet list.
180             $this->element('p', null, _m('This person is already a user and you were automatically subscribed:',
181                                          'These people are already users and you were automatically subscribed to them:',
182                                          count($this->subbed)));
183             $this->elementStart('ul');
184             foreach ($this->subbed as $other) {
185                 // TRANS: Used as list item for already registered people (%1$s is nickname, %2$s is e-mail address).
186                 $this->element('li', null, sprintf(_m('INVITE','%1$s (%2$s)'), $other->nickname, $other->email));
187             }
188             $this->elementEnd('ul');
189         }
190         if ($this->sent) {
191             // TRANS: Message displayed inviting users to use a StatusNet site. Plural form is
192             // TRANS: based on the number of invitations sent. Followed by a bullet list of
193             // TRANS: e-mail addresses to which invitations were sent.
194             $this->element('p', null, _m('Invitation sent to the following person:',
195                                          'Invitations sent to the following people:',
196                                          count($this->sent)));
197             $this->elementStart('ul');
198             foreach ($this->sent as $other) {
199                 $this->element('li', null, $other);
200             }
201             $this->elementEnd('ul');
202             // TRANS: Generic message displayed after sending out one or more invitations to
203             // TRANS: people to join a StatusNet site.
204             $this->element('p', null, _('You will be notified when your invitees accept the invitation and register on the site. Thanks for growing the community!'));
205         }
206     }
207
208     function showPageNotice()
209     {
210         if ($this->mode != 'sent') {
211             if ($this->error) {
212                 $this->element('p', 'error', $this->error);
213             } else {
214                 $this->elementStart('div', 'instructions');
215                 $this->element('p', null,
216                                // TRANS: Form instructions.
217                                _('Use this form to invite your friends and colleagues to use this service.'));
218                 $this->elementEnd('div');
219             }
220         }
221     }
222
223     function showForm($error=null)
224     {
225         $this->mode = 'form';
226         $this->error = $error;
227         $this->showPage();
228     }
229
230     function showInviteForm()
231     {
232         $this->elementStart('form', array('method' => 'post',
233                                            'id' => 'form_invite',
234                                            'class' => 'form_settings',
235                                            'action' => common_local_url('invite')));
236         $this->elementStart('fieldset');
237         // TRANS: Form legend.
238         $this->element('legend', null, 'Send an invitation');
239         $this->hidden('token', common_session_token());
240
241         $this->elementStart('ul', 'form_data');
242         $this->elementStart('li');
243         // TRANS: Field label for a list of e-mail addresses.
244         $this->textarea('addresses', _('Email addresses'),
245                         $this->trimmed('addresses'),
246                         // TRANS: Tooltip for field label for a list of e-mail addresses.
247                         _('Addresses of friends to invite (one per line).'));
248         $this->elementEnd('li');
249         $this->elementStart('li');
250         // TRANS: Field label for a personal message to send to invitees.
251         $this->textarea('personal', _('Personal message'),
252                         $this->trimmed('personal'),
253                         // TRANS: Tooltip for field label for a personal message to send to invitees.
254                         _('Optionally add a personal message to the invitation.'));
255         $this->elementEnd('li');
256         $this->elementEnd('ul');
257         // TRANS: Send button for inviting friends
258         $this->submit('send', _m('BUTTON', 'Send'));
259         $this->elementEnd('fieldset');
260         $this->elementEnd('form');
261     }
262
263     function sendInvitation($email, $user, $personal)
264     {
265         $profile = $user->getProfile();
266         $bestname = $profile->getBestName();
267
268         $sitename = common_config('site', 'name');
269
270         $invite = new Invitation();
271
272         $invite->address = $email;
273         $invite->address_type = 'email';
274         $invite->code = common_confirmation_code(128);
275         $invite->user_id = $user->id;
276         $invite->created = common_sql_now();
277
278         if (!$invite->insert()) {
279             common_log_db_error($invite, 'INSERT', __FILE__);
280             return false;
281         }
282
283         $confirmUrl = common_local_url('register', array('code' => $invite->code));
284
285         $recipients = array($email);
286
287         $headers['From'] = mail_notify_from();
288         $headers['To'] = trim($email);
289         $headers['Content-Type'] = 'text/html; charset=UTF-8';
290
291         // TRANS: Subject for invitation email. Note that 'them' is correct as a gender-neutral
292         // TRANS: singular 3rd-person pronoun in English. %1$s is the inviting user, $2$s is
293         // TRANS: the StatusNet sitename.
294         $headers['Subject'] = sprintf(_('%1$s has invited you to join them on %2$s'), $bestname, $sitename);
295
296         $title = (empty($personal)) ? 'invite' : 'invitepersonal';
297
298         // @todo FIXME: i18n issue.
299         $inviteTemplate = DocFile::forTitle($title, DocFile::mailPaths());
300
301         $body = $inviteTemplate->toHTML(array('inviter' => $bestname,
302                                               'inviterurl' => $profile->profileurl,
303                                               'confirmurl' => $confirmUrl,
304                                               'personal' => $personal));
305
306         common_debug('Confirm URL is ' . common_local_url('register', array('code' => $invite->code)));
307
308         mail_send($recipients, $headers, $body);
309     }
310 }