3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, 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 Action
30 function isReadOnly($args)
35 function handle($args)
37 parent::handle($args);
38 if (!common_logged_in()) {
39 $this->clientError(sprintf(_('You must be logged in to invite other users to use %s'),
40 common_config('site', 'name')));
42 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
43 $this->sendInvitations();
49 function sendInvitations()
52 $token = $this->trimmed('token');
53 if (!$token || $token != common_session_token()) {
54 $this->showForm(_('There was a problem with your session token. Try again, please.'));
58 $user = common_current_user();
59 $profile = $user->getProfile();
61 $bestname = $profile->getBestName();
62 $sitename = common_config('site', 'name');
63 $personal = $this->trimmed('personal');
65 $addresses = explode("\n", $this->trimmed('addresses'));
67 foreach ($addresses as $email) {
68 $email = trim($email);
69 if (!Validate::email($email, true)) {
70 $this->showForm(sprintf(_('Invalid email address: %s'), $email));
75 $this->already = array();
76 $this->subbed = array();
78 foreach ($addresses as $email) {
79 $email = common_canonical_email($email);
80 $other = User::staticGet('email', $email);
82 if ($user->isSubscribed($other)) {
83 $this->already[] = $other;
85 subs_subscribe_to($user, $other);
86 $this->subbed[] = $other;
89 $this->sent[] = $email;
90 $this->sendInvitation($email, $user, $personal);
101 if ($this->mode == 'sent') {
102 return _('Invitation(s) sent');
104 return _('Invite new users');
108 function showContent()
110 if ($this->mode == 'sent') {
111 $this->showInvitationSuccess();
113 $this->showInviteForm();
117 function showInvitationSuccess()
119 if ($this->already) {
120 $this->element('p', null, _('You are already subscribed to these users:'));
121 $this->elementStart('ul');
122 foreach ($this->already as $other) {
123 $this->element('li', null, sprintf(_('%s (%s)'), $other->nickname, $other->email));
125 $this->elementEnd('ul');
128 $this->element('p', null, _('These people are already users and you were automatically subscribed to them:'));
129 $this->elementStart('ul');
130 foreach ($this->subbed as $other) {
131 $this->element('li', null, sprintf(_('%s (%s)'), $other->nickname, $other->email));
133 $this->elementEnd('ul');
136 $this->element('p', null, _('Invitation(s) sent to the following people:'));
137 $this->elementStart('ul');
138 foreach ($this->sent as $other) {
139 $this->element('li', null, $other);
141 $this->elementEnd('ul');
142 $this->element('p', null, _('You will be notified when your invitees accept the invitation and register on the site. Thanks for growing the community!'));
146 function showPageNotice()
148 if ($this->mode != 'sent') {
150 $this->element('p', 'error', $this->error);
152 $this->elementStart('div', 'instructions');
153 $this->element('p', null,
154 _('Use this form to invite your friends and colleagues to use this service.'));
155 $this->elementEnd('div');
160 function showForm($error=null)
162 $this->mode = 'form';
163 $this->error = $error;
167 function showInviteForm()
169 $this->elementStart('form', array('method' => 'post',
170 'id' => 'form_invite',
171 'class' => 'form_settings',
172 'action' => common_local_url('invite')));
173 $this->elementStart('fieldset');
174 $this->element('legend', null, 'Send an invitation');
175 $this->hidden('token', common_session_token());
177 $this->elementStart('ul', 'form_data');
178 $this->elementStart('li');
179 $this->textarea('addresses', _('Email addresses'),
180 $this->trimmed('addresses'),
181 _('Addresses of friends to invite (one per line)'));
182 $this->elementEnd('li');
183 $this->elementStart('li');
184 $this->textarea('personal', _('Personal message'),
185 $this->trimmed('personal'),
186 _('Optionally add a personal message to the invitation.'));
187 $this->elementEnd('li');
188 $this->elementEnd('ul');
189 $this->submit('send', _('Send'));
190 $this->elementEnd('fieldset');
191 $this->elementEnd('form');
194 function sendInvitation($email, $user, $personal)
196 $profile = $user->getProfile();
197 $bestname = $profile->getBestName();
199 $sitename = common_config('site', 'name');
201 $invite = new Invitation();
203 $invite->address = $email;
204 $invite->address_type = 'email';
205 $invite->code = common_confirmation_code(128);
206 $invite->user_id = $user->id;
207 $invite->created = common_sql_now();
209 if (!$invite->insert()) {
210 common_log_db_error($invite, 'INSERT', __FILE__);
214 $recipients = array($email);
216 $headers['From'] = mail_notify_from();
217 $headers['To'] = $email;
218 $headers['Subject'] = sprintf(_('%1$s has invited you to join them on %2$s'), $bestname, $sitename);
220 $body = sprintf(_("%1\$s has invited you to join them on %2\$s (%3\$s).\n\n".
221 "%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".
222 "You can also share news about yourself, your thoughts, or your life online with people who know about you. ".
223 "It's also great for meeting new people who share your interests.\n\n".
224 "%1\$s said:\n\n%4\$s\n\n".
225 "You can see %1\$s's profile page on %2\$s here:\n\n".
227 "If you'd like to try the service, click on the link below to accept the invitation.\n\n".
229 "If not, you can ignore this message. Thanks for your patience and your time.\n\n".
230 "Sincerely, %2\$s\n"),
235 common_local_url('showstream', array('nickname' => $user->nickname)),
236 common_local_url('register', array('code' => $invite->code)));
238 mail_send($recipients, $headers, $body);
241 function showLocalNav()
243 $nav = new SubGroupNav($this, common_current_user());