]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/invite.php
Merge branch '0.8.x' into invite-enabled
[quix0rs-gnu-social.git] / actions / invite.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, Control Yourself, 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 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, true)) {
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 title()
102     {
103         if ($this->mode == 'sent') {
104             return _('Invitation(s) sent');
105         } else {
106             return _('Invite new users');
107         }
108     }
109
110     function showContent()
111     {
112         if ($this->mode == 'sent') {
113             $this->showInvitationSuccess();
114         } else {
115             $this->showInviteForm();
116         }
117     }
118
119     function showInvitationSuccess()
120     {
121         if ($this->already) {
122             $this->element('p', null, _('You are already subscribed to these users:'));
123             $this->elementStart('ul');
124             foreach ($this->already as $other) {
125                 $this->element('li', null, sprintf(_('%s (%s)'), $other->nickname, $other->email));
126             }
127             $this->elementEnd('ul');
128         }
129         if ($this->subbed) {
130             $this->element('p', null, _('These people are already users and you were automatically subscribed to them:'));
131             $this->elementStart('ul');
132             foreach ($this->subbed as $other) {
133                 $this->element('li', null, sprintf(_('%s (%s)'), $other->nickname, $other->email));
134             }
135             $this->elementEnd('ul');
136         }
137         if ($this->sent) {
138             $this->element('p', null, _('Invitation(s) sent to the following people:'));
139             $this->elementStart('ul');
140             foreach ($this->sent as $other) {
141                 $this->element('li', null, $other);
142             }
143             $this->elementEnd('ul');
144             $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
148     function showPageNotice()
149     {
150         if ($this->mode != 'sent') {
151             if ($this->error) {
152                 $this->element('p', 'error', $this->error);
153             } else {
154                 $this->elementStart('div', 'instructions');
155                 $this->element('p', null,
156                                _('Use this form to invite your friends and colleagues to use this service.'));
157                 $this->elementEnd('div');
158             }
159         }
160     }
161
162     function showForm($error=null)
163     {
164         $this->mode = 'form';
165         $this->error = $error;
166         $this->showPage();
167     }
168
169     function showInviteForm()
170     {
171         $this->elementStart('form', array('method' => 'post',
172                                            'id' => 'form_invite',
173                                            'class' => 'form_settings',
174                                            'action' => common_local_url('invite')));
175         $this->elementStart('fieldset');
176         $this->element('legend', null, 'Send an invitation');
177         $this->hidden('token', common_session_token());
178
179         $this->elementStart('ul', 'form_data');
180         $this->elementStart('li');
181         $this->textarea('addresses', _('Email addresses'),
182                         $this->trimmed('addresses'),
183                         _('Addresses of friends to invite (one per line)'));
184         $this->elementEnd('li');
185         $this->elementStart('li');
186         $this->textarea('personal', _('Personal message'),
187                         $this->trimmed('personal'),
188                         _('Optionally add a personal message to the invitation.'));
189         $this->elementEnd('li');
190         $this->elementEnd('ul');
191         $this->submit('send', _('Send'));
192         $this->elementEnd('fieldset');
193         $this->elementEnd('form');
194     }
195
196     function sendInvitation($email, $user, $personal)
197     {
198         $profile = $user->getProfile();
199         $bestname = $profile->getBestName();
200
201         $sitename = common_config('site', 'name');
202
203         $invite = new Invitation();
204
205         $invite->address = $email;
206         $invite->address_type = 'email';
207         $invite->code = common_confirmation_code(128);
208         $invite->user_id = $user->id;
209         $invite->created = common_sql_now();
210
211         if (!$invite->insert()) {
212             common_log_db_error($invite, 'INSERT', __FILE__);
213             return false;
214         }
215
216         $recipients = array($email);
217
218         $headers['From'] = mail_notify_from();
219         $headers['To'] = $email;
220         $headers['Subject'] = sprintf(_('%1$s has invited you to join them on %2$s'), $bestname, $sitename);
221
222         $body = sprintf(_("%1\$s has invited you to join them on %2\$s (%3\$s).\n\n".
223                           "%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".
224                           "You can also share news about yourself, your thoughts, or your life online with people who know about you. ".
225                           "It's also great for meeting new people who share your interests.\n\n".
226                           "%1\$s said:\n\n%4\$s\n\n".
227                           "You can see %1\$s's profile page on %2\$s here:\n\n".
228                           "%5\$s\n\n".
229                           "If you'd like to try the service, click on the link below to accept the invitation.\n\n".
230                           "%6\$s\n\n".
231                           "If not, you can ignore this message. Thanks for your patience and your time.\n\n".
232                           "Sincerely, %2\$s\n"),
233                         $bestname,
234                         $sitename,
235                         common_root_url(),
236                         $personal,
237                         common_local_url('showstream', array('nickname' => $user->nickname)),
238                         common_local_url('register', array('code' => $invite->code)));
239
240         mail_send($recipients, $headers, $body);
241     }
242
243     function showLocalNav()
244     {
245         $nav = new SubGroupNav($this, common_current_user());
246         $nav->show();
247     }
248 }