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