3 * StatusNet - a 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('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, true)) {
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);
103 if ($this->mode == 'sent') {
104 return _('Invitation(s) sent');
106 return _('Invite new users');
110 function showContent()
112 if ($this->mode == 'sent') {
113 $this->showInvitationSuccess();
115 $this->showInviteForm();
119 function showInvitationSuccess()
121 if ($this->already) {
122 $this->element('p', null, _('You are already subscribed to these users:'));
123 $this->elementStart('ul');
124 foreach ($this->already as $other) {
125 $this->element('li', null, sprintf(_('%s (%s)'), $other->nickname, $other->email));
127 $this->elementEnd('ul');
130 $this->element('p', null, _('These people are already users and you were automatically subscribed to them:'));
131 $this->elementStart('ul');
132 foreach ($this->subbed as $other) {
133 $this->element('li', null, sprintf(_('%s (%s)'), $other->nickname, $other->email));
135 $this->elementEnd('ul');
138 $this->element('p', null, _('Invitation(s) sent to the following people:'));
139 $this->elementStart('ul');
140 foreach ($this->sent as $other) {
141 $this->element('li', null, $other);
143 $this->elementEnd('ul');
144 $this->element('p', null, _('You will be notified when your invitees accept the invitation and register on the site. Thanks for growing the community!'));
148 function showPageNotice()
150 if ($this->mode != 'sent') {
152 $this->element('p', 'error', $this->error);
154 $this->elementStart('div', 'instructions');
155 $this->element('p', null,
156 _('Use this form to invite your friends and colleagues to use this service.'));
157 $this->elementEnd('div');
162 function showForm($error=null)
164 $this->mode = 'form';
165 $this->error = $error;
169 function showInviteForm()
171 $this->elementStart('form', array('method' => 'post',
172 'id' => 'form_invite',
173 'class' => 'form_settings',
174 'action' => common_local_url('invite')));
175 $this->elementStart('fieldset');
176 $this->element('legend', null, 'Send an invitation');
177 $this->hidden('token', common_session_token());
179 $this->elementStart('ul', 'form_data');
180 $this->elementStart('li');
181 $this->textarea('addresses', _('Email addresses'),
182 $this->trimmed('addresses'),
183 _('Addresses of friends to invite (one per line)'));
184 $this->elementEnd('li');
185 $this->elementStart('li');
186 $this->textarea('personal', _('Personal message'),
187 $this->trimmed('personal'),
188 _('Optionally add a personal message to the invitation.'));
189 $this->elementEnd('li');
190 $this->elementEnd('ul');
191 $this->submit('send', _('Send'));
192 $this->elementEnd('fieldset');
193 $this->elementEnd('form');
196 function sendInvitation($email, $user, $personal)
198 $profile = $user->getProfile();
199 $bestname = $profile->getBestName();
201 $sitename = common_config('site', 'name');
203 $invite = new Invitation();
205 $invite->address = $email;
206 $invite->address_type = 'email';
207 $invite->code = common_confirmation_code(128);
208 $invite->user_id = $user->id;
209 $invite->created = common_sql_now();
211 if (!$invite->insert()) {
212 common_log_db_error($invite, 'INSERT', __FILE__);
216 $recipients = array($email);
218 $headers['From'] = mail_notify_from();
219 $headers['To'] = trim($email);
220 $headers['Subject'] = sprintf(_('%1$s has invited you to join them on %2$s'), $bestname, $sitename);
222 $body = sprintf(_("%1\$s has invited you to join them on %2\$s (%3\$s).\n\n".
223 "%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".
224 "You can also share news about yourself, your thoughts, or your life online with people who know about you. ".
225 "It's also great for meeting new people who share your interests.\n\n".
226 "%1\$s said:\n\n%4\$s\n\n".
227 "You can see %1\$s's profile page on %2\$s here:\n\n".
229 "If you'd like to try the service, click on the link below to accept the invitation.\n\n".
231 "If not, you can ignore this message. Thanks for your patience and your time.\n\n".
232 "Sincerely, %2\$s\n"),
237 common_local_url('showstream', array('nickname' => $user->nickname)),
238 common_local_url((!common_config('site', 'openidonly')) ? 'register' : 'openidlogin', array('code' => $invite->code)));
240 mail_send($recipients, $headers, $body);
243 function showLocalNav()
245 $nav = new SubGroupNav($this, common_current_user());