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