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