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 class InviteAction extends CurrentUserDesignAction
30 function isReadOnly($args)
35 function handle($args)
37 parent::handle($args);
38 if (!common_config('invite', 'enabled')) {
39 $this->clientError(_('Invites have been disabled.'));
40 } else if (!common_logged_in()) {
41 $this->clientError(sprintf(_('You must be logged in to invite other users to use %s'),
42 common_config('site', 'name')));
44 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
45 $this->sendInvitations();
51 function sendInvitations()
54 $token = $this->trimmed('token');
55 if (!$token || $token != common_session_token()) {
56 $this->showForm(_('There was a problem with your session token. Try again, please.'));
60 $user = common_current_user();
61 $profile = $user->getProfile();
63 $bestname = $profile->getBestName();
64 $sitename = common_config('site', 'name');
65 $personal = $this->trimmed('personal');
67 $addresses = explode("\n", $this->trimmed('addresses'));
69 foreach ($addresses as $email) {
70 $email = trim($email);
71 if (!Validate::email($email, common_config('email', 'check_domain'))) {
72 $this->showForm(sprintf(_('Invalid email address: %s'), $email));
77 $this->already = array();
78 $this->subbed = array();
80 foreach ($addresses as $email) {
81 $email = common_canonical_email($email);
82 $other = User::staticGet('email', $email);
84 if ($user->isSubscribed($other)) {
85 $this->already[] = $other;
87 subs_subscribe_to($user, $other);
88 $this->subbed[] = $other;
91 $this->sent[] = $email;
92 $this->sendInvitation($email, $user, $personal);
101 function showScripts()
103 parent::showScripts();
104 $this->autofocus('addresses');
109 if ($this->mode == 'sent') {
110 return _('Invitation(s) sent');
112 return _('Invite new users');
116 function showContent()
118 if ($this->mode == 'sent') {
119 $this->showInvitationSuccess();
121 $this->showInviteForm();
125 function showInvitationSuccess()
127 if ($this->already) {
128 $this->element('p', null, _('You are already subscribed to these users:'));
129 $this->elementStart('ul');
130 foreach ($this->already as $other) {
131 $this->element('li', null, sprintf(_('%s (%s)'), $other->nickname, $other->email));
133 $this->elementEnd('ul');
136 $this->element('p', null, _('These people are already users and you were automatically subscribed to them:'));
137 $this->elementStart('ul');
138 foreach ($this->subbed as $other) {
139 $this->element('li', null, sprintf(_('%s (%s)'), $other->nickname, $other->email));
141 $this->elementEnd('ul');
144 $this->element('p', null, _('Invitation(s) sent to the following people:'));
145 $this->elementStart('ul');
146 foreach ($this->sent as $other) {
147 $this->element('li', null, $other);
149 $this->elementEnd('ul');
150 $this->element('p', null, _('You will be notified when your invitees accept the invitation and register on the site. Thanks for growing the community!'));
154 function showPageNotice()
156 if ($this->mode != 'sent') {
158 $this->element('p', 'error', $this->error);
160 $this->elementStart('div', 'instructions');
161 $this->element('p', null,
162 _('Use this form to invite your friends and colleagues to use this service.'));
163 $this->elementEnd('div');
168 function showForm($error=null)
170 $this->mode = 'form';
171 $this->error = $error;
175 function showInviteForm()
177 $this->elementStart('form', array('method' => 'post',
178 'id' => 'form_invite',
179 'class' => 'form_settings',
180 'action' => common_local_url('invite')));
181 $this->elementStart('fieldset');
182 $this->element('legend', null, 'Send an invitation');
183 $this->hidden('token', common_session_token());
185 $this->elementStart('ul', 'form_data');
186 $this->elementStart('li');
187 $this->textarea('addresses', _('Email addresses'),
188 $this->trimmed('addresses'),
189 _('Addresses of friends to invite (one per line)'));
190 $this->elementEnd('li');
191 $this->elementStart('li');
192 $this->textarea('personal', _('Personal message'),
193 $this->trimmed('personal'),
194 _('Optionally add a personal message to the invitation.'));
195 $this->elementEnd('li');
196 $this->elementEnd('ul');
197 $this->submit('send', _('Send'));
198 $this->elementEnd('fieldset');
199 $this->elementEnd('form');
202 function sendInvitation($email, $user, $personal)
204 $profile = $user->getProfile();
205 $bestname = $profile->getBestName();
207 $sitename = common_config('site', 'name');
209 $invite = new Invitation();
211 $invite->address = $email;
212 $invite->address_type = 'email';
213 $invite->code = common_confirmation_code(128);
214 $invite->user_id = $user->id;
215 $invite->created = common_sql_now();
217 if (!$invite->insert()) {
218 common_log_db_error($invite, 'INSERT', __FILE__);
222 $recipients = array($email);
224 $headers['From'] = mail_notify_from();
225 $headers['To'] = trim($email);
226 $headers['Subject'] = sprintf(_('%1$s has invited you to join them on %2$s'), $bestname, $sitename);
228 $body = sprintf(_("%1\$s has invited you to join them on %2\$s (%3\$s).\n\n".
229 "%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".
230 "You can also share news about yourself, your thoughts, or your life online with people who know about you. ".
231 "It's also great for meeting new people who share your interests.\n\n".
232 "%1\$s said:\n\n%4\$s\n\n".
233 "You can see %1\$s's profile page on %2\$s here:\n\n".
235 "If you'd like to try the service, click on the link below to accept the invitation.\n\n".
237 "If not, you can ignore this message. Thanks for your patience and your time.\n\n".
238 "Sincerely, %2\$s\n"),
243 common_local_url('showstream', array('nickname' => $user->nickname)),
244 common_local_url('register', array('code' => $invite->code)));
246 mail_send($recipients, $headers, $body);
249 function showLocalNav()
251 $nav = new SubGroupNav($this, common_current_user());