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