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