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