]> git.mxchange.org Git - friendica.git/blob - src/Module/Invite.php
Merge pull request #8261 from MrPetovan/task/8251-use-about-for-pdesc
[friendica.git] / src / Module / Invite.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\DI;
27 use Friendica\Model;
28 use Friendica\Network\HTTPException;
29 use Friendica\Protocol\Email;
30 use Friendica\Util\Strings;
31
32 /**
33  * Invite people to friendica
34  */
35 class Invite extends BaseModule
36 {
37         public static function post(array $parameters = [])
38         {
39                 if (!local_user()) {
40                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
41                 }
42
43                 self::checkFormSecurityTokenRedirectOnError('/', 'send_invite');
44
45                 $app = DI::app();
46                 $config = DI::config();
47
48                 $max_invites = intval($config->get('system', 'max_invites'));
49                 if (!$max_invites) {
50                         $max_invites = 50;
51                 }
52
53                 $current_invites = intval(DI::pConfig()->get(local_user(), 'system', 'sent_invites'));
54                 if ($current_invites > $max_invites) {
55                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Total invitation limit exceeded.'));
56                 }
57
58
59                 $recipients = !empty($_POST['recipients']) ? explode("\n", $_POST['recipients']) : [];
60                 $message = !empty($_POST['message']) ? Strings::escapeTags(trim($_POST['message'])) : '';
61
62                 $total = 0;
63                 $invitation_only = false;
64                 $invites_remaining = null;
65
66                 if ($config->get('system', 'invitation_only')) {
67                         $invitation_only = true;
68                         $invites_remaining = DI::pConfig()->get(local_user(), 'system', 'invites_remaining');
69                         if ((!$invites_remaining) && (!is_site_admin())) {
70                                 throw new HTTPException\ForbiddenException();
71                         }
72                 }
73
74                 foreach ($recipients as $recipient) {
75                         $recipient = trim($recipient);
76
77                         if (!filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
78                                 notice(DI::l10n()->t('%s : Not a valid email address.', $recipient) . EOL);
79                                 continue;
80                         }
81
82                         if ($invitation_only && ($invites_remaining || is_site_admin())) {
83                                 $code = Model\Register::createForInvitation();
84                                 $nmessage = str_replace('$invite_code', $code, $message);
85
86                                 if (!is_site_admin()) {
87                                         $invites_remaining--;
88                                         if ($invites_remaining >= 0) {
89                                                 DI::pConfig()->set(local_user(), 'system', 'invites_remaining', $invites_remaining);
90                                         } else {
91                                                 return;
92                                         }
93                                 }
94                         } else {
95                                 $nmessage = $message;
96                         }
97
98                         $additional_headers = 'From: ' . $app->user['email'] . "\n"
99                                 . 'Sender: ' . DI::emailer()->getSiteEmailAddress() . "\n"
100                                 . 'Content-type: text/plain; charset=UTF-8' . "\n"
101                                 . 'Content-transfer-encoding: 8bit';
102
103                         $res = mail(
104                                 $recipient,
105                                 Email::encodeHeader(DI::l10n()->t('Please join us on Friendica'), 'UTF-8'),
106                                 $nmessage,
107                                 $additional_headers);
108
109                         if ($res) {
110                                 $total++;
111                                 $current_invites++;
112                                 DI::pConfig()->set(local_user(), 'system', 'sent_invites', $current_invites);
113                                 if ($current_invites > $max_invites) {
114                                         notice(DI::l10n()->t('Invitation limit exceeded. Please contact your site administrator.') . EOL);
115                                         return;
116                                 }
117                         } else {
118                                 notice(DI::l10n()->t('%s : Message delivery failed.', $recipient) . EOL);
119                         }
120
121                 }
122                 notice(DI::l10n()->tt('%d message sent.', '%d messages sent.', $total) . EOL);
123         }
124
125         public static function content(array $parameters = [])
126         {
127                 if (!local_user()) {
128                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
129                 }
130
131                 $app = DI::app();
132                 $config = DI::config();
133
134                 $inviteOnly = false;
135
136                 if ($config->get('system', 'invitation_only')) {
137                         $inviteOnly = true;
138                         $x = DI::pConfig()->get(local_user(), 'system', 'invites_remaining');
139                         if ((!$x) && (!is_site_admin())) {
140                                 throw new HTTPException\ForbiddenException(DI::l10n()->t('You have no more invitations available'));
141                         }
142                 }
143
144                 $dirLocation = $config->get('system', 'directory');
145                 if (strlen($dirLocation)) {
146                         if ($config->get('config', 'register_policy') === Register::CLOSED) {
147                                 $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');
148                         } else {
149                                 $linkTxt = DI::l10n()->t('To accept this invitation, please visit and register at %s or any other public Friendica website.', DI::baseUrl()->get())
150                                         . "\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');
151                         }
152                 } else { // there is no global directory URL defined
153                         if ($config->get('config', 'register_policy') === Register::CLOSED) {
154                                 return DI::l10n()->t('Our apologies. This system is not currently configured to connect with other public sites or invite members.');
155                         } else {
156                                 $linkTxt = DI::l10n()->t('To accept this invitation, please visit and register at %s.', DI::baseUrl()->get()
157                                         . "\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.'));
158                         }
159                 }
160
161                 $tpl = Renderer::getMarkupTemplate('invite.tpl');
162                 return Renderer::replaceMacros($tpl, [
163                         '$form_security_token' => self::getFormSecurityToken('send_invite'),
164                         '$title'               => DI::l10n()->t('Send invitations'),
165                         '$recipients'          => ['recipients', DI::l10n()->t('Enter email addresses, one per line:')],
166                         '$message'             => [
167                                 'message',
168                                 DI::l10n()->t('Your message:'),
169                                 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"
170                                 . $linkTxt
171                                 . "\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:')
172                                 . "\r\n" . "\r\n" . DI::baseUrl()->get() . '/profile/' . $app->user['nickname']
173                                 . "\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",
174                         ],
175                         '$submit'              => DI::l10n()->t('Submit')
176                 ]);
177         }
178 }