We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

invite.php 8.75 KB
Newer Older
Evan Prodromou's avatar
Evan Prodromou committed
1 2 3
<?php
/*
 * Laconica - a distributed open-source microblogging tool
4
 * Copyright (C) 2008, 2009, Control Yourself, Inc.
Evan Prodromou's avatar
Evan Prodromou committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

if (!defined('LACONICA')) { exit(1); }

22
class InviteAction extends CurrentUserDesignAction
23
{
Evan Prodromou's avatar
Evan Prodromou committed
24
    var $mode = null;
Evan Prodromou's avatar
Evan Prodromou committed
25 26 27 28
    var $error = null;
    var $already = null;
    var $subbed = null;
    var $sent = null;
Evan Prodromou's avatar
Evan Prodromou committed
29

30
    function isReadOnly($args)
31
    {
32 33
        return false;
    }
Evan Prodromou's avatar
Evan Prodromou committed
34

35 36
    function handle($args)
    {
Evan Prodromou's avatar
Evan Prodromou committed
37
        parent::handle($args);
38 39 40
        if (!common_config('invite', 'enabled')) {
            $this->clientError(_('Invites have been disabled.'));
        } else if (!common_logged_in()) {
41
            $this->clientError(sprintf(_('You must be logged in to invite other users to use %s'),
42 43 44
                                        common_config('site', 'name')));
            return;
        } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
Evan Prodromou's avatar
Evan Prodromou committed
45
            $this->sendInvitations();
46
        } else {
Evan Prodromou's avatar
Evan Prodromou committed
47
            $this->showForm();
48 49 50
        }
    }

Evan Prodromou's avatar
Evan Prodromou committed
51
    function sendInvitations()
52
    {
53 54 55
        # CSRF protection
        $token = $this->trimmed('token');
        if (!$token || $token != common_session_token()) {
Evan Prodromou's avatar
Evan Prodromou committed
56
            $this->showForm(_('There was a problem with your session token. Try again, please.'));
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
            return;
        }

        $user = common_current_user();
        $profile = $user->getProfile();

        $bestname = $profile->getBestName();
        $sitename = common_config('site', 'name');
        $personal = $this->trimmed('personal');

        $addresses = explode("\n", $this->trimmed('addresses'));

        foreach ($addresses as $email) {
            $email = trim($email);
            if (!Validate::email($email, true)) {
Evan Prodromou's avatar
Evan Prodromou committed
72
                $this->showForm(sprintf(_('Invalid email address: %s'), $email));
73 74 75 76
                return;
            }
        }

Evan Prodromou's avatar
Evan Prodromou committed
77 78
        $this->already = array();
        $this->subbed = array();
79 80 81 82 83 84

        foreach ($addresses as $email) {
            $email = common_canonical_email($email);
            $other = User::staticGet('email', $email);
            if ($other) {
                if ($user->isSubscribed($other)) {
Evan Prodromou's avatar
Evan Prodromou committed
85
                    $this->already[] = $other;
86 87
                } else {
                    subs_subscribe_to($user, $other);
Evan Prodromou's avatar
Evan Prodromou committed
88
                    $this->subbed[] = $other;
89 90
                }
            } else {
Evan Prodromou's avatar
Evan Prodromou committed
91 92
                $this->sent[] = $email;
                $this->sendInvitation($email, $user, $personal);
93 94 95
            }
        }

Evan Prodromou's avatar
Evan Prodromou committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        $this->mode = 'sent';

        $this->showPage();
    }

    function title()
    {
        if ($this->mode == 'sent') {
            return _('Invitation(s) sent');
        } else {
            return _('Invite new users');
        }
    }

    function showContent()
    {
        if ($this->mode == 'sent') {
            $this->showInvitationSuccess();
        } else {
            $this->showInviteForm();
        }
    }

    function showInvitationSuccess()
    {
        if ($this->already) {
122 123
            $this->element('p', null, _('You are already subscribed to these users:'));
            $this->elementStart('ul');
Evan Prodromou's avatar
Evan Prodromou committed
124
            foreach ($this->already as $other) {
125
                $this->element('li', null, sprintf(_('%s (%s)'), $other->nickname, $other->email));
126
            }
127
            $this->elementEnd('ul');
128
        }
Evan Prodromou's avatar
Evan Prodromou committed
129
        if ($this->subbed) {
130 131
            $this->element('p', null, _('These people are already users and you were automatically subscribed to them:'));
            $this->elementStart('ul');
Evan Prodromou's avatar
Evan Prodromou committed
132
            foreach ($this->subbed as $other) {
133
                $this->element('li', null, sprintf(_('%s (%s)'), $other->nickname, $other->email));
134
            }
135
            $this->elementEnd('ul');
136
        }
Evan Prodromou's avatar
Evan Prodromou committed
137
        if ($this->sent) {
138 139
            $this->element('p', null, _('Invitation(s) sent to the following people:'));
            $this->elementStart('ul');
Evan Prodromou's avatar
Evan Prodromou committed
140
            foreach ($this->sent as $other) {
141
                $this->element('li', null, $other);
142
            }
143 144
            $this->elementEnd('ul');
            $this->element('p', null, _('You will be notified when your invitees accept the invitation and register on the site. Thanks for growing the community!'));
145 146 147
        }
    }

Evan Prodromou's avatar
Evan Prodromou committed
148
    function showPageNotice()
149
    {
Evan Prodromou's avatar
Evan Prodromou committed
150 151 152 153 154 155 156 157 158
        if ($this->mode != 'sent') {
            if ($this->error) {
                $this->element('p', 'error', $this->error);
            } else {
                $this->elementStart('div', 'instructions');
                $this->element('p', null,
                               _('Use this form to invite your friends and colleagues to use this service.'));
                $this->elementEnd('div');
            }
159 160 161
        }
    }

Evan Prodromou's avatar
Evan Prodromou committed
162
    function showForm($error=null)
163
    {
Evan Prodromou's avatar
Evan Prodromou committed
164 165 166 167
        $this->mode = 'form';
        $this->error = $error;
        $this->showPage();
    }
168

Evan Prodromou's avatar
Evan Prodromou committed
169 170
    function showInviteForm()
    {
171
        $this->elementStart('form', array('method' => 'post',
sarven's avatar
sarven committed
172 173
                                           'id' => 'form_invite',
                                           'class' => 'form_settings',
174
                                           'action' => common_local_url('invite')));
sarven's avatar
sarven committed
175 176
        $this->elementStart('fieldset');
        $this->element('legend', null, 'Send an invitation');
177
        $this->hidden('token', common_session_token());
178

sarven's avatar
sarven committed
179 180
        $this->elementStart('ul', 'form_data');
        $this->elementStart('li');
181
        $this->textarea('addresses', _('Email addresses'),
182 183
                        $this->trimmed('addresses'),
                        _('Addresses of friends to invite (one per line)'));
sarven's avatar
sarven committed
184 185
        $this->elementEnd('li');
        $this->elementStart('li');
186
        $this->textarea('personal', _('Personal message'),
187 188
                        $this->trimmed('personal'),
                        _('Optionally add a personal message to the invitation.'));
sarven's avatar
sarven committed
189 190
        $this->elementEnd('li');
        $this->elementEnd('ul');
191
        $this->submit('send', _('Send'));
sarven's avatar
sarven committed
192
        $this->elementEnd('fieldset');
193
        $this->elementEnd('form');
194 195
    }

Evan Prodromou's avatar
Evan Prodromou committed
196
    function sendInvitation($email, $user, $personal)
197
    {
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
        $profile = $user->getProfile();
        $bestname = $profile->getBestName();

        $sitename = common_config('site', 'name');

        $invite = new Invitation();

        $invite->address = $email;
        $invite->address_type = 'email';
        $invite->code = common_confirmation_code(128);
        $invite->user_id = $user->id;
        $invite->created = common_sql_now();

        if (!$invite->insert()) {
            common_log_db_error($invite, 'INSERT', __FILE__);
            return false;
        }

        $recipients = array($email);

        $headers['From'] = mail_notify_from();
219
        $headers['To'] = trim($email);
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
        $headers['Subject'] = sprintf(_('%1$s has invited you to join them on %2$s'), $bestname, $sitename);

        $body = sprintf(_("%1\$s has invited you to join them on %2\$s (%3\$s).\n\n".
                          "%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".
                          "You can also share news about yourself, your thoughts, or your life online with people who know about you. ".
                          "It's also great for meeting new people who share your interests.\n\n".
                          "%1\$s said:\n\n%4\$s\n\n".
                          "You can see %1\$s's profile page on %2\$s here:\n\n".
                          "%5\$s\n\n".
                          "If you'd like to try the service, click on the link below to accept the invitation.\n\n".
                          "%6\$s\n\n".
                          "If not, you can ignore this message. Thanks for your patience and your time.\n\n".
                          "Sincerely, %2\$s\n"),
                        $bestname,
                        $sitename,
                        common_root_url(),
                        $personal,
                        common_local_url('showstream', array('nickname' => $user->nickname)),
238
                        common_local_url('register', array('code' => $invite->code)));
239 240 241

        mail_send($recipients, $headers, $body);
    }
Evan Prodromou's avatar
Evan Prodromou committed
242

Evan Prodromou's avatar
Evan Prodromou committed
243 244 245 246 247
    function showLocalNav()
    {
        $nav = new SubGroupNav($this, common_current_user());
        $nav->show();
    }
Evan Prodromou's avatar
Evan Prodromou committed
248
}