]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/invite.php
* L10n updates: consistent puctuation
[quix0rs-gnu-social.git] / actions / invite.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, 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('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 class InviteAction extends CurrentUserDesignAction
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_config('invite', 'enabled')) {
39             $this->clientError(_('Invites have been disabled.'));
40         } else if (!common_logged_in()) {
41             $this->clientError(sprintf(_('You must be logged in to invite other users to use %s'),
42                                         common_config('site', 'name')));
43             return;
44         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
45             $this->sendInvitations();
46         } else {
47             $this->showForm();
48         }
49     }
50
51     function sendInvitations()
52     {
53         # CSRF protection
54         $token = $this->trimmed('token');
55         if (!$token || $token != common_session_token()) {
56             $this->showForm(_('There was a problem with your session token. Try again, please.'));
57             return;
58         }
59
60         $user = common_current_user();
61         $profile = $user->getProfile();
62
63         $bestname = $profile->getBestName();
64         $sitename = common_config('site', 'name');
65         $personal = $this->trimmed('personal');
66
67         $addresses = explode("\n", $this->trimmed('addresses'));
68
69         foreach ($addresses as $email) {
70             $email = trim($email);
71             if (!Validate::email($email, common_config('email', 'check_domain'))) {
72                 $this->showForm(sprintf(_('Invalid email address: %s'), $email));
73                 return;
74             }
75         }
76
77         $this->already = array();
78         $this->subbed = array();
79
80         foreach ($addresses as $email) {
81             $email = common_canonical_email($email);
82             $other = User::staticGet('email', $email);
83             if ($other) {
84                 if ($user->isSubscribed($other)) {
85                     $this->already[] = $other;
86                 } else {
87                     subs_subscribe_to($user, $other);
88                     $this->subbed[] = $other;
89                 }
90             } else {
91                 $this->sent[] = $email;
92                 $this->sendInvitation($email, $user, $personal);
93             }
94         }
95
96         $this->mode = 'sent';
97
98         $this->showPage();
99     }
100
101     function showScripts()
102     {
103         parent::showScripts();
104         $this->autofocus('addresses');
105     }
106
107     function title()
108     {
109         if ($this->mode == 'sent') {
110             return _('Invitation(s) sent');
111         } else {
112             return _('Invite new users');
113         }
114     }
115
116     function showContent()
117     {
118         if ($this->mode == 'sent') {
119             $this->showInvitationSuccess();
120         } else {
121             $this->showInviteForm();
122         }
123     }
124
125     function showInvitationSuccess()
126     {
127         if ($this->already) {
128             $this->element('p', null, _('You are already subscribed to these users:'));
129             $this->elementStart('ul');
130             foreach ($this->already as $other) {
131                 $this->element('li', null, sprintf(_('%1$s (%2$s)'), $other->nickname, $other->email));
132             }
133             $this->elementEnd('ul');
134         }
135         if ($this->subbed) {
136             $this->element('p', null, _('These people are already users and you were automatically subscribed to them:'));
137             $this->elementStart('ul');
138             foreach ($this->subbed as $other) {
139                 $this->element('li', null, sprintf(_('%1$s (%2$s)'), $other->nickname, $other->email));
140             }
141             $this->elementEnd('ul');
142         }
143         if ($this->sent) {
144             $this->element('p', null, _('Invitation(s) sent to the following people:'));
145             $this->elementStart('ul');
146             foreach ($this->sent as $other) {
147                 $this->element('li', null, $other);
148             }
149             $this->elementEnd('ul');
150             $this->element('p', null, _('You will be notified when your invitees accept the invitation and register on the site. Thanks for growing the community!'));
151         }
152     }
153
154     function showPageNotice()
155     {
156         if ($this->mode != 'sent') {
157             if ($this->error) {
158                 $this->element('p', 'error', $this->error);
159             } else {
160                 $this->elementStart('div', 'instructions');
161                 $this->element('p', null,
162                                _('Use this form to invite your friends and colleagues to use this service.'));
163                 $this->elementEnd('div');
164             }
165         }
166     }
167
168     function showForm($error=null)
169     {
170         $this->mode = 'form';
171         $this->error = $error;
172         $this->showPage();
173     }
174
175     function showInviteForm()
176     {
177         $this->elementStart('form', array('method' => 'post',
178                                            'id' => 'form_invite',
179                                            'class' => 'form_settings',
180                                            'action' => common_local_url('invite')));
181         $this->elementStart('fieldset');
182         $this->element('legend', null, 'Send an invitation');
183         $this->hidden('token', common_session_token());
184
185         $this->elementStart('ul', 'form_data');
186         $this->elementStart('li');
187         $this->textarea('addresses', _('Email addresses'),
188                         $this->trimmed('addresses'),
189                         _('Addresses of friends to invite (one per line)'));
190         $this->elementEnd('li');
191         $this->elementStart('li');
192         $this->textarea('personal', _('Personal message'),
193                         $this->trimmed('personal'),
194                         _('Optionally add a personal message to the invitation.'));
195         $this->elementEnd('li');
196         $this->elementEnd('ul');
197         $this->submit('send', _('Send'));
198         $this->elementEnd('fieldset');
199         $this->elementEnd('form');
200     }
201
202     function sendInvitation($email, $user, $personal)
203     {
204         $profile = $user->getProfile();
205         $bestname = $profile->getBestName();
206
207         $sitename = common_config('site', 'name');
208
209         $invite = new Invitation();
210
211         $invite->address = $email;
212         $invite->address_type = 'email';
213         $invite->code = common_confirmation_code(128);
214         $invite->user_id = $user->id;
215         $invite->created = common_sql_now();
216
217         if (!$invite->insert()) {
218             common_log_db_error($invite, 'INSERT', __FILE__);
219             return false;
220         }
221
222         $recipients = array($email);
223
224         $headers['From'] = mail_notify_from();
225         $headers['To'] = trim($email);
226         $headers['Subject'] = sprintf(_('%1$s has invited you to join them on %2$s'), $bestname, $sitename);
227
228         $body = sprintf(_("%1\$s has invited you to join them on %2\$s (%3\$s).\n\n".
229                           "%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".
230                           "You can also share news about yourself, your thoughts, or your life online with people who know about you. ".
231                           "It's also great for meeting new people who share your interests.\n\n".
232                           "%1\$s said:\n\n%4\$s\n\n".
233                           "You can see %1\$s's profile page on %2\$s here:\n\n".
234                           "%5\$s\n\n".
235                           "If you'd like to try the service, click on the link below to accept the invitation.\n\n".
236                           "%6\$s\n\n".
237                           "If not, you can ignore this message. Thanks for your patience and your time.\n\n".
238                           "Sincerely, %2\$s\n"),
239                         $bestname,
240                         $sitename,
241                         common_root_url(),
242                         $personal,
243                         common_local_url('showstream', array('nickname' => $user->nickname)),
244                         common_local_url('register', array('code' => $invite->code)));
245
246         mail_send($recipients, $headers, $body);
247     }
248
249     function showLocalNav()
250     {
251         $nav = new SubGroupNav($this, common_current_user());
252         $nav->show();
253     }
254 }