]> git.mxchange.org Git - friendica.git/blob - src/Module/Invite.php
Issue 11566: More detailled notification configuration
[friendica.git] / src / Module / Invite.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\Renderer;
26 use Friendica\Core\Search;
27 use Friendica\DI;
28 use Friendica\Model;
29 use Friendica\Model\User;
30 use Friendica\Network\HTTPException;
31 use Friendica\Protocol\Email;
32 use Friendica\Util\Strings;
33
34 /**
35  * Invite people to friendica
36  */
37 class Invite extends BaseModule
38 {
39         protected function post(array $request = [])
40         {
41                 if (!local_user()) {
42                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
43                 }
44
45                 self::checkFormSecurityTokenRedirectOnError('/', 'send_invite');
46
47                 $app = DI::app();
48                 $config = DI::config();
49
50                 $max_invites = intval($config->get('system', 'max_invites'));
51                 if (!$max_invites) {
52                         $max_invites = 50;
53                 }
54
55                 $current_invites = intval(DI::pConfig()->get(local_user(), 'system', 'sent_invites'));
56                 if ($current_invites > $max_invites) {
57                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Total invitation limit exceeded.'));
58                 }
59
60
61                 $recipients = !empty($_POST['recipients']) ? explode("\n", $_POST['recipients']) : [];
62                 $message = !empty($_POST['message']) ? Strings::escapeHtml(trim($_POST['message'])) : '';
63
64                 $total = 0;
65                 $invitation_only = false;
66                 $invites_remaining = null;
67
68                 if ($config->get('system', 'invitation_only')) {
69                         $invitation_only = true;
70                         $invites_remaining = DI::pConfig()->get(local_user(), 'system', 'invites_remaining');
71                         if ((!$invites_remaining) && (!$app->isSiteAdmin())) {
72                                 throw new HTTPException\ForbiddenException();
73                         }
74                 }
75
76                 $user = User::getById(local_user());
77
78                 foreach ($recipients as $recipient) {
79                         $recipient = trim($recipient);
80
81                         if (!filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
82                                 notice(DI::l10n()->t('%s : Not a valid email address.', $recipient));
83                                 continue;
84                         }
85
86                         if ($invitation_only && ($invites_remaining || $app->isSiteAdmin())) {
87                                 $code = Model\Register::createForInvitation();
88                                 $nmessage = str_replace('$invite_code', $code, $message);
89
90                                 if (!$app->isSiteAdmin()) {
91                                         $invites_remaining--;
92                                         if ($invites_remaining >= 0) {
93                                                 DI::pConfig()->set(local_user(), 'system', 'invites_remaining', $invites_remaining);
94                                         } else {
95                                                 return;
96                                         }
97                                 }
98                         } else {
99                                 $nmessage = $message;
100                         }
101
102                         $additional_headers = 'From: "' . $user['email'] . '" <' . DI::emailer()->getSiteEmailAddress() . ">\n"
103                                 . 'Content-type: text/plain; charset=UTF-8' . "\n"
104                                 . 'Content-transfer-encoding: 8bit';
105
106                         $res = mail(
107                                 $recipient,
108                                 Email::encodeHeader(DI::l10n()->t('Please join us on Friendica'), 'UTF-8'),
109                                 $nmessage,
110                                 $additional_headers);
111
112                         if ($res) {
113                                 $total++;
114                                 $current_invites++;
115                                 DI::pConfig()->set(local_user(), 'system', 'sent_invites', $current_invites);
116                                 if ($current_invites > $max_invites) {
117                                         notice(DI::l10n()->t('Invitation limit exceeded. Please contact your site administrator.'));
118                                         return;
119                                 }
120                         } else {
121                                 notice(DI::l10n()->t('%s : Message delivery failed.', $recipient));
122                         }
123
124                 }
125                 info(DI::l10n()->tt('%d message sent.', '%d messages sent.', $total));
126         }
127
128         protected function content(array $request = []): string
129         {
130                 if (!local_user()) {
131                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
132                 }
133
134                 $app = DI::app();
135                 $config = DI::config();
136
137                 $inviteOnly = false;
138
139                 if ($config->get('system', 'invitation_only')) {
140                         $inviteOnly = true;
141                         $x = DI::pConfig()->get(local_user(), 'system', 'invites_remaining');
142                         if ((!$x) && (!$app->isSiteAdmin())) {
143                                 throw new HTTPException\ForbiddenException(DI::l10n()->t('You have no more invitations available'));
144                         }
145                 }
146
147                 $dirLocation = Search::getGlobalDirectory();
148                 if (strlen($dirLocation)) {
149                         if ($config->get('config', 'register_policy') === Register::CLOSED) {
150                                 $linkTxt = DI::l10n()->t('Visit %s for a list of public sites that you can join. Friendica members on other sites can all connect with each other, as well as with members of many other social networks.', $dirLocation . '/servers');
151                         } else {
152                                 $linkTxt = DI::l10n()->t('To accept this invitation, please visit and register at %s or any other public Friendica website.', DI::baseUrl()->get())
153                                         . "\r\n" . "\r\n" . DI::l10n()->t('Friendica sites all inter-connect to create a huge privacy-enhanced social web that is owned and controlled by its members. They can also connect with many traditional social networks. See %s for a list of alternate Friendica sites you can join.', $dirLocation . '/servers');
154                         }
155                 } else { // there is no global directory URL defined
156                         if ($config->get('config', 'register_policy') === Register::CLOSED) {
157                                 return DI::l10n()->t('Our apologies. This system is not currently configured to connect with other public sites or invite members.');
158                         } else {
159                                 $linkTxt = DI::l10n()->t('To accept this invitation, please visit and register at %s.', DI::baseUrl()->get()
160                                         . "\r\n" . "\r\n" . DI::l10n()->t('Friendica sites all inter-connect to create a huge privacy-enhanced social web that is owned and controlled by its members. They can also connect with many traditional social networks.'));
161                         }
162                 }
163
164                 $tpl = Renderer::getMarkupTemplate('invite.tpl');
165                 return Renderer::replaceMacros($tpl, [
166                         '$form_security_token' => self::getFormSecurityToken('send_invite'),
167                         '$title'               => DI::l10n()->t('Send invitations'),
168                         '$recipients'          => ['recipients', DI::l10n()->t('Enter email addresses, one per line:')],
169                         '$message'             => [
170                                 'message',
171                                 DI::l10n()->t('Your message:'),
172                                 DI::l10n()->t('You are cordially invited to join me and other close friends on Friendica - and help us to create a better social web.') . "\r\n" . "\r\n"
173                                 . $linkTxt
174                                 . "\r\n" . "\r\n" . (($inviteOnly) ? DI::l10n()->t('You will need to supply this invitation code: $invite_code') . "\r\n" . "\r\n" : '') . DI::l10n()->t('Once you have registered, please connect with me via my profile page at:')
175                                 . "\r\n" . "\r\n" . DI::baseUrl()->get() . '/profile/' . $app->getLoggedInUserNickname()
176                                 . "\r\n" . "\r\n" . DI::l10n()->t('For more information about the Friendica project and why we feel it is important, please visit http://friendi.ca') . "\r\n" . "\r\n",
177                         ],
178                         '$submit'              => DI::l10n()->t('Submit')
179                 ]);
180         }
181 }