]> 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             if (Event::handle('StartValidateEmailInvite', array($user, $email, &$valid))) {
84                 $valid = Validate::email($email, common_config('email', 'check_domain'));
85                 Event::handle('EndValidateEmailInvite', array($user, $email, &$valid));
86             }
87
88             if (!$valid) {
89                 // TRANS: Form validation message when providing an e-mail address that does not validate.
90                 // TRANS: %s is an invalid e-mail address.
91                 $this->showForm(sprintf(_('Invalid email address: %s.'), $email));
92                 return;
93             }
94         }
95
96         $this->already = array();
97         $this->subbed = array();
98
99         foreach ($addresses as $email) {
100             $email = common_canonical_email($email);
101             $other = User::staticGet('email', $email);
102             if ($other) {
103                 if ($user->isSubscribed($other)) {
104                     $this->already[] = $other;
105                 } else {
106                     subs_subscribe_to($user, $other);
107                     $this->subbed[] = $other;
108                 }
109             } else {
110                 $this->sent[] = $email;
111                 $this->sendInvitation($email, $user, $personal);
112             }
113         }
114
115         $this->mode = 'sent';
116
117         $this->showPage();
118     }
119
120     function showScripts()
121     {
122         parent::showScripts();
123         $this->autofocus('addresses');
124     }
125
126     function title()
127     {
128         if ($this->mode == 'sent') {
129             // TRANS: Page title when invitations have been sent.
130             return _('Invitations sent');
131         } else {
132             // TRANS: Page title when inviting potential users.
133             return _('Invite new users');
134         }
135     }
136
137     function showContent()
138     {
139         if ($this->mode == 'sent') {
140             $this->showInvitationSuccess();
141         } else {
142             $this->showInviteForm();
143         }
144     }
145
146     function showInvitationSuccess()
147     {
148         if ($this->already) {
149             // TRANS: Message displayed inviting users to use a StatusNet site while the inviting user
150             // TRANS: is already subscribed to one or more users with the given e-mail address(es).
151             // TRANS: Plural form is based on the number of reported already subscribed e-mail addresses.
152             // TRANS: Followed by a bullet list.
153             $this->element('p', null, _m('You are already subscribed to this user:',
154                                          'You are already subscribed to these users:',
155                                          count($this->already)));
156             $this->elementStart('ul');
157             foreach ($this->already as $other) {
158                 // TRANS: Used as list item for already subscribed users (%1$s is nickname, %2$s is e-mail address).
159                 $this->element('li', null, sprintf(_m('INVITE','%1$s (%2$s)'), $other->nickname, $other->email));
160             }
161             $this->elementEnd('ul');
162         }
163         if ($this->subbed) {
164             // TRANS: Message displayed inviting users to use a StatusNet site while the invited user
165             // TRANS: already uses a this StatusNet site. Plural form is based on the number of
166             // TRANS: reported already present people. Followed by a bullet list.
167             $this->element('p', null, _m('This person is already a user and you were automatically subscribed:',
168                                          'These people are already users and you were automatically subscribed to them:',
169                                          count($this->subbed)));
170             $this->elementStart('ul');
171             foreach ($this->subbed as $other) {
172                 // TRANS: Used as list item for already registered people (%1$s is nickname, %2$s is e-mail address).
173                 $this->element('li', null, sprintf(_m('INVITE','%1$s (%2$s)'), $other->nickname, $other->email));
174             }
175             $this->elementEnd('ul');
176         }
177         if ($this->sent) {
178             // TRANS: Message displayed inviting users to use a StatusNet site. Plural form is
179             // TRANS: based on the number of invitations sent. Followed by a bullet list of
180             // TRANS: e-mail addresses to which invitations were sent.
181             $this->element('p', null, _m('Invitation sent to the following person:',
182                                          'Invitations sent to the following people:',
183                                          count($this->sent)));
184             $this->elementStart('ul');
185             foreach ($this->sent as $other) {
186                 $this->element('li', null, $other);
187             }
188             $this->elementEnd('ul');
189             // TRANS: Generic message displayed after sending out one or more invitations to
190             // TRANS: people to join a StatusNet site.
191             $this->element('p', null, _('You will be notified when your invitees accept the invitation and register on the site. Thanks for growing the community!'));
192         }
193     }
194
195     function showPageNotice()
196     {
197         if ($this->mode != 'sent') {
198             if ($this->error) {
199                 $this->element('p', 'error', $this->error);
200             } else {
201                 $this->elementStart('div', 'instructions');
202                 $this->element('p', null,
203                                // TRANS: Form instructions.
204                                _('Use this form to invite your friends and colleagues to use this service.'));
205                 $this->elementEnd('div');
206             }
207         }
208     }
209
210     function showForm($error=null)
211     {
212         $this->mode = 'form';
213         $this->error = $error;
214         $this->showPage();
215     }
216
217     function showInviteForm()
218     {
219         $this->elementStart('form', array('method' => 'post',
220                                            'id' => 'form_invite',
221                                            'class' => 'form_settings',
222                                            'action' => common_local_url('invite')));
223         $this->elementStart('fieldset');
224         // TRANS: Form legend.
225         $this->element('legend', null, 'Send an invitation');
226         $this->hidden('token', common_session_token());
227
228         $this->elementStart('ul', 'form_data');
229         $this->elementStart('li');
230         // TRANS: Field label for a list of e-mail addresses.
231         $this->textarea('addresses', _('Email addresses'),
232                         $this->trimmed('addresses'),
233                         // TRANS: Tooltip for field label for a list of e-mail addresses.
234                         _('Addresses of friends to invite (one per line).'));
235         $this->elementEnd('li');
236         $this->elementStart('li');
237         // TRANS: Field label for a personal message to send to invitees.
238         $this->textarea('personal', _('Personal message'),
239                         $this->trimmed('personal'),
240                         // TRANS: Tooltip for field label for a personal message to send to invitees.
241                         _('Optionally add a personal message to the invitation.'));
242         $this->elementEnd('li');
243         $this->elementEnd('ul');
244         // TRANS: Send button for inviting friends
245         $this->submit('send', _m('BUTTON', 'Send'));
246         $this->elementEnd('fieldset');
247         $this->elementEnd('form');
248     }
249
250     function sendInvitation($email, $user, $personal)
251     {
252         $profile = $user->getProfile();
253         $bestname = $profile->getBestName();
254
255         $sitename = common_config('site', 'name');
256
257         $invite = new Invitation();
258
259         $invite->address = $email;
260         $invite->address_type = 'email';
261         $invite->code = common_confirmation_code(128);
262         $invite->user_id = $user->id;
263         $invite->created = common_sql_now();
264
265         if (!$invite->insert()) {
266             common_log_db_error($invite, 'INSERT', __FILE__);
267             return false;
268         }
269
270         $confirmUrl = common_local_url('register', array('code' => $invite->code));
271
272         $recipients = array($email);
273
274         $headers['From'] = mail_notify_from();
275         $headers['To'] = trim($email);
276         $headers['Content-Type'] = 'text/html; charset=UTF-8';
277
278         // TRANS: Subject for invitation email. Note that 'them' is correct as a gender-neutral
279         // TRANS: singular 3rd-person pronoun in English. %1$s is the inviting user, $2$s is
280         // TRANS: the StatusNet sitename.
281
282         $headers['Subject'] = sprintf(_('%1$s has invited you to join them on %2$s'), $bestname, $sitename);
283
284         $title = (empty($personal)) ? 'invite' : 'invitepersonal';
285
286         $inviteTemplate = DocFile::forTitle($title, DocFile::mailPaths());
287
288         $body = $inviteTemplate->toHTML(array('inviter' => $bestname,
289                                               'inviteurl' => $profile->profileurl,
290                                               'confirmurl' => $confirmUrl,
291                                               'personal' => $personal));
292
293         common_debug('Confirm URL is ' . common_local_url('register', array('code' => $invite->code)));
294
295         mail_send($recipients, $headers, $body);
296     }
297 }