3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
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.
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.
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/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
22 // @todo XXX: Add documentation.
23 class InviteAction extends CurrentUserDesignAction
31 function showNoticeForm()
36 function isReadOnly($args)
41 function handle($args)
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')));
53 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
54 $this->sendInvitations();
60 function sendInvitations()
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.'));
70 $user = common_current_user();
71 $profile = $user->getProfile();
73 $bestname = $profile->getBestName();
74 $sitename = common_config('site', 'name');
75 $personal = $this->trimmed('personal');
77 $addresses = explode("\n", $this->trimmed('addresses'));
79 foreach ($addresses as $email) {
80 $email = trim($email);
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));
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));
96 $this->already = array();
97 $this->subbed = array();
99 foreach ($addresses as $email) {
100 $email = common_canonical_email($email);
101 $other = User::staticGet('email', $email);
103 if ($user->isSubscribed($other)) {
104 $this->already[] = $other;
106 subs_subscribe_to($user, $other);
107 $this->subbed[] = $other;
110 $this->sent[] = $email;
111 $this->sendInvitation($email, $user, $personal);
115 $this->mode = 'sent';
120 function showScripts()
122 parent::showScripts();
123 $this->autofocus('addresses');
128 if ($this->mode == 'sent') {
129 // TRANS: Page title when invitations have been sent.
130 return _('Invitations sent');
132 // TRANS: Page title when inviting potential users.
133 return _('Invite new users');
137 function showContent()
139 if ($this->mode == 'sent') {
140 $this->showInvitationSuccess();
142 $this->showInviteForm();
146 function showInvitationSuccess()
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));
161 $this->elementEnd('ul');
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));
175 $this->elementEnd('ul');
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);
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!'));
195 function showPageNotice()
197 if ($this->mode != 'sent') {
199 $this->element('p', 'error', $this->error);
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');
210 function showForm($error=null)
212 $this->mode = 'form';
213 $this->error = $error;
217 function showInviteForm()
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());
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');
250 function sendInvitation($email, $user, $personal)
252 $profile = $user->getProfile();
253 $bestname = $profile->getBestName();
255 $sitename = common_config('site', 'name');
257 $invite = new Invitation();
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();
265 if (!$invite->insert()) {
266 common_log_db_error($invite, 'INSERT', __FILE__);
270 $confirmUrl = common_local_url('register', array('code' => $invite->code));
272 $recipients = array($email);
274 $headers['From'] = mail_notify_from();
275 $headers['To'] = trim($email);
276 $headers['Content-Type'] = 'text/html; charset=UTF-8';
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.
282 $headers['Subject'] = sprintf(_('%1$s has invited you to join them on %2$s'), $bestname, $sitename);
284 $title = (empty($personal)) ? 'invite' : 'invitepersonal';
286 $inviteTemplate = DocFile::forTitle($title, DocFile::mailPaths());
288 $body = $inviteTemplate->toHTML(array('inviter' => $bestname,
289 'inviteurl' => $profile->profileurl,
290 'confirmurl' => $confirmUrl,
291 'personal' => $personal));
293 common_debug('Confirm URL is ' . common_local_url('register', array('code' => $invite->code)));
295 mail_send($recipients, $headers, $body);