3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008-2011, 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()
62 if (Event::handle('StartSendInvitations', array(&$this))) {
65 $token = $this->trimmed('token');
66 if (!$token || $token != common_session_token()) {
67 // TRANS: Client error displayed when the session token does not match or is not given.
68 $this->showForm(_('There was a problem with your session token. Try again, please.'));
72 $user = common_current_user();
73 $profile = $user->getProfile();
75 $bestname = $profile->getBestName();
76 $sitename = common_config('site', 'name');
77 $personal = $this->trimmed('personal');
79 $addresses = explode("\n", $this->trimmed('addresses'));
80 foreach ($addresses as $email) {
81 $email = trim($email);
86 if (Event::handle('StartValidateUserEmail', array(null, $email, &$valid))) {
87 $valid = Validate::email($email, common_config('email', 'check_domain'));
88 Event::handle('EndValidateUserEmail', array(null, $email, &$valid));
92 if (Event::handle('StartValidateEmailInvite', array($user, $email, &$valid))) {
94 Event::handle('EndValidateEmailInvite', array($user, $email, &$valid));
99 // TRANS: Form validation message when providing an e-mail address that does not validate.
100 // TRANS: %s is an invalid e-mail address.
101 $this->showForm(sprintf(_('Invalid email address: %s.'), $email));
104 } catch (ClientException $e) {
105 $this->showForm($e->getMessage());
110 $this->already = array();
111 $this->subbed = array();
113 foreach ($addresses as $email) {
114 $email = common_canonical_email($email);
115 $other = User::staticGet('email', $email);
117 if ($user->isSubscribed($other)) {
118 $this->already[] = $other;
120 subs_subscribe_to($user, $other);
121 $this->subbed[] = $other;
124 $this->sent[] = $email;
125 $this->sendInvitation($email, $user, $personal);
129 $this->mode = 'sent';
132 Event::handle('EndSendInvitations', array($this));
136 function showScripts()
138 parent::showScripts();
139 $this->autofocus('addresses');
144 if ($this->mode == 'sent') {
145 // TRANS: Page title when invitations have been sent.
146 return _('Invitations sent');
148 // TRANS: Page title when inviting potential users.
149 return _('Invite new users');
153 function showContent()
155 if ($this->mode == 'sent') {
156 $this->showInvitationSuccess();
158 $this->showInviteForm();
162 function showInvitationSuccess()
164 if (Event::handle('StartShowInvitationSuccess', array($this))) {
166 if ($this->already) {
167 // TRANS: Message displayed inviting users to use a StatusNet site while the inviting user
168 // TRANS: is already subscribed to one or more users with the given e-mail address(es).
169 // TRANS: Plural form is based on the number of reported already subscribed e-mail addresses.
170 // TRANS: Followed by a bullet list.
171 $this->element('p', null, _m('You are already subscribed to this user:',
172 'You are already subscribed to these users:',
173 count($this->already)));
174 $this->elementStart('ul');
175 foreach ($this->already as $other) {
176 // TRANS: Used as list item for already subscribed users (%1$s is nickname, %2$s is e-mail address).
177 $this->element('li', null, sprintf(_m('INVITE','%1$s (%2$s)'), $other->nickname, $other->email));
179 $this->elementEnd('ul');
182 // TRANS: Message displayed inviting users to use a StatusNet site while the invited user
183 // TRANS: already uses a this StatusNet site. Plural form is based on the number of
184 // TRANS: reported already present people. Followed by a bullet list.
185 $this->element('p', null, _m('This person is already a user and you were automatically subscribed:',
186 'These people are already users and you were automatically subscribed to them:',
187 count($this->subbed)));
188 $this->elementStart('ul');
189 foreach ($this->subbed as $other) {
190 // TRANS: Used as list item for already registered people (%1$s is nickname, %2$s is e-mail address).
191 $this->element('li', null, sprintf(_m('INVITE','%1$s (%2$s)'), $other->nickname, $other->email));
193 $this->elementEnd('ul');
196 // TRANS: Message displayed inviting users to use a StatusNet site. Plural form is
197 // TRANS: based on the number of invitations sent. Followed by a bullet list of
198 // TRANS: e-mail addresses to which invitations were sent.
199 $this->element('p', null, _m('Invitation sent to the following person:',
200 'Invitations sent to the following people:',
201 count($this->sent)));
202 $this->elementStart('ul');
203 foreach ($this->sent as $other) {
204 $this->element('li', null, $other);
206 $this->elementEnd('ul');
207 // TRANS: Generic message displayed after sending out one or more invitations to
208 // TRANS: people to join a StatusNet site.
209 $this->element('p', null, _('You will be notified when your invitees accept the invitation and register on the site. Thanks for growing the community!'));
211 Event::handle('EndShowInvitationSuccess', array($this));
215 function showPageNotice()
217 if ($this->mode != 'sent') {
219 $this->element('p', 'error', $this->error);
221 $this->elementStart('div', 'instructions');
222 $this->element('p', null,
223 // TRANS: Form instructions.
224 _('Use this form to invite your friends and colleagues to use this service.'));
225 $this->elementEnd('div');
230 function showForm($error=null)
232 $this->mode = 'form';
233 $this->error = $error;
237 function showInviteForm()
239 if (Event::handle('StartShowInviteForm', array($this))) {
240 $form = new InviteForm($this);
242 Event::handle('EndShowInviteForm', array($this));
246 function sendInvitation($email, $user, $personal)
248 $profile = $user->getProfile();
249 $bestname = $profile->getBestName();
251 $sitename = common_config('site', 'name');
253 $invite = new Invitation();
255 $invite->address = $email;
256 $invite->address_type = 'email';
257 $invite->code = common_confirmation_code(128);
258 $invite->user_id = $user->id;
259 $invite->created = common_sql_now();
261 if (!$invite->insert()) {
262 common_log_db_error($invite, 'INSERT', __FILE__);
266 $confirmUrl = common_local_url('register', array('code' => $invite->code));
268 $recipients = array($email);
270 $headers['From'] = mail_notify_from();
271 $headers['To'] = trim($email);
272 $headers['Content-Type'] = 'text/html; charset=UTF-8';
274 // TRANS: Subject for invitation email. Note that 'them' is correct as a gender-neutral
275 // TRANS: singular 3rd-person pronoun in English. %1$s is the inviting user, $2$s is
276 // TRANS: the StatusNet sitename.
277 $headers['Subject'] = sprintf(_('%1$s has invited you to join them on %2$s'), $bestname, $sitename);
279 $title = (empty($personal)) ? 'invite' : 'invitepersonal';
281 // @todo FIXME: i18n issue.
282 $inviteTemplate = DocFile::forTitle($title, DocFile::mailPaths());
284 $body = $inviteTemplate->toHTML(array('inviter' => $bestname,
285 'inviterurl' => $profile->profileurl,
286 'confirmurl' => $confirmUrl,
287 'personal' => $personal));
289 common_debug('Confirm URL is ' . common_local_url('register', array('code' => $invite->code)));
291 mail_send($recipients, $headers, $body);