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