]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/invite.php
Add attributes for invite
[quix0rs-gnu-social.git] / actions / invite.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 class InviteAction extends Action
23 {
24     var $mode = null;
25     var $error = null;
26     var $already = null;
27     var $subbed = null;
28     var $sent = null;
29
30     function isReadOnly()
31     {
32         return false;
33     }
34
35     function handle($args)
36     {
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')));
41             return;
42         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
43             $this->sendInvitations();
44         } else {
45             $this->showForm();
46         }
47     }
48
49     function sendInvitations()
50     {
51         # CSRF protection
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.'));
55             return;
56         }
57
58         $user = common_current_user();
59         $profile = $user->getProfile();
60
61         $bestname = $profile->getBestName();
62         $sitename = common_config('site', 'name');
63         $personal = $this->trimmed('personal');
64
65         $addresses = explode("\n", $this->trimmed('addresses'));
66
67         foreach ($addresses as $email) {
68             $email = trim($email);
69             if (!Validate::email($email, true)) {
70                 $this->showForm(sprintf(_('Invalid email address: %s'), $email));
71                 return;
72             }
73         }
74
75         $this->already = array();
76         $this->subbed = array();
77
78         foreach ($addresses as $email) {
79             $email = common_canonical_email($email);
80             $other = User::staticGet('email', $email);
81             if ($other) {
82                 if ($user->isSubscribed($other)) {
83                     $this->already[] = $other;
84                 } else {
85                     subs_subscribe_to($user, $other);
86                     $this->subbed[] = $other;
87                 }
88             } else {
89                 $this->sent[] = $email;
90                 $this->sendInvitation($email, $user, $personal);
91             }
92         }
93
94         $this->mode = 'sent';
95
96         $this->showPage();
97     }
98
99     function title()
100     {
101         if ($this->mode == 'sent') {
102             return _('Invitation(s) sent');
103         } else {
104             return _('Invite new users');
105         }
106     }
107
108     function showContent()
109     {
110         if ($this->mode == 'sent') {
111             $this->showInvitationSuccess();
112         } else {
113             $this->showInviteForm();
114         }
115     }
116
117     function showInvitationSuccess()
118     {
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));
124             }
125             $this->elementEnd('ul');
126         }
127         if ($this->subbed) {
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));
132             }
133             $this->elementEnd('ul');
134         }
135         if ($this->sent) {
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);
140             }
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!'));
143         }
144     }
145
146     function showPageNotice()
147     {
148         if ($this->mode != 'sent') {
149             if ($this->error) {
150                 $this->element('p', 'error', $this->error);
151             } else {
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');
156             }
157         }
158     }
159
160     function showForm($error=null)
161     {
162         $this->mode = 'form';
163         $this->error = $error;
164         $this->showPage();
165     }
166
167     function showInviteForm()
168     {
169         $this->elementStart('form', array('method' => 'post',
170                                            'id' => 'invite',
171                                            'action' => common_local_url('invite')));
172         $this->hidden('token', common_session_token());
173
174         $this->textarea('addresses', _('Email addresses'),
175                         $this->trimmed('addresses'),
176                         _('Addresses of friends to invite (one per line)'));
177
178         $this->textarea('personal', _('Personal message'),
179                         $this->trimmed('personal'),
180                         _('Optionally add a personal message to the invitation.'));
181
182         $this->submit('send', _('Send'));
183
184         $this->elementEnd('form');
185     }
186
187     function sendInvitation($email, $user, $personal)
188     {
189         $profile = $user->getProfile();
190         $bestname = $profile->getBestName();
191
192         $sitename = common_config('site', 'name');
193
194         $invite = new Invitation();
195
196         $invite->address = $email;
197         $invite->address_type = 'email';
198         $invite->code = common_confirmation_code(128);
199         $invite->user_id = $user->id;
200         $invite->created = common_sql_now();
201
202         if (!$invite->insert()) {
203             common_log_db_error($invite, 'INSERT', __FILE__);
204             return false;
205         }
206
207         $recipients = array($email);
208
209         $headers['From'] = mail_notify_from();
210         $headers['To'] = $email;
211         $headers['Subject'] = sprintf(_('%1$s has invited you to join them on %2$s'), $bestname, $sitename);
212
213         $body = sprintf(_("%1\$s has invited you to join them on %2\$s (%3\$s).\n\n".
214                           "%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".
215                           "You can also share news about yourself, your thoughts, or your life online with people who know about you. ".
216                           "It's also great for meeting new people who share your interests.\n\n".
217                           "%1\$s said:\n\n%4\$s\n\n".
218                           "You can see %1\$s's profile page on %2\$s here:\n\n".
219                           "%5\$s\n\n".
220                           "If you'd like to try the service, click on the link below to accept the invitation.\n\n".
221                           "%6\$s\n\n".
222                           "If not, you can ignore this message. Thanks for your patience and your time.\n\n".
223                           "Sincerely, %2\$s\n"),
224                         $bestname,
225                         $sitename,
226                         common_root_url(),
227                         $personal,
228                         common_local_url('showstream', array('nickname' => $user->nickname)),
229                         common_local_url('register', array('code' => $invite->code)));
230
231         mail_send($recipients, $headers, $body);
232     }
233
234     function showLocalNav()
235     {
236         $nav = new SubGroupNav($this, common_current_user());
237         $nav->show();
238     }
239 }