]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/invite.php
1bfc9f76d31b5da30a3b20b0969443ee473e1768
[quix0rs-gnu-social.git] / actions / invite.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008-2011, 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         if (Event::handle('StartSendInvitations', array(&$this))) {
63             // CSRF protection
64             $token = $this->trimmed('token');
65             if (!$token || $token != common_session_token()) {
66                 // TRANS: Client error displayed when the session token does not match or is not given.
67                 $this->showForm(_('There was a problem with your session token. Try again, please.'));
68                 return;
69             }
70
71             $user = common_current_user();
72             $profile = $user->getProfile();
73
74             $bestname = $profile->getBestName();
75             $sitename = common_config('site', 'name');
76             $personal = $this->trimmed('personal');
77
78             $addresses = explode("\n", $this->trimmed('addresses'));
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             Event::handle('EndSendInvitations', array($this));
132         }
133     }
134
135     function showScripts()
136     {
137         parent::showScripts();
138         $this->autofocus('addresses');
139     }
140
141     function title()
142     {
143         if ($this->mode == 'sent') {
144             // TRANS: Page title when invitations have been sent.
145             return _('Invitations sent');
146         } else {
147             // TRANS: Page title when inviting potential users.
148             return _('Invite new users');
149         }
150     }
151
152     function showContent()
153     {
154         if ($this->mode == 'sent') {
155             $this->showInvitationSuccess();
156         } else {
157             $this->showInviteForm();
158         }
159     }
160
161     function showInvitationSuccess()
162     {
163         if (Event::handle('StartShowInvitationSuccess', array($this))) {
164             if ($this->already) {
165                 // TRANS: Message displayed inviting users to use a StatusNet site while the inviting user
166                 // TRANS: is already subscribed to one or more users with the given e-mail address(es).
167                 // TRANS: Plural form is based on the number of reported already subscribed e-mail addresses.
168                 // TRANS: Followed by a bullet list.
169                 $this->element('p', null, _m('You are already subscribed to this user:',
170                                              'You are already subscribed to these users:',
171                                              count($this->already)));
172                 $this->elementStart('ul');
173                 foreach ($this->already as $other) {
174                     // TRANS: Used as list item for already subscribed users (%1$s is nickname, %2$s is e-mail address).
175                     $this->element('li', null, sprintf(_m('INVITE','%1$s (%2$s)'), $other->nickname, $other->email));
176                 }
177                 $this->elementEnd('ul');
178             }
179             if ($this->subbed) {
180                 // TRANS: Message displayed inviting users to use a StatusNet site while the invited user
181                 // TRANS: already uses a this StatusNet site. Plural form is based on the number of
182                 // TRANS: reported already present people. Followed by a bullet list.
183                 $this->element('p', null, _m('This person is already a user and you were automatically subscribed:',
184                                              'These people are already users and you were automatically subscribed to them:',
185                                              count($this->subbed)));
186                 $this->elementStart('ul');
187                 foreach ($this->subbed as $other) {
188                     // TRANS: Used as list item for already registered people (%1$s is nickname, %2$s is e-mail address).
189                     $this->element('li', null, sprintf(_m('INVITE','%1$s (%2$s)'), $other->nickname, $other->email));
190                 }
191                 $this->elementEnd('ul');
192             }
193             if ($this->sent) {
194                 // TRANS: Message displayed inviting users to use a StatusNet site. Plural form is
195                 // TRANS: based on the number of invitations sent. Followed by a bullet list of
196                 // TRANS: e-mail addresses to which invitations were sent.
197                 $this->element('p', null, _m('Invitation sent to the following person:',
198                                              'Invitations sent to the following people:',
199                                              count($this->sent)));
200                 $this->elementStart('ul');
201                 foreach ($this->sent as $other) {
202                     $this->element('li', null, $other);
203                 }
204                 $this->elementEnd('ul');
205                 // TRANS: Generic message displayed after sending out one or more invitations to
206                 // TRANS: people to join a StatusNet site.
207                 $this->element('p', null, _('You will be notified when your invitees accept the invitation and register on the site. Thanks for growing the community!'));
208             }
209             Event::handle('EndShowInvitationSuccess', array($this));
210         }
211     }
212
213     function showPageNotice()
214     {
215         if ($this->mode != 'sent') {
216             if ($this->error) {
217                 $this->element('p', 'error', $this->error);
218             } else {
219                 $this->elementStart('div', 'instructions');
220                 $this->element('p', null,
221                                // TRANS: Form instructions.
222                                _('Use this form to invite your friends and colleagues to use this service.'));
223                 $this->elementEnd('div');
224             }
225         }
226     }
227
228     function showForm($error=null)
229     {
230         $this->mode = 'form';
231         $this->error = $error;
232         $this->showPage();
233     }
234
235     function showInviteForm()
236     {
237         if (Event::handle('StartShowInviteForm', array($this))) {
238             $form = new InviteForm($this);
239             $form->show();
240             Event::handle('EndShowInviteForm', array($this));
241         }
242     }
243
244     function sendInvitation($email, $user, $personal)
245     {
246         $profile = $user->getProfile();
247         $bestname = $profile->getBestName();
248
249         $sitename = common_config('site', 'name');
250
251         $invite = new Invitation();
252
253         $invite->address = $email;
254         $invite->address_type = 'email';
255         $invite->code = common_confirmation_code(128);
256         $invite->user_id = $user->id;
257         $invite->created = common_sql_now();
258
259         if (!$invite->insert()) {
260             common_log_db_error($invite, 'INSERT', __FILE__);
261             return false;
262         }
263
264         $confirmUrl = common_local_url('register', array('code' => $invite->code));
265
266         $recipients = array($email);
267
268         $headers['From'] = mail_notify_from();
269         $headers['To'] = trim($email);
270         $headers['Content-Type'] = 'text/html; charset=UTF-8';
271
272         // TRANS: Subject for invitation email. Note that 'them' is correct as a gender-neutral
273         // TRANS: singular 3rd-person pronoun in English. %1$s is the inviting user, $2$s is
274         // TRANS: the StatusNet sitename.
275         $headers['Subject'] = sprintf(_('%1$s has invited you to join them on %2$s'), $bestname, $sitename);
276
277         $title = (empty($personal)) ? 'invite' : 'invitepersonal';
278
279         // @todo FIXME: i18n issue.
280         $inviteTemplate = DocFile::forTitle($title, DocFile::mailPaths());
281
282         $body = $inviteTemplate->toHTML(array('inviter' => $bestname,
283                                               'inviterurl' => $profile->profileurl,
284                                               'confirmurl' => $confirmUrl,
285                                               'personal' => $personal));
286
287         common_debug('Confirm URL is ' . common_local_url('register', array('code' => $invite->code)));
288
289         mail_send($recipients, $headers, $body);
290     }
291 }