]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/invite.php
isReadOnly() now takes arguments
[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($args)
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' => '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());
176
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');
192     }
193
194     function sendInvitation($email, $user, $personal)
195     {
196         $profile = $user->getProfile();
197         $bestname = $profile->getBestName();
198
199         $sitename = common_config('site', 'name');
200
201         $invite = new Invitation();
202
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();
208
209         if (!$invite->insert()) {
210             common_log_db_error($invite, 'INSERT', __FILE__);
211             return false;
212         }
213
214         $recipients = array($email);
215
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);
219
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".
226                           "%5\$s\n\n".
227                           "If you'd like to try the service, click on the link below to accept the invitation.\n\n".
228                           "%6\$s\n\n".
229                           "If not, you can ignore this message. Thanks for your patience and your time.\n\n".
230                           "Sincerely, %2\$s\n"),
231                         $bestname,
232                         $sitename,
233                         common_root_url(),
234                         $personal,
235                         common_local_url('showstream', array('nickname' => $user->nickname)),
236                         common_local_url('register', array('code' => $invite->code)));
237
238         mail_send($recipients, $headers, $body);
239     }
240
241     function showLocalNav()
242     {
243         $nav = new SubGroupNav($this, common_current_user());
244         $nav->show();
245     }
246 }