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
31 function handle($args)
33 parent::handle($args);
34 if (!common_logged_in()) {
35 $this->clientError(sprintf(_('You must be logged in to invite other users to use %s'),
36 common_config('site', 'name')));
38 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
39 $this->sendInvitations();
45 function sendInvitations()
48 $token = $this->trimmed('token');
49 if (!$token || $token != common_session_token()) {
50 $this->showForm(_('There was a problem with your session token. Try again, please.'));
54 $user = common_current_user();
55 $profile = $user->getProfile();
57 $bestname = $profile->getBestName();
58 $sitename = common_config('site', 'name');
59 $personal = $this->trimmed('personal');
61 $addresses = explode("\n", $this->trimmed('addresses'));
63 foreach ($addresses as $email) {
64 $email = trim($email);
65 if (!Validate::email($email, true)) {
66 $this->showForm(sprintf(_('Invalid email address: %s'), $email));
71 $this->already = array();
72 $this->subbed = array();
74 foreach ($addresses as $email) {
75 $email = common_canonical_email($email);
76 $other = User::staticGet('email', $email);
78 if ($user->isSubscribed($other)) {
79 $this->already[] = $other;
81 subs_subscribe_to($user, $other);
82 $this->subbed[] = $other;
85 $this->sent[] = $email;
86 $this->sendInvitation($email, $user, $personal);
97 if ($this->mode == 'sent') {
98 return _('Invitation(s) sent');
100 return _('Invite new users');
104 function showContent()
106 if ($this->mode == 'sent') {
107 $this->showInvitationSuccess();
109 $this->showInviteForm();
113 function showInvitationSuccess()
115 if ($this->already) {
116 $this->element('p', null, _('You are already subscribed to these users:'));
117 $this->elementStart('ul');
118 foreach ($this->already as $other) {
119 $this->element('li', null, sprintf(_('%s (%s)'), $other->nickname, $other->email));
121 $this->elementEnd('ul');
124 $this->element('p', null, _('These people are already users and you were automatically subscribed to them:'));
125 $this->elementStart('ul');
126 foreach ($this->subbed as $other) {
127 $this->element('li', null, sprintf(_('%s (%s)'), $other->nickname, $other->email));
129 $this->elementEnd('ul');
132 $this->element('p', null, _('Invitation(s) sent to the following people:'));
133 $this->elementStart('ul');
134 foreach ($this->sent as $other) {
135 $this->element('li', null, $other);
137 $this->elementEnd('ul');
138 $this->element('p', null, _('You will be notified when your invitees accept the invitation and register on the site. Thanks for growing the community!'));
142 function showPageNotice()
144 if ($this->mode != 'sent') {
146 $this->element('p', 'error', $this->error);
148 $this->elementStart('div', 'instructions');
149 $this->element('p', null,
150 _('Use this form to invite your friends and colleagues to use this service.'));
151 $this->elementEnd('div');
156 function showForm($error=null)
158 $this->mode = 'form';
159 $this->error = $error;
163 function showInviteForm()
165 $this->elementStart('form', array('method' => 'post',
167 'action' => common_local_url('invite')));
168 $this->hidden('token', common_session_token());
170 $this->textarea('addresses', _('Email addresses'),
171 $this->trimmed('addresses'),
172 _('Addresses of friends to invite (one per line)'));
174 $this->textarea('personal', _('Personal message'),
175 $this->trimmed('personal'),
176 _('Optionally add a personal message to the invitation.'));
178 $this->submit('send', _('Send'));
180 $this->elementEnd('form');
183 function sendInvitation($email, $user, $personal)
185 $profile = $user->getProfile();
186 $bestname = $profile->getBestName();
188 $sitename = common_config('site', 'name');
190 $invite = new Invitation();
192 $invite->address = $email;
193 $invite->address_type = 'email';
194 $invite->code = common_confirmation_code(128);
195 $invite->user_id = $user->id;
196 $invite->created = common_sql_now();
198 if (!$invite->insert()) {
199 common_log_db_error($invite, 'INSERT', __FILE__);
203 $recipients = array($email);
205 $headers['From'] = mail_notify_from();
206 $headers['To'] = $email;
207 $headers['Subject'] = sprintf(_('%1$s has invited you to join them on %2$s'), $bestname, $sitename);
209 $body = sprintf(_("%1\$s has invited you to join them on %2\$s (%3\$s).\n\n".
210 "%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".
211 "You can also share news about yourself, your thoughts, or your life online with people who know about you. ".
212 "It's also great for meeting new people who share your interests.\n\n".
213 "%1\$s said:\n\n%4\$s\n\n".
214 "You can see %1\$s's profile page on %2\$s here:\n\n".
216 "If you'd like to try the service, click on the link below to accept the invitation.\n\n".
218 "If not, you can ignore this message. Thanks for your patience and your time.\n\n".
219 "Sincerely, %2\$s\n"),
224 common_local_url('showstream', array('nickname' => $user->nickname)),
225 common_local_url('register', array('code' => $invite->code)));
227 mail_send($recipients, $headers, $body);
230 function showLocalNav()
232 $nav = new SubGroupNav($this, common_current_user());